Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
fix combinations count
  • Loading branch information
staabm committed Jan 5, 2025
commit f161750f3e7c1867234de02126839fe9cc1766ed
9 changes: 7 additions & 2 deletions src/Type/Php/ImplodeFunctionReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use function count;
use function implode;
use function in_array;
use const COUNT_RECURSIVE;

final class ImplodeFunctionReturnTypeExtension implements DynamicFunctionReturnTypeExtension
{
Expand Down Expand Up @@ -116,15 +115,17 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT
$valueTypes = $array->getValueTypes();

$arrayValues = [];
$combinationsCount = 1;
foreach ($valueTypes as $valueType) {
$constScalars = $valueType->getConstantScalarValues();
if (count($constScalars) === 0) {
return null;
}
$arrayValues[] = $constScalars;
$combinationsCount *= count($constScalars);
}

if (count($strings) + count($arrayValues, COUNT_RECURSIVE) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
return null;
}

Expand All @@ -134,6 +135,10 @@ private function inferConstantType(ConstantArrayType $arrayType, ConstantStringT
}
}

if (count($strings) > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally you'd count the combinations before generating them, it can explode as well.

return null;
}

return TypeCombinator::union(...$strings);
}

Expand Down
Loading