coding-standards icon indicating copy to clipboard operation
coding-standards copied to clipboard

Add rule to avoid redundant ternaries

Open rmccue opened this issue 6 years ago • 3 comments

I've seen in a few places we have code like this:

return $foo ? true : false;

The ternary here is redundant and is actually just casting the variable to a boolean. Using an actual cast is cleaner:

return (bool) $foo;

(The reverse is true as well, return $foo ? false : true; can be replaced with return ! $foo)

rmccue avatar Oct 16 '19 14:10 rmccue

Is there a PHPCS sniff for this? @rmccue to investigate. I'm :+1: on the change proposed here

kadamwhite avatar May 19 '20 15:05 kadamwhite

Neither PHPCS nor WP have a sniff for this.

kadamwhite avatar May 19 '20 16:05 kadamwhite

Also nothing in the wider market :( Let's move to backlog; it's nice, but would require some custom work, so someday...

rmccue avatar May 19 '20 16:05 rmccue