phpstan-exception-rules icon indicating copy to clipboard operation
phpstan-exception-rules copied to clipboard

How to avoid `DivisionByZeroError`

Open patrickkusebauch opened this issue 5 years ago • 4 comments

For a code like this:

        $lin = (int) (($value - $min) / ($d === 0.0 ? 0.5 : $d));

It is impossible for the error to occur. Nonetheless, this extension will produce an error in PHPStan. Is there a way to avoid it without editing the code itself?

patrickkusebauch avatar Jan 17 '21 01:01 patrickkusebauch

It is, because $d === 0.0 ? 0.5 : $d is resolved as float, not SubtracedType<float, 0>. But this scenario must be implemented on the phpstan side. See https://github.com/phpstan/phpstan-src/pull/185

pepakriz avatar Jan 17 '21 09:01 pepakriz

Ok, so in the meantime, is there any way at all to divide by variable without throwing this error?

patrickkusebauch avatar Jan 17 '21 21:01 patrickkusebauch

@patrickkusebauch No, there's no way to ignore all cases with variable, but ignoring these cases by inline phpdoc as a temporary solution looks OK (https://phpstan.org/user-guide/ignoring-errors#ignoring-in-code-using-phpdocs).

pepakriz avatar Jan 19 '21 09:01 pepakriz

It would be good if we could avoid this error by typing a variable as @var positive-int.

ndench avatar Oct 20 '23 02:10 ndench