-
Notifications
You must be signed in to change notification settings - Fork 48
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
Comments
Yes, this is actually a good idea for phpstan-strict-rules 😊 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Feature request
I sometimes see PR's in my organisation where a cast to
(int)
is used to silence a PhpStan error about cases whereint|null
is passed somewhere that only allowsint
. This has resulted in bugs where inadvertent0
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
orcasting 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!
The text was updated successfully, but these errors were encountered: