Coding-standard: UselessConditionWithReturnSniff autofixer may result in wrong return type

Created on 27 Sep 2018  路  5Comments  路  Source: slevomat/coding-standard

Following code:

public function exists(): bool
{
        $order = $someQueryBuilder->getQuery()->getOneOrNullResult();
        return $order ? true : false;
}

will be "fixed" into:

public function exists(): bool
{
        $order = $someQueryBuilder->getQuery()->getOneOrNullResult();
        return $order;
}

...which actually breaks the code. This sniff now relies on that the condition is boolean, which may not be true. It should fix only those, where it is boolean for sure (e.g. $order !== null comparison is used).

Wontfix

Most helpful comment

Yes it is. You just fix only those where there is a comparison or boolean operator in condition.

  • return $order ? true : false; - not fixable
  • return $order->isValid() ? true : false; - not fixable
  • return $order === null ? true : false; - fixable
  • return $order->getPrice() > 0 ? true : false; - fixable
  • return $order || $oldOrder ? true : false; - fixable

All 5 comments

It鈥檚 not possible with PHPCS.

Yes it is. You just fix only those where there is a comparison or boolean operator in condition.

  • return $order ? true : false; - not fixable
  • return $order->isValid() ? true : false; - not fixable
  • return $order === null ? true : false; - fixable
  • return $order->getPrice() > 0 ? true : false; - fixable
  • return $order || $oldOrder ? true : false; - fixable

I want the second example to be fixable.

Then it should be in readme that it may break code and it is implemented that way intentionally.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dereuromark picture dereuromark  路  3Comments

carusogabriel picture carusogabriel  路  4Comments

xthiago picture xthiago  路  4Comments

alcaeus picture alcaeus  路  6Comments

donatj picture donatj  路  3Comments