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