Skip to content

Commit

Permalink
Add templates to Eloquent
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Dec 28, 2024
1 parent a25b947 commit 0abcb74
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion resources/views/helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public static function <?= $method->getName() ?>(<?= $method->getParamsWithDefau
namespace <?= $namespace === '__root' ? '' : trim($namespace, '\\') ?> {
<?php foreach ($aliases as $alias) : ?>
<?php if ($alias->getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?>
/** @template TModel of static */
<?= $alias->getPhpDocTemplates(' ') ?>
<?php endif?>
<?= $alias->getClassType() ?> <?= $alias->getShortName() ?> extends <?= $alias->getExtends() ?> {<?php if ($alias->getExtendsNamespace() === '\Illuminate\Database\Eloquent') : ?>
<?php foreach ($alias->getMethods() as $method) : ?>
Expand Down
29 changes: 29 additions & 0 deletions src/Alias.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 0abcb74

Please sign in to comment.