Skip to content

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
phpstan-bot:create-pull-request/patch-hh57qkz
Closed

Fix phpstan/phpstan#13609: "sprintf is expected to be int by placeholder" and numeric-string#5344
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-hh57qkz

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When using strict printf placeholder type checking, passing a numeric-string to a %d placeholder reported a false positive error. The %f placeholder already accepted numeric-string in strict mode, but %d only accepted int. This fix makes %d consistent with %f by also accepting numeric-string.

Changes

  • Modified src/Rules/Functions/PrintfPlaceholder.php to accept numeric-string (via AccessoryNumericStringType) for the int accepting type in strict mode
  • Added regression test in tests/PHPStan/Rules/Functions/data/bug-13609.php with numeric-string parameters passed to %d and %f placeholders
  • Added test methods testBug13609 and testBug13609Strict in tests/PHPStan/Rules/Functions/PrintfParameterTypeRuleTest.php
  • Removed now-accepted '1.23' literal (a numeric-string) from expected strict errors

Root cause

In PrintfPlaceholder::doesArgumentTypeMatchPlaceholder(), the strict mode check for 'int' only accepted IntegerType. The 'float' case already had a union allowing numeric-string, but this was not applied to 'int'. The fix adds the same numeric-string allowance to the 'int' case.

Test

The regression test passes numeric-string typed parameters to both %d and %f sprintf placeholders and expects no errors in both strict and non-strict modes.

Fixes phpstan/phpstan#13609

…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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants