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: /** @return array<string, mixed> */
69: public function exportToCache(): array
70: {
71: return [
72: 'name' => $this->name,
73: 'isIdentifier' => $this->isIdentifier,
74: ];
75: }
76:
77: /**
78: * @param array<string, mixed> $data
79: * @param ReflectionParameter|ReflectionMethod|ReflectionFunction|ReflectionEnum|ReflectionProperty|ReflectionClassConstant $owner
80: */
81: public static function importFromCache(Reflector $reflector, array $data, $owner): self
82: {
83: $reflection = new CoreReflectionClass(self::class);
84: $ref = $reflection->newInstanceWithoutConstructor();
85: assert($ref instanceof self);
86: $ref->reflector = $reflector;
87: $ref->owner = $owner;
88: $ref->name = $data['name'];
89: $ref->isIdentifier = $data['isIdentifier'];
90:
91: return $ref;
92: }
93:
94: /** @internal
95: * @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
96: * @return static */
97: public function withOwner($owner)
98: {
99: // The owner is only ever consulted when resolving self/parent/static
100: // to a class - any other name reads the same through any owner, so
101: // re-owning would only duplicate the instance: one fresh 'void' per
102: // inherited-method clone was ~45k duplicate instances in a PHPStan
103: // worker process.
104: switch (strtolower($this->name)) {
105: case 'self':
106: case 'parent':
107: case 'static':
108: $clone = clone $this;
109: $clone->owner = $owner;
110:
111: return $clone;
112: }
113:
114: return $this;
115: }
116:
117: /** @return non-empty-string */
118: public function getName(): string
119: {
120: return $this->name;
121: }
122:
123: /**
124: * Checks if it is a built-in type (i.e., it's not an object...)
125: *
126: * @see https://php.net/manual/en/reflectiontype.isbuiltin.php
127: */
128: public function isBuiltin(): bool
129: {
130: return array_key_exists(strtolower($this->name), self::BUILT_IN_TYPES);
131: }
132:
133: public function getClass(): ReflectionClass
134: {
135: if (! $this->isBuiltin()) {
136: return $this->reflector->reflectClass($this->name);
137: }
138:
139: if (
140: $this->owner instanceof ReflectionEnum
141: || $this->owner instanceof ReflectionFunction
142: || ($this->owner instanceof ReflectionParameter && $this->owner->getDeclaringFunction() instanceof ReflectionFunction)
143: ) {
144: throw new LogicException(sprintf('The type %s cannot be resolved to class', $this->name));
145: }
146:
147: $lowercaseName = strtolower($this->name);
148:
149: if ($lowercaseName === 'self') {
150: $class = $this->owner->getImplementingClass();
151: assert($class instanceof ReflectionClass);
152:
153: return $class;
154: }
155:
156: if ($lowercaseName === 'parent') {
157: $class = $this->owner->getDeclaringClass();
158: assert($class instanceof ReflectionClass);
159: $parentClass = $class->getParentClass();
160: assert($parentClass instanceof ReflectionClass);
161:
162: return $parentClass;
163: }
164:
165: if (
166: $this->owner instanceof ReflectionMethod
167: && $lowercaseName === 'static'
168: ) {
169: return $this->owner->getCurrentClass();
170: }
171:
172: throw new LogicException(sprintf('The type %s cannot be resolved to class', $this->name));
173: }
174:
175: public function allowsNull(): bool
176: {
177: switch (strtolower($this->name)) {
178: case 'mixed':
179: return true;
180: case 'null':
181: return true;
182: default:
183: return false;
184: }
185: }
186:
187: public function isIdentifier(): bool
188: {
189: return $this->isIdentifier;
190: }
191:
192: /** @return non-empty-string */
193: public function __toString(): string
194: {
195: return $this->getName();
196: }
197: }
198: