1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type;
4:
5: use PHPStan\TrinaryLogic;
6: use PHPStan\Type\Accessory\AccessoryArrayListType;
7: use PHPStan\Type\Accessory\AccessoryLowercaseStringType;
8: use PHPStan\Type\Accessory\AccessoryNonEmptyStringType;
9: use PHPStan\Type\Accessory\AccessoryType;
10: use PHPStan\Type\Accessory\AccessoryUppercaseStringType;
11: use PHPStan\Type\Accessory\HasOffsetType;
12: use PHPStan\Type\Accessory\HasOffsetValueType;
13: use PHPStan\Type\Accessory\HasPropertyType;
14: use PHPStan\Type\Accessory\NonEmptyArrayType;
15: use PHPStan\Type\Accessory\OversizedArrayType;
16: use PHPStan\Type\Constant\ConstantArrayType;
17: use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
18: use PHPStan\Type\Constant\ConstantBooleanType;
19: use PHPStan\Type\Constant\ConstantFloatType;
20: use PHPStan\Type\Constant\ConstantIntegerType;
21: use PHPStan\Type\Constant\ConstantStringType;
22: use PHPStan\Type\Generic\GenericClassStringType;
23: use PHPStan\Type\Generic\TemplateArrayType;
24: use PHPStan\Type\Generic\TemplateBenevolentUnionType;
25: use PHPStan\Type\Generic\TemplateType;
26: use PHPStan\Type\Generic\TemplateTypeFactory;
27: use PHPStan\Type\Generic\TemplateUnionType;
28: use function array_key_exists;
29: use function array_key_first;
30: use function array_map;
31: use function array_merge;
32: use function array_slice;
33: use function array_splice;
34: use function array_values;
35: use function count;
36: use function get_class;
37: use function is_int;
38: use function md5;
39: use function sprintf;
40: use function usort;
41: use const PHP_INT_MAX;
42: use const PHP_INT_MIN;
43:
44: /**
45: * @api
46: */
47: final class TypeCombinator
48: {
49:
50: public static function addNull(Type $type): Type
51: {
52: $nullType = new NullType();
53:
54: if ($nullType->isSuperTypeOf($type)->no()) {
55: return self::union($type, $nullType);
56: }
57:
58: return $type;
59: }
60:
61: public static function remove(Type $fromType, Type $typeToRemove): Type
62: {
63: if ($typeToRemove instanceof UnionType) {
64: foreach ($typeToRemove->getTypes() as $unionTypeToRemove) {
65: $fromType = self::remove($fromType, $unionTypeToRemove);
66: }
67: return $fromType;
68: }
69:
70: $isSuperType = $typeToRemove->isSuperTypeOf($fromType);
71: if ($isSuperType->yes()) {
72: return new NeverType();
73: }
74: if ($isSuperType->no()) {
75: return $fromType;
76: }
77:
78: if ($typeToRemove instanceof MixedType) {
79: $typeToRemoveSubtractedType = $typeToRemove->getSubtractedType();
80: if ($typeToRemoveSubtractedType !== null) {
81: return self::intersect($fromType, $typeToRemoveSubtractedType);
82: }
83: }
84:
85: $removed = $fromType->tryRemove($typeToRemove);
86: if ($removed !== null) {
87: return $removed;
88: }
89:
90: $fromFiniteTypes = $fromType->getFiniteTypes();
91: if (count($fromFiniteTypes) > 0) {
92: $finiteTypesToRemove = $typeToRemove->getFiniteTypes();
93: if (count($finiteTypesToRemove) === 1) {
94: $result = [];
95: foreach ($fromFiniteTypes as $finiteType) {
96: if ($finiteType->equals($finiteTypesToRemove[0])) {
97: continue;
98: }
99:
100: $result[] = $finiteType;
101: }
102:
103: if (count($result) === count($fromFiniteTypes)) {
104: return $fromType;
105: }
106:
107: if (count($result) === 0) {
108: return new NeverType();
109: }
110:
111: if (count($result) === 1) {
112: return $result[0];
113: }
114:
115: return new UnionType($result);
116: }
117: }
118:
119: return $fromType;
120: }
121:
122: public static function removeNull(Type $type): Type
123: {
124: if (self::containsNull($type)) {
125: return self::remove($type, new NullType());
126: }
127:
128: return $type;
129: }
130:
131: public static function containsNull(Type $type): bool
132: {
133: if ($type instanceof UnionType) {
134: foreach ($type->getTypes() as $innerType) {
135: if ($innerType instanceof NullType) {
136: return true;
137: }
138: }
139:
140: return false;
141: }
142:
143: return $type instanceof NullType;
144: }
145:
146: public static function union(Type ...$types): Type
147: {
148: $typesCount = count($types);
149: if ($typesCount === 0) {
150: return new NeverType();
151: }
152:
153: $benevolentTypes = [];
154: $benevolentUnionObject = null;
155: // transform A | (B | C) to A | B | C
156: for ($i = 0; $i < $typesCount; $i++) {
157: if ($types[$i] instanceof BenevolentUnionType) {
158: if ($types[$i] instanceof TemplateBenevolentUnionType && $benevolentUnionObject === null) {
159: $benevolentUnionObject = $types[$i];
160: }
161: $benevolentTypesCount = 0;
162: $typesInner = $types[$i]->getTypes();
163: foreach ($typesInner as $benevolentInnerType) {
164: $benevolentTypesCount++;
165: $benevolentTypes[$benevolentInnerType->describe(VerbosityLevel::value())] = $benevolentInnerType;
166: }
167: array_splice($types, $i, 1, $typesInner);
168: $typesCount += $benevolentTypesCount - 1;
169: continue;
170: }
171: if (!($types[$i] instanceof UnionType)) {
172: continue;
173: }
174: if ($types[$i] instanceof TemplateType) {
175: continue;
176: }
177:
178: $typesInner = $types[$i]->getTypes();
179: array_splice($types, $i, 1, $typesInner);
180: $typesCount += count($typesInner) - 1;
181: }
182:
183: if ($typesCount === 1) {
184: return $types[0];
185: }
186:
187: $arrayTypes = [];
188: $scalarTypes = [];
189: $hasGenericScalarTypes = [];
190: $enumCaseTypes = [];
191: $integerRangeTypes = [];
192: for ($i = 0; $i < $typesCount; $i++) {
193: if ($types[$i] instanceof ConstantScalarType) {
194: $type = $types[$i];
195: $scalarTypes[get_class($type)][md5($type->describe(VerbosityLevel::cache()))] = $type;
196: unset($types[$i]);
197: continue;
198: }
199: if ($types[$i] instanceof BooleanType) {
200: $hasGenericScalarTypes[ConstantBooleanType::class] = true;
201: }
202: if ($types[$i] instanceof FloatType) {
203: $hasGenericScalarTypes[ConstantFloatType::class] = true;
204: }
205: if ($types[$i] instanceof IntegerType && !$types[$i] instanceof IntegerRangeType) {
206: $hasGenericScalarTypes[ConstantIntegerType::class] = true;
207: }
208: if ($types[$i] instanceof StringType && !$types[$i] instanceof ClassStringType) {
209: $hasGenericScalarTypes[ConstantStringType::class] = true;
210: }
211: $enumCases = $types[$i]->getEnumCases();
212: if (count($enumCases) === 1) {
213: $enumCaseTypes[$types[$i]->describe(VerbosityLevel::cache())] = $types[$i];
214:
215: unset($types[$i]);
216: continue;
217: }
218:
219: if ($types[$i] instanceof IntegerRangeType) {
220: $integerRangeTypes[] = $types[$i];
221: unset($types[$i]);
222:
223: continue;
224: }
225:
226: if (!$types[$i]->isArray()->yes()) {
227: continue;
228: }
229:
230: $arrayTypes[] = $types[$i];
231: unset($types[$i]);
232: }
233:
234: foreach ($scalarTypes as $classType => $scalarTypeItems) {
235: $scalarTypes[$classType] = array_values($scalarTypeItems);
236: }
237:
238: $enumCaseTypes = array_values($enumCaseTypes);
239: usort(
240: $integerRangeTypes,
241: static fn (IntegerRangeType $a, IntegerRangeType $b): int => ($a->getMin() ?? PHP_INT_MIN) <=> ($b->getMin() ?? PHP_INT_MIN)
242: ?: ($a->getMax() ?? PHP_INT_MAX) <=> ($b->getMax() ?? PHP_INT_MAX)
243: );
244: $types = array_merge($types, $integerRangeTypes);
245: $types = array_values($types);
246: $typesCount = count($types);
247:
248: foreach ($scalarTypes as $classType => $scalarTypeItems) {
249: if (isset($hasGenericScalarTypes[$classType])) {
250: unset($scalarTypes[$classType]);
251: continue;
252: }
253: if ($classType === ConstantBooleanType::class && count($scalarTypeItems) === 2) {
254: $types[] = new BooleanType();
255: $typesCount++;
256: unset($scalarTypes[$classType]);
257: continue;
258: }
259:
260: $scalarTypeItemsCount = count($scalarTypeItems);
261: for ($i = 0; $i < $typesCount; $i++) {
262: for ($j = 0; $j < $scalarTypeItemsCount; $j++) {
263: $compareResult = self::compareTypesInUnion($types[$i], $scalarTypeItems[$j]);
264: if ($compareResult === null) {
265: continue;
266: }
267:
268: [$a, $b] = $compareResult;
269: if ($a !== null) {
270: $types[$i] = $a;
271: array_splice($scalarTypeItems, $j--, 1);
272: $scalarTypeItemsCount--;
273: continue 1;
274: }
275: if ($b !== null) {
276: $scalarTypeItems[$j] = $b;
277: array_splice($types, $i--, 1);
278: $typesCount--;
279: continue 2;
280: }
281: }
282: }
283:
284: $scalarTypes[$classType] = $scalarTypeItems;
285: }
286:
287: if (count($types) > 16) {
288: $newTypes = [];
289: foreach ($types as $type) {
290: $newTypes[$type->describe(VerbosityLevel::cache())] = $type;
291: }
292: $types = array_values($newTypes);
293: }
294:
295: $types = array_merge(
296: $types,
297: self::processArrayTypes($arrayTypes),
298: );
299: $typesCount = count($types);
300:
301: // transform A | A to A
302: // transform A | never to A
303: for ($i = 0; $i < $typesCount; $i++) {
304: for ($j = $i + 1; $j < $typesCount; $j++) {
305: $compareResult = self::compareTypesInUnion($types[$i], $types[$j]);
306: if ($compareResult === null) {
307: continue;
308: }
309:
310: [$a, $b] = $compareResult;
311: if ($a !== null) {
312: $types[$i] = $a;
313: array_splice($types, $j--, 1);
314: $typesCount--;
315: continue 1;
316: }
317: if ($b !== null) {
318: $types[$j] = $b;
319: array_splice($types, $i--, 1);
320: $typesCount--;
321: continue 2;
322: }
323: }
324: }
325:
326: $enumCasesCount = count($enumCaseTypes);
327: for ($i = 0; $i < $typesCount; $i++) {
328: for ($j = 0; $j < $enumCasesCount; $j++) {
329: $compareResult = self::compareTypesInUnion($types[$i], $enumCaseTypes[$j]);
330: if ($compareResult === null) {
331: continue;
332: }
333:
334: [$a, $b] = $compareResult;
335: if ($a !== null) {
336: $types[$i] = $a;
337: array_splice($enumCaseTypes, $j--, 1);
338: $enumCasesCount--;
339: continue 1;
340: }
341: if ($b !== null) {
342: $enumCaseTypes[$j] = $b;
343: array_splice($types, $i--, 1);
344: $typesCount--;
345: continue 2;
346: }
347: }
348: }
349:
350: foreach ($enumCaseTypes as $enumCaseType) {
351: $types[] = $enumCaseType;
352: $typesCount++;
353: }
354:
355: foreach ($scalarTypes as $scalarTypeItems) {
356: foreach ($scalarTypeItems as $scalarType) {
357: $types[] = $scalarType;
358: $typesCount++;
359: }
360: }
361:
362: if ($typesCount === 0) {
363: return new NeverType();
364: }
365: if ($typesCount === 1) {
366: return $types[0];
367: }
368:
369: if ($benevolentTypes !== []) {
370: $tempTypes = $types;
371: foreach ($tempTypes as $i => $type) {
372: if (!isset($benevolentTypes[$type->describe(VerbosityLevel::value())])) {
373: break;
374: }
375:
376: unset($tempTypes[$i]);
377: }
378:
379: if ($tempTypes === []) {
380: if ($benevolentUnionObject instanceof TemplateBenevolentUnionType) {
381: return $benevolentUnionObject->withTypes($types);
382: }
383:
384: return new BenevolentUnionType($types, true);
385: }
386: }
387:
388: return new UnionType($types, true);
389: }
390:
391: /**
392: * @return array{Type, null}|array{null, Type}|null
393: */
394: private static function compareTypesInUnion(Type $a, Type $b): ?array
395: {
396: if ($a instanceof IntegerRangeType) {
397: $type = $a->tryUnion($b);
398: if ($type !== null) {
399: $a = $type;
400: return [$a, null];
401: }
402: }
403: if ($b instanceof IntegerRangeType) {
404: $type = $b->tryUnion($a);
405: if ($type !== null) {
406: $b = $type;
407: return [null, $b];
408: }
409: }
410: if ($a instanceof IntegerRangeType && $b instanceof IntegerRangeType) {
411: return null;
412: }
413: if ($a instanceof HasOffsetValueType && $b instanceof HasOffsetValueType) {
414: if ($a->getOffsetType()->equals($b->getOffsetType())) {
415: return [new HasOffsetValueType($a->getOffsetType(), self::union($a->getValueType(), $b->getValueType())), null];
416: }
417: }
418: if ($a->isConstantArray()->yes() && $b->isConstantArray()->yes()) {
419: return null;
420: }
421:
422: // simplify string[] | int[] to (string|int)[]
423: if ($a instanceof IterableType && $b instanceof IterableType) {
424: return [
425: new IterableType(
426: self::union($a->getIterableKeyType(), $b->getIterableKeyType()),
427: self::union($a->getIterableValueType(), $b->getIterableValueType()),
428: ),
429: null,
430: ];
431: }
432:
433: if ($a instanceof SubtractableType) {
434: $typeWithoutSubtractedTypeA = $a->getTypeWithoutSubtractedType();
435: if ($typeWithoutSubtractedTypeA instanceof MixedType && $b instanceof MixedType) {
436: $isSuperType = $typeWithoutSubtractedTypeA->isSuperTypeOfMixed($b);
437: } else {
438: $isSuperType = $typeWithoutSubtractedTypeA->isSuperTypeOf($b);
439: }
440: if ($isSuperType->yes()) {
441: $a = self::intersectWithSubtractedType($a, $b);
442: return [$a, null];
443: }
444: }
445:
446: if ($b instanceof SubtractableType) {
447: $typeWithoutSubtractedTypeB = $b->getTypeWithoutSubtractedType();
448: if ($typeWithoutSubtractedTypeB instanceof MixedType && $a instanceof MixedType) {
449: $isSuperType = $typeWithoutSubtractedTypeB->isSuperTypeOfMixed($a);
450: } else {
451: $isSuperType = $typeWithoutSubtractedTypeB->isSuperTypeOf($a);
452: }
453: if ($isSuperType->yes()) {
454: $b = self::intersectWithSubtractedType($b, $a);
455: return [null, $b];
456: }
457: }
458:
459: if ($b->isSuperTypeOf($a)->yes()) {
460: return [null, $b];
461: }
462:
463: if ($a->isSuperTypeOf($b)->yes()) {
464: return [$a, null];
465: }
466:
467: if (
468: $a instanceof ConstantStringType
469: && $a->getValue() === ''
470: && ($b->describe(VerbosityLevel::value()) === 'non-empty-string'
471: || $b->describe(VerbosityLevel::value()) === 'non-falsy-string')
472: ) {
473: return [null, self::intersect(
474: new StringType(),
475: ...self::getAccessoryCaseStringTypes($b),
476: )];
477: }
478:
479: if (
480: $b instanceof ConstantStringType
481: && $b->getValue() === ''
482: && ($a->describe(VerbosityLevel::value()) === 'non-empty-string'
483: || $a->describe(VerbosityLevel::value()) === 'non-falsy-string')
484: ) {
485: return [self::intersect(
486: new StringType(),
487: ...self::getAccessoryCaseStringTypes($a),
488: ), null];
489: }
490:
491: if (
492: $a instanceof ConstantStringType
493: && $a->getValue() === '0'
494: && $b->describe(VerbosityLevel::value()) === 'non-falsy-string'
495: ) {
496: return [null, self::intersect(
497: new StringType(),
498: new AccessoryNonEmptyStringType(),
499: ...self::getAccessoryCaseStringTypes($b),
500: )];
501: }
502:
503: if (
504: $b instanceof ConstantStringType
505: && $b->getValue() === '0'
506: && $a->describe(VerbosityLevel::value()) === 'non-falsy-string'
507: ) {
508: return [self::intersect(
509: new StringType(),
510: new AccessoryNonEmptyStringType(),
511: ...self::getAccessoryCaseStringTypes($a),
512: ), null];
513: }
514:
515: return null;
516: }
517:
518: /**
519: * @return array<Type>
520: */
521: private static function getAccessoryCaseStringTypes(Type $type): array
522: {
523: $accessory = [];
524: if ($type->isLowercaseString()->yes()) {
525: $accessory[] = new AccessoryLowercaseStringType();
526: }
527: if ($type->isUppercaseString()->yes()) {
528: $accessory[] = new AccessoryUppercaseStringType();
529: }
530:
531: return $accessory;
532: }
533:
534: private static function unionWithSubtractedType(
535: Type $type,
536: ?Type $subtractedType,
537: ): Type
538: {
539: if ($subtractedType === null) {
540: return $type;
541: }
542:
543: if ($type instanceof SubtractableType) {
544: $subtractedType = $type->getSubtractedType() === null
545: ? $subtractedType
546: : self::union($type->getSubtractedType(), $subtractedType);
547:
548: $subtractedType = self::intersect(
549: $type->getTypeWithoutSubtractedType(),
550: $subtractedType,
551: );
552: if ($subtractedType instanceof NeverType) {
553: $subtractedType = null;
554: }
555:
556: return $type->changeSubtractedType($subtractedType);
557: }
558:
559: if ($subtractedType->isSuperTypeOf($type)->yes()) {
560: return new NeverType();
561: }
562:
563: return self::remove($type, $subtractedType);
564: }
565:
566: private static function intersectWithSubtractedType(
567: SubtractableType $a,
568: Type $b,
569: ): Type
570: {
571: if ($a->getSubtractedType() === null) {
572: return $a;
573: }
574:
575: if ($b instanceof IntersectionType) {
576: $subtractableTypes = [];
577: foreach ($b->getTypes() as $innerType) {
578: if (!$innerType instanceof SubtractableType) {
579: continue;
580: }
581:
582: $subtractableTypes[] = $innerType;
583: }
584:
585: if (count($subtractableTypes) === 0) {
586: return $a->getTypeWithoutSubtractedType();
587: }
588:
589: $subtractedTypes = [];
590: foreach ($subtractableTypes as $subtractableType) {
591: if ($subtractableType->getSubtractedType() === null) {
592: continue;
593: }
594:
595: $subtractedTypes[] = $subtractableType->getSubtractedType();
596: }
597:
598: if (count($subtractedTypes) === 0) {
599: return $a->getTypeWithoutSubtractedType();
600:
601: }
602:
603: $subtractedType = self::union(...$subtractedTypes);
604: } else {
605: $isBAlreadySubtracted = $a->getSubtractedType()->isSuperTypeOf($b);
606:
607: if ($isBAlreadySubtracted->no()) {
608: return $a;
609: } elseif ($isBAlreadySubtracted->yes()) {
610: $subtractedType = self::remove($a->getSubtractedType(), $b);
611:
612: if ($subtractedType instanceof NeverType) {
613: $subtractedType = null;
614: }
615:
616: return $a->changeSubtractedType($subtractedType);
617: } elseif ($b instanceof SubtractableType) {
618: $subtractedType = $b->getSubtractedType();
619: if ($subtractedType === null) {
620: return $a->getTypeWithoutSubtractedType();
621: }
622: } else {
623: $subtractedTypeTmp = self::intersect($a->getTypeWithoutSubtractedType(), $a->getSubtractedType());
624: if ($b->isSuperTypeOf($subtractedTypeTmp)->yes()) {
625: return $a->getTypeWithoutSubtractedType();
626: }
627: $subtractedType = new MixedType(subtractedType: $b);
628: }
629: }
630:
631: $subtractedType = self::intersect(
632: $a->getSubtractedType(),
633: $subtractedType,
634: );
635: if ($subtractedType instanceof NeverType) {
636: $subtractedType = null;
637: }
638:
639: return $a->changeSubtractedType($subtractedType);
640: }
641:
642: /**
643: * @param Type[] $arrayTypes
644: * @return Type[]
645: */
646: private static function processArrayAccessoryTypes(array $arrayTypes): array
647: {
648: $isIterableAtLeastOnce = [];
649: $accessoryTypes = [];
650: foreach ($arrayTypes as $i => $arrayType) {
651: $isIterableAtLeastOnce[] = $arrayType->isIterableAtLeastOnce();
652:
653: if ($arrayType instanceof IntersectionType) {
654: foreach ($arrayType->getTypes() as $innerType) {
655: if ($innerType instanceof TemplateType) {
656: break;
657: }
658: if (!($innerType instanceof AccessoryType) && !($innerType instanceof CallableType)) {
659: continue;
660: }
661: if ($innerType instanceof HasOffsetType) {
662: $offset = $innerType->getOffsetType();
663: if ($offset instanceof ConstantStringType || $offset instanceof ConstantIntegerType) {
664: $innerType = new HasOffsetValueType($offset, $arrayType->getIterableValueType());
665: }
666: }
667: if ($innerType instanceof HasOffsetValueType) {
668: $accessoryTypes[sprintf('hasOffsetValue(%s)', $innerType->getOffsetType()->describe(VerbosityLevel::cache()))][$i] = $innerType;
669: continue;
670: }
671:
672: $accessoryTypes[$innerType->describe(VerbosityLevel::cache())][$i] = $innerType;
673: }
674: }
675:
676: if (!$arrayType->isConstantArray()->yes()) {
677: continue;
678: }
679: $constantArrays = $arrayType->getConstantArrays();
680:
681: foreach ($constantArrays as $constantArray) {
682: if ($constantArray->isList()->yes()) {
683: $list = new AccessoryArrayListType();
684: $accessoryTypes[$list->describe(VerbosityLevel::cache())][$i] = $list;
685: }
686:
687: if (!$constantArray->isIterableAtLeastOnce()->yes()) {
688: continue;
689: }
690:
691: $nonEmpty = new NonEmptyArrayType();
692: $accessoryTypes[$nonEmpty->describe(VerbosityLevel::cache())][$i] = $nonEmpty;
693: }
694: }
695:
696: $commonAccessoryTypes = [];
697: $arrayTypeCount = count($arrayTypes);
698: foreach ($accessoryTypes as $accessoryType) {
699: if (count($accessoryType) !== $arrayTypeCount) {
700: $firstKey = array_key_first($accessoryType);
701: if ($accessoryType[$firstKey] instanceof OversizedArrayType) {
702: $commonAccessoryTypes[] = $accessoryType[$firstKey];
703: }
704: continue;
705: }
706:
707: if ($accessoryType[0] instanceof HasOffsetValueType) {
708: $commonAccessoryTypes[] = self::union(...$accessoryType);
709: continue;
710: }
711:
712: $commonAccessoryTypes[] = $accessoryType[0];
713: }
714:
715: if (TrinaryLogic::createYes()->and(...$isIterableAtLeastOnce)->yes()) {
716: $commonAccessoryTypes[] = new NonEmptyArrayType();
717: }
718:
719: return $commonAccessoryTypes;
720: }
721:
722: /**
723: * @param list<Type> $arrayTypes
724: * @return Type[]
725: */
726: private static function processArrayTypes(array $arrayTypes): array
727: {
728: if ($arrayTypes === []) {
729: return [];
730: }
731:
732: $accessoryTypes = self::processArrayAccessoryTypes($arrayTypes);
733:
734: if (count($arrayTypes) === 1) {
735: return [
736: self::intersect(...$arrayTypes, ...$accessoryTypes),
737: ];
738: }
739:
740: $keyTypesForGeneralArray = [];
741: $valueTypesForGeneralArray = [];
742: $generalArrayOccurred = false;
743: $constantKeyTypesNumbered = [];
744: $filledArrays = 0;
745: $overflowed = false;
746:
747: /** @var int|float $nextConstantKeyTypeIndex */
748: $nextConstantKeyTypeIndex = 1;
749: $constantArraysMap = array_map(
750: static fn (Type $t) => $t->getConstantArrays(),
751: $arrayTypes,
752: );
753:
754: foreach ($arrayTypes as $arrayIdx => $arrayType) {
755: $constantArrays = $constantArraysMap[$arrayIdx];
756: $isConstantArray = $constantArrays !== [];
757: if (!$isConstantArray || !$arrayType->isIterableAtLeastOnce()->no()) {
758: $filledArrays++;
759: }
760:
761: if ($generalArrayOccurred || !$isConstantArray) {
762: foreach ($arrayType->getArrays() as $type) {
763: $keyTypesForGeneralArray[] = $type->getIterableKeyType();
764: $valueTypesForGeneralArray[] = $type->getItemType();
765: $generalArrayOccurred = true;
766: }
767: continue;
768: }
769:
770: $constantArrays = $arrayType->getConstantArrays();
771: foreach ($constantArrays as $constantArray) {
772: foreach ($constantArray->getKeyTypes() as $i => $keyType) {
773: $keyTypesForGeneralArray[] = $keyType;
774: $valueTypesForGeneralArray[] = $constantArray->getValueTypes()[$i];
775:
776: $keyTypeValue = $keyType->getValue();
777: if (array_key_exists($keyTypeValue, $constantKeyTypesNumbered)) {
778: continue;
779: }
780:
781: $constantKeyTypesNumbered[$keyTypeValue] = $nextConstantKeyTypeIndex;
782: $nextConstantKeyTypeIndex *= 2;
783: if (!is_int($nextConstantKeyTypeIndex)) {
784: $generalArrayOccurred = true;
785: $overflowed = true;
786: continue 2;
787: }
788: }
789: }
790: }
791:
792: if ($generalArrayOccurred && (!$overflowed || $filledArrays > 1)) {
793: $reducedArrayTypes = self::reduceArrays($arrayTypes, false);
794: if (count($reducedArrayTypes) === 1) {
795: return [self::intersect($reducedArrayTypes[0], ...$accessoryTypes)];
796: }
797: $scopes = [];
798: $useTemplateArray = true;
799: foreach ($arrayTypes as $arrayType) {
800: if (!$arrayType instanceof TemplateArrayType) {
801: $useTemplateArray = false;
802: break;
803: }
804:
805: $scopes[$arrayType->getScope()->describe()] = $arrayType;
806: }
807:
808: $arrayType = new ArrayType(
809: self::union(...$keyTypesForGeneralArray),
810: self::union(...self::optimizeConstantArrays($valueTypesForGeneralArray)),
811: );
812:
813: if ($useTemplateArray && count($scopes) === 1) {
814: $templateArray = array_values($scopes)[0];
815: $arrayType = new TemplateArrayType(
816: $templateArray->getScope(),
817: $templateArray->getStrategy(),
818: $templateArray->getVariance(),
819: $templateArray->getName(),
820: $arrayType,
821: $templateArray->getDefault(),
822: );
823: }
824:
825: return [
826: self::intersect($arrayType, ...$accessoryTypes),
827: ];
828: }
829:
830: $reducedArrayTypes = self::reduceArrays($arrayTypes, true);
831:
832: return array_map(
833: static fn (Type $arrayType) => self::intersect($arrayType, ...$accessoryTypes),
834: self::optimizeConstantArrays($reducedArrayTypes),
835: );
836: }
837:
838: /**
839: * @param Type[] $types
840: * @return Type[]
841: */
842: private static function optimizeConstantArrays(array $types): array
843: {
844: $constantArrayValuesCount = self::countConstantArrayValueTypes($types);
845:
846: if ($constantArrayValuesCount <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
847: return $types;
848: }
849:
850: $results = [];
851: $eachIsOversized = true;
852: foreach ($types as $type) {
853: $isOversized = false;
854: $result = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$isOversized): Type {
855: if (!$type instanceof ConstantArrayType) {
856: return $traverse($type);
857: }
858:
859: if ($type->isIterableAtLeastOnce()->no()) {
860: return $type;
861: }
862:
863: $isOversized = true;
864:
865: $isList = true;
866: $valueTypes = [];
867: $keyTypes = [];
868: $nextAutoIndex = 0;
869: foreach ($type->getKeyTypes() as $i => $innerKeyType) {
870: if (!$innerKeyType instanceof ConstantIntegerType) {
871: $isList = false;
872: } elseif ($innerKeyType->getValue() !== $nextAutoIndex) {
873: $isList = false;
874: $nextAutoIndex = $innerKeyType->getValue() + 1;
875: } else {
876: $nextAutoIndex++;
877: }
878:
879: $generalizedKeyType = $innerKeyType->generalize(GeneralizePrecision::moreSpecific());
880: $keyTypes[$generalizedKeyType->describe(VerbosityLevel::precise())] = $generalizedKeyType;
881:
882: $innerValueType = $type->getValueTypes()[$i];
883: $generalizedValueType = TypeTraverser::map($innerValueType, static function (Type $type) use ($traverse): Type {
884: if ($type instanceof ArrayType || $type instanceof ConstantArrayType) {
885: return TypeCombinator::intersect($type, new OversizedArrayType());
886: }
887:
888: if ($type instanceof ConstantScalarType) {
889: return $type->generalize(GeneralizePrecision::moreSpecific());
890: }
891:
892: return $traverse($type);
893: });
894: $valueTypes[$generalizedValueType->describe(VerbosityLevel::precise())] = $generalizedValueType;
895: }
896:
897: $keyType = TypeCombinator::union(...array_values($keyTypes));
898: $valueType = TypeCombinator::union(...array_values($valueTypes));
899:
900: $arrayType = new ArrayType($keyType, $valueType);
901: if ($isList) {
902: $arrayType = TypeCombinator::intersect($arrayType, new AccessoryArrayListType());
903: }
904:
905: return TypeCombinator::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType());
906: });
907:
908: if (!$isOversized) {
909: $eachIsOversized = false;
910: }
911:
912: $results[] = $result;
913: }
914:
915: if ($eachIsOversized) {
916: $eachIsList = true;
917: $keyTypes = [];
918: $valueTypes = [];
919: foreach ($results as $result) {
920: $keyTypes[] = $result->getIterableKeyType();
921: $valueTypes[] = $result->getLastIterableValueType();
922: if ($result->isList()->yes()) {
923: continue;
924: }
925: $eachIsList = false;
926: }
927:
928: $keyType = self::union(...$keyTypes);
929: $valueType = self::union(...$valueTypes);
930:
931: if ($valueType instanceof UnionType && count($valueType->getTypes()) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
932: $valueType = $valueType->generalize(GeneralizePrecision::lessSpecific());
933: }
934:
935: $arrayType = new ArrayType($keyType, $valueType);
936: if ($eachIsList) {
937: $arrayType = self::intersect($arrayType, new AccessoryArrayListType());
938: }
939:
940: return [self::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType())];
941: }
942:
943: return $results;
944: }
945:
946: /**
947: * @param Type[] $types
948: */
949: public static function countConstantArrayValueTypes(array $types): int
950: {
951: $constantArrayValuesCount = 0;
952: foreach ($types as $type) {
953: TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type {
954: if ($type instanceof ConstantArrayType) {
955: $constantArrayValuesCount += count($type->getValueTypes());
956: }
957:
958: return $traverse($type);
959: });
960: }
961: return $constantArrayValuesCount;
962: }
963:
964: /**
965: * @param list<Type> $constantArrays
966: * @return list<Type>
967: */
968: private static function reduceArrays(array $constantArrays, bool $preserveTaggedUnions): array
969: {
970: $newArrays = [];
971: $arraysToProcess = [];
972: $emptyArray = null;
973: foreach ($constantArrays as $constantArray) {
974: if (!$constantArray->isConstantArray()->yes()) {
975: // This is an optimization for current use-case of $preserveTaggedUnions=false, where we need
976: // one constant array as a result, or we generalize the $constantArrays.
977: if (!$preserveTaggedUnions) {
978: return $constantArrays;
979: }
980: $newArrays[] = $constantArray;
981: continue;
982: }
983:
984: if ($constantArray->isIterableAtLeastOnce()->no()) {
985: $emptyArray = $constantArray;
986: continue;
987: }
988:
989: $arraysToProcess = array_merge($arraysToProcess, $constantArray->getConstantArrays());
990: }
991:
992: if ($emptyArray !== null) {
993: $newArrays[] = $emptyArray;
994: }
995:
996: $arraysToProcessPerKey = [];
997: foreach ($arraysToProcess as $i => $arrayToProcess) {
998: foreach ($arrayToProcess->getKeyTypes() as $keyType) {
999: $arraysToProcessPerKey[$keyType->getValue()][] = $i;
1000: }
1001: }
1002:
1003: $eligibleCombinations = [];
1004:
1005: foreach ($arraysToProcessPerKey as $arrays) {
1006: for ($i = 0, $arraysCount = count($arrays); $i < $arraysCount - 1; $i++) {
1007: for ($j = $i + 1; $j < $arraysCount; $j++) {
1008: $eligibleCombinations[$arrays[$i]][$arrays[$j]] ??= 0;
1009: $eligibleCombinations[$arrays[$i]][$arrays[$j]]++;
1010: }
1011: }
1012: }
1013:
1014: foreach ($eligibleCombinations as $i => $other) {
1015: if (!array_key_exists($i, $arraysToProcess)) {
1016: continue;
1017: }
1018:
1019: foreach ($other as $j => $overlappingKeysCount) {
1020: if (!array_key_exists($j, $arraysToProcess)) {
1021: continue;
1022: }
1023:
1024: if (
1025: $preserveTaggedUnions
1026: && $overlappingKeysCount === count($arraysToProcess[$i]->getKeyTypes())
1027: && $arraysToProcess[$j]->isKeysSupersetOf($arraysToProcess[$i])
1028: ) {
1029: $arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
1030: unset($arraysToProcess[$i]);
1031: continue 2;
1032: }
1033:
1034: if (
1035: $preserveTaggedUnions
1036: && $overlappingKeysCount === count($arraysToProcess[$j]->getKeyTypes())
1037: && $arraysToProcess[$i]->isKeysSupersetOf($arraysToProcess[$j])
1038: ) {
1039: $arraysToProcess[$i] = $arraysToProcess[$i]->mergeWith($arraysToProcess[$j]);
1040: unset($arraysToProcess[$j]);
1041: continue 1;
1042: }
1043:
1044: if (
1045: !$preserveTaggedUnions
1046: // both arrays have same keys
1047: && $overlappingKeysCount === count($arraysToProcess[$i]->getKeyTypes())
1048: && $overlappingKeysCount === count($arraysToProcess[$j]->getKeyTypes())
1049: ) {
1050: $arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
1051: unset($arraysToProcess[$i]);
1052: continue 2;
1053: }
1054: }
1055: }
1056:
1057: return array_merge($newArrays, $arraysToProcess);
1058: }
1059:
1060: public static function intersect(Type ...$types): Type
1061: {
1062: $types = array_values($types);
1063:
1064: $typesCount = count($types);
1065: if ($typesCount === 0) {
1066: return new NeverType();
1067: }
1068: if ($typesCount === 1) {
1069: return $types[0];
1070: }
1071:
1072: $sortTypes = static function (Type $a, Type $b): int {
1073: if (!$a instanceof UnionType || !$b instanceof UnionType) {
1074: return 0;
1075: }
1076:
1077: if ($a instanceof TemplateType) {
1078: return -1;
1079: }
1080: if ($b instanceof TemplateType) {
1081: return 1;
1082: }
1083:
1084: if ($a instanceof BenevolentUnionType) {
1085: return -1;
1086: }
1087: if ($b instanceof BenevolentUnionType) {
1088: return 1;
1089: }
1090:
1091: return 0;
1092: };
1093: usort($types, $sortTypes);
1094: // transform A & (B | C) to (A & B) | (A & C)
1095: foreach ($types as $i => $type) {
1096: if (!$type instanceof UnionType) {
1097: continue;
1098: }
1099:
1100: $topLevelUnionSubTypes = [];
1101: $innerTypes = $type->getTypes();
1102: usort($innerTypes, $sortTypes);
1103: $slice1 = array_slice($types, 0, $i);
1104: $slice2 = array_slice($types, $i + 1);
1105: foreach ($innerTypes as $innerUnionSubType) {
1106: $topLevelUnionSubTypes[] = self::intersect(
1107: $innerUnionSubType,
1108: ...$slice1,
1109: ...$slice2,
1110: );
1111: }
1112:
1113: $union = self::union(...$topLevelUnionSubTypes);
1114: if ($union instanceof NeverType) {
1115: return $union;
1116: }
1117:
1118: if ($type instanceof BenevolentUnionType) {
1119: $union = TypeUtils::toBenevolentUnion($union);
1120: }
1121:
1122: if ($type instanceof TemplateUnionType || $type instanceof TemplateBenevolentUnionType) {
1123: $union = TemplateTypeFactory::create(
1124: $type->getScope(),
1125: $type->getName(),
1126: $union,
1127: $type->getVariance(),
1128: $type->getStrategy(),
1129: $type->getDefault(),
1130: );
1131: }
1132:
1133: return $union;
1134: }
1135: $typesCount = count($types);
1136:
1137: // transform A & (B & C) to A & B & C
1138: for ($i = 0; $i < $typesCount; $i++) {
1139: $type = $types[$i];
1140:
1141: if (!($type instanceof IntersectionType)) {
1142: continue;
1143: }
1144:
1145: array_splice($types, $i--, 1, $type->getTypes());
1146: $typesCount = count($types);
1147: }
1148:
1149: $hasOffsetValueTypeCount = 0;
1150: $newTypes = [];
1151: foreach ($types as $type) {
1152: if (!$type instanceof HasOffsetValueType) {
1153: $newTypes[] = $type;
1154: continue;
1155: }
1156:
1157: $hasOffsetValueTypeCount++;
1158: }
1159:
1160: if ($hasOffsetValueTypeCount > 32) {
1161: $newTypes[] = new OversizedArrayType();
1162: $types = $newTypes;
1163: $typesCount = count($types);
1164: }
1165:
1166: usort($types, static function (Type $a, Type $b): int {
1167: // move subtractables with subtracts before those without to avoid losing them in the union logic
1168: if ($a instanceof SubtractableType && $a->getSubtractedType() !== null) {
1169: return -1;
1170: }
1171: if ($b instanceof SubtractableType && $b->getSubtractedType() !== null) {
1172: return 1;
1173: }
1174:
1175: if ($a instanceof ConstantArrayType && !$b instanceof ConstantArrayType) {
1176: return -1;
1177: }
1178: if ($b instanceof ConstantArrayType && !$a instanceof ConstantArrayType) {
1179: return 1;
1180: }
1181:
1182: return 0;
1183: });
1184:
1185: // transform IntegerType & ConstantIntegerType to ConstantIntegerType
1186: // transform Child & Parent to Child
1187: // transform Object & ~null to Object
1188: // transform A & A to A
1189: // transform int[] & string to never
1190: // transform callable & int to never
1191: // transform A & ~A to never
1192: // transform int & string to never
1193: for ($i = 0; $i < $typesCount; $i++) {
1194: for ($j = $i + 1; $j < $typesCount; $j++) {
1195: if ($types[$j] instanceof SubtractableType) {
1196: $typeWithoutSubtractedTypeA = $types[$j]->getTypeWithoutSubtractedType();
1197:
1198: if ($typeWithoutSubtractedTypeA instanceof MixedType && $types[$i] instanceof MixedType) {
1199: $isSuperTypeSubtractableA = $typeWithoutSubtractedTypeA->isSuperTypeOfMixed($types[$i]);
1200: } else {
1201: $isSuperTypeSubtractableA = $typeWithoutSubtractedTypeA->isSuperTypeOf($types[$i]);
1202: }
1203: if ($isSuperTypeSubtractableA->yes()) {
1204: $types[$i] = self::unionWithSubtractedType($types[$i], $types[$j]->getSubtractedType());
1205: array_splice($types, $j--, 1);
1206: $typesCount--;
1207: continue 1;
1208: }
1209: }
1210:
1211: if ($types[$i] instanceof SubtractableType) {
1212: $typeWithoutSubtractedTypeB = $types[$i]->getTypeWithoutSubtractedType();
1213:
1214: if ($typeWithoutSubtractedTypeB instanceof MixedType && $types[$j] instanceof MixedType) {
1215: $isSuperTypeSubtractableB = $typeWithoutSubtractedTypeB->isSuperTypeOfMixed($types[$j]);
1216: } else {
1217: $isSuperTypeSubtractableB = $typeWithoutSubtractedTypeB->isSuperTypeOf($types[$j]);
1218: }
1219: if ($isSuperTypeSubtractableB->yes()) {
1220: $types[$j] = self::unionWithSubtractedType($types[$j], $types[$i]->getSubtractedType());
1221: array_splice($types, $i--, 1);
1222: $typesCount--;
1223: continue 2;
1224: }
1225: }
1226:
1227: if ($types[$i] instanceof IntegerRangeType) {
1228: $intersectionType = $types[$i]->tryIntersect($types[$j]);
1229: if ($intersectionType !== null) {
1230: $types[$j] = $intersectionType;
1231: array_splice($types, $i--, 1);
1232: $typesCount--;
1233: continue 2;
1234: }
1235: }
1236:
1237: if ($types[$j] instanceof IterableType) {
1238: $isSuperTypeA = $types[$j]->isSuperTypeOfMixed($types[$i]);
1239: } else {
1240: $isSuperTypeA = $types[$j]->isSuperTypeOf($types[$i]);
1241: }
1242:
1243: if ($isSuperTypeA->yes()) {
1244: array_splice($types, $j--, 1);
1245: $typesCount--;
1246: continue;
1247: }
1248:
1249: if ($types[$i] instanceof IterableType) {
1250: $isSuperTypeB = $types[$i]->isSuperTypeOfMixed($types[$j]);
1251: } else {
1252: $isSuperTypeB = $types[$i]->isSuperTypeOf($types[$j]);
1253: }
1254:
1255: if ($isSuperTypeB->maybe()) {
1256: if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetType) {
1257: $types[$i] = $types[$i]->makeOffsetRequired($types[$j]->getOffsetType());
1258: array_splice($types, $j--, 1);
1259: $typesCount--;
1260: continue;
1261: }
1262:
1263: if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetType) {
1264: $types[$j] = $types[$j]->makeOffsetRequired($types[$i]->getOffsetType());
1265: array_splice($types, $i--, 1);
1266: $typesCount--;
1267: continue 2;
1268: }
1269:
1270: if (
1271: $types[$i] instanceof ConstantArrayType
1272: && count($types[$i]->getKeyTypes()) === 1
1273: && $types[$i]->isOptionalKey(0)
1274: && $types[$j] instanceof NonEmptyArrayType
1275: ) {
1276: $types[$i] = $types[$i]->makeOffsetRequired($types[$i]->getKeyTypes()[0]);
1277: array_splice($types, $j--, 1);
1278: $typesCount--;
1279: continue;
1280: }
1281:
1282: if (
1283: $types[$j] instanceof ConstantArrayType
1284: && count($types[$j]->getKeyTypes()) === 1
1285: && $types[$j]->isOptionalKey(0)
1286: && $types[$i] instanceof NonEmptyArrayType
1287: ) {
1288: $types[$j] = $types[$j]->makeOffsetRequired($types[$j]->getKeyTypes()[0]);
1289: array_splice($types, $i--, 1);
1290: $typesCount--;
1291: continue 2;
1292: }
1293:
1294: if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetValueType) {
1295: $offsetType = $types[$j]->getOffsetType();
1296: $valueType = $types[$j]->getValueType();
1297: $newValueType = self::intersect($types[$i]->getOffsetValueType($offsetType), $valueType);
1298: if ($newValueType instanceof NeverType) {
1299: return $newValueType;
1300: }
1301: $types[$i] = $types[$i]->setOffsetValueType($offsetType, $newValueType);
1302: array_splice($types, $j--, 1);
1303: $typesCount--;
1304: continue;
1305: }
1306:
1307: if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetValueType) {
1308: $offsetType = $types[$i]->getOffsetType();
1309: $valueType = $types[$i]->getValueType();
1310: $newValueType = self::intersect($types[$j]->getOffsetValueType($offsetType), $valueType);
1311: if ($newValueType instanceof NeverType) {
1312: return $newValueType;
1313: }
1314:
1315: $types[$j] = $types[$j]->setOffsetValueType($offsetType, $newValueType);
1316: array_splice($types, $i--, 1);
1317: $typesCount--;
1318: continue 2;
1319: }
1320:
1321: if ($types[$i] instanceof OversizedArrayType && $types[$j] instanceof HasOffsetValueType) {
1322: array_splice($types, $j--, 1);
1323: $typesCount--;
1324: continue;
1325: }
1326:
1327: if ($types[$j] instanceof OversizedArrayType && $types[$i] instanceof HasOffsetValueType) {
1328: array_splice($types, $i--, 1);
1329: $typesCount--;
1330: continue 2;
1331: }
1332:
1333: if ($types[$i] instanceof ObjectShapeType && $types[$j] instanceof HasPropertyType) {
1334: $types[$i] = $types[$i]->makePropertyRequired($types[$j]->getPropertyName());
1335: array_splice($types, $j--, 1);
1336: $typesCount--;
1337: continue;
1338: }
1339:
1340: if ($types[$j] instanceof ObjectShapeType && $types[$i] instanceof HasPropertyType) {
1341: $types[$j] = $types[$j]->makePropertyRequired($types[$i]->getPropertyName());
1342: array_splice($types, $i--, 1);
1343: $typesCount--;
1344: continue 2;
1345: }
1346:
1347: if ($types[$i] instanceof ConstantArrayType && ($types[$j] instanceof ArrayType || $types[$j] instanceof ConstantArrayType)) {
1348: $newArray = ConstantArrayTypeBuilder::createEmpty();
1349: $valueTypes = $types[$i]->getValueTypes();
1350: foreach ($types[$i]->getKeyTypes() as $k => $keyType) {
1351: $newArray->setOffsetValueType(
1352: self::intersect($keyType, $types[$j]->getIterableKeyType()),
1353: self::intersect($valueTypes[$k], $types[$j]->getIterableValueType()),
1354: $types[$i]->isOptionalKey($k) && !$types[$j]->hasOffsetValueType($keyType)->yes(),
1355: );
1356: }
1357: $types[$i] = $newArray->getArray();
1358: array_splice($types, $j--, 1);
1359: $typesCount--;
1360: continue 2;
1361: }
1362:
1363: if ($types[$j] instanceof ConstantArrayType && ($types[$i] instanceof ArrayType || $types[$i] instanceof ConstantArrayType)) {
1364: $newArray = ConstantArrayTypeBuilder::createEmpty();
1365: $valueTypes = $types[$j]->getValueTypes();
1366: foreach ($types[$j]->getKeyTypes() as $k => $keyType) {
1367: $newArray->setOffsetValueType(
1368: self::intersect($keyType, $types[$i]->getIterableKeyType()),
1369: self::intersect($valueTypes[$k], $types[$i]->getIterableValueType()),
1370: $types[$j]->isOptionalKey($k) && !$types[$i]->hasOffsetValueType($keyType)->yes(),
1371: );
1372: }
1373: $types[$j] = $newArray->getArray();
1374: array_splice($types, $i--, 1);
1375: $typesCount--;
1376: continue 2;
1377: }
1378:
1379: if (
1380: ($types[$i] instanceof ArrayType || $types[$i] instanceof ConstantArrayType || $types[$i] instanceof IterableType) &&
1381: ($types[$j] instanceof ArrayType || $types[$j] instanceof ConstantArrayType || $types[$j] instanceof IterableType)
1382: ) {
1383: $keyType = self::intersect($types[$i]->getIterableKeyType(), $types[$j]->getKeyType());
1384: $itemType = self::intersect($types[$i]->getItemType(), $types[$j]->getItemType());
1385: if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
1386: $types[$j] = new IterableType($keyType, $itemType);
1387: } else {
1388: $types[$j] = new ArrayType($keyType, $itemType);
1389: }
1390: array_splice($types, $i--, 1);
1391: $typesCount--;
1392: continue 2;
1393: }
1394:
1395: if ($types[$i] instanceof GenericClassStringType && $types[$j] instanceof GenericClassStringType) {
1396: $genericType = self::intersect($types[$i]->getGenericType(), $types[$j]->getGenericType());
1397: $types[$i] = new GenericClassStringType($genericType);
1398: array_splice($types, $j--, 1);
1399: $typesCount--;
1400: continue;
1401: }
1402:
1403: if (
1404: $types[$i] instanceof ArrayType
1405: && get_class($types[$i]) === ArrayType::class
1406: && $types[$j] instanceof AccessoryArrayListType
1407: && !$types[$j]->getIterableKeyType()->isSuperTypeOf($types[$i]->getIterableKeyType())->yes()
1408: ) {
1409: $keyType = self::intersect($types[$i]->getIterableKeyType(), $types[$j]->getIterableKeyType());
1410: if ($keyType instanceof NeverType) {
1411: return $keyType;
1412: }
1413: $types[$i] = new ArrayType($keyType, $types[$i]->getItemType());
1414: continue;
1415: }
1416:
1417: continue;
1418: }
1419:
1420: if ($isSuperTypeB->yes()) {
1421: array_splice($types, $i--, 1);
1422: $typesCount--;
1423: continue 2;
1424: }
1425:
1426: if ($isSuperTypeA->no()) {
1427: return new NeverType();
1428: }
1429: }
1430: }
1431:
1432: if ($typesCount === 1) {
1433: return $types[0];
1434: }
1435:
1436: return new IntersectionType($types);
1437: }
1438:
1439: public static function removeFalsey(Type $type): Type
1440: {
1441: return self::remove($type, StaticTypeFactory::falsey());
1442: }
1443:
1444: public static function removeTruthy(Type $type): Type
1445: {
1446: return self::remove($type, StaticTypeFactory::truthy());
1447: }
1448:
1449: }
1450: