Skip to content

Check printf parameter types #3977

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

Open
wants to merge 5 commits into
base: 2.1.x
Choose a base branch
from

Conversation

schlndh
Copy link
Contributor

@schlndh schlndh commented May 2, 2025

Motivation: I refactored code to use BcMath\Number instead of float, and I didn't notice a printf usage with %f, which lead to incorrect result. PHPStan didn't complain, because BcMath\Number has __toString().

Here is an executed version of the test file: https://3v4l.org/3pXMc

I left some things for possible future PRs to keep things simple:

  • Strict mode: enforce int for %d, int|float for %f, __stringandstringable for %s. This may be too strict for default PHPStan, but IMO it would be a good candidate for phpstan-strict-rules.
  • Handle multiple possibilities for format string.
  • Add support for vprintf/vsprintf.

@schlndh schlndh force-pushed the feature-checkPrintfParameterTypes branch from 8866323 to 9a4e490 Compare May 2, 2025 06:53
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.

1 participant