Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;

final class SkipFunctionEqualsFalse
{
public function run($value, array $items)
{
$isMatch = in_array($value, $items, TRUE) === FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use PhpParser\Node\Expr\Variable;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -31,6 +30,7 @@ class SomeClass
public function run(bool $value, string $items)
{
$match = in_array($value, $items, TRUE) === TRUE;

$match = in_array($value, $items, TRUE) !== FALSE;
}
}
Expand All @@ -42,6 +42,7 @@ class SomeClass
public function run(bool $value, string $items)
{
$match = in_array($value, $items, TRUE);

$match = in_array($value, $items, TRUE);
}
}
Expand All @@ -64,19 +65,15 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->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
Expand All @@ -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
Expand All @@ -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();
}
}