Skip to content

Commit

Permalink
Optimization: Degrade constant arrays if there's too many values recu…
Browse files Browse the repository at this point in the history
…rsively
  • Loading branch information
ondrejmirtes committed Sep 3, 2022
1 parent 84852ab commit 0cc87f3
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Type/TypeCombinator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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[]
Expand Down

0 comments on commit 0cc87f3

Please sign in to comment.