| 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: | |
| 21: | |
| 22: | private $sourceLocator; |
| 23: | public function __construct(SourceLocator $sourceLocator) |
| 24: | { |
| 25: | $this->sourceLocator = $sourceLocator; |
| 26: | } |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 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: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | public function reflectAllClasses(): iterable |
| 54: | { |
| 55: | |
| 56: | $allClasses = $this->sourceLocator->locateIdentifiersByType($this, new IdentifierType(IdentifierType::IDENTIFIER_CLASS)); |
| 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($this, new IdentifierType(IdentifierType::IDENTIFIER_FUNCTION)); |
| 90: | |
| 91: | return $allFunctions; |
| 92: | } |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 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: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | public function reflectAllConstants(): iterable |
| 120: | { |
| 121: | |
| 122: | $allConstants = $this->sourceLocator->locateIdentifiersByType($this, new IdentifierType(IdentifierType::IDENTIFIER_CONSTANT)); |
| 123: | |
| 124: | return $allConstants; |
| 125: | } |
| 126: | } |
| 127: | |