Skip to content

Commit

Permalink
Extract countConstantArrayValueTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 7, 2022
1 parent ab7760e commit 6d88882
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -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[]
Expand Down

0 comments on commit 6d88882

Please sign in to comment.