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: /** @psalm-immutable */
19: class ReflectionNamedType extends ReflectionType
20: {
21: private Reflector $reflector;
22: /**
23: * @var \PHPStan\BetterReflection\Reflection\ReflectionParameter|\PHPStan\BetterReflection\Reflection\ReflectionMethod|\PHPStan\BetterReflection\Reflection\ReflectionFunction|\PHPStan\BetterReflection\Reflection\ReflectionEnum|\PHPStan\BetterReflection\Reflection\ReflectionProperty|\PHPStan\BetterReflection\Reflection\ReflectionClassConstant
24: */
25: private $owner;
26: /**
27: * @var mixed[]
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: /** @var non-empty-string */
50: private string $name;
51:
52: private bool $isIdentifier;
53:
54: /** @internal
55: * @param \PHPStan\BetterReflection\Reflection\ReflectionParameter|\PHPStan\BetterReflection\Reflection\ReflectionMethod|\PHPStan\BetterReflection\Reflection\ReflectionFunction|\PHPStan\BetterReflection\Reflection\ReflectionEnum|\PHPStan\BetterReflection\Reflection\ReflectionProperty|\PHPStan\BetterReflection\Reflection\ReflectionClassConstant $owner
56: * @param \PhpParser\Node\Identifier|\PhpParser\Node\Name $type */
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: * @return array<string, mixed>
70: */
71: public function exportToCache(): array
72: {
73: return [
74: 'name' => $this->name,
75: 'isIdentifier' => $this->isIdentifier,
76: ];
77: }
78:
79: /**
80: * @param array<string, mixed> $data
81: * @param ReflectionParameter|ReflectionMethod|ReflectionFunction|ReflectionEnum|ReflectionProperty|ReflectionClassConstant $owner
82: */
83: public static function importFromCache(Reflector $reflector, array $data, $owner): self
84: {
85: $reflection = new CoreReflectionClass(self::class);
86: /** @var self $ref */
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: /** @internal
97: * @param \PHPStan\BetterReflection\Reflection\ReflectionParameter|\PHPStan\BetterReflection\Reflection\ReflectionMethod|\PHPStan\BetterReflection\Reflection\ReflectionFunction|\PHPStan\BetterReflection\Reflection\ReflectionEnum|\PHPStan\BetterReflection\Reflection\ReflectionProperty|\PHPStan\BetterReflection\Reflection\ReflectionClassConstant $owner
98: * @return static */
99: public function withOwner($owner)
100: {
101: $clone = clone $this;
102: $clone->owner = $owner;
103:
104: return $clone;
105: }
106:
107: /** @return non-empty-string */
108: public function getName(): string
109: {
110: return $this->name;
111: }
112:
113: /**
114: * Checks if it is a built-in type (i.e., it's not an object...)
115: *
116: * @see https://php.net/manual/en/reflectiontype.isbuiltin.php
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: /** @return non-empty-string */
183: public function __toString(): string
184: {
185: return $this->getName();
186: }
187: }
188: