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
Sprintf with only one argument without formatting directives leads to false TooFewArguments #10021
Comments
I found these snippets: https://psalm.dev/r/6c9dd0f8b3<?php
echo sprintf('text');
|
I guess this relates to #9987 |
+1 change the error to @kkmuffme Im interested in your thoughts on this? |
Yes, makes sense to move it to In fact, in this case with sprintf it's even more likely this is a bug though, that will not be caught: This is a classic (and common) mistake, that should definitely get reported, but can only get reported when the 1st arg is a literal string - for all other cases like https://psalm.dev/r/730db01a6d it would now only report as redundant function call. I'll fix change this and also take care of #9987 (comment) (@orklah):
Could you all please let me know what you think, then I'll take care of that in the next days |
I found these snippets: https://psalm.dev/r/ac4e6664e2<?php
$a = 'text';
if ( is_string( $a ) ) {
}
https://psalm.dev/r/4f60fa8c62<?php
printf('Hi %s');
https://psalm.dev/r/730db01a6d<?php
/** @var string $greeting_text */
echo sprintf( $greeting_text );
|
Sounds great! I hadn't thought about the non-literal case at all. I wonder how worthwhile it'd be to add a new issue type for this case in general, even if there are 2 arguments or more, because it's never clear how many placeholders there really are. (e.g. a "possibly too few arguments" or "non analyzable function argument" - not sure if a similar type already exists) |
IMO we should not push our feelings/views/opinions on others, that is why I think we should let people use All other proposed changes are looking like good improvements! |
We definitely can, it's just a bad practice - it's slower than print, if you accidentally use a placeholder in it it will throw/crash (PHP 8) or not output anything (PHP 7) and makes identifying whether it should be a pattern or just a regular string harder for people who work on that codebase. Taking it into account though, I'll change printf with 1 arg to only report |
100% agree!
I would either not report an issue (since |
As a user ideally I'd want to:
|
Psalm v6 will not support PHP 7 anymore, the minimum supported version will be PHP 8.1.17. |
To expand on that, we've started work on Psalm 6 (the |
Fix vimeo#10021 Fix vimeo#9987 again now for all cases (specifically vimeo#9987 (comment)) Use RedundantFunctionCall instead of InvalidArgument, where it's technically valid. Report TooManyArguments when a format without placeholders is used Report an error for splat with vprintf/vsprintf
https://psalm.dev/r/6c9dd0f8b3
Presumably caused by
psalm/src/Psalm/Internal/Provider/ReturnTypeProvider/SprintfReturnTypeProvider.php
Lines 73 to 86 in 564c6de
It states
Which sure, is true, but that's a code style issue and not statically incorrect code.
The text was updated successfully, but these errors were encountered: