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]->isConstantScalarValue()->yes()) {
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]->isBoolean()->yes()) {
200: $hasGenericScalarTypes[ConstantBooleanType::class] = true;
201: }
202: if ($types[$i]->isFloat()->yes()) {
203: $hasGenericScalarTypes[ConstantFloatType::class] = true;
204: }
205: if ($types[$i]->isInteger()->yes() && !$types[$i] instanceof IntegerRangeType) {
206: $hasGenericScalarTypes[ConstantIntegerType::class] = true;
207: }
208: if ($types[$i]->isString()->yes() && $types[$i]->isClassString()->no() && TypeUtils::getAccessoryTypes($types[$i]) === []) {
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 ($subtractedType instanceof SubtractableType) {
544: $withoutSubtracted = $subtractedType->getTypeWithoutSubtractedType();
545: if ($withoutSubtracted->isSuperTypeOf($type)->yes()) {
546: $subtractedSubtractedType = $subtractedType->getSubtractedType();
547: if ($subtractedSubtractedType === null) {
548: return new NeverType();
549: }
550:
551: return self::intersect($type, $subtractedSubtractedType);
552: }
553: }
554:
555: if ($type instanceof SubtractableType) {
556: $subtractedType = $type->getSubtractedType() === null
557: ? $subtractedType
558: : self::union($type->getSubtractedType(), $subtractedType);
559:
560: $subtractedType = self::intersect(
561: $type->getTypeWithoutSubtractedType(),
562: $subtractedType,
563: );
564: if ($subtractedType instanceof NeverType) {
565: $subtractedType = null;
566: }
567:
568: return $type->changeSubtractedType($subtractedType);
569: }
570:
571: if ($subtractedType->isSuperTypeOf($type)->yes()) {
572: return new NeverType();
573: }
574:
575: return self::remove($type, $subtractedType);
576: }
577:
578: private static function intersectWithSubtractedType(
579: SubtractableType $a,
580: Type $b,
581: ): Type
582: {
583: if ($a->getSubtractedType() === null) {
584: return $a;
585: }
586:
587: if ($b instanceof IntersectionType) {
588: $subtractableTypes = [];
589: foreach ($b->getTypes() as $innerType) {
590: if (!$innerType instanceof SubtractableType) {
591: continue;
592: }
593:
594: $subtractableTypes[] = $innerType;
595: }
596:
597: if (count($subtractableTypes) === 0) {
598: return $a->getTypeWithoutSubtractedType();
599: }
600:
601: $subtractedTypes = [];
602: foreach ($subtractableTypes as $subtractableType) {
603: if ($subtractableType->getSubtractedType() === null) {
604: continue;
605: }
606:
607: $subtractedTypes[] = $subtractableType->getSubtractedType();
608: }
609:
610: if (count($subtractedTypes) === 0) {
611: return $a->getTypeWithoutSubtractedType();
612:
613: }
614:
615: $subtractedType = self::union(...$subtractedTypes);
616: } else {
617: $isBAlreadySubtracted = $a->getSubtractedType()->isSuperTypeOf($b);
618:
619: if ($isBAlreadySubtracted->no()) {
620: return $a;
621: } elseif ($isBAlreadySubtracted->yes()) {
622: $subtractedType = self::remove($a->getSubtractedType(), $b);
623:
624: if ($subtractedType instanceof NeverType) {
625: $subtractedType = null;
626: }
627:
628: return $a->changeSubtractedType($subtractedType);
629: } elseif ($b instanceof SubtractableType) {
630: $subtractedType = $b->getSubtractedType();
631: if ($subtractedType === null) {
632: return $a->getTypeWithoutSubtractedType();
633: }
634: } else {
635: $subtractedTypeTmp = self::intersect($a->getTypeWithoutSubtractedType(), $a->getSubtractedType());
636: if ($b->isSuperTypeOf($subtractedTypeTmp)->yes()) {
637: return $a->getTypeWithoutSubtractedType();
638: }
639: $subtractedType = new MixedType(subtractedType: $b);
640: }
641: }
642:
643: $subtractedType = self::intersect(
644: $a->getSubtractedType(),
645: $subtractedType,
646: );
647: if ($subtractedType instanceof NeverType) {
648: $subtractedType = null;
649: }
650:
651: return $a->changeSubtractedType($subtractedType);
652: }
653:
654: /**
655: * @param Type[] $arrayTypes
656: * @return Type[]
657: */
658: private static function processArrayAccessoryTypes(array $arrayTypes): array
659: {
660: $isIterableAtLeastOnce = [];
661: $accessoryTypes = [];
662: foreach ($arrayTypes as $i => $arrayType) {
663: $isIterableAtLeastOnce[] = $arrayType->isIterableAtLeastOnce();
664:
665: if ($arrayType instanceof IntersectionType) {
666: foreach ($arrayType->getTypes() as $innerType) {
667: if ($innerType instanceof TemplateType) {
668: break;
669: }
670: if (!($innerType instanceof AccessoryType) && !($innerType instanceof CallableType)) {
671: continue;
672: }
673: if ($innerType instanceof HasOffsetType) {
674: $offset = $innerType->getOffsetType();
675: if ($offset instanceof ConstantStringType || $offset instanceof ConstantIntegerType) {
676: $innerType = new HasOffsetValueType($offset, $arrayType->getIterableValueType());
677: }
678: }
679: if ($innerType instanceof HasOffsetValueType) {
680: $accessoryTypes[sprintf('hasOffsetValue(%s)', $innerType->getOffsetType()->describe(VerbosityLevel::cache()))][$i] = $innerType;
681: continue;
682: }
683:
684: $accessoryTypes[$innerType->describe(VerbosityLevel::cache())][$i] = $innerType;
685: }
686: }
687:
688: if (!$arrayType->isConstantArray()->yes()) {
689: continue;
690: }
691: $constantArrays = $arrayType->getConstantArrays();
692:
693: foreach ($constantArrays as $constantArray) {
694: if ($constantArray->isList()->yes()) {
695: $list = new AccessoryArrayListType();
696: $accessoryTypes[$list->describe(VerbosityLevel::cache())][$i] = $list;
697: }
698:
699: if (!$constantArray->isIterableAtLeastOnce()->yes()) {
700: continue;
701: }
702:
703: $nonEmpty = new NonEmptyArrayType();
704: $accessoryTypes[$nonEmpty->describe(VerbosityLevel::cache())][$i] = $nonEmpty;
705: }
706: }
707:
708: $commonAccessoryTypes = [];
709: $arrayTypeCount = count($arrayTypes);
710: foreach ($accessoryTypes as $accessoryType) {
711: if (count($accessoryType) !== $arrayTypeCount) {
712: $firstKey = array_key_first($accessoryType);
713: if ($accessoryType[$firstKey] instanceof OversizedArrayType) {
714: $commonAccessoryTypes[] = $accessoryType[$firstKey];
715: }
716: continue;
717: }
718:
719: if ($accessoryType[0] instanceof HasOffsetValueType) {
720: $commonAccessoryTypes[] = self::union(...$accessoryType);
721: continue;
722: }
723:
724: $commonAccessoryTypes[] = $accessoryType[0];
725: }
726:
727: if (TrinaryLogic::createYes()->and(...$isIterableAtLeastOnce)->yes()) {
728: $commonAccessoryTypes[] = new NonEmptyArrayType();
729: }
730:
731: return $commonAccessoryTypes;
732: }
733:
734: /**
735: * @param list<Type> $arrayTypes
736: * @return Type[]
737: */
738: private static function processArrayTypes(array $arrayTypes): array
739: {
740: if ($arrayTypes === []) {
741: return [];
742: }
743:
744: $accessoryTypes = self::processArrayAccessoryTypes($arrayTypes);
745:
746: if (count($arrayTypes) === 1) {
747: return [
748: self::intersect(...$arrayTypes, ...$accessoryTypes),
749: ];
750: }
751:
752: $keyTypesForGeneralArray = [];
753: $valueTypesForGeneralArray = [];
754: $generalArrayOccurred = false;
755: $constantKeyTypesNumbered = [];
756: $filledArrays = 0;
757: $overflowed = false;
758:
759: /** @var int|float $nextConstantKeyTypeIndex */
760: $nextConstantKeyTypeIndex = 1;
761: $constantArraysMap = array_map(
762: static fn (Type $t) => $t->getConstantArrays(),
763: $arrayTypes,
764: );
765:
766: foreach ($arrayTypes as $arrayIdx => $arrayType) {
767: $constantArrays = $constantArraysMap[$arrayIdx];
768: $isConstantArray = $constantArrays !== [];
769: if (!$isConstantArray || !$arrayType->isIterableAtLeastOnce()->no()) {
770: $filledArrays++;
771: }
772:
773: if ($generalArrayOccurred || !$isConstantArray) {
774: foreach ($arrayType->getArrays() as $type) {
775: $keyTypesForGeneralArray[] = $type->getIterableKeyType();
776: $valueTypesForGeneralArray[] = $type->getItemType();
777: $generalArrayOccurred = true;
778: }
779: continue;
780: }
781:
782: $constantArrays = $arrayType->getConstantArrays();
783: foreach ($constantArrays as $constantArray) {
784: foreach ($constantArray->getKeyTypes() as $i => $keyType) {
785: $keyTypesForGeneralArray[] = $keyType;
786: $valueTypesForGeneralArray[] = $constantArray->getValueTypes()[$i];
787:
788: $keyTypeValue = $keyType->getValue();
789: if (array_key_exists($keyTypeValue, $constantKeyTypesNumbered)) {
790: continue;
791: }
792:
793: $constantKeyTypesNumbered[$keyTypeValue] = $nextConstantKeyTypeIndex;
794: $nextConstantKeyTypeIndex *= 2;
795: if (!is_int($nextConstantKeyTypeIndex)) {
796: $generalArrayOccurred = true;
797: $overflowed = true;
798: continue 2;
799: }
800: }
801: }
802: }
803:
804: if ($generalArrayOccurred && (!$overflowed || $filledArrays > 1)) {
805: $reducedArrayTypes = self::reduceArrays($arrayTypes, false);
806: if (count($reducedArrayTypes) === 1) {
807: return [self::intersect($reducedArrayTypes[0], ...$accessoryTypes)];
808: }
809: $scopes = [];
810: $useTemplateArray = true;
811: foreach ($arrayTypes as $arrayType) {
812: if (!$arrayType instanceof TemplateArrayType) {
813: $useTemplateArray = false;
814: break;
815: }
816:
817: $scopes[$arrayType->getScope()->describe()] = $arrayType;
818: }
819:
820: $arrayType = new ArrayType(
821: self::union(...$keyTypesForGeneralArray),
822: self::union(...self::optimizeConstantArrays($valueTypesForGeneralArray)),
823: );
824:
825: if ($useTemplateArray && count($scopes) === 1) {
826: $templateArray = array_values($scopes)[0];
827: $arrayType = new TemplateArrayType(
828: $templateArray->getScope(),
829: $templateArray->getStrategy(),
830: $templateArray->getVariance(),
831: $templateArray->getName(),
832: $arrayType,
833: $templateArray->getDefault(),
834: );
835: }
836:
837: return [
838: self::intersect($arrayType, ...$accessoryTypes),
839: ];
840: }
841:
842: $reducedArrayTypes = self::reduceArrays($arrayTypes, true);
843:
844: return array_map(
845: static fn (Type $arrayType) => self::intersect($arrayType, ...$accessoryTypes),
846: self::optimizeConstantArrays($reducedArrayTypes),
847: );
848: }
849:
850: /**
851: * @param Type[] $types
852: * @return Type[]
853: */
854: private static function optimizeConstantArrays(array $types): array
855: {
856: $constantArrayValuesCount = self::countConstantArrayValueTypes($types);
857:
858: if ($constantArrayValuesCount <= ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
859: return $types;
860: }
861:
862: $results = [];
863: $eachIsOversized = true;
864: foreach ($types as $type) {
865: $isOversized = false;
866: $result = TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$isOversized): Type {
867: if (!$type instanceof ConstantArrayType) {
868: return $traverse($type);
869: }
870:
871: if ($type->isIterableAtLeastOnce()->no()) {
872: return $type;
873: }
874:
875: $isOversized = true;
876:
877: $isList = true;
878: $valueTypes = [];
879: $keyTypes = [];
880: $nextAutoIndex = 0;
881: foreach ($type->getKeyTypes() as $i => $innerKeyType) {
882: if (!$innerKeyType instanceof ConstantIntegerType) {
883: $isList = false;
884: } elseif ($innerKeyType->getValue() !== $nextAutoIndex) {
885: $isList = false;
886: $nextAutoIndex = $innerKeyType->getValue() + 1;
887: } else {
888: $nextAutoIndex++;
889: }
890:
891: $generalizedKeyType = $innerKeyType->generalize(GeneralizePrecision::moreSpecific());
892: $keyTypes[$generalizedKeyType->describe(VerbosityLevel::precise())] = $generalizedKeyType;
893:
894: $innerValueType = $type->getValueTypes()[$i];
895: $generalizedValueType = TypeTraverser::map($innerValueType, static function (Type $type) use ($traverse): Type {
896: if ($type instanceof ArrayType || $type instanceof ConstantArrayType) {
897: return TypeCombinator::intersect($type, new OversizedArrayType());
898: }
899:
900: if ($type instanceof ConstantScalarType) {
901: return $type->generalize(GeneralizePrecision::moreSpecific());
902: }
903:
904: return $traverse($type);
905: });
906: $valueTypes[$generalizedValueType->describe(VerbosityLevel::precise())] = $generalizedValueType;
907: }
908:
909: $keyType = TypeCombinator::union(...array_values($keyTypes));
910: $valueType = TypeCombinator::union(...array_values($valueTypes));
911:
912: $arrayType = new ArrayType($keyType, $valueType);
913: if ($isList) {
914: $arrayType = TypeCombinator::intersect($arrayType, new AccessoryArrayListType());
915: }
916:
917: return TypeCombinator::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType());
918: });
919:
920: if (!$isOversized) {
921: $eachIsOversized = false;
922: }
923:
924: $results[] = $result;
925: }
926:
927: if ($eachIsOversized) {
928: $eachIsList = true;
929: $keyTypes = [];
930: $valueTypes = [];
931: foreach ($results as $result) {
932: $keyTypes[] = $result->getIterableKeyType();
933: $valueTypes[] = $result->getLastIterableValueType();
934: if ($result->isList()->yes()) {
935: continue;
936: }
937: $eachIsList = false;
938: }
939:
940: $keyType = self::union(...$keyTypes);
941: $valueType = self::union(...$valueTypes);
942:
943: if ($valueType instanceof UnionType && count($valueType->getTypes()) > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
944: $valueType = $valueType->generalize(GeneralizePrecision::lessSpecific());
945: }
946:
947: $arrayType = new ArrayType($keyType, $valueType);
948: if ($eachIsList) {
949: $arrayType = self::intersect($arrayType, new AccessoryArrayListType());
950: }
951:
952: return [self::intersect($arrayType, new NonEmptyArrayType(), new OversizedArrayType())];
953: }
954:
955: return $results;
956: }
957:
958: /**
959: * @param Type[] $types
960: */
961: public static function countConstantArrayValueTypes(array $types): int
962: {
963: $constantArrayValuesCount = 0;
964: foreach ($types as $type) {
965: TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type {
966: if ($type instanceof ConstantArrayType) {
967: $constantArrayValuesCount += count($type->getValueTypes());
968: }
969:
970: return $traverse($type);
971: });
972: }
973: return $constantArrayValuesCount;
974: }
975:
976: /**
977: * @param list<Type> $constantArrays
978: * @return list<Type>
979: */
980: private static function reduceArrays(array $constantArrays, bool $preserveTaggedUnions): array
981: {
982: $newArrays = [];
983: $arraysToProcess = [];
984: $emptyArray = null;
985: foreach ($constantArrays as $constantArray) {
986: if (!$constantArray->isConstantArray()->yes()) {
987: // This is an optimization for current use-case of $preserveTaggedUnions=false, where we need
988: // one constant array as a result, or we generalize the $constantArrays.
989: if (!$preserveTaggedUnions) {
990: return $constantArrays;
991: }
992: $newArrays[] = $constantArray;
993: continue;
994: }
995:
996: if ($constantArray->isIterableAtLeastOnce()->no()) {
997: $emptyArray = $constantArray;
998: continue;
999: }
1000:
1001: $arraysToProcess = array_merge($arraysToProcess, $constantArray->getConstantArrays());
1002: }
1003:
1004: if ($emptyArray !== null) {
1005: $newArrays[] = $emptyArray;
1006: }
1007:
1008: $arraysToProcessPerKey = [];
1009: foreach ($arraysToProcess as $i => $arrayToProcess) {
1010: foreach ($arrayToProcess->getKeyTypes() as $keyType) {
1011: $arraysToProcessPerKey[$keyType->getValue()][] = $i;
1012: }
1013: }
1014:
1015: $eligibleCombinations = [];
1016:
1017: foreach ($arraysToProcessPerKey as $arrays) {
1018: for ($i = 0, $arraysCount = count($arrays); $i < $arraysCount - 1; $i++) {
1019: for ($j = $i + 1; $j < $arraysCount; $j++) {
1020: $eligibleCombinations[$arrays[$i]][$arrays[$j]] ??= 0;
1021: $eligibleCombinations[$arrays[$i]][$arrays[$j]]++;
1022: }
1023: }
1024: }
1025:
1026: foreach ($eligibleCombinations as $i => $other) {
1027: if (!array_key_exists($i, $arraysToProcess)) {
1028: continue;
1029: }
1030:
1031: foreach ($other as $j => $overlappingKeysCount) {
1032: if (!array_key_exists($j, $arraysToProcess)) {
1033: continue;
1034: }
1035:
1036: if (
1037: $preserveTaggedUnions
1038: && $overlappingKeysCount === count($arraysToProcess[$i]->getKeyTypes())
1039: && $arraysToProcess[$j]->isKeysSupersetOf($arraysToProcess[$i])
1040: ) {
1041: $arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
1042: unset($arraysToProcess[$i]);
1043: continue 2;
1044: }
1045:
1046: if (
1047: $preserveTaggedUnions
1048: && $overlappingKeysCount === count($arraysToProcess[$j]->getKeyTypes())
1049: && $arraysToProcess[$i]->isKeysSupersetOf($arraysToProcess[$j])
1050: ) {
1051: $arraysToProcess[$i] = $arraysToProcess[$i]->mergeWith($arraysToProcess[$j]);
1052: unset($arraysToProcess[$j]);
1053: continue 1;
1054: }
1055:
1056: if (
1057: !$preserveTaggedUnions
1058: // both arrays have same keys
1059: && $overlappingKeysCount === count($arraysToProcess[$i]->getKeyTypes())
1060: && $overlappingKeysCount === count($arraysToProcess[$j]->getKeyTypes())
1061: ) {
1062: $arraysToProcess[$j] = $arraysToProcess[$j]->mergeWith($arraysToProcess[$i]);
1063: unset($arraysToProcess[$i]);
1064: continue 2;
1065: }
1066: }
1067: }
1068:
1069: return array_merge($newArrays, $arraysToProcess);
1070: }
1071:
1072: public static function intersect(Type ...$types): Type
1073: {
1074: $types = array_values($types);
1075:
1076: $typesCount = count($types);
1077: if ($typesCount === 0) {
1078: return new NeverType();
1079: }
1080: if ($typesCount === 1) {
1081: return $types[0];
1082: }
1083:
1084: $sortTypes = static function (Type $a, Type $b): int {
1085: if (!$a instanceof UnionType || !$b instanceof UnionType) {
1086: return 0;
1087: }
1088:
1089: if ($a instanceof TemplateType) {
1090: return -1;
1091: }
1092: if ($b instanceof TemplateType) {
1093: return 1;
1094: }
1095:
1096: if ($a instanceof BenevolentUnionType) {
1097: return -1;
1098: }
1099: if ($b instanceof BenevolentUnionType) {
1100: return 1;
1101: }
1102:
1103: return 0;
1104: };
1105: usort($types, $sortTypes);
1106: // transform A & (B | C) to (A & B) | (A & C)
1107: foreach ($types as $i => $type) {
1108: if (!$type instanceof UnionType) {
1109: continue;
1110: }
1111:
1112: $topLevelUnionSubTypes = [];
1113: $innerTypes = $type->getTypes();
1114: usort($innerTypes, $sortTypes);
1115: $slice1 = array_slice($types, 0, $i);
1116: $slice2 = array_slice($types, $i + 1);
1117: foreach ($innerTypes as $innerUnionSubType) {
1118: $topLevelUnionSubTypes[] = self::intersect(
1119: $innerUnionSubType,
1120: ...$slice1,
1121: ...$slice2,
1122: );
1123: }
1124:
1125: $union = self::union(...$topLevelUnionSubTypes);
1126: if ($union instanceof NeverType) {
1127: return $union;
1128: }
1129:
1130: if ($type instanceof BenevolentUnionType) {
1131: $union = TypeUtils::toBenevolentUnion($union);
1132: }
1133:
1134: if ($type instanceof TemplateUnionType || $type instanceof TemplateBenevolentUnionType) {
1135: $union = TemplateTypeFactory::create(
1136: $type->getScope(),
1137: $type->getName(),
1138: $union,
1139: $type->getVariance(),
1140: $type->getStrategy(),
1141: $type->getDefault(),
1142: );
1143: }
1144:
1145: return $union;
1146: }
1147: $typesCount = count($types);
1148:
1149: // transform A & (B & C) to A & B & C
1150: for ($i = 0; $i < $typesCount; $i++) {
1151: $type = $types[$i];
1152:
1153: if (!($type instanceof IntersectionType)) {
1154: continue;
1155: }
1156:
1157: array_splice($types, $i--, 1, $type->getTypes());
1158: $typesCount = count($types);
1159: }
1160:
1161: $hasOffsetValueTypeCount = 0;
1162: $newTypes = [];
1163: foreach ($types as $type) {
1164: if (!$type instanceof HasOffsetValueType) {
1165: $newTypes[] = $type;
1166: continue;
1167: }
1168:
1169: $hasOffsetValueTypeCount++;
1170: }
1171:
1172: if ($hasOffsetValueTypeCount > 32) {
1173: $newTypes[] = new OversizedArrayType();
1174: $types = $newTypes;
1175: $typesCount = count($types);
1176: }
1177:
1178: usort($types, static function (Type $a, Type $b): int {
1179: // move subtractables with subtracts before those without to avoid losing them in the union logic
1180: if ($a instanceof SubtractableType && $a->getSubtractedType() !== null) {
1181: return -1;
1182: }
1183: if ($b instanceof SubtractableType && $b->getSubtractedType() !== null) {
1184: return 1;
1185: }
1186:
1187: if ($a instanceof ConstantArrayType && !$b instanceof ConstantArrayType) {
1188: return -1;
1189: }
1190: if ($b instanceof ConstantArrayType && !$a instanceof ConstantArrayType) {
1191: return 1;
1192: }
1193:
1194: return 0;
1195: });
1196:
1197: // transform IntegerType & ConstantIntegerType to ConstantIntegerType
1198: // transform Child & Parent to Child
1199: // transform Object & ~null to Object
1200: // transform A & A to A
1201: // transform int[] & string to never
1202: // transform callable & int to never
1203: // transform A & ~A to never
1204: // transform int & string to never
1205: for ($i = 0; $i < $typesCount; $i++) {
1206: for ($j = $i + 1; $j < $typesCount; $j++) {
1207: if ($types[$j] instanceof SubtractableType) {
1208: $typeWithoutSubtractedTypeA = $types[$j]->getTypeWithoutSubtractedType();
1209:
1210: if ($typeWithoutSubtractedTypeA instanceof MixedType && $types[$i] instanceof MixedType) {
1211: $isSuperTypeSubtractableA = $typeWithoutSubtractedTypeA->isSuperTypeOfMixed($types[$i]);
1212: } else {
1213: $isSuperTypeSubtractableA = $typeWithoutSubtractedTypeA->isSuperTypeOf($types[$i]);
1214: }
1215: if ($isSuperTypeSubtractableA->yes()) {
1216: $types[$i] = self::unionWithSubtractedType($types[$i], $types[$j]->getSubtractedType());
1217: array_splice($types, $j--, 1);
1218: $typesCount--;
1219: continue 1;
1220: }
1221: }
1222:
1223: if ($types[$i] instanceof SubtractableType) {
1224: $typeWithoutSubtractedTypeB = $types[$i]->getTypeWithoutSubtractedType();
1225:
1226: if ($typeWithoutSubtractedTypeB instanceof MixedType && $types[$j] instanceof MixedType) {
1227: $isSuperTypeSubtractableB = $typeWithoutSubtractedTypeB->isSuperTypeOfMixed($types[$j]);
1228: } else {
1229: $isSuperTypeSubtractableB = $typeWithoutSubtractedTypeB->isSuperTypeOf($types[$j]);
1230: }
1231: if ($isSuperTypeSubtractableB->yes()) {
1232: $types[$j] = self::unionWithSubtractedType($types[$j], $types[$i]->getSubtractedType());
1233: array_splice($types, $i--, 1);
1234: $typesCount--;
1235: continue 2;
1236: }
1237: }
1238:
1239: if ($types[$i] instanceof IntegerRangeType) {
1240: $intersectionType = $types[$i]->tryIntersect($types[$j]);
1241: if ($intersectionType !== null) {
1242: $types[$j] = $intersectionType;
1243: array_splice($types, $i--, 1);
1244: $typesCount--;
1245: continue 2;
1246: }
1247: }
1248:
1249: if ($types[$j] instanceof IterableType) {
1250: $isSuperTypeA = $types[$j]->isSuperTypeOfMixed($types[$i]);
1251: } else {
1252: $isSuperTypeA = $types[$j]->isSuperTypeOf($types[$i]);
1253: }
1254:
1255: if ($isSuperTypeA->yes()) {
1256: array_splice($types, $j--, 1);
1257: $typesCount--;
1258: continue;
1259: }
1260:
1261: if ($types[$i] instanceof IterableType) {
1262: $isSuperTypeB = $types[$i]->isSuperTypeOfMixed($types[$j]);
1263: } else {
1264: $isSuperTypeB = $types[$i]->isSuperTypeOf($types[$j]);
1265: }
1266:
1267: if ($isSuperTypeB->maybe()) {
1268: if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetType) {
1269: $types[$i] = $types[$i]->makeOffsetRequired($types[$j]->getOffsetType());
1270: array_splice($types, $j--, 1);
1271: $typesCount--;
1272: continue;
1273: }
1274:
1275: if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetType) {
1276: $types[$j] = $types[$j]->makeOffsetRequired($types[$i]->getOffsetType());
1277: array_splice($types, $i--, 1);
1278: $typesCount--;
1279: continue 2;
1280: }
1281:
1282: if (
1283: $types[$i] instanceof ConstantArrayType
1284: && count($types[$i]->getKeyTypes()) === 1
1285: && $types[$i]->isOptionalKey(0)
1286: && $types[$j] instanceof NonEmptyArrayType
1287: ) {
1288: $types[$i] = $types[$i]->makeOffsetRequired($types[$i]->getKeyTypes()[0]);
1289: array_splice($types, $j--, 1);
1290: $typesCount--;
1291: continue;
1292: }
1293:
1294: if (
1295: $types[$j] instanceof ConstantArrayType
1296: && count($types[$j]->getKeyTypes()) === 1
1297: && $types[$j]->isOptionalKey(0)
1298: && $types[$i] instanceof NonEmptyArrayType
1299: ) {
1300: $types[$j] = $types[$j]->makeOffsetRequired($types[$j]->getKeyTypes()[0]);
1301: array_splice($types, $i--, 1);
1302: $typesCount--;
1303: continue 2;
1304: }
1305:
1306: if ($types[$i] instanceof ConstantArrayType && $types[$j] instanceof HasOffsetValueType) {
1307: $offsetType = $types[$j]->getOffsetType();
1308: $valueType = $types[$j]->getValueType();
1309: $newValueType = self::intersect($types[$i]->getOffsetValueType($offsetType), $valueType);
1310: if ($newValueType instanceof NeverType) {
1311: return $newValueType;
1312: }
1313: $types[$i] = $types[$i]->setOffsetValueType($offsetType, $newValueType);
1314: array_splice($types, $j--, 1);
1315: $typesCount--;
1316: continue;
1317: }
1318:
1319: if ($types[$j] instanceof ConstantArrayType && $types[$i] instanceof HasOffsetValueType) {
1320: $offsetType = $types[$i]->getOffsetType();
1321: $valueType = $types[$i]->getValueType();
1322: $newValueType = self::intersect($types[$j]->getOffsetValueType($offsetType), $valueType);
1323: if ($newValueType instanceof NeverType) {
1324: return $newValueType;
1325: }
1326:
1327: $types[$j] = $types[$j]->setOffsetValueType($offsetType, $newValueType);
1328: array_splice($types, $i--, 1);
1329: $typesCount--;
1330: continue 2;
1331: }
1332:
1333: if ($types[$i] instanceof OversizedArrayType && $types[$j] instanceof HasOffsetValueType) {
1334: array_splice($types, $j--, 1);
1335: $typesCount--;
1336: continue;
1337: }
1338:
1339: if ($types[$j] instanceof OversizedArrayType && $types[$i] instanceof HasOffsetValueType) {
1340: array_splice($types, $i--, 1);
1341: $typesCount--;
1342: continue 2;
1343: }
1344:
1345: if ($types[$i] instanceof ObjectShapeType && $types[$j] instanceof HasPropertyType) {
1346: $types[$i] = $types[$i]->makePropertyRequired($types[$j]->getPropertyName());
1347: array_splice($types, $j--, 1);
1348: $typesCount--;
1349: continue;
1350: }
1351:
1352: if ($types[$j] instanceof ObjectShapeType && $types[$i] instanceof HasPropertyType) {
1353: $types[$j] = $types[$j]->makePropertyRequired($types[$i]->getPropertyName());
1354: array_splice($types, $i--, 1);
1355: $typesCount--;
1356: continue 2;
1357: }
1358:
1359: if ($types[$i] instanceof ConstantArrayType && ($types[$j] instanceof ArrayType || $types[$j] instanceof ConstantArrayType)) {
1360: $newArray = ConstantArrayTypeBuilder::createEmpty();
1361: $valueTypes = $types[$i]->getValueTypes();
1362: foreach ($types[$i]->getKeyTypes() as $k => $keyType) {
1363: $newArray->setOffsetValueType(
1364: self::intersect($keyType, $types[$j]->getIterableKeyType()),
1365: self::intersect($valueTypes[$k], $types[$j]->getIterableValueType()),
1366: $types[$i]->isOptionalKey($k) && !$types[$j]->hasOffsetValueType($keyType)->yes(),
1367: );
1368: }
1369: $types[$i] = $newArray->getArray();
1370: array_splice($types, $j--, 1);
1371: $typesCount--;
1372: continue 2;
1373: }
1374:
1375: if ($types[$j] instanceof ConstantArrayType && ($types[$i] instanceof ArrayType || $types[$i] instanceof ConstantArrayType)) {
1376: $newArray = ConstantArrayTypeBuilder::createEmpty();
1377: $valueTypes = $types[$j]->getValueTypes();
1378: foreach ($types[$j]->getKeyTypes() as $k => $keyType) {
1379: $newArray->setOffsetValueType(
1380: self::intersect($keyType, $types[$i]->getIterableKeyType()),
1381: self::intersect($valueTypes[$k], $types[$i]->getIterableValueType()),
1382: $types[$j]->isOptionalKey($k) && !$types[$i]->hasOffsetValueType($keyType)->yes(),
1383: );
1384: }
1385: $types[$j] = $newArray->getArray();
1386: array_splice($types, $i--, 1);
1387: $typesCount--;
1388: continue 2;
1389: }
1390:
1391: if (
1392: ($types[$i] instanceof ArrayType || $types[$i] instanceof ConstantArrayType || $types[$i] instanceof IterableType) &&
1393: ($types[$j] instanceof ArrayType || $types[$j] instanceof ConstantArrayType || $types[$j] instanceof IterableType)
1394: ) {
1395: $keyType = self::intersect($types[$i]->getIterableKeyType(), $types[$j]->getKeyType());
1396: $itemType = self::intersect($types[$i]->getItemType(), $types[$j]->getItemType());
1397: if ($types[$i] instanceof IterableType && $types[$j] instanceof IterableType) {
1398: $types[$j] = new IterableType($keyType, $itemType);
1399: } else {
1400: $types[$j] = new ArrayType($keyType, $itemType);
1401: }
1402: array_splice($types, $i--, 1);
1403: $typesCount--;
1404: continue 2;
1405: }
1406:
1407: if ($types[$i] instanceof GenericClassStringType && $types[$j] instanceof GenericClassStringType) {
1408: $genericType = self::intersect($types[$i]->getGenericType(), $types[$j]->getGenericType());
1409: $types[$i] = new GenericClassStringType($genericType);
1410: array_splice($types, $j--, 1);
1411: $typesCount--;
1412: continue;
1413: }
1414:
1415: if (
1416: $types[$i] instanceof ArrayType
1417: && get_class($types[$i]) === ArrayType::class
1418: && $types[$j] instanceof AccessoryArrayListType
1419: && !$types[$j]->getIterableKeyType()->isSuperTypeOf($types[$i]->getIterableKeyType())->yes()
1420: ) {
1421: $keyType = self::intersect($types[$i]->getIterableKeyType(), $types[$j]->getIterableKeyType());
1422: if ($keyType instanceof NeverType) {
1423: return $keyType;
1424: }
1425: $types[$i] = new ArrayType($keyType, $types[$i]->getItemType());
1426: continue;
1427: }
1428:
1429: continue;
1430: }
1431:
1432: if ($isSuperTypeB->yes()) {
1433: array_splice($types, $i--, 1);
1434: $typesCount--;
1435: continue 2;
1436: }
1437:
1438: if ($isSuperTypeA->no()) {
1439: return new NeverType();
1440: }
1441: }
1442: }
1443:
1444: if ($typesCount === 1) {
1445: return $types[0];
1446: }
1447:
1448: return new IntersectionType($types);
1449: }
1450:
1451: public static function removeFalsey(Type $type): Type
1452: {
1453: return self::remove($type, StaticTypeFactory::falsey());
1454: }
1455:
1456: public static function removeTruthy(Type $type): Type
1457: {
1458: return self::remove($type, StaticTypeFactory::truthy());
1459: }
1460:
1461: }
1462: