Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Conversation

Copy link
Contributor

@schlndh schlndh commented Sep 19, 2025

Closes phpstan/phpstan#13496
Previous PR: #3977

Would you accept turning this on by default in phpstan-strict-rules?

angeleg reacted with heart emoji
'strict-int' => 'int',
'int' => 'int',
'float' => 'float',
'string' => 'int|float|string|Stringable',
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 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?

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 19, 2025

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.

schlndh reacted with thumbs up emoji
Copy link
Member

Yes, please contribute seting thing parameter to true in phpstan-strict-rules once this is released 😊

@ondrejmirtes ondrejmirtes merged commit f6ed272 into phpstan:2.1.x Sep 19, 2025
455 of 457 checks passed
Copy link
Member

Thank you!

schlndh reacted with thumbs up emoji

@schlndh schlndh deleted the feature-strictPrintfPlaceholderTypes branch September 20, 2025 05:39
Copy link
Member

Please also send a docs update for phpstan.org about checkStrictPrintfPlaceholderTypes. Thanks!

45,
],
[
'Parameter #2 of function printf is expected to be __stringandstringable by placeholder #1 ("%s"), null given.',
Copy link
Contributor

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?

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 25, 2025

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.

derrabus reacted with thumbs up emoji
Copy link
Contributor Author

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?

Copy link
Contributor

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.

Copy link
Member

@ondrejmirtes ondrejmirtes Sep 25, 2025

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.

derrabus reacted with thumbs up emoji
Copy link
Contributor

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 🙈)

Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@ondrejmirtes ondrejmirtes ondrejmirtes left review comments

+2 more reviewers

@ruudk ruudk ruudk left review comments

@angeleg angeleg angeleg left review comments

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

Validate sprintf argument types against format specifiers

AltStyle によって変換されたページ (->オリジナル) /