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 @@ -10,7 +10,6 @@ final class Directly
$match = in_array($value, $items, TRUE) !== FALSE;

$match = true === in_array($value, $items, TRUE);
$match = false !== $match;
}
}

Expand All @@ -28,7 +27,6 @@ final class Directly
$match = in_array($value, $items, TRUE);

$match = in_array($value, $items, TRUE);
$match = $match;
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

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

final class SelfAssignNegation
{
public function run(bool $value, array $items)
{
$match = in_array($value, $items, true);
$match = $match !== true;
}
}

?>
-----
<?php

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

final class SelfAssignNegation
{
public function run(bool $value, array $items)
{
$match = in_array($value, $items, true);
$match = !$match;
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

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

final class SkipSelfAssign
{
public function run(bool $value, array $items)
{
$match = in_array($value, $items, true);
$match = false !== $match;
$match = $match === true;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\BooleanNot;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
Expand Down Expand Up @@ -88,17 +89,18 @@ public function refactor(Node $node): ?Node
return null;
}

private function processBoolTypeToNotBool(Node $node, Expr $leftExpr, Expr $rightExpr): ?Expr
private function processBoolTypeToNotBool(Identical|NotIdentical $node, Expr $leftExpr, Expr $rightExpr): ?Expr
{
if ($node instanceof Identical) {
return $this->refactorIdentical($leftExpr, $rightExpr);
}
$expr = $node instanceof Identical
? $this->refactorIdentical($leftExpr, $rightExpr)
: $this->refactorNotIdentical($leftExpr, $rightExpr);

if ($node instanceof NotIdentical) {
return $this->refactorNotIdentical($leftExpr, $rightExpr);
// $value = $value !== false would become a self-assign
if ($expr instanceof Expr && $expr->getAttribute(AttributeKey::IS_ASSIGN_VAR_IN_COMPARISON) === true) {
return null;
}

return null;
return $expr;
}

private function refactorIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr
Expand Down
5 changes: 5 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ final class AttributeKey

public const string IS_MULTI_ASSIGN = 'is_multi_assign';

/**
* The assigned variable used in its own compared value, e.g. the 2nd $value in $value = $value !== false
*/
public const string IS_ASSIGN_VAR_IN_COMPARISON = 'is_assign_var_in_comparison';

public const string IS_IN_LOOP_OR_SWITCH = 'is_in_loop';

public const string IS_VARIABLE_LOOP = 'is_variable_loop';
Expand Down
21 changes: 21 additions & 0 deletions src/PhpParser/NodeVisitor/AssignedToNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignRef;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Expr\Variable;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand Down Expand Up @@ -48,6 +51,11 @@ public function enterNode(Node $node): ?Node

$node->expr->setAttribute(AttributeKey::IS_ASSIGNED_TO, true);

if ($node->expr instanceof Identical || $node->expr instanceof NotIdentical) {
$this->markAssignVarInComparison($node->var, $node->expr->left);
$this->markAssignVarInComparison($node->var, $node->expr->right);
}

if ($node->expr instanceof Assign) {
$node->var->setAttribute(AttributeKey::IS_MULTI_ASSIGN, true);
$node->expr->setAttribute(AttributeKey::IS_MULTI_ASSIGN, true);
Expand All @@ -71,4 +79,17 @@ private function markAssignOpVar(Expr $expr): void
$expr->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true);
}
}

private function markAssignVarInComparison(Expr $assignVar, Expr $comparedExpr): void
{
if (! $assignVar instanceof Variable || ! $comparedExpr instanceof Variable) {
return;
}

if (! is_string($assignVar->name) || $assignVar->name !== $comparedExpr->name) {
return;
}

$comparedExpr->setAttribute(AttributeKey::IS_ASSIGN_VAR_IN_COMPARISON, true);
}
}
Loading