humanmade / humanmade/coding-standards

Add rule to avoid redundant ternaries

Open
#154 3 comments 4 reactions 1 assignee Claimed by @tfrommen View on GitHub
phpcs
Dominant language
PHP
Stars
160
Forks
17
PR merge metrics
No merged PRs in 30d

Description

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

```php
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:

```php
return (bool) $foo;
```

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.