From c33eacdefcb75c35fb0a296a712b1caafbd6ae51 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 28 Dec 2024 11:17:17 +0100 Subject: [PATCH] Add templates to Eloquent (#1634) --- composer.json | 2 +- resources/views/helper.php | 2 +- src/Alias.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index 48fe4f181..0290657ab 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "require": { "php": "^8.2", "ext-json": "*", - "barryvdh/reflection-docblock": "^2.1.2", + "barryvdh/reflection-docblock": "^2.2", "composer/class-map-generator": "^1.0", "illuminate/console": "^11.15", "illuminate/database": "^11.15", diff --git a/resources/views/helper.php b/resources/views/helper.php index e9b8a0ef8..7db3875fc 100644 --- a/resources/views/helper.php +++ b/resources/views/helper.php @@ -50,7 +50,7 @@ public static function getName() ?>(getParamsWithDefau namespace { getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?> - /** @template TModel of static */ + getPhpDocTemplates(' ') ?> getClassType() ?> getShortName() ?> extends getExtends() ?> {getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?> getMethods() as $method) : ?> diff --git a/src/Alias.php b/src/Alias.php index 74bd8cec5..0e484a04d 100644 --- a/src/Alias.php +++ b/src/Alias.php @@ -468,6 +468,35 @@ public function getDocComment($prefix = "\t\t") return $serializer->getDocComment($this->phpdoc); } + /** + * @param $prefix + * @return string + * @throws \ReflectionException + */ + public function getPhpDocTemplates($prefix = "\t\t") + { + $templateDoc = new DocBlock(''); + $serializer = new DocBlockSerializer(1, $prefix); + + foreach ($this->classes as $class) { + $reflection = new ReflectionClass($class); + $traits = collect($reflection->getTraitNames()); + + foreach ($traits as $trait) { + $phpdoc = new DocBlock(new ReflectionClass($trait)); + $templates = $phpdoc->getTagsByName('template'); + + /** @var DocBlock\Tag\TemplateTag $template */ + foreach ($templates as $template) { + $template->setBound('static'); + $template->setDocBlock($templateDoc); + $templateDoc->appendTag($template); + } + } + } + return $serializer->getDocComment($templateDoc); + } + /** * Removes method tags from the doc comment that already appear as functions inside the class. * This prevents duplicate function errors in the IDE.