diff --git a/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/negate.php.inc b/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/negate.php.inc index 4530137e010..aebefd0ffc2 100644 --- a/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/negate.php.inc +++ b/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/negate.php.inc @@ -7,7 +7,6 @@ final class Negate public function run($value, array $items) { $isMatch = in_array($value, $items, TRUE) !== TRUE; - $isMatch = in_array($value, $items, TRUE) === FALSE; $isMatch = true !== in_array($value, $items, TRUE); } @@ -24,7 +23,6 @@ final class Negate public function run($value, array $items) { $isMatch = !in_array($value, $items, TRUE); - $isMatch = !in_array($value, $items, TRUE); $isMatch = !in_array($value, $items, TRUE); } diff --git a/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/skip_function_equals_false.php.inc b/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/skip_function_equals_false.php.inc new file mode 100644 index 00000000000..34ab2112126 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/skip_function_equals_false.php.inc @@ -0,0 +1,11 @@ +valueResolver->isTrueOrFalse($node->left) && $this->getType($node->left)->isBoolean()->yes()) { + if ($this->isBooleanButNotTrueAndFalse($node->left)) { return $this->processBoolTypeToNotBool($node, $node->left, $node->right); } - if ($this->valueResolver->isTrueOrFalse($node->right)) { - return null; - } - - if (! $this->getType($node->right)->isBoolean()->yes()) { - return null; + if ($this->isBooleanButNotTrueAndFalse($node->right)) { + return $this->processBoolTypeToNotBool($node, $node->right, $node->left); } - return $this->processBoolTypeToNotBool($node, $node->right, $node->left); + return null; } private function processBoolTypeToNotBool(Node $node, Expr $leftExpr, Expr $rightExpr): ?Expr @@ -97,22 +94,14 @@ private function refactorIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr if ($this->valueResolver->isTrue($rightExpr)) { return $leftExpr; } - - if ($this->valueResolver->isFalse($rightExpr)) { - // prevent !! - if ($leftExpr instanceof BooleanNot) { - return $leftExpr->expr; - } - - // keep as it is, readable enough - if ($leftExpr instanceof Variable && $this->getType($leftExpr)->isBoolean()->yes()) { - return null; - } - - return new BooleanNot($leftExpr); + // prevent double negation !! + if (!$this->valueResolver->isFalse($rightExpr)) { + return null; } - - return null; + if (!$leftExpr instanceof BooleanNot) { + return null; + } + return $leftExpr->expr; } private function refactorNotIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr @@ -127,4 +116,15 @@ private function refactorNotIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr return null; } + + private function isBooleanButNotTrueAndFalse(Expr $expr): bool + { + if ($this->valueResolver->isTrueOrFalse($expr)) { + return false; + } + + return $this->getType($expr) + ->isBoolean() + ->yes(); + } }