phpstan-src icon indicating copy to clipboard operation
phpstan-src copied to clipboard

Add PossiblyDivisionByZero rule

Open VincentLanglet opened this issue 3 years ago • 1 comments

First try of the implementation of PossiblyDivisionByZero

The config declaration is missing. At which level should I add this @ondrejmirtes ? (with a conditional tag ?)

VincentLanglet avatar Aug 20 '22 21:08 VincentLanglet

Friendly ping @ondrejmirtes.

To finish this PR I just need to know to which level I should add this rule ? Should it be under bleeding edge ?

VincentLanglet avatar Oct 22 '22 21:10 VincentLanglet

Hi, let's try it on level 7 to see how annoying it'd be. It's correct but I worry too annoying in practice.

I want to see it without bleedingEdge first, to test it on real projects in CI.

ondrejmirtes avatar Oct 23 '22 06:10 ondrejmirtes

Also - base it on 1.9.x.

ondrejmirtes avatar Oct 23 '22 06:10 ondrejmirtes

As a followup it might be usefull to cover modulo division by zero?

staabm avatar Oct 23 '22 11:10 staabm

As a followup it might be usefull to cover modulo division by zero?

Nice idea. I've added the support. I just need now to understand what is wrong with my config.

VincentLanglet avatar Oct 23 '22 11:10 VincentLanglet

The PR is now ready :)

VincentLanglet avatar Oct 23 '22 12:10 VincentLanglet

The pipeline results show that although correct, this would really be annoying in practice...

ondrejmirtes avatar Nov 05 '22 12:11 ondrejmirtes

The pipeline results show that although correct, this would really be annoying in practice...

For every projects tested where there is failure about possibly division by zero there is not a lot and adding a check wouldn't hurt. But I understand the "annoying" thing.

Should it be an option then ?

VincentLanglet avatar Nov 05 '22 13:11 VincentLanglet

I feel like PHPStan doesn't have to contain every custom rule idea under an option. I encourage you to publish and market your own package for this problem. Similar to how https://github.com/Roave/no-floaters is done.

ondrejmirtes avatar Nov 05 '22 13:11 ondrejmirtes

I feel like PHPStan doesn't have to contain every custom rule idea under an option. I encourage you to publish and market your own package for this problem. Similar to how https://github.com/Roave/no-floaters is done.

I just had a new thought about this rule and start to think this is not needed, and maybe should not exists.

The following code could be considered as correct

/**
 * @throws DivisionByZeroError
 */
public function compute(int $int): float {
     return 42 / $int;
}

And PHPStan is already correctly reporting the DivisionByZeroError when using the exception features.

I totally forgot about this because I had

exceptions:
        uncheckedExceptionClasses:
                - Error

I guess I cannot modify the config to say "I'd like to ignore all the Error, except DivisionByZeroError". ^^'

VincentLanglet avatar Nov 05 '22 22:11 VincentLanglet