Skip to content

Commit

Permalink
SprintfFunctionDynamicReturnTypeExtension - limit combinatorial explo…
Browse files Browse the repository at this point in the history
…sion
  • Loading branch information
ondrejmirtes committed Feb 5, 2024
1 parent aeadbe2 commit 51cffd4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,20 @@ public function getTypeFromFunctionCall(
}

$values = [];
$combinationsCount = 1;
foreach ($args as $arg) {
$argType = $scope->getType($arg->value);
if (count($argType->getConstantScalarValues()) === 0) {
return $returnType;
}

$values[] = $argType->getConstantScalarValues();
$constantScalarValues = $argType->getConstantScalarValues();
$values[] = $constantScalarValues;
$combinationsCount *= count($constantScalarValues);
}

if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
return $returnType;
}

$combinations = CombinationsHelper::combinations($values);
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1294,6 +1294,12 @@ public function testBug10509(): void
$this->assertSame('PHPDoc tag @return contains unresolvable type.', $errors[1]->getMessage());
}

public function testBug10538(): void
{
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10538.php');
$this->assertNoErrors($errors);
}

/**
* @param string[]|null $allAnalysedFiles
* @return Error[]
Expand Down
Loading

0 comments on commit 51cffd4

Please sign in to comment.