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

Report PossiblyDivisionByZero #7584

Closed
VincentLanglet opened this issue Jul 6, 2022 · 6 comments
Closed

Report PossiblyDivisionByZero #7584

VincentLanglet opened this issue Jul 6, 2022 · 6 comments

Comments

@VincentLanglet
Copy link
Contributor

Feature request

It could be useful to have phpstan report a possibly division by zero error.
Maybe it require to be an option or a strict rule.

https://phpstan.org/r/43bf5d53-27c4-4684-81f3-8dddcd805472

It might be easier for int since adding a 0 check is changing the value to int<min, -1>|int<1, max>.
For float, currently, a !== 0.0 check is not narrowing the type to something like non-negative-float or positive-float|negative-float.

@rajyan
Copy link
Contributor

rajyan commented Jul 18, 2022

I think this feature seems implemented already
phpstan/phpstan-src#1280
you should enable some options to make phpstan report
https://phpstan.org/blog/bring-your-exceptions-under-control

as you mentioned, report for float is not possible yet.

@VincentLanglet
Copy link
Contributor Author

I think this feature seems implemented already phpstan/phpstan-src#1280 you should enable some options to make phpstan report https://phpstan.org/blog/bring-your-exceptions-under-control

I often enable the exceptions checks of phpstan, but always disable it for \LogicException and \Error since it's an error from the code which shouldn't be try/catch.

To me DivisionByError is in the same situation and should have his own error reported by phpstan.

In the same way, TypeError is an error but phpstan reports when you're passing a possibly wrong type to a method.
Level 7 of phpstan reports passing string|int to a method which only accepts string.

@ondrejmirtes
Copy link
Member

This is trivial to implement for integers: Report it if !(new ConstantIntegerType(0))->isSuperTypeOf($rightType)->no().

For floats it's not possible right now as we don't have FloatRangeType.

@VincentLanglet
Copy link
Contributor Author

Started in phpstan/phpstan-src#1636

@VincentLanglet
Copy link
Contributor Author

Closed because of phpstan/phpstan-src#1636 (comment)

@github-actions
Copy link

github-actions bot commented Mar 6, 2023

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 Mar 6, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants