-
Notifications
You must be signed in to change notification settings - Fork 37
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
[Implement sniff] Warn about short ternary usage in themes #241
Comments
|
Once core moves to PHP 7.0 (beginning of the next year) this shouldn't have to be an issue imo. |
But this is for themes, not core. And themes can run on different versions of core, with different versions of PHP. |
Both If I remember correctly, the "default" presumption is that themes support the current WP version + up to three versions back. That would currently mean WP |
Yes, but considering that WP doesn't check the |
Well, the |
The decision from the triage: include this sniff but change it so that it throws a warning instead of an error. This shouldn't be too hard, just extend the sniff and override the |
No need to extend the sniff, this can simply be changed from the ruleset |
Even better 🙂 |
Rule:
https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#ternary-operator
Short ternaries look like
In this case the value of
$some_result
will be$some_value
unless the$some_value
evaluates tofalse
, in that case, the value of$some_result
will be the string'default'
.Inspecting the usage in the theme directory I saw that most authors are using it wrongly - they assume that the value of the array key will be used if it exists
However, the value of
$output
will never be$input['key']
.It will either bedefault
string in case the key doesn't exist ortrue
if it does. Which is (in the majority of cases) not intended behavior.The intended behavior is (probably)
But if the
$input['key']
doesn't exist, the above will throw an undefined index notice. Most likely the operator that the author intended to use is null coalesce (??
).Decision needed:
A decision is needed whether we'd include this in the first place or no?
If we include it, it could be downgraded to warning to be a caution for theme authors. On the other hand, reviewers who see this warning could report this to an author, even if they really used this with a correct intention. That could cause confusion during the review process.
Although it would be a good way for both authors and reviewers to distinguish the difference between the short ternary (
?:
) and null coalesce operators (??
).To do:
sniffname
sniff to the ruleset.The text was updated successfully, but these errors were encountered: