1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PhpParser\Node\Name;
6: use PHPStan\Php\PhpVersion;
7: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
8: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
9: use PHPStan\Reflection\ClassConstantReflection;
10: use PHPStan\Reflection\ClassMemberAccessAnswerer;
11: use PHPStan\Reflection\ClassReflection;
12: use PHPStan\Reflection\ExtendedMethodReflection;
13: use PHPStan\Reflection\ExtendedPropertyReflection;
14: use PHPStan\Reflection\ReflectionProvider;
15: use PHPStan\Reflection\Type\CallbackUnresolvedMethodPrototypeReflection;
16: use PHPStan\Reflection\Type\CallbackUnresolvedPropertyPrototypeReflection;
17: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
18: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
19: use PHPStan\TrinaryLogic;
20: use PHPStan\Turbo\ShadowedByTurboExtension;
21: use PHPStan\Type\Accessory\AccessoryLiteralStringType;
22: use PHPStan\Type\Enum\EnumCaseObjectType;
23: use PHPStan\Type\Generic\GenericClassStringType;
24: use PHPStan\Type\Generic\GenericObjectType;
25: use PHPStan\Type\Generic\TemplateTypeHelper;
26: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
27: use PHPStan\Type\Traits\NonGenericTypeTrait;
28: use PHPStan\Type\Traits\OperatorTypeTrait;
29: use PHPStan\Type\Traits\UndecidedComparisonTypeTrait;
30: use function get_class;
31: use function sprintf;
32:
33: /** @api */
34: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/StaticType.cpp')]
35: class StaticType implements TypeWithClassName, SubtractableType
36: {
37:
38: use NonGenericTypeTrait;
39: use UndecidedComparisonTypeTrait;
40: use NonGeneralizableTypeTrait;
41: use OperatorTypeTrait;
42:
43: private ?Type $subtractedType;
44:
45: private ?ObjectType $staticObjectType = null;
46:
47: private string $baseClass;
48:
49: /** @var array<string, ExtendedMethodReflection> */
50: private array $methodCache = [];
51:
52: /**
53: * @api
54: */
55: public function __construct(
56: private ClassReflection $classReflection,
57: ?Type $subtractedType = null,
58: )
59: {
60: if ($subtractedType instanceof NeverType) {
61: $subtractedType = null;
62: }
63:
64: $this->subtractedType = $subtractedType;
65: $this->baseClass = $classReflection->getName();
66: }
67:
68: public function getClassName(): string
69: {
70: return $this->baseClass;
71: }
72:
73: public function getClassReflection(): ClassReflection
74: {
75: return $this->classReflection;
76: }
77:
78: public function getAncestorWithClassName(string $className): ?TypeWithClassName
79: {
80: $ancestor = $this->getStaticObjectType()->getAncestorWithClassName($className);
81: if ($ancestor === null) {
82: return null;
83: }
84:
85: $classReflection = $ancestor->getClassReflection();
86: if ($classReflection !== null) {
87: return $this->changeBaseClass($classReflection);
88: }
89:
90: return null;
91: }
92:
93: public function getStaticObjectType(): ObjectType
94: {
95: if ($this->staticObjectType === null) {
96: if ($this->classReflection->isGeneric()) {
97: $typeMap = $this->classReflection->getActiveTemplateTypeMap()->map(static fn (string $name, Type $type): Type => TemplateTypeHelper::toArgument($type));
98: $varianceMap = $this->classReflection->getCallSiteVarianceMap();
99: return $this->staticObjectType = new GenericObjectType(
100: $this->classReflection->getName(),
101: $this->classReflection->typeMapToList($typeMap),
102: $this->subtractedType,
103: variances: $this->classReflection->varianceMapToList($varianceMap),
104: );
105: }
106:
107: return $this->staticObjectType = new ObjectType($this->classReflection->getName(), $this->subtractedType, $this->classReflection);
108: }
109:
110: return $this->staticObjectType;
111: }
112:
113: public function getReferencedClasses(): array
114: {
115: return $this->getStaticObjectType()->getReferencedClasses();
116: }
117:
118: public function getObjectClassNames(): array
119: {
120: return $this->getStaticObjectType()->getObjectClassNames();
121: }
122:
123: public function getObjectClassReflections(): array
124: {
125: return $this->getStaticObjectType()->getObjectClassReflections();
126: }
127:
128: public function getArrays(): array
129: {
130: return $this->getStaticObjectType()->getArrays();
131: }
132:
133: public function getConstantArrays(): array
134: {
135: return $this->getStaticObjectType()->getConstantArrays();
136: }
137:
138: public function getConstantStrings(): array
139: {
140: return $this->getStaticObjectType()->getConstantStrings();
141: }
142:
143: public function accepts(Type $type, bool $strictTypes): AcceptsResult
144: {
145: if ($type instanceof CompoundType) {
146: return $type->isAcceptedBy($this, $strictTypes);
147: }
148:
149: if (!$type instanceof static) {
150: return AcceptsResult::createNo();
151: }
152:
153: return $this->getStaticObjectType()->accepts($type->getStaticObjectType(), $strictTypes);
154: }
155:
156: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
157: {
158: if ($type instanceof self) {
159: return $this->getStaticObjectType()->isSuperTypeOf($type);
160: }
161:
162: if ($type instanceof ObjectWithoutClassType) {
163: return IsSuperTypeOfResult::createMaybe();
164: }
165:
166: if ($type instanceof ObjectType) {
167: $result = $this->getStaticObjectType()->isSuperTypeOf($type);
168: if ($result->yes()) {
169: $classReflection = $type->getClassReflection();
170: if ($classReflection !== null && $classReflection->isFinal()) {
171: return $result;
172: }
173: }
174:
175: return $result->and(IsSuperTypeOfResult::createMaybe());
176: }
177:
178: if ($type instanceof CompoundType) {
179: return $type->isSubTypeOf($this);
180: }
181:
182: return IsSuperTypeOfResult::createNo();
183: }
184:
185: public function equals(Type $type): bool
186: {
187: if (get_class($type) !== static::class) {
188: return false;
189: }
190:
191: return $this->getStaticObjectType()->equals($type->getStaticObjectType());
192: }
193:
194: public function describe(VerbosityLevel $level): string
195: {
196: return sprintf('static(%s)', $this->getStaticObjectType()->describe($level));
197: }
198:
199: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
200: {
201: return $this->getStaticObjectType()->getTemplateType($ancestorClassName, $templateTypeName);
202: }
203:
204: public function isObject(): TrinaryLogic
205: {
206: return $this->getStaticObjectType()->isObject();
207: }
208:
209: public function getClassStringType(): Type
210: {
211: return new GenericClassStringType($this);
212: }
213:
214: public function isEnum(): TrinaryLogic
215: {
216: return $this->getStaticObjectType()->isEnum();
217: }
218:
219: public function canAccessProperties(): TrinaryLogic
220: {
221: return $this->getStaticObjectType()->canAccessProperties();
222: }
223:
224: public function hasProperty(string $propertyName): TrinaryLogic
225: {
226: return $this->getStaticObjectType()->hasProperty($propertyName);
227: }
228:
229: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
230: {
231: return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty();
232: }
233:
234: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
235: {
236: $staticObject = $this->getStaticObjectType();
237: $nakedProperty = $staticObject->getUnresolvedPropertyPrototype($propertyName, $scope)->getNakedProperty();
238:
239: $ancestor = $this->getAncestorWithClassName($nakedProperty->getDeclaringClass()->getName());
240: $classReflection = null;
241: if ($ancestor !== null) {
242: $classReflection = $ancestor->getClassReflection();
243: }
244: if ($classReflection === null) {
245: $classReflection = $nakedProperty->getDeclaringClass();
246: }
247:
248: return new CallbackUnresolvedPropertyPrototypeReflection(
249: $nakedProperty,
250: $classReflection,
251: false,
252: fn (Type $type): Type => $this->transformStaticType($type, $scope),
253: );
254: }
255:
256: public function hasInstanceProperty(string $propertyName): TrinaryLogic
257: {
258: return $this->getStaticObjectType()->hasInstanceProperty($propertyName);
259: }
260:
261: public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
262: {
263: return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty();
264: }
265:
266: public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
267: {
268: $staticObject = $this->getStaticObjectType();
269: $nakedProperty = $staticObject->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getNakedProperty();
270:
271: $ancestor = $this->getAncestorWithClassName($nakedProperty->getDeclaringClass()->getName());
272: $classReflection = null;
273: if ($ancestor !== null) {
274: $classReflection = $ancestor->getClassReflection();
275: }
276: if ($classReflection === null) {
277: $classReflection = $nakedProperty->getDeclaringClass();
278: }
279:
280: return new CallbackUnresolvedPropertyPrototypeReflection(
281: $nakedProperty,
282: $classReflection,
283: false,
284: fn (Type $type): Type => $this->transformStaticType($type, $scope),
285: );
286: }
287:
288: public function hasStaticProperty(string $propertyName): TrinaryLogic
289: {
290: return $this->getStaticObjectType()->hasStaticProperty($propertyName);
291: }
292:
293: public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
294: {
295: return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty();
296: }
297:
298: public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
299: {
300: $staticObject = $this->getStaticObjectType();
301: $nakedProperty = $staticObject->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getNakedProperty();
302:
303: $ancestor = $this->getAncestorWithClassName($nakedProperty->getDeclaringClass()->getName());
304: $classReflection = null;
305: if ($ancestor !== null) {
306: $classReflection = $ancestor->getClassReflection();
307: }
308: if ($classReflection === null) {
309: $classReflection = $nakedProperty->getDeclaringClass();
310: }
311:
312: return new CallbackUnresolvedPropertyPrototypeReflection(
313: $nakedProperty,
314: $classReflection,
315: false,
316: fn (Type $type): Type => $this->transformStaticType($type, $scope),
317: );
318: }
319:
320: public function canCallMethods(): TrinaryLogic
321: {
322: return $this->getStaticObjectType()->canCallMethods();
323: }
324:
325: public function hasMethod(string $methodName): TrinaryLogic
326: {
327: return $this->getStaticObjectType()->hasMethod($methodName);
328: }
329:
330: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
331: {
332: $key = $methodName;
333: if ($scope->isInClass()) {
334: $key = sprintf('%s-%s', $key, $scope->getClassReflection()->getCacheKey());
335: }
336: return $this->methodCache[$key] ??= $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
337: }
338:
339: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection
340: {
341: $staticObject = $this->getStaticObjectType();
342: $nakedMethod = $staticObject->getUnresolvedMethodPrototype($methodName, $scope)->getNakedMethod();
343:
344: $ancestor = $this->getAncestorWithClassName($nakedMethod->getDeclaringClass()->getName());
345: $classReflection = null;
346: if ($ancestor !== null) {
347: $classReflection = $ancestor->getClassReflection();
348: }
349: if ($classReflection === null) {
350: $classReflection = $nakedMethod->getDeclaringClass();
351: }
352:
353: return new CallbackUnresolvedMethodPrototypeReflection(
354: $nakedMethod,
355: $classReflection,
356: false,
357: fn (Type $type): Type => $this->transformStaticType($type, $scope),
358: );
359: }
360:
361: private function transformStaticType(Type $type, ClassMemberAccessAnswerer $scope): Type
362: {
363: return TypeTraverser::map($type, function (Type $type, callable $traverse) use ($scope): Type {
364: if ($type instanceof StaticType) {
365: $classReflection = $this->classReflection;
366: $isFinal = false;
367: if ($scope->isInClass()) {
368: $classReflection = $scope->getClassReflection();
369: $isFinal = $classReflection->isFinal();
370: }
371: $type = $type->changeBaseClass($classReflection);
372:
373: // When calling a method on a `static` type (not `$this`),
374: // `$this` return type should be downgraded to `static`
375: // because we can't guarantee the exact instance.
376: if ($type instanceof ThisType && !$this instanceof ThisType) {
377: $type = new self($type->getClassReflection(), $type->getSubtractedType());
378: }
379:
380: if ($this->getSubtractedType() !== null) {
381: $type = $type->subtract($this->getSubtractedType());
382: if (!$type instanceof StaticType) {
383: return $traverse($type);
384: }
385: }
386:
387: if (!$isFinal || $type instanceof ThisType) {
388: return RecursionGuard::run($type, static fn () => $traverse($type));
389: }
390:
391: return $traverse($type->getStaticObjectType());
392: }
393:
394: return $traverse($type);
395: });
396: }
397:
398: public function canAccessConstants(): TrinaryLogic
399: {
400: return $this->getStaticObjectType()->canAccessConstants();
401: }
402:
403: public function hasConstant(string $constantName): TrinaryLogic
404: {
405: return $this->getStaticObjectType()->hasConstant($constantName);
406: }
407:
408: public function getConstant(string $constantName): ClassConstantReflection
409: {
410: return $this->getStaticObjectType()->getConstant($constantName);
411: }
412:
413: public function changeBaseClass(ClassReflection $classReflection): self
414: {
415: return new self($classReflection, $this->subtractedType);
416: }
417:
418: public function isIterable(): TrinaryLogic
419: {
420: return $this->getStaticObjectType()->isIterable();
421: }
422:
423: public function isIterableAtLeastOnce(): TrinaryLogic
424: {
425: return $this->getStaticObjectType()->isIterableAtLeastOnce();
426: }
427:
428: public function getArraySize(): Type
429: {
430: return $this->getStaticObjectType()->getArraySize();
431: }
432:
433: public function getIterableKeyType(): Type
434: {
435: return $this->getStaticObjectType()->getIterableKeyType();
436: }
437:
438: public function getFirstIterableKeyType(): Type
439: {
440: return $this->getStaticObjectType()->getIterableKeyType();
441: }
442:
443: public function getLastIterableKeyType(): Type
444: {
445: return $this->getStaticObjectType()->getIterableKeyType();
446: }
447:
448: public function getIterableValueType(): Type
449: {
450: return $this->getStaticObjectType()->getIterableValueType();
451: }
452:
453: public function getFirstIterableValueType(): Type
454: {
455: return $this->getStaticObjectType()->getIterableValueType();
456: }
457:
458: public function getLastIterableValueType(): Type
459: {
460: return $this->getStaticObjectType()->getIterableValueType();
461: }
462:
463: public function isOffsetAccessible(): TrinaryLogic
464: {
465: return $this->getStaticObjectType()->isOffsetAccessible();
466: }
467:
468: public function isOffsetAccessLegal(): TrinaryLogic
469: {
470: return $this->getStaticObjectType()->isOffsetAccessLegal();
471: }
472:
473: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
474: {
475: return $this->getStaticObjectType()->hasOffsetValueType($offsetType);
476: }
477:
478: public function getOffsetValueType(Type $offsetType): Type
479: {
480: return $this->getStaticObjectType()->getOffsetValueType($offsetType);
481: }
482:
483: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
484: {
485: return $this->getStaticObjectType()->setOffsetValueType($offsetType, $valueType, $unionValues);
486: }
487:
488: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
489: {
490: return $this->getStaticObjectType()->setExistingOffsetValueType($offsetType, $valueType);
491: }
492:
493: public function unsetOffset(Type $offsetType): Type
494: {
495: return $this->getStaticObjectType()->unsetOffset($offsetType);
496: }
497:
498: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
499: {
500: return $this->getStaticObjectType()->getKeysArrayFiltered($filterValueType, $strict);
501: }
502:
503: public function getKeysArray(): Type
504: {
505: return $this->getStaticObjectType()->getKeysArray();
506: }
507:
508: public function getValuesArray(): Type
509: {
510: return $this->getStaticObjectType()->getValuesArray();
511: }
512:
513: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
514: {
515: return $this->getStaticObjectType()->chunkArray($lengthType, $preserveKeys);
516: }
517:
518: public function fillKeysArray(Type $valueType): Type
519: {
520: return $this->getStaticObjectType()->fillKeysArray($valueType);
521: }
522:
523: public function flipArray(): Type
524: {
525: return $this->getStaticObjectType()->flipArray();
526: }
527:
528: public function intersectKeyArray(Type $otherArraysType): Type
529: {
530: return $this->getStaticObjectType()->intersectKeyArray($otherArraysType);
531: }
532:
533: public function popArray(): Type
534: {
535: return $this->getStaticObjectType()->popArray();
536: }
537:
538: public function reverseArray(TrinaryLogic $preserveKeys): Type
539: {
540: return $this->getStaticObjectType()->reverseArray($preserveKeys);
541: }
542:
543: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
544: {
545: return $this->getStaticObjectType()->searchArray($needleType, $strict);
546: }
547:
548: public function shiftArray(): Type
549: {
550: return $this->getStaticObjectType()->shiftArray();
551: }
552:
553: public function shuffleArray(): Type
554: {
555: return $this->getStaticObjectType()->shuffleArray();
556: }
557:
558: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
559: {
560: return $this->getStaticObjectType()->sliceArray($offsetType, $lengthType, $preserveKeys);
561: }
562:
563: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
564: {
565: return $this->getStaticObjectType()->spliceArray($offsetType, $lengthType, $replacementType);
566: }
567:
568: public function truncateListToSize(Type $sizeType): Type
569: {
570: return $this->getStaticObjectType()->truncateListToSize($sizeType);
571: }
572:
573: public function makeListMaybe(): Type
574: {
575: return $this->getStaticObjectType()->makeListMaybe();
576: }
577:
578: public function mapValueType(callable $cb): Type
579: {
580: return $this->getStaticObjectType()->mapValueType($cb);
581: }
582:
583: public function mapKeyType(callable $cb): Type
584: {
585: return $this->getStaticObjectType()->mapKeyType($cb);
586: }
587:
588: public function makeAllArrayKeysOptional(): Type
589: {
590: return $this->getStaticObjectType()->makeAllArrayKeysOptional();
591: }
592:
593: public function changeKeyCaseArray(?int $case): Type
594: {
595: return $this->getStaticObjectType()->changeKeyCaseArray($case);
596: }
597:
598: public function filterArrayRemovingFalsey(): Type
599: {
600: return $this->getStaticObjectType()->filterArrayRemovingFalsey();
601: }
602:
603: public function isCallable(): TrinaryLogic
604: {
605: return $this->getStaticObjectType()->isCallable();
606: }
607:
608: public function getEnumCases(): array
609: {
610: return $this->getStaticObjectType()->getEnumCases();
611: }
612:
613: public function getEnumCaseObject(): ?EnumCaseObjectType
614: {
615: return $this->getStaticObjectType()->getEnumCaseObject();
616: }
617:
618: public function isArray(): TrinaryLogic
619: {
620: return $this->getStaticObjectType()->isArray();
621: }
622:
623: public function isConstantArray(): TrinaryLogic
624: {
625: return $this->getStaticObjectType()->isConstantArray();
626: }
627:
628: public function isOversizedArray(): TrinaryLogic
629: {
630: return $this->getStaticObjectType()->isOversizedArray();
631: }
632:
633: public function isList(): TrinaryLogic
634: {
635: return $this->getStaticObjectType()->isList();
636: }
637:
638: public function isNull(): TrinaryLogic
639: {
640: return $this->getStaticObjectType()->isNull();
641: }
642:
643: public function isConstantValue(): TrinaryLogic
644: {
645: return $this->getStaticObjectType()->isConstantValue();
646: }
647:
648: public function isConstantScalarValue(): TrinaryLogic
649: {
650: return $this->getStaticObjectType()->isConstantScalarValue();
651: }
652:
653: public function getConstantScalarTypes(): array
654: {
655: return $this->getStaticObjectType()->getConstantScalarTypes();
656: }
657:
658: public function getConstantScalarValues(): array
659: {
660: return $this->getStaticObjectType()->getConstantScalarValues();
661: }
662:
663: public function isTrue(): TrinaryLogic
664: {
665: return $this->getStaticObjectType()->isTrue();
666: }
667:
668: public function isFalse(): TrinaryLogic
669: {
670: return $this->getStaticObjectType()->isFalse();
671: }
672:
673: public function isBoolean(): TrinaryLogic
674: {
675: return $this->getStaticObjectType()->isBoolean();
676: }
677:
678: public function isFloat(): TrinaryLogic
679: {
680: return $this->getStaticObjectType()->isFloat();
681: }
682:
683: public function isInteger(): TrinaryLogic
684: {
685: return $this->getStaticObjectType()->isInteger();
686: }
687:
688: public function isString(): TrinaryLogic
689: {
690: return $this->getStaticObjectType()->isString();
691: }
692:
693: public function isNumericString(): TrinaryLogic
694: {
695: return $this->getStaticObjectType()->isNumericString();
696: }
697:
698: public function isDecimalIntegerString(): TrinaryLogic
699: {
700: return $this->getStaticObjectType()->isDecimalIntegerString();
701: }
702:
703: public function isNonEmptyString(): TrinaryLogic
704: {
705: return $this->getStaticObjectType()->isNonEmptyString();
706: }
707:
708: public function isNonFalsyString(): TrinaryLogic
709: {
710: return $this->getStaticObjectType()->isNonFalsyString();
711: }
712:
713: public function isLiteralString(): TrinaryLogic
714: {
715: return $this->getStaticObjectType()->isLiteralString();
716: }
717:
718: public function isLowercaseString(): TrinaryLogic
719: {
720: return $this->getStaticObjectType()->isLowercaseString();
721: }
722:
723: public function isUppercaseString(): TrinaryLogic
724: {
725: return $this->getStaticObjectType()->isUppercaseString();
726: }
727:
728: public function isClassString(): TrinaryLogic
729: {
730: return $this->getStaticObjectType()->isClassString();
731: }
732:
733: public function getClassStringObjectType(): Type
734: {
735: return $this->getStaticObjectType()->getClassStringObjectType();
736: }
737:
738: public function getObjectTypeOrClassStringObjectType(): Type
739: {
740: return $this;
741: }
742:
743: public function isVoid(): TrinaryLogic
744: {
745: return $this->getStaticObjectType()->isVoid();
746: }
747:
748: public function isScalar(): TrinaryLogic
749: {
750: return $this->getStaticObjectType()->isScalar();
751: }
752:
753: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
754: {
755: return new BooleanType();
756: }
757:
758: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
759: {
760: return $this->getStaticObjectType()->getCallableParametersAcceptors($scope);
761: }
762:
763: public function isCloneable(): TrinaryLogic
764: {
765: return TrinaryLogic::createYes();
766: }
767:
768: public function toNumber(): Type
769: {
770: return new ErrorType();
771: }
772:
773: public function toBitwiseNotType(): Type
774: {
775: return new ErrorType();
776: }
777:
778: public function toGetClassResultType(): Type
779: {
780: // Preserve static binding (`class-string<static>` /
781: // `class-string<$this>`) by going through `getClassStringType()`
782: // directly instead of delegating to the underlying object type,
783: // which would resolve `static` away.
784: return $this->getClassStringType();
785: }
786:
787: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type
788: {
789: // Like `toGetClassResultType()`, project through this `StaticType`'s
790: // own `getClassStringType()` so that `static::class` reads as
791: // `class-string<static>` rather than the underlying class.
792: return new IntersectionType([$this->getClassStringType(), new AccessoryLiteralStringType()]);
793: }
794:
795: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult
796: {
797: return new ClassNameToObjectTypeResult($this, true);
798: }
799:
800: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult
801: {
802: if ($allowString) {
803: return new ClassNameToObjectTypeResult(
804: new UnionType([new ObjectWithoutClassType(), new ClassStringType()]),
805: false,
806: );
807: }
808:
809: return new ClassNameToObjectTypeResult(new ObjectWithoutClassType(), false);
810: }
811:
812: public function toAbsoluteNumber(): Type
813: {
814: return new ErrorType();
815: }
816:
817: public function toString(): Type
818: {
819: return $this->getStaticObjectType()->toString();
820: }
821:
822: public function toInteger(): Type
823: {
824: return new ErrorType();
825: }
826:
827: public function toFloat(): Type
828: {
829: return new ErrorType();
830: }
831:
832: public function toArray(): Type
833: {
834: return $this->getStaticObjectType()->toArray();
835: }
836:
837: public function toArrayKey(): Type
838: {
839: return $this->getStaticObjectType()->toArrayKey();
840: }
841:
842: public function toCoercedArgumentType(bool $strictTypes): Type
843: {
844: return $this->getStaticObjectType()->toCoercedArgumentType($strictTypes);
845: }
846:
847: public function toBoolean(): BooleanType
848: {
849: return $this->getStaticObjectType()->toBoolean();
850: }
851:
852: public function traverse(callable $cb): Type
853: {
854: $subtractedType = $this->subtractedType !== null ? $cb($this->subtractedType) : null;
855:
856: if ($subtractedType !== $this->subtractedType) {
857: return new self(
858: $this->classReflection,
859: $subtractedType,
860: );
861: }
862:
863: return $this;
864: }
865:
866: public function traverseSimultaneously(Type $right, callable $cb): Type
867: {
868: if ($this->subtractedType === null) {
869: return $this;
870: }
871:
872: return new self($this->classReflection);
873: }
874:
875: public function subtract(Type $type): Type
876: {
877: if ($this->subtractedType !== null) {
878: $type = TypeCombinator::union($this->subtractedType, $type);
879: }
880:
881: return $this->changeSubtractedType($type);
882: }
883:
884: public function getTypeWithoutSubtractedType(): Type
885: {
886: return $this->changeSubtractedType(null);
887: }
888:
889: public function changeSubtractedType(?Type $subtractedType): Type
890: {
891: if ($subtractedType !== null) {
892: $classReflection = $this->getClassReflection();
893: if ($classReflection->getAllowedSubTypes() !== null) {
894: $objectType = $this->getStaticObjectType()->changeSubtractedType($subtractedType);
895: if ($objectType instanceof NeverType) {
896: return $objectType;
897: }
898:
899: if ($objectType instanceof ObjectType && $objectType->getSubtractedType() !== null) {
900: return new self($classReflection, $objectType->getSubtractedType());
901: }
902:
903: return TypeCombinator::intersect($this, $objectType);
904: }
905: }
906:
907: return new self($this->classReflection, $subtractedType);
908: }
909:
910: public function getSubtractedType(): ?Type
911: {
912: return $this->subtractedType;
913: }
914:
915: public function tryRemove(Type $typeToRemove): ?Type
916: {
917: if ($this->getStaticObjectType()->isSuperTypeOf($typeToRemove)->yes()) {
918: return $this->subtract($typeToRemove);
919: }
920:
921: return null;
922: }
923:
924: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
925: {
926: return $this->getStaticObjectType()->arithmeticPower($exponent, $phpVersion);
927: }
928:
929: public function getFiniteTypes(): array
930: {
931: return $this->getStaticObjectType()->getFiniteTypes();
932: }
933:
934: public function toPhpDocNode(): TypeNode
935: {
936: return new IdentifierTypeNode('static');
937: }
938:
939: public function toNativePhpTypeNode(NativeTypePosition $position)
940: {
941: if ($position->isStaticAllowed()) {
942: return new Name('static');
943: }
944:
945: return $position->isClassNameAllowed() ? new Name('self') : null;
946: }
947:
948: public function hasTemplateOrLateResolvableType(): bool
949: {
950: return $this->getStaticObjectType()->hasTemplateOrLateResolvableType();
951: }
952:
953: public function toValueSet(): ValueSet
954: {
955: return ValueSet::createObjects($this);
956: }
957:
958: }
959: