Skip to content

Commit 8b93038

Browse files
committed
fix
1 parent 9b5a686 commit 8b93038

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ public function getTypeFromFunctionCall(
8181
// of stringy type, then the return value will be of the same type
8282
$checkArgType = $scope->getType($args[$checkArg]->value);
8383

84-
if ($matches[2] === 's' && $checkArgType->isScalar()->yes()) {
84+
if ($matches[2] === 's'
85+
&& $checkArgType->isScalar()->yes()
86+
&& $checkArgType->getFiniteTypes() === []
87+
) {
8588
$singlePlaceholderEarlyReturn = $checkArgType->toString();
8689
} elseif ($matches[2] !== 's') {
8790
$singlePlaceholderEarlyReturn = new IntersectionType([

tests/PHPStan/Analyser/nsrt/bug-11201.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function returnsJustString(): string
2727
return rand(0,1) === 1 ? 'foo' : '';
2828
}
2929

30+
function returnsBool(): bool {
31+
return true;
32+
}
33+
3034
$s = sprintf("%s", returnsNonEmptyString());
3135
assertType('non-empty-string', $s);
3236

@@ -41,3 +45,12 @@ function returnsJustString(): string
4145

4246
$s = sprintf('%2$s', 1234, returnsNonFalsyString());
4347
assertType('non-falsy-string', $s);
48+
49+
$s = sprintf('%20s', 'abc');
50+
assertType("' abc'", $s);
51+
52+
$s = sprintf('%20s', true);
53+
assertType("' 1'", $s);
54+
55+
$s = sprintf('%20s', returnsBool());
56+
assertType("non-falsy-string", $s);

0 commit comments

Comments
 (0)