Skip to content

Commit

Permalink
get from parent attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 2, 2024
1 parent 9add978 commit 235daf7
Showing 1 changed file with 3 additions and 30 deletions.
33 changes: 3 additions & 30 deletions src/Traits/AttributeResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,6 @@
namespace Go\ParserReflection\Traits;

use Go\ParserReflection\ReflectionAttribute;
use Go\ParserReflection\ReflectionException;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;

trait AttributeResolverTrait
{
Expand All @@ -30,32 +23,12 @@ public function getAttributes(?string $name = null, int $flags = 0): array
{
$attributes = [];

/** @var Class_|ClassMethod|Property|ClassConst|Function_|Param $node */
$node = $this->getNode();

foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
$attributeName = $name === null
? $attr->name->toString()
: $name;

$attributes[] = new ReflectionAttribute($attributeName, $this, $flags, $attr->args, $this->resolveTarget($attributeName, $flags));
}
}

return $attributes;
}

private function resolveTarget(string $name, int $flags): int
{
$originalAttributes = parent::getAttributes($name, $flags);

foreach ($originalAttributes as $originalAttribute) {
if ($originalAttribute->getName() === $name) {
return $originalAttribute->getTarget();
}
$attributeName = $originalAttribute->getName();
$attributes[] = new ReflectionAttribute($attributeName, $this, $flags, $originalAttribute->getArguments(), $originalAttribute->getTarget());
}

throw new ReflectionException(sprintf('target not found on attribute %s', $name));
return $attributes;
}
}

0 comments on commit 235daf7

Please sign in to comment.