Error Identifier: argument.printf
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
$name = 'world';
echo sprintf('%s is %d years old', $name);
Why is it reported? #
The number of format placeholders in the printf/sprintf format string does not match the number of values passed. PHP’s sprintf and printf functions require exactly as many value arguments as there are placeholders in the format string. A mismatch will result in missing values or ignored extra arguments at runtime.
In the example above, the format string '%s is %d years old' contains 2 placeholders (%s and %d), but only 1 value ($name) is provided.
How to fix it #
Pass the correct number of arguments to match the placeholders:
<?php declare(strict_types = 1);
$name = 'world';
-echo sprintf('%s is %d years old', $name);
+echo sprintf('%s is %d years old', $name, 25);
Or adjust the format string if fewer values are intended:
<?php declare(strict_types = 1);
$name = 'world';
-echo sprintf('%s is %d years old', $name);
+echo sprintf('%s says hello', $name);
How to ignore this error #
You can use the identifier argument.printf to ignore this error using a comment:
// @phpstan-ignore argument.printf
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: argument.printf