1: | <?php |
2: | |
3: | declare(strict_types=1); |
4: | |
5: | namespace PHPStan\BetterReflection\Reflector; |
6: | |
7: | use PHPStan\BetterReflection\Identifier\Identifier; |
8: | use PHPStan\BetterReflection\Identifier\IdentifierType; |
9: | use PHPStan\BetterReflection\Reflection\ReflectionClass; |
10: | use PHPStan\BetterReflection\Reflection\ReflectionConstant; |
11: | use PHPStan\BetterReflection\Reflection\ReflectionFunction; |
12: | use PHPStan\BetterReflection\Reflector\Exception\IdentifierNotFound; |
13: | use PHPStan\BetterReflection\SourceLocator\Type\SourceLocator; |
14: | |
15: | use function assert; |
16: | |
17: | final class DefaultReflector implements Reflector |
18: | { |
19: | private SourceLocator $sourceLocator; |
20: | public function __construct(SourceLocator $sourceLocator) |
21: | { |
22: | $this->sourceLocator = $sourceLocator; |
23: | } |
24: | |
25: | |
26: | |
27: | |
28: | |
29: | |
30: | public function reflectClass(string $identifierName): ReflectionClass |
31: | { |
32: | $identifier = new Identifier($identifierName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)); |
33: | |
34: | $classInfo = $this->sourceLocator->locateIdentifier($this, $identifier); |
35: | |
36: | if ($classInfo === null) { |
37: | throw Exception\IdentifierNotFound::fromIdentifier($identifier); |
38: | } |
39: | |
40: | assert($classInfo instanceof ReflectionClass); |
41: | |
42: | return $classInfo; |
43: | } |
44: | |
45: | |
46: | |
47: | |
48: | |
49: | |
50: | public function reflectAllClasses(): iterable |
51: | { |
52: | |
53: | $allClasses = $this->sourceLocator->locateIdentifiersByType( |
54: | $this, |
55: | new IdentifierType(IdentifierType::IDENTIFIER_CLASS), |
56: | ); |
57: | |
58: | return $allClasses; |
59: | } |
60: | |
61: | |
62: | |
63: | |
64: | |
65: | |
66: | public function reflectFunction(string $identifierName): ReflectionFunction |
67: | { |
68: | $identifier = new Identifier($identifierName, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)); |
69: | |
70: | $functionInfo = $this->sourceLocator->locateIdentifier($this, $identifier); |
71: | |
72: | if ($functionInfo === null) { |
73: | throw Exception\IdentifierNotFound::fromIdentifier($identifier); |
74: | } |
75: | |
76: | assert($functionInfo instanceof ReflectionFunction); |
77: | |
78: | return $functionInfo; |
79: | } |
80: | |
81: | |
82: | |
83: | |
84: | |
85: | |
86: | public function reflectAllFunctions(): iterable |
87: | { |
88: | |
89: | $allFunctions = $this->sourceLocator->locateIdentifiersByType( |
90: | $this, |
91: | new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION), |
92: | ); |
93: | |
94: | return $allFunctions; |
95: | } |
96: | |
97: | |
98: | |
99: | |
100: | |
101: | |
102: | public function reflectConstant(string $identifierName): ReflectionConstant |
103: | { |
104: | $identifier = new Identifier($identifierName, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT)); |
105: | |
106: | $constantInfo = $this->sourceLocator->locateIdentifier($this, $identifier); |
107: | |
108: | if ($constantInfo === null) { |
109: | throw Exception\IdentifierNotFound::fromIdentifier($identifier); |
110: | } |
111: | |
112: | assert($constantInfo instanceof ReflectionConstant); |
113: | |
114: | return $constantInfo; |
115: | } |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | public function reflectAllConstants(): iterable |
123: | { |
124: | |
125: | $allConstants = $this->sourceLocator->locateIdentifiersByType( |
126: | $this, |
127: | new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT), |
128: | ); |
129: | |
130: | return $allConstants; |
131: | } |
132: | } |
133: | |