From 0cc87f37855611868d784deb3d5ec26c5058fda4 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Sat, 3 Sep 2022 10:45:06 +0200 Subject: [PATCH] Optimization: Degrade constant arrays if there's too many values recursively --- src/Type/TypeCombinator.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index 0b344f4456..7e4f8ee93c 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -597,7 +597,7 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp $keyTypesForGeneralArray = []; $valueTypesForGeneralArray = []; - $generalArrayOccurred = false; + $generalArrayOccurred = self::shouldDegradeConstantArrays($arrayTypes); $constantKeyTypesNumbered = []; /** @var int|float $nextConstantKeyTypeIndex */ @@ -644,6 +644,34 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp ); } + /** + * @param ArrayType[] $arrayTypes + */ + private static function shouldDegradeConstantArrays(array $arrayTypes): bool + { + $constantArrayValuesCount = 0; + foreach ($arrayTypes as $arrayType) { + TypeTraverser::map($arrayType, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type { + if ($type instanceof ConstantArrayType) { + $constantArrayValuesCount += count($type->getValueTypes()); + if ($constantArrayValuesCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) { + return $type; + } + + return $traverse($type); + } + + if ($type instanceof UnionType || $type instanceof IntersectionType) { + return $traverse($type); + } + + return $type; + }); + } + + return $constantArrayValuesCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT; + } + /** * @param Type[] $constantArrays * @return Type[]