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

Disallow casting potential nulls to int/string/array #224

Open
leongersen opened this issue Aug 2, 2023 · 1 comment
Open

Disallow casting potential nulls to int/string/array #224

leongersen opened this issue Aug 2, 2023 · 1 comment

Comments

@leongersen
Copy link

Feature request

I sometimes see PR's in my organisation where a cast to (int) is used to silence a PhpStan error about cases where int|null is passed somewhere that only allows int. This has resulted in bugs where inadvertent 0 values end up getting passed.

I've written a simple custom rule for Expr\Cast to check that the type of the cast expression is not "maybe a supertype of null". This results in helpful errors that can be used to improve some iffy code.

I'd say this rule would be a good addition to either one of the higher levels, or to phpstan-strict-rules. Would you be interested in such a rule?

The messaging could be something along the lines of casting should not be used to narrow types or casting to (int) should not be used to remove null from int|null.

Did PHPStan help you today? Did it make you happy in any way?

Yes! I've also found the processes used to build PhpStan hugely inspirational in improving my own engineering practises, so thank you!

@ondrejmirtes
Copy link
Member

Yes, this is actually a good idea for phpstan-strict-rules 😊

@ondrejmirtes ondrejmirtes transferred this issue from phpstan/phpstan Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants