Skip to content

Commit c33eacd

Browse files
authored
Add templates to Eloquent (#1634)
1 parent a25b947 commit c33eacd

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"require": {
2323
"php": "^8.2",
2424
"ext-json": "*",
25-
"barryvdh/reflection-docblock": "^2.1.2",
25+
"barryvdh/reflection-docblock": "^2.2",
2626
"composer/class-map-generator": "^1.0",
2727
"illuminate/console": "^11.15",
2828
"illuminate/database": "^11.15",

resources/views/helper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefau
5050
namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> {
5151
<?php foreach ($aliases as $alias) : ?>
5252
<?php if ($alias->getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?>
53-
/** @template TModel of static */
53+
<?= $alias->getPhpDocTemplates(' ') ?>
5454
<?php endif?>
5555
<?= $alias->getClassType() ?> <?= $alias->getShortName() ?> extends <?= $alias->getExtends() ?> {<?php if ($alias->getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?>
5656
<?php foreach ($alias->getMethods() as $method) : ?>

src/Alias.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,35 @@ public function getDocComment($prefix = "\t\t")
468468
return $serializer->getDocComment($this->phpdoc);
469469
}
470470

471+
/**
472+
* @param $prefix
473+
* @return string
474+
* @throws \ReflectionException
475+
*/
476+
public function getPhpDocTemplates($prefix = "\t\t")
477+
{
478+
$templateDoc = new DocBlock('');
479+
$serializer = new DocBlockSerializer(1, $prefix);
480+
481+
foreach ($this->classes as $class) {
482+
$reflection = new ReflectionClass($class);
483+
$traits = collect($reflection->getTraitNames());
484+
485+
foreach ($traits as $trait) {
486+
$phpdoc = new DocBlock(new ReflectionClass($trait));
487+
$templates = $phpdoc->getTagsByName('template');
488+
489+
/** @var DocBlock\Tag\TemplateTag $template */
490+
foreach ($templates as $template) {
491+
$template->setBound('static');
492+
$template->setDocBlock($templateDoc);
493+
$templateDoc->appendTag($template);
494+
}
495+
}
496+
}
497+
return $serializer->getDocComment($templateDoc);
498+
}
499+
471500
/**
472501
* Removes method tags from the doc comment that already appear as functions inside the class.
473502
* This prevents duplicate function errors in the IDE.

0 commit comments

Comments
 (0)