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: * Create a ReflectionClass for the specified $className.
27: *
28: * @throws IdentifierNotFound
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: * Get all the classes available in the scope specified by the SourceLocator.
47: *
48: * @return list<ReflectionClass>
49: */
50: public function reflectAllClasses(): iterable
51: {
52: /** @var list<ReflectionClass> $allClasses */
53: $allClasses = $this->sourceLocator->locateIdentifiersByType(
54: $this,
55: new IdentifierType(IdentifierType::IDENTIFIER_CLASS),
56: );
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(
90: $this,
91: new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION),
92: );
93:
94: return $allFunctions;
95: }
96:
97: /**
98: * Create a ReflectionConstant for the specified $constantName.
99: *
100: * @throws IdentifierNotFound
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: * Get all the constants available in the scope specified by the SourceLocator.
119: *
120: * @return list<ReflectionConstant>
121: */
122: public function reflectAllConstants(): iterable
123: {
124: /** @var list<ReflectionConstant> $allConstants */
125: $allConstants = $this->sourceLocator->locateIdentifiersByType(
126: $this,
127: new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT),
128: );
129:
130: return $allConstants;
131: }
132: }
133: