1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Traits;
4:
5: use PHPStan\Php\PhpVersion;
6: use PHPStan\Reflection\ClassConstantReflection;
7: use PHPStan\Reflection\ClassMemberAccessAnswerer;
8: use PHPStan\Reflection\ExtendedMethodReflection;
9: use PHPStan\Reflection\ExtendedPropertyReflection;
10: use PHPStan\Reflection\ReflectionProvider;
11: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
12: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
13: use PHPStan\TrinaryLogic;
14: use PHPStan\Type\AcceptsResult;
15: use PHPStan\Type\BooleanType;
16: use PHPStan\Type\ClassNameToObjectTypeResult;
17: use PHPStan\Type\CompoundType;
18: use PHPStan\Type\Enum\EnumCaseObjectType;
19: use PHPStan\Type\Generic\TemplateTypeMap;
20: use PHPStan\Type\IsSuperTypeOfResult;
21: use PHPStan\Type\LateResolvableType;
22: use PHPStan\Type\NativeTypePosition;
23: use PHPStan\Type\NeverType;
24: use PHPStan\Type\Type;
25: use PHPStan\Type\ValueSet;
26:
27: /** @api */
28: trait LateResolvableTypeTrait
29: {
30:
31: use OperatorTypeTrait;
32:
33: private ?Type $result = null;
34:
35: public function getObjectClassNames(): array
36: {
37: return $this->resolve()->getObjectClassNames();
38: }
39:
40: public function getObjectClassReflections(): array
41: {
42: return $this->resolve()->getObjectClassReflections();
43: }
44:
45: public function getArrays(): array
46: {
47: return $this->resolve()->getArrays();
48: }
49:
50: public function getConstantArrays(): array
51: {
52: return $this->resolve()->getConstantArrays();
53: }
54:
55: public function getConstantStrings(): array
56: {
57: return $this->resolve()->getConstantStrings();
58: }
59:
60: public function accepts(Type $type, bool $strictTypes): AcceptsResult
61: {
62: return $this->resolve()->accepts($type, $strictTypes);
63: }
64:
65: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
66: {
67: return $this->isSuperTypeOfDefault($type);
68: }
69:
70: private function isSuperTypeOfDefault(Type $type): IsSuperTypeOfResult
71: {
72: if ($type instanceof NeverType) {
73: return IsSuperTypeOfResult::createYes();
74: }
75:
76: if ($type instanceof LateResolvableType) {
77: $type = $type->resolve();
78: }
79:
80: $isSuperType = $this->resolve()->isSuperTypeOf($type);
81:
82: if (!$this->isResolvable()) {
83: $isSuperType = $isSuperType->and(IsSuperTypeOfResult::createMaybe());
84: }
85:
86: return $isSuperType;
87: }
88:
89: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
90: {
91: return $this->resolve()->getTemplateType($ancestorClassName, $templateTypeName);
92: }
93:
94: public function isObject(): TrinaryLogic
95: {
96: return $this->resolve()->isObject();
97: }
98:
99: public function getClassStringType(): Type
100: {
101: return $this->resolve()->getClassStringType();
102: }
103:
104: public function isEnum(): TrinaryLogic
105: {
106: return $this->resolve()->isEnum();
107: }
108:
109: public function canAccessProperties(): TrinaryLogic
110: {
111: return $this->resolve()->canAccessProperties();
112: }
113:
114: public function hasProperty(string $propertyName): TrinaryLogic
115: {
116: return $this->resolve()->hasProperty($propertyName);
117: }
118:
119: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
120: {
121: return $this->resolve()->getProperty($propertyName, $scope);
122: }
123:
124: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
125: {
126: return $this->resolve()->getUnresolvedPropertyPrototype($propertyName, $scope);
127: }
128:
129: public function hasInstanceProperty(string $propertyName): TrinaryLogic
130: {
131: return $this->resolve()->hasInstanceProperty($propertyName);
132: }
133:
134: public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
135: {
136: return $this->resolve()->getInstanceProperty($propertyName, $scope);
137: }
138:
139: public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
140: {
141: return $this->resolve()->getUnresolvedInstancePropertyPrototype($propertyName, $scope);
142: }
143:
144: public function hasStaticProperty(string $propertyName): TrinaryLogic
145: {
146: return $this->resolve()->hasStaticProperty($propertyName);
147: }
148:
149: public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
150: {
151: return $this->resolve()->getStaticProperty($propertyName, $scope);
152: }
153:
154: public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
155: {
156: return $this->resolve()->getUnresolvedStaticPropertyPrototype($propertyName, $scope);
157: }
158:
159: public function canCallMethods(): TrinaryLogic
160: {
161: return $this->resolve()->canCallMethods();
162: }
163:
164: public function hasMethod(string $methodName): TrinaryLogic
165: {
166: return $this->resolve()->hasMethod($methodName);
167: }
168:
169: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
170: {
171: return $this->resolve()->getMethod($methodName, $scope);
172: }
173:
174: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection
175: {
176: return $this->resolve()->getUnresolvedMethodPrototype($methodName, $scope);
177: }
178:
179: public function canAccessConstants(): TrinaryLogic
180: {
181: return $this->resolve()->canAccessConstants();
182: }
183:
184: public function hasConstant(string $constantName): TrinaryLogic
185: {
186: return $this->resolve()->hasConstant($constantName);
187: }
188:
189: public function getConstant(string $constantName): ClassConstantReflection
190: {
191: return $this->resolve()->getConstant($constantName);
192: }
193:
194: public function isIterable(): TrinaryLogic
195: {
196: return $this->resolve()->isIterable();
197: }
198:
199: public function isIterableAtLeastOnce(): TrinaryLogic
200: {
201: return $this->resolve()->isIterableAtLeastOnce();
202: }
203:
204: public function getArraySize(): Type
205: {
206: return $this->resolve()->getArraySize();
207: }
208:
209: public function getIterableKeyType(): Type
210: {
211: return $this->resolve()->getIterableKeyType();
212: }
213:
214: public function getFirstIterableKeyType(): Type
215: {
216: return $this->resolve()->getIterableKeyType();
217: }
218:
219: public function getLastIterableKeyType(): Type
220: {
221: return $this->resolve()->getIterableKeyType();
222: }
223:
224: public function getIterableValueType(): Type
225: {
226: return $this->resolve()->getIterableValueType();
227: }
228:
229: public function getFirstIterableValueType(): Type
230: {
231: return $this->resolve()->getIterableValueType();
232: }
233:
234: public function getLastIterableValueType(): Type
235: {
236: return $this->resolve()->getIterableValueType();
237: }
238:
239: public function isArray(): TrinaryLogic
240: {
241: return $this->resolve()->isArray();
242: }
243:
244: public function isConstantArray(): TrinaryLogic
245: {
246: return $this->resolve()->isConstantArray();
247: }
248:
249: public function isOversizedArray(): TrinaryLogic
250: {
251: return $this->resolve()->isOversizedArray();
252: }
253:
254: public function isList(): TrinaryLogic
255: {
256: return $this->resolve()->isList();
257: }
258:
259: public function isOffsetAccessible(): TrinaryLogic
260: {
261: return $this->resolve()->isOffsetAccessible();
262: }
263:
264: public function isOffsetAccessLegal(): TrinaryLogic
265: {
266: return $this->resolve()->isOffsetAccessLegal();
267: }
268:
269: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
270: {
271: return $this->resolve()->hasOffsetValueType($offsetType);
272: }
273:
274: public function getOffsetValueType(Type $offsetType): Type
275: {
276: return $this->resolve()->getOffsetValueType($offsetType);
277: }
278:
279: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
280: {
281: return $this->resolve()->setOffsetValueType($offsetType, $valueType, $unionValues);
282: }
283:
284: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
285: {
286: return $this->resolve()->setExistingOffsetValueType($offsetType, $valueType);
287: }
288:
289: public function unsetOffset(Type $offsetType): Type
290: {
291: return $this->resolve()->unsetOffset($offsetType);
292: }
293:
294: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
295: {
296: return $this->resolve()->getKeysArrayFiltered($filterValueType, $strict);
297: }
298:
299: public function getKeysArray(): Type
300: {
301: return $this->resolve()->getKeysArray();
302: }
303:
304: public function getValuesArray(): Type
305: {
306: return $this->resolve()->getValuesArray();
307: }
308:
309: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
310: {
311: return $this->resolve()->chunkArray($lengthType, $preserveKeys);
312: }
313:
314: public function fillKeysArray(Type $valueType): Type
315: {
316: return $this->resolve()->fillKeysArray($valueType);
317: }
318:
319: public function flipArray(): Type
320: {
321: return $this->resolve()->flipArray();
322: }
323:
324: public function intersectKeyArray(Type $otherArraysType): Type
325: {
326: return $this->resolve()->intersectKeyArray($otherArraysType);
327: }
328:
329: public function popArray(): Type
330: {
331: return $this->resolve()->popArray();
332: }
333:
334: public function reverseArray(TrinaryLogic $preserveKeys): Type
335: {
336: return $this->resolve()->reverseArray($preserveKeys);
337: }
338:
339: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
340: {
341: return $this->resolve()->searchArray($needleType, $strict);
342: }
343:
344: public function shiftArray(): Type
345: {
346: return $this->resolve()->shiftArray();
347: }
348:
349: public function shuffleArray(): Type
350: {
351: return $this->resolve()->shuffleArray();
352: }
353:
354: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
355: {
356: return $this->resolve()->sliceArray($offsetType, $lengthType, $preserveKeys);
357: }
358:
359: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
360: {
361: return $this->resolve()->spliceArray($offsetType, $lengthType, $replacementType);
362: }
363:
364: public function truncateListToSize(Type $sizeType): Type
365: {
366: return $this->resolve()->truncateListToSize($sizeType);
367: }
368:
369: public function makeListMaybe(): Type
370: {
371: return $this->resolve()->makeListMaybe();
372: }
373:
374: public function mapValueType(callable $cb): Type
375: {
376: return $this->resolve()->mapValueType($cb);
377: }
378:
379: public function mapKeyType(callable $cb): Type
380: {
381: return $this->resolve()->mapKeyType($cb);
382: }
383:
384: public function makeAllArrayKeysOptional(): Type
385: {
386: return $this->resolve()->makeAllArrayKeysOptional();
387: }
388:
389: public function changeKeyCaseArray(?int $case): Type
390: {
391: return $this->resolve()->changeKeyCaseArray($case);
392: }
393:
394: public function filterArrayRemovingFalsey(): Type
395: {
396: return $this->resolve()->filterArrayRemovingFalsey();
397: }
398:
399: public function isCallable(): TrinaryLogic
400: {
401: return $this->resolve()->isCallable();
402: }
403:
404: public function getEnumCases(): array
405: {
406: return $this->resolve()->getEnumCases();
407: }
408:
409: public function getEnumCaseObject(): ?EnumCaseObjectType
410: {
411: return $this->resolve()->getEnumCaseObject();
412: }
413:
414: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
415: {
416: return $this->resolve()->getCallableParametersAcceptors($scope);
417: }
418:
419: public function isCloneable(): TrinaryLogic
420: {
421: return $this->resolve()->isCloneable();
422: }
423:
424: public function toBoolean(): BooleanType
425: {
426: return $this->resolve()->toBoolean();
427: }
428:
429: public function toNumber(): Type
430: {
431: return $this->resolve()->toNumber();
432: }
433:
434: public function toBitwiseNotType(): Type
435: {
436: return $this->resolve()->toBitwiseNotType();
437: }
438:
439: public function toGetClassResultType(): Type
440: {
441: return $this->resolve()->toGetClassResultType();
442: }
443:
444: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type
445: {
446: return $this->resolve()->toClassConstantType($reflectionProvider);
447: }
448:
449: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult
450: {
451: return $this->resolve()->toObjectTypeForInstanceofCheck();
452: }
453:
454: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult
455: {
456: return $this->resolve()->toObjectTypeForIsACheck($objectOrClassType, $allowString, $allowSameClass);
457: }
458:
459: public function toAbsoluteNumber(): Type
460: {
461: return $this->resolve()->toAbsoluteNumber();
462: }
463:
464: public function toInteger(): Type
465: {
466: return $this->resolve()->toInteger();
467: }
468:
469: public function toFloat(): Type
470: {
471: return $this->resolve()->toFloat();
472: }
473:
474: public function toString(): Type
475: {
476: return $this->resolve()->toString();
477: }
478:
479: public function toArray(): Type
480: {
481: return $this->resolve()->toArray();
482: }
483:
484: public function toArrayKey(): Type
485: {
486: return $this->resolve()->toArrayKey();
487: }
488:
489: public function toCoercedArgumentType(bool $strictTypes): Type
490: {
491: return $this->resolve()->toCoercedArgumentType($strictTypes);
492: }
493:
494: public function isSmallerThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
495: {
496: return $this->resolve()->isSmallerThan($otherType, $phpVersion);
497: }
498:
499: public function isSmallerThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
500: {
501: return $this->resolve()->isSmallerThanOrEqual($otherType, $phpVersion);
502: }
503:
504: public function isNull(): TrinaryLogic
505: {
506: return $this->resolve()->isNull();
507: }
508:
509: public function isConstantValue(): TrinaryLogic
510: {
511: return $this->resolve()->isConstantValue();
512: }
513:
514: public function isConstantScalarValue(): TrinaryLogic
515: {
516: return $this->resolve()->isConstantScalarValue();
517: }
518:
519: public function getConstantScalarTypes(): array
520: {
521: return $this->resolve()->getConstantScalarTypes();
522: }
523:
524: public function getConstantScalarValues(): array
525: {
526: return $this->resolve()->getConstantScalarValues();
527: }
528:
529: public function isTrue(): TrinaryLogic
530: {
531: return $this->resolve()->isTrue();
532: }
533:
534: public function isFalse(): TrinaryLogic
535: {
536: return $this->resolve()->isFalse();
537: }
538:
539: public function isBoolean(): TrinaryLogic
540: {
541: return $this->resolve()->isBoolean();
542: }
543:
544: public function isFloat(): TrinaryLogic
545: {
546: return $this->resolve()->isFloat();
547: }
548:
549: public function isInteger(): TrinaryLogic
550: {
551: return $this->resolve()->isInteger();
552: }
553:
554: public function isString(): TrinaryLogic
555: {
556: return $this->resolve()->isString();
557: }
558:
559: public function isNumericString(): TrinaryLogic
560: {
561: return $this->resolve()->isNumericString();
562: }
563:
564: public function isDecimalIntegerString(): TrinaryLogic
565: {
566: return $this->resolve()->isDecimalIntegerString();
567: }
568:
569: public function isNonEmptyString(): TrinaryLogic
570: {
571: return $this->resolve()->isNonEmptyString();
572: }
573:
574: public function isNonFalsyString(): TrinaryLogic
575: {
576: return $this->resolve()->isNonFalsyString();
577: }
578:
579: public function isLiteralString(): TrinaryLogic
580: {
581: return $this->resolve()->isLiteralString();
582: }
583:
584: public function isLowercaseString(): TrinaryLogic
585: {
586: return $this->resolve()->isLowercaseString();
587: }
588:
589: public function isUppercaseString(): TrinaryLogic
590: {
591: return $this->resolve()->isUppercaseString();
592: }
593:
594: public function isClassString(): TrinaryLogic
595: {
596: return $this->resolve()->isClassString();
597: }
598:
599: public function getClassStringObjectType(): Type
600: {
601: return $this->resolve()->getClassStringObjectType();
602: }
603:
604: public function getObjectTypeOrClassStringObjectType(): Type
605: {
606: return $this->resolve()->getObjectTypeOrClassStringObjectType();
607: }
608:
609: public function isVoid(): TrinaryLogic
610: {
611: return $this->resolve()->isVoid();
612: }
613:
614: public function isScalar(): TrinaryLogic
615: {
616: return $this->resolve()->isScalar();
617: }
618:
619: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
620: {
621: return new BooleanType();
622: }
623:
624: public function getSmallerType(PhpVersion $phpVersion): Type
625: {
626: return $this->resolve()->getSmallerType($phpVersion);
627: }
628:
629: public function getSmallerOrEqualType(PhpVersion $phpVersion): Type
630: {
631: return $this->resolve()->getSmallerOrEqualType($phpVersion);
632: }
633:
634: public function getGreaterType(PhpVersion $phpVersion): Type
635: {
636: return $this->resolve()->getGreaterType($phpVersion);
637: }
638:
639: public function getGreaterOrEqualType(PhpVersion $phpVersion): Type
640: {
641: return $this->resolve()->getGreaterOrEqualType($phpVersion);
642: }
643:
644: public function inferTemplateTypes(Type $receivedType): TemplateTypeMap
645: {
646: return $this->resolve()->inferTemplateTypes($receivedType);
647: }
648:
649: public function tryRemove(Type $typeToRemove): ?Type
650: {
651: return $this->resolve()->tryRemove($typeToRemove);
652: }
653:
654: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
655: {
656: $result = $this->resolve();
657:
658: if ($result instanceof CompoundType) {
659: return $result->isSubTypeOf($otherType);
660: }
661:
662: return $otherType->isSuperTypeOf($result);
663: }
664:
665: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
666: {
667: $result = $this->resolve();
668:
669: if ($result instanceof CompoundType) {
670: return $result->isAcceptedBy($acceptingType, $strictTypes);
671: }
672:
673: return $acceptingType->accepts($result, $strictTypes);
674: }
675:
676: public function isGreaterThan(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
677: {
678: $result = $this->resolve();
679:
680: if ($result instanceof CompoundType) {
681: return $result->isGreaterThan($otherType, $phpVersion);
682: }
683:
684: return $otherType->isSmallerThan($result, $phpVersion);
685: }
686:
687: public function isGreaterThanOrEqual(Type $otherType, PhpVersion $phpVersion): TrinaryLogic
688: {
689: $result = $this->resolve();
690:
691: if ($result instanceof CompoundType) {
692: return $result->isGreaterThanOrEqual($otherType, $phpVersion);
693: }
694:
695: return $otherType->isSmallerThanOrEqual($result, $phpVersion);
696: }
697:
698: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
699: {
700: return $this->resolve()->arithmeticPower($exponent, $phpVersion);
701: }
702:
703: public function getFiniteTypes(): array
704: {
705: return $this->resolve()->getFiniteTypes();
706: }
707:
708: public function resolve(): Type
709: {
710: return $this->result ??= $this->getResult();
711: }
712:
713: abstract protected function getResult(): Type;
714:
715: public function toNativePhpTypeNode(NativeTypePosition $position)
716: {
717: return $this->resolve()->toNativePhpTypeNode($position);
718: }
719:
720: public function hasTemplateOrLateResolvableType(): bool
721: {
722: return true;
723: }
724:
725: public function toValueSet(): ValueSet
726: {
727: return $this->resolve()->toValueSet();
728: }
729:
730: }
731: