-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
ReflectionTraitCollection.php
95 lines (82 loc) · 3.64 KB
/
ReflectionTraitCollection.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
<?php
namespace Phpactor\WorseReflection\Core\Reflection\Collection;
use Microsoft\PhpParser\Node\ClassMembersNode;
use Microsoft\PhpParser\Node\Expression\ObjectCreationExpression;
use Microsoft\PhpParser\Node\Statement\EnumDeclaration;
use Microsoft\PhpParser\Node\Statement\TraitDeclaration;
use Phpactor\WorseReflection\Core\Exception\NotFound;
use Phpactor\WorseReflection\Core\Reflection\ReflectionTrait;
use Phpactor\WorseReflection\Core\ServiceLocator;
use Microsoft\PhpParser\Node\Statement\ClassDeclaration;
use Phpactor\WorseReflection\Core\ClassName;
use Microsoft\PhpParser\Node\TraitUseClause;
use Phpactor\WorseReflection\Bridge\TolerantParser\Patch\TolerantQualifiedNameResolver;
/**
* @extends AbstractReflectionCollection<ReflectionTrait>
*/
class ReflectionTraitCollection extends AbstractReflectionCollection
{
public static function fromClassDeclaration(
ServiceLocator $serviceLocator,
ClassDeclaration|ObjectCreationExpression $class,
): self {
if (!$class->classMembers instanceof ClassMembersNode) {
throw new NotFound('ObjectCreationExpression does not contain anonymous class');
}
$items = [];
foreach ($class->classMembers->classMemberDeclarations as $memberDeclaration) {
if (false === $memberDeclaration instanceof TraitUseClause) {
continue;
}
if ($memberDeclaration->traitNameList === null) {
continue;
}
foreach ($memberDeclaration->traitNameList->getValues() as $traitName) {
$traitName = TolerantQualifiedNameResolver::getResolvedName($traitName);
try {
$items[(string) $traitName] = $serviceLocator->reflector()->reflectTrait(ClassName::fromString($traitName));
} catch (NotFound) {
}
}
}
return new self($items);
}
/**
* @param array<string,bool> $visited
*/
public static function fromTraitDeclaration(ServiceLocator $serviceLocator, TraitDeclaration $traitDeclaration, array $visited = []): self
{
$items = [];
foreach ($traitDeclaration->traitMembers->traitMemberDeclarations as $memberDeclaration) {
if (false === $memberDeclaration instanceof TraitUseClause) {
continue;
}
foreach ($memberDeclaration->traitNameList->getValues() as $traitName) {
$traitName = TolerantQualifiedNameResolver::getResolvedName($traitName);
try {
$items[(string) $traitName] = $serviceLocator->reflector()->reflectTrait(ClassName::fromString($traitName), $visited);
} catch (NotFound) {
}
}
}
return new self($items);
}
public static function fromEnumDeclaration(ServiceLocator $serviceLocator, EnumDeclaration $enumDeclaration): ReflectionTraitCollection
{
$items = [];
/** @phpstan-ignore-next-line not trusting TP */
foreach ($enumDeclaration?->enumMembers?->enumMemberDeclarations ?? [] as $memberDeclaration) {
if (false === $memberDeclaration instanceof TraitUseClause) {
continue;
}
foreach ($memberDeclaration->traitNameList->getValues() as $traitName) {
$traitName = TolerantQualifiedNameResolver::getResolvedName($traitName);
try {
$items[(string) $traitName] = $serviceLocator->reflector()->reflectTrait(ClassName::fromString($traitName));
} catch (NotFound) {
}
}
}
return new self($items);
}
}