Fixed generating PHPDoc for methods with class templates (#1647)

* Fixed generating PHPDoc for methods with class templates

* Fix style

* Use getter for alias template names

* Fixed missing class in static analysis
This commit is contained in:
Nereo Berardozzi
2024-12-31 17:18:28 +01:00
committed by GitHub
parent f8381565ad
commit 0bbca8f5e2
4 changed files with 88 additions and 15 deletions
+41 -12
View File
@@ -16,6 +16,7 @@ use Barryvdh\Reflection\DocBlock\Context;
use Barryvdh\Reflection\DocBlock\ContextFactory; use Barryvdh\Reflection\DocBlock\ContextFactory;
use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer; use Barryvdh\Reflection\DocBlock\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag\MethodTag; use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
use Barryvdh\Reflection\DocBlock\Tag\TemplateTag;
use Closure; use Closure;
use Illuminate\Config\Repository as ConfigRepository; use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
@@ -48,6 +49,9 @@ class Alias
/** @var ConfigRepository */ /** @var ConfigRepository */
protected $config; protected $config;
/** @var string[] */
protected $templateNames;
/** /**
* @param ConfigRepository $config * @param ConfigRepository $config
* @param string $alias * @param string $alias
@@ -347,7 +351,8 @@ class Alias
$magic, $magic,
$this->interfaces, $this->interfaces,
$this->classAliases, $this->classAliases,
$this->getReturnTypeNormalizers($class) $this->getReturnTypeNormalizers($class),
$this->getTemplateNames()
); );
} }
$this->usedMethods[] = $magic; $this->usedMethods[] = $magic;
@@ -379,7 +384,8 @@ class Alias
$method->name, $method->name,
$this->interfaces, $this->interfaces,
$this->classAliases, $this->classAliases,
$this->getReturnTypeNormalizers($reflection) $this->getReturnTypeNormalizers($reflection),
$this->getTemplateNames(),
); );
} }
$this->usedMethods[] = $method->name; $this->usedMethods[] = $method->name;
@@ -477,39 +483,62 @@ class Alias
/** /**
* @param $prefix * @param $prefix
* @return string * @return string
* @throws \ReflectionException
*/ */
public function getPhpDocTemplates($prefix = "\t\t") public function getPhpDocTemplates($prefix = "\t\t")
{ {
$templateDoc = new DocBlock(''); $templateDoc = new DocBlock('');
$serializer = new DocBlockSerializer(1, $prefix); $serializer = new DocBlockSerializer(1, $prefix);
foreach ($this->getTemplateNames() as $templateName) {
$template = new TemplateTag('template', $templateName);
$template->setBound('static');
$template->setDocBlock($templateDoc);
$templateDoc->appendTag($template);
}
return $serializer->getDocComment($templateDoc);
}
/**
* @return string[]
*/
public function getTemplateNames()
{
if (!isset($this->templateNames)) {
$this->detectTemplateNames();
}
return $this->templateNames;
}
/**
* @return void
* @throws \ReflectionException
*/
protected function detectTemplateNames()
{
$templateNames = [];
foreach ($this->classes as $class) { foreach ($this->classes as $class) {
$reflection = new ReflectionClass($class); $reflection = new ReflectionClass($class);
$traits = collect($reflection->getTraitNames()); $traits = collect($reflection->getTraitNames());
$phpdoc = new DocBlock($reflection); $phpdoc = new DocBlock($reflection);
$templates = $phpdoc->getTagsByName('template'); $templates = $phpdoc->getTagsByName('template');
/** @var DocBlock\Tag\TemplateTag $template */ /** @var TemplateTag $template */
foreach ($templates as $template) { foreach ($templates as $template) {
$template->setBound('static'); $templateNames[] = $template->getTemplateName();
$template->setDocBlock($templateDoc);
$templateDoc->appendTag($template);
} }
foreach ($traits as $trait) { foreach ($traits as $trait) {
$phpdoc = new DocBlock(new ReflectionClass($trait)); $phpdoc = new DocBlock(new ReflectionClass($trait));
$templates = $phpdoc->getTagsByName('template'); $templates = $phpdoc->getTagsByName('template');
/** @var DocBlock\Tag\TemplateTag $template */ /** @var TemplateTag $template */
foreach ($templates as $template) { foreach ($templates as $template) {
$template->setBound('static'); $templateNames[] = $template->getTemplateName();
$template->setDocBlock($templateDoc);
$templateDoc->appendTag($template);
} }
} }
} }
return $serializer->getDocComment($templateDoc); $this->templateNames = $templateNames;
} }
/** /**
+8 -3
View File
@@ -39,6 +39,9 @@ class Method
protected $classAliases; protected $classAliases;
protected $returnTypeNormalizers; protected $returnTypeNormalizers;
/** @var string[] */
protected $templateNames = [];
/** /**
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method * @param \ReflectionMethod|\ReflectionFunctionAbstract $method
* @param string $alias * @param string $alias
@@ -47,8 +50,9 @@ class Method
* @param array $interfaces * @param array $interfaces
* @param array $classAliases * @param array $classAliases
* @param array $returnTypeNormalizers * @param array $returnTypeNormalizers
* @param string[] $templateNames
*/ */
public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], array $returnTypeNormalizers = []) public function __construct($method, $alias, $class, $methodName = null, $interfaces = [], array $classAliases = [], array $returnTypeNormalizers = [], array $templateNames = [])
{ {
$this->method = $method; $this->method = $method;
$this->interfaces = $interfaces; $this->interfaces = $interfaces;
@@ -56,6 +60,7 @@ class Method
$this->returnTypeNormalizers = $returnTypeNormalizers; $this->returnTypeNormalizers = $returnTypeNormalizers;
$this->name = $methodName ?: $method->name; $this->name = $methodName ?: $method->name;
$this->real_name = $method->isClosure() ? $this->name : $method->name; $this->real_name = $method->isClosure() ? $this->name : $method->name;
$this->templateNames = $templateNames;
$this->initClassDefinedProperties($method, $class); $this->initClassDefinedProperties($method, $class);
//Reference the 'real' function in the declaring class //Reference the 'real' function in the declaring class
@@ -84,7 +89,7 @@ class Method
*/ */
protected function initPhpDoc($method) protected function initPhpDoc($method)
{ {
$this->phpdoc = new DocBlock($method, new Context($this->namespace, $this->classAliases)); $this->phpdoc = new DocBlock($method, new Context($this->namespace, $this->classAliases, generics: $this->templateNames));
} }
/** /**
@@ -386,7 +391,7 @@ class Method
} }
if ($method) { if ($method) {
$namespace = $method->getDeclaringClass()->getNamespaceName(); $namespace = $method->getDeclaringClass()->getNamespaceName();
$phpdoc = new DocBlock($method, new Context($namespace, $this->classAliases)); $phpdoc = new DocBlock($method, new Context($namespace, $this->classAliases, generics: $this->templateNames));
if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) { if (strpos($phpdoc->getText(), '{@inheritdoc}') !== false) {
//Not at the end yet, try another parent/interface.. //Not at the end yet, try another parent/interface..
+16
View File
@@ -66,6 +66,21 @@ class AliasTest extends TestCase
$this->assertNotNull($this->getAliasMacro($alias, EloquentBuilder::class, $macro)); $this->assertNotNull($this->getAliasMacro($alias, EloquentBuilder::class, $macro));
} }
/**
* @covers ::detectTemplateNames
*/
public function testTemplateNamesAreDetected(): void
{
// Mock
$alias = new AliasMock();
// Prepare
$alias->setClasses([EloquentBuilder::class]);
// Test
$this->assertSame(['TModel', 'TValue'], $alias->getTemplateNames());
}
protected function getAliasMacro(Alias $alias, string $class, string $method): ?Macro protected function getAliasMacro(Alias $alias, string $class, string $method): ?Macro
{ {
return Arr::first( return Arr::first(
@@ -82,6 +97,7 @@ class AliasTest extends TestCase
/** /**
* @internal * @internal
* @noinspection PhpMultipleClassesDeclarationsInOneFile * @noinspection PhpMultipleClassesDeclarationsInOneFile
* @template TValue
*/ */
class AliasMock extends Alias class AliasMock extends Alias
{ {
+23
View File
@@ -193,6 +193,29 @@ DOC;
$this->assertSame([], $method->getParamsWithDefault(false)); $this->assertSame([], $method->getParamsWithDefault(false));
$this->assertTrue($method->shouldReturn()); $this->assertTrue($method->shouldReturn());
} }
public function testEloquentBuilderWithTemplates()
{
$reflectionClass = new \ReflectionClass(EloquentBuilder::class);
$reflectionMethod = $reflectionClass->getMethod('firstOr');
$method = new Method($reflectionMethod, 'Builder', $reflectionClass, null, [], [], [], ['TModel']);
$output = <<<'DOC'
/**
* Execute the query and get the first result or call a callback.
*
* @template TValue
* @param (\Closure(): TValue)|list<string> $columns
* @param (\Closure(): TValue)|null $callback
* @return TModel|TValue
* @static
*/
DOC;
$this->assertSame($output, $method->getDocComment(''));
$this->assertSame('firstOr', $method->getName());
$this->assertSame('\\' . EloquentBuilder::class, $method->getDeclaringClass());
}
} }
class ExampleClass class ExampleClass