-
Notifications
You must be signed in to change notification settings - Fork 534
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
add optional strict check for printf parameter types #4349
Conversation
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 seems a bit long and it's not completely accurate either (it accepts classes with __toString()
even if they don't implement Stringable
). The other alternative I considered was just string
, but that might be a bit confusing to users. Is there anything better?
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.
Let's try __stringandstringable
. I can see you're already using new StringAlwaysAcceptingObjectWithToStringType
in a related place.
Yes, please contribute seting thing parameter to true in phpstan-strict-rules once this is released 😊
f6ed272
into
phpstan:2.1.x
Thank you!
Please also send a docs update for phpstan.org about checkStrictPrintfPlaceholderTypes
. Thanks!
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 what sprintf
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
to sprintf
. You might want to deal with null
differently than what sprintf
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.
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.
This is an excellent point.
Another thing to consider is whether it should be allowed only in
%s
or also other placehoders?
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 🙈)
@angeleg
angeleg
Sep 25, 2025
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 would say only for
%s
.
To expand on that point: %d
+ null
make 0
, which is confusable with, well, passing an actual 0
integer. 😄 So for that reason I wouldn't allow it either.
Closes phpstan/phpstan#13496
Previous PR: #3977
Would you accept turning this on by default in phpstan-strict-rules?