Skip to content

Commit 51cffd4

Browse files
committed
SprintfFunctionDynamicReturnTypeExtension - limit combinatorial explosion
1 parent aeadbe2 commit 51cffd4

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,20 @@ public function getTypeFromFunctionCall(
8585
}
8686

8787
$values = [];
88+
$combinationsCount = 1;
8889
foreach ($args as $arg) {
8990
$argType = $scope->getType($arg->value);
9091
if (count($argType->getConstantScalarValues()) === 0) {
9192
return $returnType;
9293
}
9394

94-
$values[] = $argType->getConstantScalarValues();
95+
$constantScalarValues = $argType->getConstantScalarValues();
96+
$values[] = $constantScalarValues;
97+
$combinationsCount *= count($constantScalarValues);
98+
}
99+
100+
if ($combinationsCount > InitializerExprTypeResolver::CALCULATE_SCALARS_LIMIT) {
101+
return $returnType;
95102
}
96103

97104
$combinations = CombinationsHelper::combinations($values);

tests/PHPStan/Analyser/AnalyserIntegrationTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,6 +1294,12 @@ public function testBug10509(): void
12941294
$this->assertSame('PHPDoc tag @return contains unresolvable type.', $errors[1]->getMessage());
12951295
}
12961296

1297+
public function testBug10538(): void
1298+
{
1299+
$errors = $this->runAnalyse(__DIR__ . '/data/bug-10538.php');
1300+
$this->assertNoErrors($errors);
1301+
}
1302+
12971303
/**
12981304
* @param string[]|null $allAnalysedFiles
12991305
* @return Error[]

0 commit comments

Comments
 (0)