Skip to content

False positive on "Division per 0 error" when dividing by integers incremented by booleans #8827

@niconoe-

Description

@niconoe-

The bug I encounter is a little bit hard to explain so this snippet is better than a long text: https://phpstan.org/r/13f4b1df-ce73-4db0-849c-ec6369ac75d9

To me, there are 2 issues, but one is linked to the other.

The first issue is the visible one, that PHPStan yells that a division per 0 may occur while it's not possible thanks to the ternary check.
The second issue is hidden in the fact that doing $int += bool is wrongly interpreted as $int to be 0|1 while it's actually int (type can be narrowed depending on the context).

For instance, if I try with my boolean checks type-casted to (int), there are no more errors: https://phpstan.org/r/ac373a15-f927-43b3-bd3c-101cffb49599

The point is I don't want to add this cast because it shouldn't be mandatory here. PHP Infection could create any mutant that removes the cast and my unit-tests will still pass, without any way for me to kill that mutant and to keep the cast.

I think that solving the wrong interpretation of $int += bool could change the dumpType of it, and it will fix the visible error reported by PHPStan in my first example.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions