From 6d88882f98b285b6a83437d8325d2609b8669bb3 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 7 Sep 2022 17:25:00 +0200 Subject: [PATCH] Extract countConstantArrayValueTypes --- src/Type/TypeCombinator.php | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/Type/TypeCombinator.php b/src/Type/TypeCombinator.php index f36d061567..75f3e17b74 100644 --- a/src/Type/TypeCombinator.php +++ b/src/Type/TypeCombinator.php @@ -651,20 +651,7 @@ private static function processArrayTypes(array $arrayTypes, array $accessoryTyp */ private static function optimizeConstantArrays(array $types): array { - $constantArrayValuesCount = 0; - foreach ($types as $type) { - if ($type instanceof ConstantArrayType) { - $constantArrayValuesCount += count($type->getValueTypes()); - } - - TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type { - if ($type instanceof ConstantArrayType) { - $constantArrayValuesCount += count($type->getValueTypes()); - } - - return $traverse($type); - }); - } + $constantArrayValuesCount = self::countConstantArrayValueTypes($types); if ($constantArrayValuesCount > ConstantArrayTypeBuilder::ARRAY_COUNT_LIMIT) { $results = []; @@ -684,6 +671,28 @@ private static function optimizeConstantArrays(array $types): array return $types; } + /** + * @param Type[] $types + */ + private static function countConstantArrayValueTypes(array $types): int + { + $constantArrayValuesCount = 0; + foreach ($types as $type) { + if ($type instanceof ConstantArrayType) { + $constantArrayValuesCount += count($type->getValueTypes()); + } + + TypeTraverser::map($type, static function (Type $type, callable $traverse) use (&$constantArrayValuesCount): Type { + if ($type instanceof ConstantArrayType) { + $constantArrayValuesCount += count($type->getValueTypes()); + } + + return $traverse($type); + }); + } + return $constantArrayValuesCount; + } + /** * @param Type[] $constantArrays * @return Type[]