Skip to content

Commit

Permalink
cover more sprintf() cases
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored and ondrejmirtes committed Jun 18, 2022
1 parent 8801edb commit b1536e8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Expand Up @@ -45,7 +45,7 @@ public function getTypeFromFunctionCall(

if ($formatType instanceof ConstantStringType) {
// The printf format is %[argnum$][flags][width][.precision]
if (preg_match('/^%([0-9]*\$)?[0-9]*\.?[0-9]+[bdeEfFgGhHouxX]$/', $formatType->getValue(), $matches) === 1) {
if (preg_match('/^%([0-9]*\$)?[0-9]*\.?[0-9]*[bdeEfFgGhHouxX]$/', $formatType->getValue(), $matches) === 1) {
// invalid positional argument
if (array_key_exists(1, $matches) && $matches[1] === '0$') {
return null;
Expand Down
7 changes: 7 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7387.php
Expand Up @@ -25,6 +25,8 @@ public function specifiers(int $i) {
// https://3v4l.org/fmVIg
assertType('non-empty-string', sprintf('%14s', $i));

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

assertType('numeric-string', sprintf('%14b', $i));
assertType('non-empty-string', sprintf('%14c', $i)); // binary string
assertType('numeric-string', sprintf('%14d', $i));
Expand Down Expand Up @@ -65,4 +67,9 @@ public function positionalArgs($mixed, int $i, float $f, string $s) {
public function invalidPositionalArgFormat($mixed, string $s) {
assertType('string', sprintf('%0$14F', $mixed, $s));
}

public function escapedPercent(int $i) {
// https://3v4l.org/2m50L
assertType('non-empty-string', sprintf("%%d", $i));
}
}

0 comments on commit b1536e8

Please sign in to comment.