Skip to content

Commit 0cc87f3

Browse files
committed
Optimization: Degrade constant arrays if there's too many values recursively
1 parent 84852ab commit 0cc87f3

1 file changed

Lines changed: 29 additions & 1 deletion

File tree

src/Type/TypeCombinator.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp
597597

598598
$keyTypesForGeneralArray = [];
599599
$valueTypesForGeneralArray = [];
600-
$generalArrayOccurred = false;
600+
$generalArrayOccurred = self::shouldDegradeConstantArrays($arrayTypes);
601601
$constantKeyTypesNumbered = [];
602602

603603
/** @var int|float $nextConstantKeyTypeIndex */
@@ -644,6 +644,34 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp
644644
);
645645
}
646646

647+
/**
648+
* @param ArrayType[] $arrayTypes
649+
*/
650+
private static function shouldDegradeConstantArrays(array $arrayTypes): bool
651+
{
652+
$constantArrayValuesCount = 0;
653+
foreach ($arrayTypes as $arrayType) {
654+
TypeTraverser::map($arrayType, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type {
655+
if ($type instanceof ConstantArrayType) {
656+
$constantArrayValuesCount += count($type->getValueTypes());
657+
if ($constantArrayValuesCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) {
658+
return $type;
659+
}
660+
661+
return $traverse($type);
662+
}
663+
664+
if ($type instanceof UnionType || $type instanceof IntersectionType) {
665+
return $traverse($type);
666+
}
667+
668+
return $type;
669+
});
670+
}
671+
672+
return $constantArrayValuesCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT;
673+
}
674+
647675
/**
648676
* @param Type[] $constantArrays
649677
* @return Type[]

0 commit comments

Comments
 (0)