From 12d87339953d606dd15deb31bf17194bd318480f Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 14 Jul 2023 15:09:51 +0200 Subject: [PATCH] Fixes --- src/PhpDoc/ClassDoc.php | 7 ++++--- src/PhpDoc/MethodDoc.php | 24 ++++++++++++++++++++---- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/PhpDoc/ClassDoc.php b/src/PhpDoc/ClassDoc.php index e45232d..9b14ca9 100644 --- a/src/PhpDoc/ClassDoc.php +++ b/src/PhpDoc/ClassDoc.php @@ -51,7 +51,8 @@ class ClassDoc extends GenericDoc $docReflection = "/**\n"; 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(); $comment = ''; foreach ($this->builder->getFactory()->create($property->getDocComment() ?: '/** */')->getTags() as $tag) { @@ -65,14 +66,14 @@ class ClassDoc extends GenericDoc } } if (!$comment) { - $comment = trim($property->getDocComment() ?: '', "\n/* "); + $comment = \trim($property->getDocComment() ?: '', "\n/* "); } $docReflection .= " * @property $type \$$name $comment\n"; } $docReflection .= " */\n"; $docReflection = $this->builder->getFactory()->create($docReflection); - $tags = array_merge($docReflection->getTags(), $doc->getTags()); + $tags = \array_merge($docReflection->getTags(), $doc->getTags()); foreach ($tags as $tag) { if ($tag instanceof Property && $tag->getVariableName()) { /** @psalm-suppress InvalidPropertyAssignmentValue */ diff --git a/src/PhpDoc/MethodDoc.php b/src/PhpDoc/MethodDoc.php index d2435ef..fa62e75 100644 --- a/src/PhpDoc/MethodDoc.php +++ b/src/PhpDoc/MethodDoc.php @@ -46,10 +46,21 @@ class MethodDoc extends GenericDoc parent::__construct($doc, $method instanceof ReflectionMethod ? $method->getDeclaringClass() : $method); $order = []; + $optional = []; $docReflection = "/**\n"; foreach ($method->getParameters() as $param) { $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'); $variadic = $param->isVariadic() ? '...' : ''; $docReflection .= " * @param $type $variadic\$".$param->getName()."\n"; @@ -65,7 +76,8 @@ class MethodDoc extends GenericDoc $params[$tag->getVariableName()] = [ $tag->getType(), $tag->getDescription(), - $tag->isVariadic() + $tag->isVariadic(), + $optional[$tag->getVariableName()] ]; } elseif ($tag instanceof Return_ && !isset($this->return) && $this->name !== '__construct') { $this->return = $tag; @@ -82,7 +94,8 @@ class MethodDoc extends GenericDoc } $psalmParams[$varName] = [ $type, - $description + $description, + $optional[$varName] ]; } } @@ -118,12 +131,15 @@ class MethodDoc extends GenericDoc { $sig = $this->name; $sig .= "("; - foreach ($this->params as $var => [$type, $description, $variadic]) { + foreach ($this->params as $var => [$type, $description, $variadic, [$optional, $default]]) { $sig .= $type.' '; if ($variadic) { $sig .= '...'; } $sig .= "$".$var; + if ($optional) { + $sig .= " = ".$default; + } $sig .= ', '; } $sig = \trim($sig, ', '); @@ -157,7 +173,7 @@ class MethodDoc extends GenericDoc $sigLink = \preg_replace('/[^\w ]+/', ' ', $sigLink); $sigLink = \preg_replace('/ +/', ' ', $sigLink); $sigLink = \str_replace(' ', '-', $sigLink); - return trim($sigLink, '-'); + return \trim($sigLink, '-'); } /** * Generate markdown for method.