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

Add rule to disallow casting potential null values

Open leongersen opened this issue 1 year ago • 0 comments

Implements #224

I've borrowed the $treatPhpDocTypesAsCertain logic from UselessCastRule.

I'm ignoring mixed type, as that would be covered by PhpStan itself on level 9.

I kept the scope small: there are many more things that could be asserted about casts, such as:

  • Casting floats to ints, this should probably use round/floor/ceil instead;
  • Objects cannot be cast to int/string/float, but they can be cast to array;
  • Many casts are nonsensical. Yes, you can (float)[2.00,2.00] and get 1.00, but should you want to?

I figured these cases would be better handled in other rule(s).

leongersen avatar Aug 03 '23 06:08 leongersen