1
0
mirror of https://github.com/danog/phpdoc.git synced 2024-11-30 04:29:12 +01:00
This commit is contained in:
Daniil Gentili 2023-07-14 15:09:51 +02:00
parent ab8444e667
commit 12d8733995
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 24 additions and 7 deletions

View File

@ -51,7 +51,8 @@ class ClassDoc extends GenericDoc
$docReflection = "/**\n"; $docReflection = "/**\n";
foreach ($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC & ~ReflectionProperty::IS_STATIC) as $property) { foreach ($reflectionClass->getProperties(ReflectionProperty::IS_PUBLIC & ~ReflectionProperty::IS_STATIC) as $property) {
$type = $property->getType()?->getName() ?? 'mixed'; $type = $property->getType() ?? 'mixed';
$type = (string) $type;
$name = $property->getName(); $name = $property->getName();
$comment = ''; $comment = '';
foreach ($this->builder->getFactory()->create($property->getDocComment() ?: '/** */')->getTags() as $tag) { foreach ($this->builder->getFactory()->create($property->getDocComment() ?: '/** */')->getTags() as $tag) {
@ -65,14 +66,14 @@ class ClassDoc extends GenericDoc
} }
} }
if (!$comment) { if (!$comment) {
$comment = trim($property->getDocComment() ?: '', "\n/* "); $comment = \trim($property->getDocComment() ?: '', "\n/* ");
} }
$docReflection .= " * @property $type \$$name $comment\n"; $docReflection .= " * @property $type \$$name $comment\n";
} }
$docReflection .= " */\n"; $docReflection .= " */\n";
$docReflection = $this->builder->getFactory()->create($docReflection); $docReflection = $this->builder->getFactory()->create($docReflection);
$tags = array_merge($docReflection->getTags(), $doc->getTags()); $tags = \array_merge($docReflection->getTags(), $doc->getTags());
foreach ($tags as $tag) { foreach ($tags as $tag) {
if ($tag instanceof Property && $tag->getVariableName()) { if ($tag instanceof Property && $tag->getVariableName()) {
/** @psalm-suppress InvalidPropertyAssignmentValue */ /** @psalm-suppress InvalidPropertyAssignmentValue */

View File

@ -46,10 +46,21 @@ class MethodDoc extends GenericDoc
parent::__construct($doc, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method); parent::__construct($doc, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method);
$order = []; $order = [];
$optional = [];
$docReflection = "/**\n"; $docReflection = "/**\n";
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$order []= $param->getName(); $order []= $param->getName();
$opt = $param->isOptional() && !$param->isVariadic();
$default = '';
if ($opt) {
if ($default = $param->getDefaultValueConstantName()) {
$default = "\\$default";
} else {
$default = \str_replace([PHP_EOL, 'array (', ')'], ['', '[', ']'], \var_export($param->getDefaultValue(), true));
}
}
$optional[$param->getName()] = [$opt, $default];
$type = (string) ($param->getType() ?? 'mixed'); $type = (string) ($param->getType() ?? 'mixed');
$variadic = $param->isVariadic() ? '...' : ''; $variadic = $param->isVariadic() ? '...' : '';
$docReflection .= " * @param $type $variadic\$".$param->getName()."\n"; $docReflection .= " * @param $type $variadic\$".$param->getName()."\n";
@ -65,7 +76,8 @@ class MethodDoc extends GenericDoc
$params[$tag->getVariableName()] = [ $params[$tag->getVariableName()] = [
$tag->getType(), $tag->getType(),
$tag->getDescription(), $tag->getDescription(),
$tag->isVariadic() $tag->isVariadic(),
$optional[$tag->getVariableName()]
]; ];
} elseif ($tag instanceof Return_ && !isset($this->return) && $this->name !== '__construct') { } elseif ($tag instanceof Return_ && !isset($this->return) && $this->name !== '__construct') {
$this->return = $tag; $this->return = $tag;
@ -82,7 +94,8 @@ class MethodDoc extends GenericDoc
} }
$psalmParams[$varName] = [ $psalmParams[$varName] = [
$type, $type,
$description $description,
$optional[$varName]
]; ];
} }
} }
@ -118,12 +131,15 @@ class MethodDoc extends GenericDoc
{ {
$sig = $this->name; $sig = $this->name;
$sig .= "("; $sig .= "(";
foreach ($this->params as $var => [$type, $description, $variadic]) { foreach ($this->params as $var => [$type, $description, $variadic, [$optional, $default]]) {
$sig .= $type.' '; $sig .= $type.' ';
if ($variadic) { if ($variadic) {
$sig .= '...'; $sig .= '...';
} }
$sig .= "$".$var; $sig .= "$".$var;
if ($optional) {
$sig .= " = ".$default;
}
$sig .= ', '; $sig .= ', ';
} }
$sig = \trim($sig, ', '); $sig = \trim($sig, ', ');
@ -157,7 +173,7 @@ class MethodDoc extends GenericDoc
$sigLink = \preg_replace('/[^\w ]+/', ' ', $sigLink); $sigLink = \preg_replace('/[^\w ]+/', ' ', $sigLink);
$sigLink = \preg_replace('/ +/', ' ', $sigLink); $sigLink = \preg_replace('/ +/', ' ', $sigLink);
$sigLink = \str_replace(' ', '-', $sigLink); $sigLink = \str_replace(' ', '-', $sigLink);
return trim($sigLink, '-'); return \trim($sigLink, '-');
} }
/** /**
* Generate markdown for method. * Generate markdown for method.