-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathReflectionAttribute.php
More file actions
138 lines (120 loc) · 3.93 KB
/
ReflectionAttribute.php
File metadata and controls
138 lines (120 loc) · 3.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
declare(strict_types=1);
/**
* Parser Reflection API
*
* @copyright Copyright 2015, Lisachenko Alexander <lisachenko.it@gmail.com>
*
* This source file is subject to the license that is bundled
* with this source code in the file LICENSE.
*/
namespace Go\ParserReflection;
use Go\ParserReflection\Resolver\NodeExpressionResolver;
use PhpParser\Node;
use PhpParser\Node\Param;
use PhpParser\Node\PropertyItem;
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 ReflectionAttribute as BaseReflectionAttribute;
/**
* ref original usage https://3v4l.org/duaQI
*
* @extends \ReflectionAttribute<object>
*/
class ReflectionAttribute extends BaseReflectionAttribute
{
/**
* Fully-qualified attribute class name.
*
* @var class-string<object>
*/
private string $attributeName;
/**
* @param class-string<object> $attributeName
* @param array<int, mixed> $arguments
*/
public function __construct(
string $attributeName,
private ReflectionClass|ReflectionMethod|ReflectionProperty|ReflectionClassConstant|ReflectionFunction|ReflectionParameter $reflector,
private array $arguments,
private bool $isRepeated,
) {
$this->attributeName = $attributeName;
}
public function getNode(): Node\Attribute
{
$reflectorNode = $this->reflector->getNode();
// attrGroups only exists in Property Stmt (not PropertyItem), so switch to the type node
if ($reflectorNode instanceof PropertyItem && $this->reflector instanceof ReflectionProperty) {
$node = $this->reflector->getTypeNode();
} else {
$node = $reflectorNode;
}
if ($node instanceof PropertyItem) {
throw new ReflectionException('ReflectionAttribute cannot resolve attrGroups from a PropertyItem node');
}
$nodeExpressionResolver = new NodeExpressionResolver($this);
foreach ($node->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
$attributeNodeName = $attr->name;
// Unpack fully-resolved class name from attribute if we have it
if ($attributeNodeName->hasAttribute('resolvedName')) {
$resolvedName = $attributeNodeName->getAttribute('resolvedName');
if ($resolvedName instanceof \PhpParser\Node\Name) {
$attributeNodeName = $resolvedName;
}
}
if ($attributeNodeName->toString() !== $this->attributeName) {
continue;
}
$arguments = [];
foreach ($attr->args as $arg) {
$nodeExpressionResolver->process($arg->value);
$arguments[] = $nodeExpressionResolver->getValue();
}
if ($arguments !== $this->arguments) {
continue;
}
return $attr;
}
}
throw new ReflectionException('ReflectionAttribute should be initiated from Go\ParserReflection Reflection classes');
}
public function isRepeated(): bool
{
return $this->isRepeated;
}
/**
* {@inheritDoc}
*
* @return array<int, mixed>
*/
public function getArguments(): array
{
return $this->arguments;
}
/**
* {@inheritDoc}
*/
public function getName(): string
{
return $this->attributeName;
}
/**
* {@inheritDoc}
*/
public function getTarget(): int
{
throw new \RuntimeException(sprintf('cannot get target from %s', $this::class));
}
/**
* {@inheritDoc}
*/
public function newInstance(): object
{
throw new \RuntimeException(sprintf('cannot create new instance from %s', $this::class));
}
}