Skip to content

forbidIncrementDecrementOnNonInteger: support BcMath\Number - #351

Closed
mpesari wants to merge 1 commit into
shipmonk-rnd:masterfrom
mpesari:master
Closed

mpesari wants to merge 1 commit into
shipmonk-rnd:masterfrom
mpesari:master

Conversation

@mpesari

@mpesari mpesari commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

This adds support for BcMath\Number increment/decrement operators (on PHP >= 8.4)

php > $x = new BcMath\Number('3.14');
php > print $x;
3.14
php > $x++;
php > print $x;
4.14

As of phpstan 2.1.40, this is supported: https://phpstan.org/r/d2f000b7-4b86-45d4-9aba-7f0b302d7db5

However, these errors are emitted:

Using -- over non-integer (BcMath\Number)                               
         🪪  shipmonk.incrementDecrementOnNonInteger
etc...

I just added the required checks to ForbidIncrementDecrementOnNonIntegerRule, but...

  • The rule name is weird after the changes. Would you prefer one of these instead:
    • Rename rule to something like ForbidIncrementDecrementOnUnsafeTypesRule?
    • Add parameter allowBcMathNumber?
  • Would be nice to include tests for PHP < 8.4 too
    • What's the preferred way to achieve this? The simple way would be to use $this->analyse($files, $errors)

@janedbal

Copy link
Copy Markdown
Member

Technically, even float++ is safe and somewhat valid, but we designed this rule to truely allow only integers in ++ and -- because we think that doing that over floats is mostly unintentional. You can always do + 1 for those. And since BcMath\Number is mainly designed to replace floats, I'd treat those the same way here.

@mpesari

mpesari commented Feb 24, 2026

Copy link
Copy Markdown
Contributor Author

I understand. This extension produces the error:

Using ++ over non-integer (BcMath\Number)

There was some confusion due to phpstan-strict-rules' error:

Only numeric types are allowed in post-increment, BcMath\Number given.

@mpesari mpesari closed this Feb 24, 2026
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