1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use ArrayAccess;
6: use PhpParser\Node\Identifier;
7: use PHPStan\Php\PhpVersion;
8: use PHPStan\PhpDocParser\Ast\Type\IdentifierTypeNode;
9: use PHPStan\PhpDocParser\Ast\Type\TypeNode;
10: use PHPStan\Reflection\ClassConstantReflection;
11: use PHPStan\Reflection\ClassMemberAccessAnswerer;
12: use PHPStan\Reflection\Dummy\DummyClassConstantReflection;
13: use PHPStan\Reflection\Dummy\DummyMethodReflection;
14: use PHPStan\Reflection\Dummy\DummyPropertyReflection;
15: use PHPStan\Reflection\ExtendedMethodReflection;
16: use PHPStan\Reflection\ExtendedPropertyReflection;
17: use PHPStan\Reflection\ReflectionProvider;
18: use PHPStan\Reflection\TrivialParametersAcceptor;
19: use PHPStan\Reflection\Type\CallbackUnresolvedMethodPrototypeReflection;
20: use PHPStan\Reflection\Type\CallbackUnresolvedPropertyPrototypeReflection;
21: use PHPStan\Reflection\Type\UnresolvedMethodPrototypeReflection;
22: use PHPStan\Reflection\Type\UnresolvedPropertyPrototypeReflection;
23: use PHPStan\TrinaryLogic;
24: use PHPStan\Turbo\ShadowedByTurboExtension;
25: use PHPStan\Type\Accessory\AccessoryArrayListType;
26: use PHPStan\Type\Accessory\AccessoryDecimalIntegerStringType;
27: use PHPStan\Type\Accessory\AccessoryLiteralStringType;
28: use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
29: use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
30: use PHPStan\Type\Accessory\AccessoryNonFalsyStringType;
31: use PHPStan\Type\Accessory\AccessoryNumericStringType;
32: use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
33: use PHPStan\Type\Accessory\OversizedArrayType;
34: use PHPStan\Type\Constant\ConstantArrayType;
35: use PHPStan\Type\Constant\ConstantBooleanType;
36: use PHPStan\Type\Constant\ConstantFloatType;
37: use PHPStan\Type\Constant\ConstantIntegerType;
38: use PHPStan\Type\Constant\ConstantStringType;
39: use PHPStan\Type\Enum\EnumCaseObjectType;
40: use PHPStan\Type\Generic\TemplateMixedType;
41: use PHPStan\Type\Generic\TemplateType;
42: use PHPStan\Type\Traits\NonGeneralizableTypeTrait;
43: use PHPStan\Type\Traits\NonGenericTypeTrait;
44: use PHPStan\Type\Traits\OperatorTypeTrait;
45: use PHPStan\Type\Traits\SubstractableTypeTrait;
46: use PHPStan\Type\Traits\UndecidedComparisonCompoundTypeTrait;
47: use function get_class;
48: use function sprintf;
49:
50: /** @api */
51: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../turbo-ext/src/MixedType.cpp')]
52: class MixedType implements CompoundType, SubtractableType
53: {
54:
55: use NonGenericTypeTrait;
56: use UndecidedComparisonCompoundTypeTrait;
57: use NonGeneralizableTypeTrait;
58: use SubstractableTypeTrait;
59: use OperatorTypeTrait;
60:
61: private ?Type $subtractedType;
62:
63: private ?Type $witnessType;
64:
65: /**
66: * @api
67: * @param Type|null $witnessType What some path is known to produce, which a union with mixed would otherwise lose, as `mixed+int` of `$c ? $mixed : 1`. Only RuleLevelHelper::accepts() reads it.
68: */
69: public function __construct(
70: private bool $isExplicitMixed = false,
71: ?Type $subtractedType = null,
72: ?Type $witnessType = null,
73: )
74: {
75: if ($subtractedType instanceof NeverType) {
76: $subtractedType = null;
77: }
78:
79: if ($witnessType instanceof NeverType || $witnessType instanceof self) {
80: $witnessType = null;
81: }
82:
83: $this->subtractedType = $subtractedType;
84: $this->witnessType = $witnessType;
85: }
86:
87: public function getReferencedClasses(): array
88: {
89: return [];
90: }
91:
92: public function getObjectClassNames(): array
93: {
94: return [];
95: }
96:
97: public function getObjectClassReflections(): array
98: {
99: return [];
100: }
101:
102: public function getArrays(): array
103: {
104: return [];
105: }
106:
107: public function getConstantArrays(): array
108: {
109: return [];
110: }
111:
112: public function getConstantStrings(): array
113: {
114: return [];
115: }
116:
117: public function accepts(Type $type, bool $strictTypes): AcceptsResult
118: {
119: return AcceptsResult::createYes();
120: }
121:
122: public function isSuperTypeOfMixed(MixedType $type): IsSuperTypeOfResult
123: {
124: if ($this->subtractedType === null) {
125: if ($this->isExplicitMixed) {
126: if ($type->isExplicitMixed) {
127: return IsSuperTypeOfResult::createYes();
128: }
129: return IsSuperTypeOfResult::createMaybe();
130: }
131:
132: return IsSuperTypeOfResult::createYes();
133: }
134:
135: if ($type->subtractedType === null) {
136: return IsSuperTypeOfResult::createMaybe();
137: }
138:
139: $isSuperType = $type->subtractedType->isSuperTypeOf($this->subtractedType);
140: if ($isSuperType->yes()) {
141: if ($this->isExplicitMixed) {
142: if ($type->isExplicitMixed) {
143: return IsSuperTypeOfResult::createYes();
144: }
145: return IsSuperTypeOfResult::createMaybe();
146: }
147:
148: return IsSuperTypeOfResult::createYes();
149: }
150:
151: return IsSuperTypeOfResult::createMaybe();
152: }
153:
154: public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
155: {
156: if ($this->subtractedType === null || $type instanceof NeverType) {
157: return IsSuperTypeOfResult::createYes();
158: }
159:
160: if ($type instanceof self) {
161: if ($type->subtractedType === null) {
162: return IsSuperTypeOfResult::createMaybe();
163: }
164: $isSuperType = $type->subtractedType->isSuperTypeOf($this->subtractedType);
165: if ($isSuperType->yes()) {
166: return $isSuperType;
167: }
168:
169: return IsSuperTypeOfResult::createMaybe();
170: }
171:
172: $result = $this->subtractedType->isSuperTypeOf($type)->negate();
173: if ($result->no()) {
174: return IsSuperTypeOfResult::createNo([
175: sprintf(
176: 'Type %s has already been eliminated from %s.',
177: $this->subtractedType->describe(VerbosityLevel::precise()),
178: $this->describe(VerbosityLevel::typeOnly()),
179: ),
180: ]);
181: }
182:
183: return $result;
184: }
185:
186: public function setOffsetValueType(?Type $offsetType, Type $valueType, bool $unionValues = true): Type
187: {
188: return new self($this->isExplicitMixed);
189: }
190:
191: public function setExistingOffsetValueType(Type $offsetType, Type $valueType): Type
192: {
193: return new self($this->isExplicitMixed);
194: }
195:
196: public function unsetOffset(Type $offsetType): Type
197: {
198: if ($this->subtractedType !== null) {
199: return new self($this->isExplicitMixed, TypeCombinator::remove($this->subtractedType, new ConstantArrayType([], [])));
200: }
201: return $this;
202: }
203:
204: public function getKeysArrayFiltered(Type $filterValueType, TrinaryLogic $strict): Type
205: {
206: return $this->getKeysArray();
207: }
208:
209: public function getKeysArray(): Type
210: {
211: if ($this->isArray()->no()) {
212: return new ErrorType();
213: }
214:
215: return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new UnionType([new IntegerType(), new StringType()])), new AccessoryArrayListType()]);
216: }
217:
218: public function getValuesArray(): Type
219: {
220: if ($this->isArray()->no()) {
221: return new ErrorType();
222: }
223:
224: return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType($this->isExplicitMixed)), new AccessoryArrayListType()]);
225: }
226:
227: public function chunkArray(Type $lengthType, TrinaryLogic $preserveKeys): Type
228: {
229: if ($this->isArray()->no()) {
230: return new ErrorType();
231: }
232:
233: return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType($this->isExplicitMixed)), new AccessoryArrayListType()]);
234: }
235:
236: public function fillKeysArray(Type $valueType): Type
237: {
238: if ($this->isArray()->no()) {
239: return new ErrorType();
240: }
241:
242: return new ArrayType($this->getIterableValueType()->toArrayKey(), $valueType);
243: }
244:
245: public function flipArray(): Type
246: {
247: if ($this->isArray()->no()) {
248: return new ErrorType();
249: }
250:
251: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
252: }
253:
254: public function intersectKeyArray(Type $otherArraysType): Type
255: {
256: if ($this->isArray()->no()) {
257: return new ErrorType();
258: }
259:
260: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
261: }
262:
263: public function popArray(): Type
264: {
265: if ($this->isArray()->no()) {
266: return new ErrorType();
267: }
268:
269: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
270: }
271:
272: public function reverseArray(TrinaryLogic $preserveKeys): Type
273: {
274: if ($this->isArray()->no()) {
275: return new ErrorType();
276: }
277:
278: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
279: }
280:
281: public function searchArray(Type $needleType, ?TrinaryLogic $strict = null): Type
282: {
283: if ($this->isArray()->no()) {
284: return new ErrorType();
285: }
286:
287: return new UnionType([new IntegerType(), new StringType(), new ConstantBooleanType(false)]);
288: }
289:
290: public function shiftArray(): Type
291: {
292: if ($this->isArray()->no()) {
293: return new ErrorType();
294: }
295:
296: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
297: }
298:
299: public function shuffleArray(): Type
300: {
301: if ($this->isArray()->no()) {
302: return new ErrorType();
303: }
304:
305: return new IntersectionType([new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType($this->isExplicitMixed)), new AccessoryArrayListType()]);
306: }
307:
308: public function sliceArray(Type $offsetType, Type $lengthType, TrinaryLogic $preserveKeys): Type
309: {
310: if ($this->isArray()->no()) {
311: return new ErrorType();
312: }
313:
314: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
315: }
316:
317: public function spliceArray(Type $offsetType, Type $lengthType, Type $replacementType): Type
318: {
319: if ($this->isArray()->no()) {
320: return new ErrorType();
321: }
322:
323: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
324: }
325:
326: public function truncateListToSize(Type $sizeType): Type
327: {
328: if ($this->isArray()->no()) {
329: return new ErrorType();
330: }
331:
332: return $this;
333: }
334:
335: public function makeListMaybe(): Type
336: {
337: // `mixed` doesn't track list-ness; nothing to weaken.
338: return $this;
339: }
340:
341: public function mapValueType(callable $cb): Type
342: {
343: if ($this->isArray()->no()) {
344: return new ErrorType();
345: }
346:
347: return new ArrayType(
348: new MixedType($this->isExplicitMixed),
349: $cb(new MixedType($this->isExplicitMixed)),
350: );
351: }
352:
353: public function mapKeyType(callable $cb): Type
354: {
355: if ($this->isArray()->no()) {
356: return new ErrorType();
357: }
358:
359: return new ArrayType(
360: $cb(new MixedType($this->isExplicitMixed)),
361: new MixedType($this->isExplicitMixed),
362: );
363: }
364:
365: public function makeAllArrayKeysOptional(): Type
366: {
367: // `mixed` is already arbitrary; nothing to weaken.
368: return $this;
369: }
370:
371: public function changeKeyCaseArray(?int $case): Type
372: {
373: if ($this->isArray()->no()) {
374: return new ErrorType();
375: }
376:
377: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
378: }
379:
380: public function filterArrayRemovingFalsey(): Type
381: {
382: if ($this->isArray()->no()) {
383: return new ErrorType();
384: }
385:
386: return new ArrayType(new MixedType($this->isExplicitMixed), new MixedType($this->isExplicitMixed));
387: }
388:
389: public function isCallable(): TrinaryLogic
390: {
391: if ($this->subtractedType !== null) {
392: if ($this->subtractedType->isSuperTypeOf(new CallableType())->yes()) {
393: return TrinaryLogic::createNo();
394: }
395: }
396:
397: return TrinaryLogic::createMaybe();
398: }
399:
400: public function getEnumCases(): array
401: {
402: return [];
403: }
404:
405: public function getEnumCaseObject(): ?EnumCaseObjectType
406: {
407: return null;
408: }
409:
410: public function getCallableParametersAcceptors(ClassMemberAccessAnswerer $scope): array
411: {
412: return [new TrivialParametersAcceptor()];
413: }
414:
415: public function equals(Type $type): bool
416: {
417: if (get_class($type) !== static::class) {
418: return false;
419: }
420:
421: if ($this->subtractedType === null) {
422: if ($type->subtractedType === null) {
423: return $this->witnessEquals($type);
424: }
425:
426: return false;
427: }
428:
429: if ($type->subtractedType === null) {
430: return false;
431: }
432:
433: if (!$this->subtractedType->equals($type->subtractedType)) {
434: return false;
435: }
436:
437: return $this->witnessEquals($type);
438: }
439:
440: private function witnessEquals(self $type): bool
441: {
442: if ($this->witnessType === null || $type->witnessType === null) {
443: return $this->witnessType === $type->witnessType;
444: }
445:
446: return $this->witnessType->equals($type->witnessType);
447: }
448:
449: public function isSubTypeOf(Type $otherType): IsSuperTypeOfResult
450: {
451: if ($otherType instanceof self && !$otherType instanceof TemplateMixedType) {
452: return IsSuperTypeOfResult::createYes();
453: }
454:
455: if ($this->subtractedType !== null) {
456: $isSuperType = $this->subtractedType->isSuperTypeOf($otherType);
457: if ($isSuperType->yes()) {
458: return IsSuperTypeOfResult::createNo();
459: }
460: }
461:
462: return IsSuperTypeOfResult::createMaybe();
463: }
464:
465: public function isAcceptedBy(Type $acceptingType, bool $strictTypes): AcceptsResult
466: {
467: $isSuperType = $this->isSuperTypeOf($acceptingType)->toAcceptsResult();
468: if ($isSuperType->no()) {
469: return $isSuperType;
470: }
471: return AcceptsResult::createYes();
472: }
473:
474: public function getTemplateType(string $ancestorClassName, string $templateTypeName): Type
475: {
476: return new self();
477: }
478:
479: public function isObject(): TrinaryLogic
480: {
481: if ($this->subtractedType !== null) {
482: if ($this->subtractedType->isSuperTypeOf(new ObjectWithoutClassType())->yes()) {
483: return TrinaryLogic::createNo();
484: }
485: }
486: return TrinaryLogic::createMaybe();
487: }
488:
489: public function getClassStringType(): Type
490: {
491: return new ClassStringType();
492: }
493:
494: public function isEnum(): TrinaryLogic
495: {
496: if ($this->subtractedType !== null) {
497: if ($this->subtractedType->isSuperTypeOf(new ObjectWithoutClassType())->yes()) {
498: return TrinaryLogic::createNo();
499: }
500: }
501: return TrinaryLogic::createMaybe();
502: }
503:
504: public function canAccessProperties(): TrinaryLogic
505: {
506: return TrinaryLogic::createYes();
507: }
508:
509: public function hasProperty(string $propertyName): TrinaryLogic
510: {
511: return TrinaryLogic::createYes();
512: }
513:
514: public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
515: {
516: return $this->getUnresolvedPropertyPrototype($propertyName, $scope)->getTransformedProperty();
517: }
518:
519: public function getUnresolvedPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
520: {
521: $property = new DummyPropertyReflection($propertyName);
522: return new CallbackUnresolvedPropertyPrototypeReflection(
523: $property,
524: $property->getDeclaringClass(),
525: false,
526: static fn (Type $type): Type => $type,
527: );
528: }
529:
530: public function hasInstanceProperty(string $propertyName): TrinaryLogic
531: {
532: return TrinaryLogic::createYes();
533: }
534:
535: public function getInstanceProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
536: {
537: return $this->getUnresolvedInstancePropertyPrototype($propertyName, $scope)->getTransformedProperty();
538: }
539:
540: public function getUnresolvedInstancePropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
541: {
542: $property = new DummyPropertyReflection($propertyName);
543: return new CallbackUnresolvedPropertyPrototypeReflection(
544: $property,
545: $property->getDeclaringClass(),
546: false,
547: static fn (Type $type): Type => $type,
548: );
549: }
550:
551: public function hasStaticProperty(string $propertyName): TrinaryLogic
552: {
553: return TrinaryLogic::createYes();
554: }
555:
556: public function getStaticProperty(string $propertyName, ClassMemberAccessAnswerer $scope): ExtendedPropertyReflection
557: {
558: return $this->getUnresolvedStaticPropertyPrototype($propertyName, $scope)->getTransformedProperty();
559: }
560:
561: public function getUnresolvedStaticPropertyPrototype(string $propertyName, ClassMemberAccessAnswerer $scope): UnresolvedPropertyPrototypeReflection
562: {
563: $property = new DummyPropertyReflection($propertyName);
564: return new CallbackUnresolvedPropertyPrototypeReflection(
565: $property,
566: $property->getDeclaringClass(),
567: false,
568: static fn (Type $type): Type => $type,
569: );
570: }
571:
572: public function canCallMethods(): TrinaryLogic
573: {
574: return TrinaryLogic::createYes();
575: }
576:
577: public function hasMethod(string $methodName): TrinaryLogic
578: {
579: return TrinaryLogic::createYes();
580: }
581:
582: public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ExtendedMethodReflection
583: {
584: return $this->getUnresolvedMethodPrototype($methodName, $scope)->getTransformedMethod();
585: }
586:
587: public function getUnresolvedMethodPrototype(string $methodName, ClassMemberAccessAnswerer $scope): UnresolvedMethodPrototypeReflection
588: {
589: $method = new DummyMethodReflection($methodName);
590: return new CallbackUnresolvedMethodPrototypeReflection(
591: $method,
592: $method->getDeclaringClass(),
593: false,
594: static fn (Type $type): Type => $type,
595: );
596: }
597:
598: public function canAccessConstants(): TrinaryLogic
599: {
600: return TrinaryLogic::createYes();
601: }
602:
603: public function hasConstant(string $constantName): TrinaryLogic
604: {
605: return TrinaryLogic::createYes();
606: }
607:
608: public function getConstant(string $constantName): ClassConstantReflection
609: {
610: return new DummyClassConstantReflection($constantName);
611: }
612:
613: public function isCloneable(): TrinaryLogic
614: {
615: return TrinaryLogic::createYes();
616: }
617:
618: public function describe(VerbosityLevel $level): string
619: {
620: return $level->handle(
621: static fn (): string => 'mixed',
622: static fn (): string => 'mixed',
623: fn (): string => 'mixed' . $this->describeSubtractedType($this->subtractedType, $level) . $this->describeWitnessType($level),
624: function () use ($level): string {
625: $description = 'mixed' . $this->describeSubtractedType($this->subtractedType, $level) . $this->describeWitnessType($level);
626:
627: if ($this->isExplicitMixed) {
628: $description .= '=explicit';
629: } else {
630: $description .= '=implicit';
631: }
632:
633: return $description;
634: },
635: );
636: }
637:
638: private function describeWitnessType(VerbosityLevel $level): string
639: {
640: if ($this->witnessType === null) {
641: return '';
642: }
643:
644: if ($this->witnessType instanceof UnionType) {
645: return sprintf('+(%s)', $this->witnessType->describe($level));
646: }
647:
648: return sprintf('+%s', $this->witnessType->describe($level));
649: }
650:
651: public function toBoolean(): BooleanType
652: {
653: if ($this->subtractedType !== null) {
654: if ($this->subtractedType->isSuperTypeOf(StaticTypeFactory::falsey())->yes()) {
655: return new ConstantBooleanType(true);
656: }
657: }
658:
659: return new BooleanType();
660: }
661:
662: public function toNumber(): Type
663: {
664: return TypeCombinator::union(
665: $this->toInteger(),
666: $this->toFloat(),
667: );
668: }
669:
670: public function toBitwiseNotType(): Type
671: {
672: return new ErrorType();
673: }
674:
675: public function toGetClassResultType(): Type
676: {
677: $isObject = $this->isObject();
678: if ($isObject->no()) {
679: return new ConstantBooleanType(false);
680: }
681:
682: $classString = $this->getClassStringType();
683: if ($isObject->yes()) {
684: return $classString;
685: }
686:
687: return new UnionType([$classString, new ConstantBooleanType(false)]);
688: }
689:
690: public function toClassConstantType(ReflectionProvider $reflectionProvider): Type
691: {
692: // `mixed::class` is undefined — the original `TypeTraverser` cb fell
693: // through to `ErrorType` for any leaf that wasn't a definite object.
694: return new ErrorType();
695: }
696:
697: public function toObjectTypeForInstanceofCheck(): ClassNameToObjectTypeResult
698: {
699: return new ClassNameToObjectTypeResult(new MixedType(), false);
700: }
701:
702: public function toObjectTypeForIsACheck(Type $objectOrClassType, bool $allowString, bool $allowSameClass): ClassNameToObjectTypeResult
703: {
704: if ($allowString) {
705: return new ClassNameToObjectTypeResult(
706: new UnionType([new ObjectWithoutClassType(), new ClassStringType()]),
707: false,
708: );
709: }
710:
711: return new ClassNameToObjectTypeResult(new ObjectWithoutClassType(), false);
712: }
713:
714: public function toAbsoluteNumber(): Type
715: {
716: return $this->toNumber()->toAbsoluteNumber();
717: }
718:
719: public function toInteger(): Type
720: {
721: $castsToZero = new UnionType([
722: new NullType(),
723: new ConstantBooleanType(false),
724: new ConstantIntegerType(0),
725: new ConstantArrayType([], []),
726: new StringType(),
727: new FloatType(), // every 0.x float casts to int(0)
728: ]);
729: if (
730: $this->subtractedType !== null
731: && $this->subtractedType->isSuperTypeOf($castsToZero)->yes()
732: ) {
733: return new UnionType([
734: IntegerRangeType::fromInterval(null, -1),
735: IntegerRangeType::fromInterval(1, null),
736: ]);
737: }
738:
739: return new IntegerType();
740: }
741:
742: public function toFloat(): Type
743: {
744: return new FloatType();
745: }
746:
747: public function toString(): Type
748: {
749: if ($this->subtractedType !== null) {
750: $castsToEmptyString = new UnionType([
751: new NullType(),
752: new ConstantBooleanType(false),
753: new ConstantStringType(''),
754: ]);
755: if ($this->subtractedType->isSuperTypeOf($castsToEmptyString)->yes()) {
756: $accessories = [
757: new StringType(),
758: new AccessoryNonEmptyStringType(),
759: ];
760:
761: $castsToZeroString = new UnionType([
762: new ConstantFloatType(0.0),
763: new ConstantStringType('0'),
764: new ConstantIntegerType(0),
765: ]);
766: if ($this->subtractedType->isSuperTypeOf($castsToZeroString)->yes()) {
767: $accessories[] = new AccessoryNonFalsyStringType();
768: }
769: return new IntersectionType(
770: $accessories,
771: );
772: }
773: }
774:
775: return new StringType();
776: }
777:
778: public function toArray(): Type
779: {
780: $mixed = new self($this->isExplicitMixed);
781:
782: return new ArrayType($mixed, $mixed);
783: }
784:
785: public function toArrayKey(): Type
786: {
787: return new BenevolentUnionType([new IntegerType(), new StringType()]);
788: }
789:
790: public function toCoercedArgumentType(bool $strictTypes): Type
791: {
792: return $this;
793: }
794:
795: public function isIterable(): TrinaryLogic
796: {
797: if ($this->subtractedType !== null) {
798: if ($this->subtractedType->isSuperTypeOf(new IterableType(new MixedType(), new MixedType()))->yes()) {
799: return TrinaryLogic::createNo();
800: }
801: }
802:
803: return TrinaryLogic::createMaybe();
804: }
805:
806: public function isIterableAtLeastOnce(): TrinaryLogic
807: {
808: return $this->isIterable();
809: }
810:
811: public function getArraySize(): Type
812: {
813: if ($this->isIterable()->no()) {
814: return new ErrorType();
815: }
816:
817: return IntegerRangeType::fromInterval(0, null);
818: }
819:
820: public function getIterableKeyType(): Type
821: {
822: return new self($this->isExplicitMixed);
823: }
824:
825: public function getFirstIterableKeyType(): Type
826: {
827: return new self($this->isExplicitMixed);
828: }
829:
830: public function getLastIterableKeyType(): Type
831: {
832: return new self($this->isExplicitMixed);
833: }
834:
835: public function getIterableValueType(): Type
836: {
837: return new self($this->isExplicitMixed);
838: }
839:
840: public function getFirstIterableValueType(): Type
841: {
842: return new self($this->isExplicitMixed);
843: }
844:
845: public function getLastIterableValueType(): Type
846: {
847: return new self($this->isExplicitMixed);
848: }
849:
850: public function isOffsetAccessible(): TrinaryLogic
851: {
852: if ($this->subtractedType !== null) {
853: $offsetAccessibles = new UnionType([
854: new StringType(),
855: new ArrayType(new MixedType(), new MixedType()),
856: new ObjectType(ArrayAccess::class),
857: ]);
858:
859: if ($this->subtractedType->isSuperTypeOf($offsetAccessibles)->yes()) {
860: return TrinaryLogic::createNo();
861: }
862: }
863: return TrinaryLogic::createMaybe();
864: }
865:
866: public function isOffsetAccessLegal(): TrinaryLogic
867: {
868: if ($this->subtractedType !== null) {
869: if ($this->subtractedType->isSuperTypeOf(new ObjectWithoutClassType())->yes()) {
870: return TrinaryLogic::createYes();
871: }
872: }
873: return TrinaryLogic::createMaybe();
874: }
875:
876: public function hasOffsetValueType(Type $offsetType): TrinaryLogic
877: {
878: if ($this->isOffsetAccessible()->no()) {
879: return TrinaryLogic::createNo();
880: }
881:
882: return TrinaryLogic::createMaybe();
883: }
884:
885: public function getOffsetValueType(Type $offsetType): Type
886: {
887: return new self($this->isExplicitMixed);
888: }
889:
890: public function isExplicitMixed(): bool
891: {
892: return $this->isExplicitMixed;
893: }
894:
895: public function subtract(Type $type): Type
896: {
897: if ($type instanceof self && !$type instanceof TemplateType) {
898: return new NeverType();
899: }
900: $witnessType = $this->witnessType !== null ? TypeCombinator::remove($this->witnessType, $type) : null;
901: if ($this->subtractedType !== null) {
902: $type = TypeCombinator::union($this->subtractedType, $type);
903: }
904:
905: return $this->recreate($this->isExplicitMixed, $type, $witnessType);
906: }
907:
908: public function getTypeWithoutSubtractedType(): Type
909: {
910: return new self($this->isExplicitMixed);
911: }
912:
913: public function changeSubtractedType(?Type $subtractedType): Type
914: {
915: return $this->recreate($this->isExplicitMixed, $subtractedType, $this->witnessType);
916: }
917:
918: /**
919: * Whether a container leaves the type out of its description, as `array` for
920: * `array<mixed>`: a mixed with nothing subtracted, and without a witness at
921: * the levels that print witnesses.
922: *
923: * @phpstan-assert-if-true MixedType $type
924: */
925: public static function isBareMixed(?Type $type, VerbosityLevel $level): bool
926: {
927: if (!$type instanceof self || $type instanceof ErrorType || $type instanceof TemplateType || $type->subtractedType !== null) {
928: return false;
929: }
930:
931: return $type->witnessType === null || $level->isTypeOnly() || $level->isValue();
932: }
933:
934: public function getWitnessType(): ?Type
935: {
936: return $this->witnessType;
937: }
938:
939: public function changeWitnessType(?Type $witnessType): self
940: {
941: return $this->recreate($this->isExplicitMixed, $this->subtractedType, $witnessType);
942: }
943:
944: /** A copy of this type with these parts, which a subclass that is a mixed of its own kind keeps its kind in. */
945: protected function recreate(bool $isExplicitMixed, ?Type $subtractedType, ?Type $witnessType): self
946: {
947: return new self($isExplicitMixed, $subtractedType, $witnessType);
948: }
949:
950: public function getSubtractedType(): ?Type
951: {
952: return $this->subtractedType;
953: }
954:
955: public function traverse(callable $cb): Type
956: {
957: if ($this->witnessType === null) {
958: return $this;
959: }
960:
961: $witnessType = $cb($this->witnessType);
962: if ($witnessType === $this->witnessType) {
963: return $this;
964: }
965:
966: return $this->recreate($this->isExplicitMixed, $this->subtractedType, $witnessType);
967: }
968:
969: public function traverseSimultaneously(Type $right, callable $cb): Type
970: {
971: return $this;
972: }
973:
974: public function isArray(): TrinaryLogic
975: {
976: if ($this->subtractedType !== null) {
977: if ($this->subtractedType->isSuperTypeOf(new ArrayType(new MixedType(), new MixedType()))->yes()) {
978: return TrinaryLogic::createNo();
979: }
980: }
981:
982: return TrinaryLogic::createMaybe();
983: }
984:
985: public function isConstantArray(): TrinaryLogic
986: {
987: return $this->isArray();
988: }
989:
990: public function isOversizedArray(): TrinaryLogic
991: {
992: if ($this->subtractedType !== null) {
993: $oversizedArray = new IntersectionType([
994: new ArrayType(new MixedType(), new MixedType()),
995: new OversizedArrayType(),
996: ]);
997:
998: if ($this->subtractedType->isSuperTypeOf($oversizedArray)->yes()) {
999: return TrinaryLogic::createNo();
1000: }
1001: }
1002:
1003: return TrinaryLogic::createMaybe();
1004: }
1005:
1006: public function isList(): TrinaryLogic
1007: {
1008: if ($this->subtractedType !== null) {
1009: $list = new IntersectionType([
1010: new ArrayType(IntegerRangeType::createAllGreaterThanOrEqualTo(0), new MixedType()),
1011: new AccessoryArrayListType(),
1012: ]);
1013:
1014: if ($this->subtractedType->isSuperTypeOf($list)->yes()) {
1015: return TrinaryLogic::createNo();
1016: }
1017: }
1018:
1019: return TrinaryLogic::createMaybe();
1020: }
1021:
1022: public function getKindMask(): int
1023: {
1024: return TypeKind::ALL;
1025: }
1026:
1027: public function isNull(): TrinaryLogic
1028: {
1029: if ($this->subtractedType !== null) {
1030: if ($this->subtractedType->isSuperTypeOf(new NullType())->yes()) {
1031: return TrinaryLogic::createNo();
1032: }
1033: }
1034:
1035: return TrinaryLogic::createMaybe();
1036: }
1037:
1038: public function isConstantValue(): TrinaryLogic
1039: {
1040: return TrinaryLogic::createNo();
1041: }
1042:
1043: public function isConstantScalarValue(): TrinaryLogic
1044: {
1045: return TrinaryLogic::createNo();
1046: }
1047:
1048: public function getConstantScalarTypes(): array
1049: {
1050: return [];
1051: }
1052:
1053: public function getConstantScalarValues(): array
1054: {
1055: return [];
1056: }
1057:
1058: public function isTrue(): TrinaryLogic
1059: {
1060: if ($this->subtractedType !== null) {
1061: if ($this->subtractedType->isSuperTypeOf(new ConstantBooleanType(true))->yes()) {
1062: return TrinaryLogic::createNo();
1063: }
1064: }
1065:
1066: return TrinaryLogic::createMaybe();
1067: }
1068:
1069: public function isFalse(): TrinaryLogic
1070: {
1071: if ($this->subtractedType !== null) {
1072: if ($this->subtractedType->isSuperTypeOf(new ConstantBooleanType(false))->yes()) {
1073: return TrinaryLogic::createNo();
1074: }
1075: }
1076:
1077: return TrinaryLogic::createMaybe();
1078: }
1079:
1080: public function isBoolean(): TrinaryLogic
1081: {
1082: if ($this->subtractedType !== null) {
1083: if ($this->subtractedType->isSuperTypeOf(new BooleanType())->yes()) {
1084: return TrinaryLogic::createNo();
1085: }
1086: }
1087:
1088: return TrinaryLogic::createMaybe();
1089: }
1090:
1091: public function isFloat(): TrinaryLogic
1092: {
1093: if ($this->subtractedType !== null) {
1094: if ($this->subtractedType->isSuperTypeOf(new FloatType())->yes()) {
1095: return TrinaryLogic::createNo();
1096: }
1097: }
1098:
1099: return TrinaryLogic::createMaybe();
1100: }
1101:
1102: public function isInteger(): TrinaryLogic
1103: {
1104: if ($this->subtractedType !== null) {
1105: if ($this->subtractedType->isSuperTypeOf(new IntegerType())->yes()) {
1106: return TrinaryLogic::createNo();
1107: }
1108: }
1109:
1110: return TrinaryLogic::createMaybe();
1111: }
1112:
1113: public function isString(): TrinaryLogic
1114: {
1115: if ($this->subtractedType !== null) {
1116: if ($this->subtractedType->isSuperTypeOf(new StringType())->yes()) {
1117: return TrinaryLogic::createNo();
1118: }
1119: }
1120: return TrinaryLogic::createMaybe();
1121: }
1122:
1123: public function isNumericString(): TrinaryLogic
1124: {
1125: if ($this->subtractedType !== null) {
1126: $numericString = new IntersectionType([
1127: new StringType(),
1128: new AccessoryNumericStringType(),
1129: ]);
1130:
1131: if ($this->subtractedType->isSuperTypeOf($numericString)->yes()) {
1132: return TrinaryLogic::createNo();
1133: }
1134: }
1135:
1136: return TrinaryLogic::createMaybe();
1137: }
1138:
1139: public function isDecimalIntegerString(): TrinaryLogic
1140: {
1141: if ($this->subtractedType !== null) {
1142: $decimalIntegerString = new IntersectionType([
1143: new StringType(),
1144: new AccessoryDecimalIntegerStringType(),
1145: ]);
1146:
1147: if ($this->subtractedType->isSuperTypeOf($decimalIntegerString)->yes()) {
1148: return TrinaryLogic::createNo();
1149: }
1150: }
1151:
1152: return TrinaryLogic::createMaybe();
1153: }
1154:
1155: public function isNonEmptyString(): TrinaryLogic
1156: {
1157: if ($this->subtractedType !== null) {
1158: $nonEmptyString = new IntersectionType([
1159: new StringType(),
1160: new AccessoryNonEmptyStringType(),
1161: ]);
1162:
1163: if ($this->subtractedType->isSuperTypeOf($nonEmptyString)->yes()) {
1164: return TrinaryLogic::createNo();
1165: }
1166: }
1167:
1168: return TrinaryLogic::createMaybe();
1169: }
1170:
1171: public function isNonFalsyString(): TrinaryLogic
1172: {
1173: if ($this->subtractedType !== null) {
1174: $nonFalsyString = new IntersectionType([
1175: new StringType(),
1176: new AccessoryNonFalsyStringType(),
1177: ]);
1178:
1179: if ($this->subtractedType->isSuperTypeOf($nonFalsyString)->yes()) {
1180: return TrinaryLogic::createNo();
1181: }
1182: }
1183:
1184: return TrinaryLogic::createMaybe();
1185: }
1186:
1187: public function isLiteralString(): TrinaryLogic
1188: {
1189: if ($this->subtractedType !== null) {
1190: $literalString = new IntersectionType([
1191: new StringType(),
1192: new AccessoryLiteralStringType(),
1193: ]);
1194:
1195: if ($this->subtractedType->isSuperTypeOf($literalString)->yes()) {
1196: return TrinaryLogic::createNo();
1197: }
1198: }
1199:
1200: return TrinaryLogic::createMaybe();
1201: }
1202:
1203: public function getStringRefinement(): ?int
1204: {
1205: return null;
1206: }
1207:
1208: public function isLowercaseString(): TrinaryLogic
1209: {
1210: if ($this->subtractedType !== null) {
1211: $lowercaseString = new IntersectionType([
1212: new StringType(),
1213: new AccessoryLowercaseStringType(),
1214: ]);
1215:
1216: if ($this->subtractedType->isSuperTypeOf($lowercaseString)->yes()) {
1217: return TrinaryLogic::createNo();
1218: }
1219: }
1220:
1221: return TrinaryLogic::createMaybe();
1222: }
1223:
1224: public function isUppercaseString(): TrinaryLogic
1225: {
1226: if ($this->subtractedType !== null) {
1227: $uppercaseString = new IntersectionType([
1228: new StringType(),
1229: new AccessoryUppercaseStringType(),
1230: ]);
1231:
1232: if ($this->subtractedType->isSuperTypeOf($uppercaseString)->yes()) {
1233: return TrinaryLogic::createNo();
1234: }
1235: }
1236:
1237: return TrinaryLogic::createMaybe();
1238: }
1239:
1240: public function isClassString(): TrinaryLogic
1241: {
1242: if ($this->subtractedType !== null) {
1243: if ($this->subtractedType->isSuperTypeOf(new StringType())->yes()) {
1244: return TrinaryLogic::createNo();
1245: }
1246: if ($this->subtractedType->isSuperTypeOf(new ClassStringType())->yes()) {
1247: return TrinaryLogic::createNo();
1248: }
1249: }
1250:
1251: return TrinaryLogic::createMaybe();
1252: }
1253:
1254: public function getClassStringObjectType(): Type
1255: {
1256: if (!$this->isClassString()->no()) {
1257: return new ObjectWithoutClassType();
1258: }
1259:
1260: return new ErrorType();
1261: }
1262:
1263: public function getObjectTypeOrClassStringObjectType(): Type
1264: {
1265: $objectOrClass = new UnionType([
1266: new ObjectWithoutClassType(),
1267: new ClassStringType(),
1268: ]);
1269: if (!$this->isSuperTypeOf($objectOrClass)->no()) {
1270: return new ObjectWithoutClassType();
1271: }
1272:
1273: return new ErrorType();
1274: }
1275:
1276: public function isVoid(): TrinaryLogic
1277: {
1278: if ($this->subtractedType !== null) {
1279: if ($this->subtractedType->isSuperTypeOf(new VoidType())->yes()) {
1280: return TrinaryLogic::createNo();
1281: }
1282: }
1283:
1284: return TrinaryLogic::createMaybe();
1285: }
1286:
1287: public function isScalar(): TrinaryLogic
1288: {
1289: if ($this->subtractedType !== null) {
1290: if ($this->subtractedType->isSuperTypeOf(new UnionType([new BooleanType(), new FloatType(), new IntegerType(), new StringType()]))->yes()) {
1291: return TrinaryLogic::createNo();
1292: }
1293: }
1294:
1295: return TrinaryLogic::createMaybe();
1296: }
1297:
1298: public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
1299: {
1300: return new BooleanType();
1301: }
1302:
1303: public function tryRemove(Type $typeToRemove): ?Type
1304: {
1305: // mixed is the top type, so removal is exactly the subtraction - also
1306: // when an earlier subtraction already lowered isSuperTypeOf() from yes
1307: // to maybe, which used to give up and remove nothing at all
1308: if ($this->isSuperTypeOf($typeToRemove)->no()) {
1309: return null;
1310: }
1311:
1312: return $this->subtract($typeToRemove);
1313: }
1314:
1315: public function arithmeticPower(Type $exponent, PhpVersion $phpVersion): Type
1316: {
1317: return new BenevolentUnionType([
1318: new FloatType(),
1319: new IntegerType(),
1320: ]);
1321: }
1322:
1323: public function getFiniteTypes(): array
1324: {
1325: return [];
1326: }
1327:
1328: public function toPhpDocNode(): TypeNode
1329: {
1330: return new IdentifierTypeNode('mixed');
1331: }
1332:
1333: public function toNativePhpTypeNode(NativeTypePosition $position)
1334: {
1335: if (!$this->isExplicitMixed || !$position->isMixedAllowed()) {
1336: return null;
1337: }
1338:
1339: return new Identifier('mixed');
1340: }
1341:
1342: public function hasTemplateOrLateResolvableType(): bool
1343: {
1344: return false;
1345: }
1346:
1347: public function hasUnresolvedTemplateArgumentType(): bool
1348: {
1349: return false;
1350: }
1351:
1352: public function toValueSet(): ValueSet
1353: {
1354: return ValueSet::createUnknown($this);
1355: }
1356:
1357: }
1358: