| 1: | <?php |
| 2: | |
| 3: | declare(strict_types=1); |
| 4: | |
| 5: | namespace PHPStan\BetterReflection\Reflection; |
| 6: | |
| 7: | use LogicException; |
| 8: | use PhpParser\Node\Identifier; |
| 9: | use PhpParser\Node\Name; |
| 10: | use ReflectionClass as CoreReflectionClass; |
| 11: | use PHPStan\BetterReflection\Reflector\Reflector; |
| 12: | |
| 13: | use function array_key_exists; |
| 14: | use function assert; |
| 15: | use function sprintf; |
| 16: | use function strtolower; |
| 17: | |
| 18: | |
| 19: | class ReflectionNamedType extends ReflectionType |
| 20: | { |
| 21: | private Reflector $reflector; |
| 22: | |
| 23: | |
| 24: | |
| 25: | private $owner; |
| 26: | |
| 27: | |
| 28: | |
| 29: | private const BUILT_IN_TYPES = [ |
| 30: | 'int' => null, |
| 31: | 'float' => null, |
| 32: | 'string' => null, |
| 33: | 'bool' => null, |
| 34: | 'callable' => null, |
| 35: | 'self' => null, |
| 36: | 'parent' => null, |
| 37: | 'array' => null, |
| 38: | 'iterable' => null, |
| 39: | 'object' => null, |
| 40: | 'void' => null, |
| 41: | 'mixed' => null, |
| 42: | 'static' => null, |
| 43: | 'null' => null, |
| 44: | 'never' => null, |
| 45: | 'false' => null, |
| 46: | 'true' => null, |
| 47: | ]; |
| 48: | |
| 49: | |
| 50: | private string $name; |
| 51: | |
| 52: | private bool $isIdentifier; |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | public function __construct( |
| 58: | Reflector $reflector, |
| 59: | $owner, |
| 60: | $type |
| 61: | ) { |
| 62: | $this->reflector = $reflector; |
| 63: | $this->owner = $owner; |
| 64: | $this->name = $type->toString(); |
| 65: | $this->isIdentifier = $type instanceof Identifier; |
| 66: | } |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | public function exportToCache(): array |
| 72: | { |
| 73: | return [ |
| 74: | 'name' => $this->name, |
| 75: | 'isIdentifier' => $this->isIdentifier, |
| 76: | ]; |
| 77: | } |
| 78: | |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | public static function importFromCache(Reflector $reflector, array $data, $owner): self |
| 84: | { |
| 85: | $reflection = new CoreReflectionClass(self::class); |
| 86: | |
| 87: | $ref = $reflection->newInstanceWithoutConstructor(); |
| 88: | $ref->reflector = $reflector; |
| 89: | $ref->owner = $owner; |
| 90: | $ref->name = $data['name']; |
| 91: | $ref->isIdentifier = $data['isIdentifier']; |
| 92: | |
| 93: | return $ref; |
| 94: | } |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | public function withOwner($owner) |
| 100: | { |
| 101: | $clone = clone $this; |
| 102: | $clone->owner = $owner; |
| 103: | |
| 104: | return $clone; |
| 105: | } |
| 106: | |
| 107: | |
| 108: | public function getName(): string |
| 109: | { |
| 110: | return $this->name; |
| 111: | } |
| 112: | |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | public function isBuiltin(): bool |
| 119: | { |
| 120: | return array_key_exists(strtolower($this->name), self::BUILT_IN_TYPES); |
| 121: | } |
| 122: | |
| 123: | public function getClass(): ReflectionClass |
| 124: | { |
| 125: | if (! $this->isBuiltin()) { |
| 126: | return $this->reflector->reflectClass($this->name); |
| 127: | } |
| 128: | |
| 129: | if ( |
| 130: | $this->owner instanceof ReflectionEnum |
| 131: | || $this->owner instanceof ReflectionFunction |
| 132: | || ($this->owner instanceof ReflectionParameter && $this->owner->getDeclaringFunction() instanceof ReflectionFunction) |
| 133: | ) { |
| 134: | throw new LogicException(sprintf('The type %s cannot be resolved to class', $this->name)); |
| 135: | } |
| 136: | |
| 137: | $lowercaseName = strtolower($this->name); |
| 138: | |
| 139: | if ($lowercaseName === 'self') { |
| 140: | $class = $this->owner->getImplementingClass(); |
| 141: | assert($class instanceof ReflectionClass); |
| 142: | |
| 143: | return $class; |
| 144: | } |
| 145: | |
| 146: | if ($lowercaseName === 'parent') { |
| 147: | $class = $this->owner->getDeclaringClass(); |
| 148: | assert($class instanceof ReflectionClass); |
| 149: | $parentClass = $class->getParentClass(); |
| 150: | assert($parentClass instanceof ReflectionClass); |
| 151: | |
| 152: | return $parentClass; |
| 153: | } |
| 154: | |
| 155: | if ( |
| 156: | $this->owner instanceof ReflectionMethod |
| 157: | && $lowercaseName === 'static' |
| 158: | ) { |
| 159: | return $this->owner->getCurrentClass(); |
| 160: | } |
| 161: | |
| 162: | throw new LogicException(sprintf('The type %s cannot be resolved to class', $this->name)); |
| 163: | } |
| 164: | |
| 165: | public function allowsNull(): bool |
| 166: | { |
| 167: | switch (strtolower($this->name)) { |
| 168: | case 'mixed': |
| 169: | return true; |
| 170: | case 'null': |
| 171: | return true; |
| 172: | default: |
| 173: | return false; |
| 174: | } |
| 175: | } |
| 176: | |
| 177: | public function isIdentifier(): bool |
| 178: | { |
| 179: | return $this->isIdentifier; |
| 180: | } |
| 181: | |
| 182: | |
| 183: | public function __toString(): string |
| 184: | { |
| 185: | return $this->getName(); |
| 186: | } |
| 187: | } |
| 188: | |