Fix phpstan/phpstan#13609: "sprintf is expected to be int by placeholder" and numeric-string#5344
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
…older in strict mode - Updated PrintfPlaceholder to accept numeric-string for %d in strict mode, matching existing %f behavior - Added regression test in tests/PHPStan/Rules/Functions/data/bug-13609.php - Updated existing strict test expectations to reflect that numeric-string literals like '1.23' are now accepted for %d
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
When using strict printf placeholder type checking, passing a
numeric-stringto a%dplaceholder reported a false positive error. The%fplaceholder already acceptednumeric-stringin strict mode, but%donly acceptedint. This fix makes%dconsistent with%fby also acceptingnumeric-string.Changes
src/Rules/Functions/PrintfPlaceholder.phpto acceptnumeric-string(viaAccessoryNumericStringType) for theintaccepting type in strict modetests/PHPStan/Rules/Functions/data/bug-13609.phpwithnumeric-stringparameters passed to%dand%fplaceholderstestBug13609andtestBug13609Strictintests/PHPStan/Rules/Functions/PrintfParameterTypeRuleTest.php'1.23'literal (anumeric-string) from expected strict errorsRoot cause
In
PrintfPlaceholder::doesArgumentTypeMatchPlaceholder(), the strict mode check for'int'only acceptedIntegerType. The'float'case already had a union allowingnumeric-string, but this was not applied to'int'. The fix adds the samenumeric-stringallowance to the'int'case.Test
The regression test passes
numeric-stringtyped parameters to both%dand%fsprintf placeholders and expects no errors in both strict and non-strict modes.Fixes phpstan/phpstan#13609