1
0
mirror of https://github.com/danog/phpdoc.git synced 2024-11-26 12:04:47 +01:00

Multiple fixes

This commit is contained in:
Daniil Gentili 2023-06-26 17:49:39 +02:00
parent b396e6d4e5
commit 563e8087d3
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -45,19 +45,27 @@ class MethodDoc extends GenericDoc
parent::__construct($doc, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method); parent::__construct($doc, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method);
$order = [];
$docReflection = "/**\n"; $docReflection = "/**\n";
foreach ($method->getParameters() as $param) { foreach ($method->getParameters() as $param) {
$order []= $param->getName();
$type = (string) ($param->getType() ?? 'mixed'); $type = (string) ($param->getType() ?? 'mixed');
$docReflection .= " * @param $type \$".$param->getName()."\n"; $variadic = $param->isVariadic() ? '...' : '';
$docReflection .= " * @param $type $variadic\$".$param->getName()."\n";
} }
$docReflection .= ' * @return '.($method->getReturnType() ?? 'mixed')."\n*/"; $docReflection .= ' * @return '.($method->getReturnType() ?? 'mixed')."\n*/";
$docReflection = $this->builder->getFactory()->create($docReflection); $docReflection = $this->builder->getFactory()->create($docReflection);
$params = [];
$psalmParams = [];
foreach ([...$doc->getTags(), ...$docReflection->getTags()] as $tag) { foreach ([...$doc->getTags(), ...$docReflection->getTags()] as $tag) {
if ($tag instanceof Param && !isset($this->params[$tag->getVariableName()])) { if ($tag instanceof Param && !isset($params[$tag->getVariableName()])) {
$this->params[$tag->getVariableName()] = [ $params[$tag->getVariableName()] = [
$tag->getType(), $tag->getType(),
$tag->getDescription() $tag->getDescription(),
$tag->isVariadic()
]; ];
} elseif ($tag instanceof Return_ && !isset($this->return)) { } elseif ($tag instanceof Return_ && !isset($this->return)) {
$this->return = $tag; $this->return = $tag;
@ -67,18 +75,25 @@ class MethodDoc extends GenericDoc
[$type, $description] = \explode(" $", $tag->getDescription(), 2); [$type, $description] = \explode(" $", $tag->getDescription(), 2);
$description .= ' '; $description .= ' ';
[$varName, $description] = \explode(" ", $description, 2); [$varName, $description] = \explode(" ", $description, 2);
if (!$description && isset($this->params[$varName])) { if (!$description && isset($params[$varName])) {
$description = (string) $this->params[$varName][1]; $description = (string) $params[$varName][1];
} else { } else {
$description = new Description($description); $description = new Description($description);
} }
$this->psalmParams[$varName] = [ $psalmParams[$varName] = [
$type, $type,
$description $description
]; ];
} }
} }
foreach ($order as $param) {
$this->params[$param] = $params[$param];
if (isset($psalmParams[$param])) {
$this->psalmParams[$param] = $psalmParams[$param];
}
}
foreach ($this->params as &$param) { foreach ($this->params as &$param) {
if (isset($param[0])) { if (isset($param[0])) {
$param[0] = $this->resolveTypeAlias($param[0]); $param[0] = $this->resolveTypeAlias($param[0]);
@ -103,8 +118,11 @@ class MethodDoc extends GenericDoc
{ {
$sig = $this->name; $sig = $this->name;
$sig .= "("; $sig .= "(";
foreach ($this->params as $var => [$type, $description]) { foreach ($this->params as $var => [$type, $description, $variadic]) {
$sig .= $type.' '; $sig .= $type.' ';
if ($variadic) {
$sig .= '...';
}
$sig .= "$".$var; $sig .= "$".$var;
$sig .= ', '; $sig .= ', ';
} }
@ -152,12 +170,13 @@ class MethodDoc extends GenericDoc
$sig .= "\n\n"; $sig .= "\n\n";
$sig .= $this->title; $sig .= $this->title;
$sig .= "\n"; $sig .= "\n";
$sig .= str_replace("\n", " \n", $this->description); $sig .= \str_replace("\n", " \n", $this->description);
$sig .= "\n"; $sig .= "\n";
if ($this->psalmParams || $this->params) { if ($this->psalmParams || $this->params) {
$sig .= "\nParameters:\n\n"; $sig .= "\nParameters:\n\n";
foreach ($this->params as $name => [$type, $description]) { foreach ($this->params as $name => [$type, $description, $variadic]) {
$sig .= "* `\$$name`: `$type` $description \n"; $variadic = $variadic ? '...' : '';
$sig .= "* `$variadic\$$name`: `$type` $description \n";
if (isset($this->psalmParams[$name])) { if (isset($this->psalmParams[$name])) {
[$psalmType] = $this->psalmParams[$name]; [$psalmType] = $this->psalmParams[$name];
$psalmType = \trim(\str_replace("\n", "\n ", $psalmType)); $psalmType = \trim(\str_replace("\n", "\n ", $psalmType));