-
Notifications
You must be signed in to change notification settings - Fork 54
POC - Allow short ternary for false #287
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
Conversation
I'm not too keen on allowing this. For the same reason I don't want to allow empty() even when it's safe in some situations (like on arrays and when the variable always exists).
Seeing $a ?: $b or empty($a) in code always triggers a red alert in my head. I'm really careful and I know loose comparisons are problematic in PHP. I don't ever want to see $a ?: $b because developers should always pause and be on alert when they see a construct like that. So seeing $a ?: $b instead of $a !== false ? $a : $b actually consumes more brain cycles and I like to avoid that in my code.
I believe it should be possible to write a https://phpstan.org/developing-extensions/ignore-error-extensions for people who'd want to keep this in their code in specific situations.
I believe it should be possible to write a https://phpstan.org/developing-extensions/ignore-error-extensions for people who'd want to keep this in their code in specific situations.
@VincentLanglet If you decide to write such ignore extension, please do share it here as well 😁 Would be good to give it a try in our project too.
Uh oh!
There was an error while loading. Please reload this page.
Hi @ondrejmirtes
I recently encountered situation where short ternary could be considered useful, for instance when using
Datetime::createfromformat. https://www.php.net/manual/en/datetime.createfromformat.phpSince the return type is
object|falsethere is no risk writingand this is way shorter than
But in other cases I do like to forbid short ternary because things like
is risky, I might have forgot that
0will end in the default value.So if you're interested I see two solutions:
nonFalsey|falseconditions (and maybe updating the error message behind the bleeding edge tag)nonFalsey|falseconditions (maybe you'll have a naming suggestion for this one ?), and when the option is enabled I think the error message should be changed too. (like "Short ternary is not allowed on non boolean falsey type. Use null coalesce operator if applicable or consider using long ternary.")This is currently a POC which should be updated based on your preferences. :)
Looking at how sometimes some rules are loosen up (like in 8afd4af), I think it could be ok to relax the forbid ternary rule without an option. But your call.
This would also close #268 (Should I assume that since you didn't close the issue, you're open to some suggestion ?)