Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support positional arguments in sprintf() constant format inference #1437

Merged
merged 2 commits into from Jun 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php
Expand Up @@ -15,6 +15,7 @@
use PHPStan\Type\StringType;
use PHPStan\Type\Type;
use Throwable;
use function array_key_exists;
use function array_shift;
use function count;
use function is_string;
Expand All @@ -33,7 +34,7 @@ public function getTypeFromFunctionCall(
FunctionReflection $functionReflection,
FuncCall $functionCall,
Scope $scope,
): Type
): ?Type
{
$args = $functionCall->getArgs();
if (count($args) === 0) {
Expand All @@ -43,7 +44,13 @@ public function getTypeFromFunctionCall(
$formatType = $scope->getType($args[0]->value);

if ($formatType instanceof ConstantStringType) {
if (preg_match('/^%[0-9]*\.?[0-9]+[bdeEfFgGhHouxX]$/', $formatType->getValue()) === 1) {
// The printf format is %[argnum$][flags][width][.precision]
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$') {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this array-key-exists check now prevents the warning we saw earlier:

Warning: Undefined array key 1 in /Users/ondrej/Development/phpstan/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php on line 49
PHP Warning:  Undefined array key 1 in /Users/ondrej/Development/phpstan/src/Type/Php/SprintfFunctionDynamicReturnTypeExtension.php on line 49

return null;
}

return new IntersectionType([
new StringType(),
new AccessoryNumericStringType(),
Expand Down
23 changes: 23 additions & 0 deletions tests/PHPStan/Analyser/data/bug-7387.php
Expand Up @@ -42,4 +42,27 @@ public function specifiers(int $i) {
assertType('numeric-string', sprintf('%14X', $i));

}

public function positionalArgs($mixed, int $i, float $f, string $s) {
// https://3v4l.org/vVL0c
assertType('non-empty-string', sprintf('%2$14s', $mixed, $i));

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

assertType('numeric-string', sprintf('%2$1.14F', $mixed, $i));
assertType('numeric-string', sprintf('%2$2.14F', $mixed, $f));
assertType('numeric-string', sprintf('%2$3.14F', $mixed, $s));

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

assertType('numeric-string', sprintf('%10$14F', $mixed, $s));
}

public function invalidPositionalArgFormat($mixed, string $s) {
assertType('string', sprintf('%0$14F', $mixed, $s));
}
}