-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
ClassLikeReflectionMemberCollection.php
340 lines (298 loc) · 12.2 KB
/
ClassLikeReflectionMemberCollection.php
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
namespace Phpactor\WorseReflection\Core\Reflection\Collection;
use Closure;
use Microsoft\PhpParser\Node;
use Microsoft\PhpParser\Node\ClassConstDeclaration;
use Microsoft\PhpParser\Node\ClassMembersNode;
use Microsoft\PhpParser\Node\EnumCaseDeclaration;
use Microsoft\PhpParser\Node\Expression\AssignmentExpression;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
use Microsoft\PhpParser\Node\Expression\Variable;
use Microsoft\PhpParser\Node\MethodDeclaration;
use Microsoft\PhpParser\Node\Parameter;
use Microsoft\PhpParser\Node\PropertyDeclaration;
use Microsoft\PhpParser\Node\Statement\ClassDeclaration;
use Microsoft\PhpParser\Node\Statement\EnumDeclaration;
use Microsoft\PhpParser\Node\Statement\InterfaceDeclaration;
use Microsoft\PhpParser\Node\Statement\TraitDeclaration;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionConstant;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionEnum;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionEnumCase as PhpactorReflectionEnumCase;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionInterface;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionMethod;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionPromotedProperty;
use Phpactor\WorseReflection\Bridge\TolerantParser\Reflection\ReflectionProperty;
use Phpactor\WorseReflection\Core\ClassName;
use Phpactor\WorseReflection\Core\Exception\NotFound;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClass;
use Phpactor\WorseReflection\Core\Reflection\ReflectionClassLike;
use Phpactor\WorseReflection\Core\Reflection\ReflectionConstant as PhpactorReflectionConstant;
use Phpactor\WorseReflection\Core\Reflection\ReflectionMember;
use Phpactor\WorseReflection\Core\Reflection\ReflectionMethod as PhpactorReflectionMethod;
use Phpactor\WorseReflection\Core\Reflection\ReflectionProperty as PhpactorReflectionProperty;
use Phpactor\WorseReflection\Core\Reflection\ReflectionTrait;
use Phpactor\WorseReflection\Core\ServiceLocator;
use Traversable;
/**
* @extends AbstractReflectionCollection<ReflectionMember>
* @implements ReflectionMemberCollection<ReflectionMember>
*/
final class ClassLikeReflectionMemberCollection extends AbstractReflectionCollection implements ReflectionMemberCollection
{
private const MEMBER_TYPES = [
'constants',
'properties',
'methods',
'enumCases',
];
/**
* @var PhpactorReflectionConstant[]
*/
private array $constants = [];
/**
* @var PhpactorReflectionProperty[]
*/
private array $properties = [];
/**
* @var PhpactorReflectionMethod[]
*/
private array $methods = [];
/**
* @var PhpactorReflectionEnumCase[]
*/
private array $enumCases = [];
public static function fromClassMemberDeclarations(
ServiceLocator $serviceLocator,
ClassDeclaration|ObjectCreationExpression $class,
ReflectionClass $reflectionClass
): self {
if (!$class->classMembers instanceof ClassMembersNode) {
throw new NotFound('ObjectCreationExpression does not contain anonymous class');
}
return self::fromDeclarations(
$serviceLocator,
$reflectionClass,
$class->classMembers->classMemberDeclarations,
);
}
public static function fromTraitMemberDeclarations(ServiceLocator $serviceLocator, TraitDeclaration $traitDeclaration, ReflectionTrait $reflectionTrait): self
{
return self::fromDeclarations(
$serviceLocator,
$reflectionTrait,
$traitDeclaration->traitMembers->traitMemberDeclarations,
);
}
public static function fromInterfaceMemberDeclarations(ServiceLocator $serviceLocator, InterfaceDeclaration $interfaceDeclaration, ReflectionInterface $reflectionInterface): self
{
return self::fromDeclarations(
$serviceLocator,
$reflectionInterface,
$interfaceDeclaration->interfaceMembers->interfaceMemberDeclarations,
);
}
public static function fromEnumMemberDeclarations(ServiceLocator $serviceLocator, EnumDeclaration $enumDeclaration, ReflectionEnum $reflectionEnum): self
{
return self::fromDeclarations(
$serviceLocator,
$reflectionEnum,
$enumDeclaration->enumMembers->enumMemberDeclarations
);
}
public function getIterator(): Traversable
{
foreach (self::MEMBER_TYPES as $collection) {
yield from $this->$collection;
}
}
public function merge(ReflectionCollection $collection): AbstractReflectionCollection
{
$new = clone $this;
foreach ($collection as $member) {
$new->items[$member->name()] = $member;
if ($member instanceof ReflectionConstant) {
$new->constants[$member->name()] = $member;
continue;
}
if ($member instanceof PhpactorReflectionProperty) {
$new->properties[$member->name()] = $member;
continue;
}
if ($member instanceof PhpactorReflectionMethod) {
$new->methods[$member->name()] = $member;
continue;
}
if ($member instanceof PhpactorReflectionEnumCase) {
$new->enumCases[$member->name()] = $member;
continue;
}
}
return $new;
}
/**
* @return static<ReflectionMember>
*/
public function byMemberClass(string $fqn): ReflectionCollection
{
return $this->filter(function (ReflectionMember $member) use ($fqn) {
return $member instanceof $fqn;
});
}
/**
* @return self
*/
public function byMemberType(string $type): ReflectionCollection
{
return $this->filter(function (ReflectionMember $member) use ($type) {
return $member->memberType() === $type;
});
}
public function byVisibilities(array $visibilities): ReflectionMemberCollection
{
return $this->filter(function (ReflectionMember $member) use ($visibilities) {
foreach ($visibilities as $visiblity) {
if ($visiblity == $member->visibility()) {
return true;
}
}
return false;
});
}
public function belongingTo(ClassName $class): ReflectionMemberCollection
{
return $this->filter(function (ReflectionMember $member) use ($class) {
return $member->declaringClass()->name() == $class;
});
}
public function atOffset(int $offset): ReflectionMemberCollection
{
return $this->filter(function (ReflectionMember $member) use ($offset) {
return $member->position()->start()->toInt() <= $offset && $member->position()->end()->toInt() >= $offset;
});
}
public function byName(string $name): ReflectionMemberCollection
{
return $this->filter(function (ReflectionMember $member) use ($name) {
return $member->name() === $name;
});
}
public function virtual(): ReflectionMemberCollection
{
return $this->filter(fn (ReflectionMember $member) => $member->isVirtual());
}
public function real(): ReflectionMemberCollection
{
return $this->filter(fn (ReflectionMember $member) => !$member->isVirtual());
}
public function methods(): ReflectionMethodCollection
{
return new ReflectionMethodCollection($this->methods);
}
public function properties(): ReflectionPropertyCollection
{
return new ReflectionPropertyCollection($this->properties);
}
public function constants(): ReflectionConstantCollection
{
return new ReflectionConstantCollection($this->constants);
}
public function enumCases(): ReflectionEnumCaseCollection
{
return new ReflectionEnumCaseCollection($this->enumCases);
}
public function map(Closure $closure)
{
$new = new self([]);
foreach (self::MEMBER_TYPES as $collection) {
/** @phpstan-ignore-next-line */
$new->$collection = array_map($closure, $this->$collection);
}
$new->items = array_merge(...array_map(fn (string $type) => $this->$type, self::MEMBER_TYPES));
return $new;
}
/**
* @param array<Node> $nodes
*/
private static function fromDeclarations(ServiceLocator $serviceLocator, ReflectionClassLike $classLike, array $nodes): self
{
$new = new self([]);
foreach ($nodes as $member) {
if ($member instanceof ClassConstDeclaration) {
/** @phpstan-ignore-next-line TP: lie */
if (!$member->constElements) {
continue;
}
foreach ($member->constElements->getElements() as $constElement) {
$constant = new ReflectionConstant($serviceLocator, $classLike, $member, $constElement);
$new->constants[$constant->name()] = $constant;
$new->items[$constant->name()] = $constant;
}
continue;
}
if ($member instanceof PropertyDeclaration) {
/** @phpstan-ignore-next-line TP lies */
foreach ($member->propertyElements as $propertyElement) {
foreach ($propertyElement as $variable) {
if ($variable instanceof AssignmentExpression) {
$variable = $variable->leftOperand;
}
if (false === $variable instanceof Variable) {
continue;
}
$property = new ReflectionProperty($serviceLocator, $classLike, $member, $variable);
$new->properties[$property->name()] = $property;
$new->items[$property->name()] = $property;
}
}
continue;
}
if ($member instanceof MethodDeclaration) {
$method = new ReflectionMethod($serviceLocator, $classLike, $member);
$new->items[$method->name()] = $method;
$new->methods[$method->name()] = $method;
// promoted properties
if ($method->name() === '__construct') {
$parameters = $member->parameters;
/** @phpstan-ignore-next-line */
if (!$parameters) {
continue;
}
$children = $parameters->children;
if (!$children) {
continue;
}
foreach (array_filter($children, function ($member) {
if (!$member instanceof Parameter) {
return false;
}
return $member->visibilityToken !== null;
}) as $promotedParameter) {
if (!$promotedParameter instanceof Parameter) {
continue;
}
$property = new ReflectionPromotedProperty($serviceLocator, $classLike, $promotedParameter);
$new->items[$property->name()] = $property;
$new->properties[$property->name()] = $property;
}
}
continue;
}
if ($member instanceof EnumCaseDeclaration && $classLike instanceof ReflectionEnum) {
$enumCase = new PhpactorReflectionEnumCase($serviceLocator, $classLike, $member);
$new->items[$enumCase->name()] = $enumCase;
$new->enumCases[$enumCase->name()] = $enumCase;
continue;
}
}
return $new;
}
private function filter(Closure $closure): self
{
$new = new self([]);
foreach (self::MEMBER_TYPES as $collection) {
$new->$collection = array_filter($this->$collection, $closure);
}
$new->items = array_merge(...array_map(fn (string $type) => $new->$type, self::MEMBER_TYPES));
return $new;
}
}