Skip to content

Commit 9b5a686

Browse files
committed
Improved sprintf() inference
1 parent 09fbc92 commit 9b5a686

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ 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->isString()->yes()) {
85-
$singlePlaceholderEarlyReturn = $checkArgType;
84+
if ($matches[2] === 's' && $checkArgType->isScalar()->yes()) {
85+
$singlePlaceholderEarlyReturn = $checkArgType->toString();
8686
} elseif ($matches[2] !== 's') {
8787
$singlePlaceholderEarlyReturn = new IntersectionType([
8888
new StringType(),

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function inputTypes(int $i, float $f, string $s) {
2323

2424
public function specifiers(int $i) {
2525
// https://3v4l.org/fmVIg
26-
assertType('non-falsy-string', sprintf('%14s', $i));
26+
assertType('numeric-string', sprintf('%14s', $i));
2727

2828
assertType('numeric-string', sprintf('%d', $i));
2929

@@ -45,9 +45,15 @@ public function specifiers(int $i) {
4545

4646
}
4747

48-
public function positionalArgs($mixed, int $i, float $f, string $s) {
48+
/**
49+
* @param positive-int $posInt
50+
* @param negative-int $negInt
51+
*/
52+
public function positionalArgs($mixed, int $i, float $f, string $s, int $posInt, int $negInt) {
4953
// https://3v4l.org/vVL0c
50-
assertType('non-falsy-string', sprintf('%2$14s', $mixed, $i));
54+
assertType('numeric-string', sprintf('%2$14s', $mixed, $i));
55+
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $posInt));
56+
assertType('non-falsy-string&numeric-string', sprintf('%2$14s', $mixed, $negInt));
5157

5258
assertType('numeric-string', sprintf('%2$.14F', $mixed, $i));
5359
assertType('numeric-string', sprintf('%2$.14F', $mixed, $f));

0 commit comments

Comments
 (0)