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: /**
20: * @var \PHPStan\BetterReflection\SourceLocator\Type\SourceLocator
21: */
22: private $sourceLocator;
23: public function __construct(SourceLocator $sourceLocator)
24: {
25: $this->sourceLocator = $sourceLocator;
26: }
27:
28: /**
29: * Create a ReflectionClass for the specified $className.
30: *
31: * @throws IdentifierNotFound
32: */
33: public function reflectClass(string $identifierName): ReflectionClass
34: {
35: $identifier = new Identifier($identifierName, new IdentifierType(IdentifierType::IDENTIFIER_CLASS));
36:
37: $classInfo = $this->sourceLocator->locateIdentifier($this, $identifier);
38:
39: if ($classInfo === null) {
40: throw Exception\IdentifierNotFound::fromIdentifier($identifier);
41: }
42:
43: assert($classInfo instanceof ReflectionClass);
44:
45: return $classInfo;
46: }
47:
48: /**
49: * Get all the classes available in the scope specified by the SourceLocator.
50: *
51: * @return list<ReflectionClass>
52: */
53: public function reflectAllClasses(): iterable
54: {
55: /** @var list<ReflectionClass> $allClasses */
56: $allClasses = $this->sourceLocator->locateIdentifiersByType($this, new IdentifierType(IdentifierType::IDENTIFIER_CLASS));
57:
58: return $allClasses;
59: }
60:
61: /**
62: * Create a ReflectionFunction for the specified $functionName.
63: *
64: * @throws IdentifierNotFound
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: * Get all the functions available in the scope specified by the SourceLocator.
83: *
84: * @return list<ReflectionFunction>
85: */
86: public function reflectAllFunctions(): iterable
87: {
88: /** @var list<ReflectionFunction> $allFunctions */
89: $allFunctions = $this->sourceLocator->locateIdentifiersByType($this, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION));
90:
91: return $allFunctions;
92: }
93:
94: /**
95: * Create a ReflectionConstant for the specified $constantName.
96: *
97: * @throws IdentifierNotFound
98: */
99: public function reflectConstant(string $identifierName): ReflectionConstant
100: {
101: $identifier = new Identifier($identifierName, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT));
102:
103: $constantInfo = $this->sourceLocator->locateIdentifier($this, $identifier);
104:
105: if ($constantInfo === null) {
106: throw Exception\IdentifierNotFound::fromIdentifier($identifier);
107: }
108:
109: assert($constantInfo instanceof ReflectionConstant);
110:
111: return $constantInfo;
112: }
113:
114: /**
115: * Get all the constants available in the scope specified by the SourceLocator.
116: *
117: * @return list<ReflectionConstant>
118: */
119: public function reflectAllConstants(): iterable
120: {
121: /** @var list<ReflectionConstant> $allConstants */
122: $allConstants = $this->sourceLocator->locateIdentifiersByType($this, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT));
123:
124: return $allConstants;
125: }
126: }
127: