1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Generic;
4:
5: use PHPStan\PhpDocParser\Ast\Type\GenericTypeNode;
6: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
7: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
8: use PHPStan\Reflection\ReflectionProviderStaticAccessor;
9: use PHPStan\Turbo\ShadowedByTurboExtension;
10: use PHPStan\Type\AcceptsResult;
11: use PHPStan\Type\ClassNameToObjectTypeResult;
12: use PHPStan\Type\ClassStringAlgebra;
13: use PHPStan\Type\ClassStringType;
14: use PHPStan\Type\CompoundType;
15: use PHPStan\Type\Constant\ConstantStringType;
16: use PHPStan\Type\InstanceofDeprecated;
17: use PHPStan\Type\IntersectionType;
18: use PHPStan\Type\IsSuperTypeOfResult;
19: use PHPStan\Type\MixedType;
20: use PHPStan\Type\NeverType;
21: use PHPStan\Type\ObjectType;
22: use PHPStan\Type\ObjectWithoutClassType;
23: use PHPStan\Type\StaticType;
24: use PHPStan\Type\StringType;
25: use PHPStan\Type\Type;
26: use PHPStan\Type\TypeCombinator;
27: use PHPStan\Type\UnionType;
28: use PHPStan\Type\VerbosityLevel;
29: use function count;
30: use function sprintf;
31:
32: /** @api */
33: #[InstanceofDeprecated(insteadUse: 'Type::isClassStringType() and Type::getClassStringObjectType()')]
34: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/GenericClassStringType.cpp')]
35: class GenericClassStringType extends ClassStringType
36: {
37:
38: /** @api */
39: public function __construct(private Type $type)
40: {
41: parent::__construct();
42: }
43:
44: public function getReferencedClasses(): array
45: {
46: return $this->type->getReferencedClasses();
47: }
48:
49: public function getGenericType(): Type
50: {
51: return $this->type;
52: }
53:
54: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult
55: {
56: // `class-string<X>` narrows to `X` for the comparison target, but
57: // the actual runtime class can be any subclass of `X` — keep
58: // uncertainty so the caller falls back to `BooleanType` instead
59: // of a definite yes when `$x instanceof Y` and `Y === X`.
60: return new ClassNameToObjectTypeResult($this->getGenericType(), true);
61: }
62:
63: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult
64: {
65: if ($allowString) {
66: return new ClassNameToObjectTypeResult(
67: TypeCombinator::union($this->getGenericType(), $this),
68: false,
69: );
70: }
71:
72: return new ClassNameToObjectTypeResult($this->getGenericType(), false);
73: }
74:
75: public function getClassStringObjectType(): Type
76: {
77: return $this->getGenericType();
78: }
79:
80: public function getObjectTypeOrClassStringObjectType(): Type
81: {
82: return $this->getClassStringObjectType();
83: }
84:
85: public function describe(VerbosityLevel $level): string
86: {
87: return sprintf('%s<%s>', parent::describe($level), $this->type->describe($level));
88: }
89:
90: public function accepts(Type $type, bool $strictTypes): AcceptsResult
91: {
92: if ($type instanceof CompoundType) {
93: return $type->isAcceptedBy($this, $strictTypes);
94: }
95:
96: if ($type instanceof ConstantStringType) {
97: if (!$type->isClassString()->yes()) {
98: return AcceptsResult::createNo();
99: }
100:
101: $objectType = new ObjectType($type->getValue());
102: } elseif ($type instanceof self) {
103: $objectType = $type->type;
104: } elseif ($type instanceof ClassStringType) {
105: $objectType = new ObjectWithoutClassType();
106: } elseif ($type instanceof StringType) {
107: return AcceptsResult::createMaybe();
108: } else {
109: return AcceptsResult::createNo();
110: }
111:
112: return $this->type->accepts($objectType, $strictTypes);
113: }
114:
115: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
116: {
117: if ($type instanceof CompoundType) {
118: return $type->isSubTypeOf($this);
119: }
120:
121: if ($type instanceof ConstantStringType) {
122: $genericType = $this->type;
123: if ($genericType instanceof MixedType) {
124: return IsSuperTypeOfResult::createYes();
125: }
126:
127: if ($genericType instanceof StaticType) {
128: $genericType = $genericType->getStaticObjectType();
129: }
130:
131: // We are transforming constant class-string to ObjectType. But we need to filter out
132: // an uncertainty originating in possible ObjectType's class subtypes.
133: $objectType = new ObjectType($type->getValue());
134:
135: // Do not use TemplateType's isSuperTypeOf handling directly because it takes ObjectType
136: // uncertainty into account.
137: if ($genericType instanceof TemplateType) {
138: $isSuperType = $genericType->getBound()->isSuperTypeOf($objectType);
139: } else {
140: $isSuperType = $genericType->isSuperTypeOf($objectType);
141: }
142:
143: if (!$type->isClassString()->yes()) {
144: $isSuperType = $isSuperType->and(IsSuperTypeOfResult::createMaybe());
145: }
146:
147: return $isSuperType;
148: } elseif ($type instanceof self) {
149: return $this->type->isSuperTypeOf($type->type);
150: } elseif ($type instanceof ClassStringType) {
151: return $this->type->isSuperTypeOf(new ObjectWithoutClassType());
152: } elseif ($type instanceof StringType) {
153: return IsSuperTypeOfResult::createMaybe();
154: }
155:
156: return IsSuperTypeOfResult::createNo();
157: }
158:
159: public function traverse(callable $cb): Type
160: {
161: $newType = $cb($this->type);
162: if ($newType === $this->type) {
163: return $this;
164: }
165:
166: return new self($newType);
167: }
168:
169: public function traverseSimultaneously(Type $right, callable $cb): Type
170: {
171: $newType = $cb($this->type, $right->getClassStringObjectType());
172: if ($newType === $this->type) {
173: return $this;
174: }
175:
176: return new self($newType);
177: }
178:
179: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
180: {
181: if ($receivedType instanceof UnionType || $receivedType instanceof IntersectionType) {
182: return $receivedType->inferTemplateTypesOn($this);
183: }
184:
185: if ($receivedType instanceof ConstantStringType) {
186: $typeToInfer = new ObjectType($receivedType->getValue());
187: } elseif ($receivedType instanceof self) {
188: $typeToInfer = $receivedType->type;
189: } elseif ($receivedType->isClassString()->yes()) {
190: $typeToInfer = $this->type;
191: if ($typeToInfer instanceof TemplateType) {
192: $typeToInfer = $typeToInfer->getBound();
193: }
194:
195: $typeToInfer = TypeCombinator::intersect($typeToInfer, new ObjectWithoutClassType());
196: } else {
197: return TemplateTypeMap::createEmpty();
198: }
199:
200: return $this->type->inferTemplateTypes($typeToInfer);
201: }
202:
203: public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array
204: {
205: $variance = $positionVariance->compose(TemplateTypeVariance::createCovariant());
206:
207: return $this->type->getReferencedTemplateTypes($variance);
208: }
209:
210: public function equals(Type $type): bool
211: {
212: if (!$type instanceof self) {
213: return false;
214: }
215:
216: if (!parent::equals($type)) {
217: return false;
218: }
219:
220: if (!$this->type->equals($type->type)) {
221: return false;
222: }
223:
224: return true;
225: }
226:
227: public function toPhpDocNode(): TypeNode
228: {
229: return new GenericTypeNode(
230: new IdentifierTypeNode('class-string'),
231: [
232: $this->type->toPhpDocNode(),
233: ],
234: );
235: }
236:
237: public function tryRemove(Type $typeToRemove): ?Type
238: {
239: if ($typeToRemove instanceof ConstantStringType && $typeToRemove->isClassString()->yes()) {
240: $generic = $this->getGenericType();
241:
242: $genericObjectClassNames = $generic->getObjectClassNames();
243: $reflectionProvider = ReflectionProviderStaticAccessor::getInstance();
244:
245: if (count($genericObjectClassNames) === 1) {
246: if ($reflectionProvider->hasClass($genericObjectClassNames[0])) {
247: $classReflection = $reflectionProvider->getClass($genericObjectClassNames[0]);
248: if ($classReflection->isFinal() && $genericObjectClassNames[0] === $typeToRemove->getValue()) {
249: return new NeverType();
250: }
251:
252: if ($classReflection->getAllowedSubTypes() !== null) {
253: $objectTypeToRemove = new ObjectType($typeToRemove->getValue());
254: $remainingType = TypeCombinator::remove($generic, $objectTypeToRemove);
255: if ($remainingType instanceof NeverType) {
256: return new NeverType();
257: }
258:
259: if (!$remainingType->equals($generic)) {
260: return new self($remainingType);
261: }
262: }
263: }
264: } elseif (count($genericObjectClassNames) > 1) {
265: $objectTypeToRemove = new ObjectType($typeToRemove->getValue());
266: if ($reflectionProvider->hasClass($typeToRemove->getValue())) {
267: $classReflection = $reflectionProvider->getClass($typeToRemove->getValue());
268: if ($classReflection->isFinal()) {
269: $remainingType = TypeCombinator::remove($generic, $objectTypeToRemove);
270: if ($remainingType instanceof NeverType) {
271: return new NeverType();
272: }
273:
274: return new self($remainingType);
275: }
276: }
277: }
278: }
279:
280: return parent::tryRemove($typeToRemove);
281: }
282:
283: public function hasTemplateOrLateResolvableType(): bool
284: {
285: return $this->type->hasTemplateOrLateResolvableType();
286: }
287:
288: public function hasUnresolvedTemplateArgumentType(): bool
289: {
290: return $this->type->hasUnresolvedTemplateArgumentType();
291: }
292:
293: public function tryIntersect(Type $otherType): ?Type
294: {
295: return ClassStringAlgebra::tryIntersect($this, $otherType);
296: }
297:
298: }
299: