-
Notifications
You must be signed in to change notification settings - Fork 540
add optional strict check for printf parameter types #4349
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
Merged
ondrejmirtes
merged 2 commits into
phpstan:2.1.x
from
schlndh:feature-strictPrintfPlaceholderTypes
Sep 19, 2025
+182
−18
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schlndh @ondrejmirtes After enabling this rule I noticed a lot of errors related to
string|null
types.Personally, I think replacing those
sprintf
call arguments with$value ?? ''
feels like making a computer happy. As it does exactly the same as whatsprintf
does for%s
.See https://3v4l.org/TlHU9#v8.4.13
I know this is a strict rule, but wonder if this was really intentional or something that should be reconsidered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feel like it's useful that PHPStan lets you know you're potentially passing
null
tosprintf
. You might want to deal withnull
differently than whatsprintf
does.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I intentionally implemented it like that to match my understanding of "strict". However, I encountered the same thing: hundreds of issues like this, most of which I don't care about (though I'm fine with letting them all fall into the baseline). So I would be open to changing it. I'm just not sure what's the best way to go about it.
Strict-rules do allow
$null . $string
and"{$null}"
, so I guess this wouldn't have to be as strict either (unless it's allowed by omission rather than intentionally). At the very least it's a bit inconsistent.On the other hand, there are third-party rules (e.g. shipmonk rules) which do prohibit it, so clearly there is also interest in the fully strict version.
Another thing to consider is whether it should be allowed only in
%s
or also other placehoders?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an excellent point.
I would say only for
%s
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An idea: string|null should be reported only on level 8+, basicallh RuleLevelHelper should be utilized.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately, that won't help me (level 9 🙈)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To expand on that point:
%d
+null
make0
, which is confusable with, well, passing an actual0
integer. 😄 So for that reason I wouldn't allow it either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ondrejmirtes Re: level 8
IMO that's stretching it a little bit. And it wouldn't be consistent with how string + null is handled in other cases (concatenation, interpolation, ...).
Let's consider consistency with phpstan-strict-rules (playground):
bool
andnull
in numerical operations (consistent with%d
and%f
).numeric-string
in numerical operations (inconsistent with%f
,%d
is probably OK since float is excluded as well).%d
and%f
).bool
and null in string operations (inconsistent with%s
).Idea:
null
to%s
. If PHPStan later adds a config option to limit type coercions then it could be used here (as well as$string . $null
,"{$null}"
, ...).numeric-string
to%f
. I excluded it, becausenumeric-string
could lose precision by doing so. But it's probably too strict.bool
to%s
prohibited. I could later try to prohibitbool
in$string . $bool
and"{$bool}"
in strict-rules (no promises).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a small side note, PHPStorm will tell me to remove
(string) $null
for%s
in asprintf
. It's only fine with$null ?? ''
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't know if that helps, but I've reviewed the new errors that popped up in my current project. The dominant error reported was that we pass something that could be null. And in most of the cases, it would be a problem is the value was actually
null
. It was also old news because we already knew that the code in question is a bit too lax when it comes to handlingnull
. So all errors that the new rule added to our baseline were actually just "more of the same".Yes, it is. In fact, you actually might lose precision when casing a numeric string to a float. And besides it doesn't make much sense to cast a string to a float just to format it as a string again.