phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Check printf parameter types

Open schlndh opened this issue 10 months ago • 0 comments

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 avatar May 02 '25 06:05 schlndh