Skip to content
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

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

Closed
niconoe- opened this issue Feb 2, 2023 · 4 comments · Fixed by phpstan/phpstan-src#2209
Labels
Milestone

Comments

@niconoe-
Copy link
Contributor

niconoe- commented Feb 2, 2023

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.

@VincentLanglet
Copy link
Contributor

VincentLanglet commented Feb 2, 2023

This seems similar to #8803
I added the bugfix to my PR phpstan/phpstan-src#2209

@phpstan-bot
Copy link
Contributor

@niconoe- After the latest push in 1.10.x, PHPStan now reports different result with your code snippet:

@@ @@
+ 9: Only numeric types are allowed in +, bool given on the right side.
+10: Only numeric types are allowed in +, bool given on the right side.
 13: Dumped type: 0|1
 14: Dumped type: 0|1
 17: Binary operation "/" between 0|1 and 0|1|2 results in an error.
Full report
Line Error
9 Only numeric types are allowed in +, bool given on the right side.
10 Only numeric types are allowed in +, bool given on the right side.
13 `Dumped type: 0
14 `Dumped type: 0
17 `Binary operation "/" between 0

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2209

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants