Skip to content

Commit

Permalink
add getAttributes() method from Node
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Feb 1, 2024
1 parent c26dd19 commit d30f29e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
26 changes: 17 additions & 9 deletions src/ReflectionAttribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
use Go\ParserReflection\Traits\InternalPropertiesEmulationTrait;
use ReflectionAttribute as BaseReflectionAttribute;
use PhpParser\Node;
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;
use Reflector;

/**
Expand All @@ -23,19 +28,22 @@
class ReflectionAttribute extends BaseReflectionAttribute
{
public function __construct(
string $attributeName,
private ?Node\Attribute $attributeNode = null
private string $attributeName,
int $flags = 0,
private Class_|ClassMethod|Function_|ClassConst|Property $attributeHolder
) {
$this->attributeNode ??= ReflectionEngine::parseAttribute($attributeName);
}

public function __debugInfo(): array
{
return [];
}

public function getNode(): Node\Attribute
{
return $this->attributeNode;
foreach ($this->attributeHolder->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() === $this->attributeName) {
return $attr;
}
}
}

throw new ReflectionException(sprintf('attribute %s not exists', $this->attributeName));
}
}
5 changes: 0 additions & 5 deletions src/ReflectionEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,6 @@ public static function parseClass(string $fullClassName): ClassLike
throw new InvalidArgumentException("Class $fullClassName was not found in the $classFileName");
}

public static function parseAttribute(string $attributeName): Node\Attribute
{
throw new Exception('to be implemented');
}

/**
* Loop through an array and find a ClassLike statement by the given class name.
*
Expand Down
58 changes: 43 additions & 15 deletions src/Traits/AttributeResolverTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,17 @@

use Go\ParserReflection\ReflectionAttribute;
use Go\ParserReflection\ReflectionClass;
use Go\ParserReflection\ReflectionClassConstant;
use Go\ParserReflection\ReflectionException;
use Go\ParserReflection\ReflectionFunction;
use Go\ParserReflection\ReflectionMethod;
use Go\ParserReflection\ReflectionProperty;
use ReflectionClassConstant;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Property;

trait AttributeResolverTrait
{
Expand All @@ -27,22 +33,44 @@ trait AttributeResolverTrait
*/
public function getAttributes(?string $name = null, int $flags = 0): array
{
if (
$this instanceof ReflectionClass ||
$this instanceof ReflectionFunction ||
$this instanceof ReflectionMethod ||
$this instanceof ReflectionProperty ||
$this instanceof ReflectionClassConstant
) {
$attributes = $this->getAttributes($name, $flags);
$reflectionAttributes = [];
foreach ($attributes as $attribute) {
$reflectionAttributes[] = new ReflectionAttribute($attribute->getName());
}
/** @var ClassLike|ClassMethod|Property|ClassConst|Function_|null $attributeHolder */
$attributeHolder = null;

if ($this instanceof ReflectionClass) {
$attributeHolder = $this->classLikeNode;
}

if ($this instanceof ReflectionMethod) {
$attributeHolder = $this->classMethodNode;
}

if ($this instanceof ReflectionProperty) {
$attributeHolder = $this->propertyNode;
}

return $reflectionAttributes;
if ($this instanceof ReflectionClassConstant) {
$attributeHolder = $this->classConstNode;
}

if ($this instanceof ReflectionFunction) {
$attributeHolder = $this->functionNode;
}

if ($attributeHolder === null) {
throw new ReflectionException(sprintf('Attribute on %s not supported yet'), $this::class);
}

$attributes = [];
foreach ($attributeHolder->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($name !== null) {
return [new ReflectionAttribute($name, $flags, $attributeHolder)];
}

$attributes[] = new ReflectionAttribute($attr->toString(), $flags, $attributeHolder);
}
}

throw new ReflectionException('not yet supported');
return $attributes;
}
}

0 comments on commit d30f29e

Please sign in to comment.