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\Serializer as DocBlockSerializer;
use Barryvdh\Reflection\DocBlock\Tag\MethodTag;
use Barryvdh\Reflection\DocBlock\Tag\TemplateTag;
use Closure;
use Illuminate\Config\Repository as ConfigRepository;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
@@ -48,6 +49,9 @@ class Alias
/** @var ConfigRepository */
protected $config;
/** @var string[] */
protected $templateNames;
/**
* @param ConfigRepository $config
* @param string $alias
@@ -347,7 +351,8 @@ class Alias
$magic,
$this->interfaces,
$this->classAliases,
$this->getReturnTypeNormalizers($class)
$this->getReturnTypeNormalizers($class),
$this->getTemplateNames()
);
}
$this->usedMethods[] = $magic;
@@ -379,7 +384,8 @@ class Alias
$method->name,
$this->interfaces,
$this->classAliases,
$this->getReturnTypeNormalizers($reflection)
$this->getReturnTypeNormalizers($reflection),
$this->getTemplateNames(),
);
}
$this->usedMethods[] = $method->name;
@@ -477,39 +483,62 @@ class Alias
/**
* @param $prefix
* @return string
* @throws \ReflectionException
*/
public function getPhpDocTemplates($prefix = "\t\t")
{
$templateDoc = new DocBlock('');
$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) {
$reflection = new ReflectionClass($class);
$traits = collect($reflection->getTraitNames());
$phpdoc = new DocBlock($reflection);
$templates = $phpdoc->getTagsByName('template');
/** @var DocBlock\Tag\TemplateTag $template */
/** @var TemplateTag $template */
foreach ($templates as $template) {
$template->setBound('static');
$template->setDocBlock($templateDoc);
$templateDoc->appendTag($template);
$templateNames[] = $template->getTemplateName();
}
foreach ($traits as $trait) {
$phpdoc = new DocBlock(new ReflectionClass($trait));
$templates = $phpdoc->getTagsByName('template');
/** @var DocBlock\Tag\TemplateTag $template */
/** @var TemplateTag $template */
foreach ($templates as $template) {
$template->setBound('static');
$template->setDocBlock($templateDoc);
$templateDoc->appendTag($template);
$templateNames[] = $template->getTemplateName();
}
}
}
return $serializer->getDocComment($templateDoc);
$this->templateNames = $templateNames;
}
/**
+8 -3
View File
@@ -39,6 +39,9 @@ class Method
protected $classAliases;
protected $returnTypeNormalizers;
/** @var string[] */
protected $templateNames = [];
/**
* @param \ReflectionMethod|\ReflectionFunctionAbstract $method
* @param string $alias
@@ -47,8 +50,9 @@ class Method
* @param array $interfaces
* @param array $classAliases
* @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->interfaces = $interfaces;
@@ -56,6 +60,7 @@ class Method
$this->returnTypeNormalizers = $returnTypeNormalizers;
$this->name = $methodName ?: $method->name;
$this->real_name = $method->isClosure() ? $this->name : $method->name;
$this->templateNames = $templateNames;
$this->initClassDefinedProperties($method, $class);
//Reference the 'real' function in the declaring class
@@ -84,7 +89,7 @@ class 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) {
$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) {
//Not at the end yet, try another parent/interface..