Skip to content

Commit 02f15c4

Browse files
authored
[CodeQuality] Skip self-assign result in SimplifyBoolIdenticalTrueRector (#9029)
1 parent 9a7d321 commit 02f15c4

6 files changed

Lines changed: 79 additions & 9 deletions

File tree

‎rules-tests/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector/Fixture/directly.php.inc‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ final class Directly
1010
$match = in_array($value, $items, TRUE) !== FALSE;
1111

1212
$match = true === in_array($value, $items, TRUE);
13-
$match = false !== $match;
1413
}
1514
}
1615

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

3029
$match = in_array($value, $items, TRUE);
31-
$match = $match;
3230
}
3331
}
3432

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;
4+
5+
final class SelfAssignNegation
6+
{
7+
public function run(bool $value, array $items)
8+
{
9+
$match = in_array($value, $items, true);
10+
$match = $match !== true;
11+
}
12+
}
13+
14+
?>
15+
-----
16+
<?php
17+
18+
namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;
19+
20+
final class SelfAssignNegation
21+
{
22+
public function run(bool $value, array $items)
23+
{
24+
$match = in_array($value, $items, true);
25+
$match = !$match;
26+
}
27+
}
28+
29+
?>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Identical\SimplifyBoolIdenticalTrueRector\Fixture;
4+
5+
final class SkipSelfAssign
6+
{
7+
public function run(bool $value, array $items)
8+
{
9+
$match = in_array($value, $items, true);
10+
$match = false !== $match;
11+
$match = $match === true;
12+
}
13+
}
14+
15+
?>

‎rules/CodeQuality/Rector/Identical/SimplifyBoolIdenticalTrueRector.php‎

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use PhpParser\Node\Expr\BinaryOp\Identical;
1010
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1111
use PhpParser\Node\Expr\BooleanNot;
12+
use Rector\NodeTypeResolver\Node\AttributeKey;
1213
use Rector\PhpParser\Node\Value\ValueResolver;
1314
use Rector\Rector\AbstractRector;
1415
use Rector\RuleDoc\CodeSample\CodeSample;
@@ -88,17 +89,18 @@ public function refactor(Node $node): ?Node
8889
return null;
8990
}
9091

91-
private function processBoolTypeToNotBool(Node $node, Expr $leftExpr, Expr $rightExpr): ?Expr
92+
private function processBoolTypeToNotBool(Identical|NotIdentical $node, Expr $leftExpr, Expr $rightExpr): ?Expr
9293
{
93-
if ($node instanceof Identical) {
94-
return $this->refactorIdentical($leftExpr, $rightExpr);
95-
}
94+
$expr = $node instanceof Identical
95+
? $this->refactorIdentical($leftExpr, $rightExpr)
96+
: $this->refactorNotIdentical($leftExpr, $rightExpr);
9697

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

101-
return null;
103+
return $expr;
102104
}
103105

104106
private function refactorIdentical(Expr $leftExpr, Expr $rightExpr): ?Expr

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ final class AttributeKey
9696

9797
public const string IS_MULTI_ASSIGN = 'is_multi_assign';
9898

99+
/**
100+
* The assigned variable used in its own compared value, e.g. the 2nd $value in $value = $value !== false
101+
*/
102+
public const string IS_ASSIGN_VAR_IN_COMPARISON = 'is_assign_var_in_comparison';
103+
99104
public const string IS_IN_LOOP_OR_SWITCH = 'is_in_loop';
100105

101106
public const string IS_VARIABLE_LOOP = 'is_variable_loop';

‎src/PhpParser/NodeVisitor/AssignedToNodeVisitor.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
use PhpParser\Node\Expr\Assign;
1212
use PhpParser\Node\Expr\AssignOp;
1313
use PhpParser\Node\Expr\AssignRef;
14+
use PhpParser\Node\Expr\BinaryOp\Identical;
15+
use PhpParser\Node\Expr\BinaryOp\NotIdentical;
1416
use PhpParser\Node\Expr\List_;
17+
use PhpParser\Node\Expr\Variable;
1518
use PhpParser\NodeVisitorAbstract;
1619
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
1720
use Rector\NodeTypeResolver\Node\AttributeKey;
@@ -48,6 +51,11 @@ public function enterNode(Node $node): ?Node
4851

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

54+
if ($node->expr instanceof Identical || $node->expr instanceof NotIdentical) {
55+
$this->markAssignVarInComparison($node->var, $node->expr->left);
56+
$this->markAssignVarInComparison($node->var, $node->expr->right);
57+
}
58+
5159
if ($node->expr instanceof Assign) {
5260
$node->var->setAttribute(AttributeKey::IS_MULTI_ASSIGN, true);
5361
$node->expr->setAttribute(AttributeKey::IS_MULTI_ASSIGN, true);
@@ -71,4 +79,17 @@ private function markAssignOpVar(Expr $expr): void
7179
$expr->setAttribute(AttributeKey::IS_ASSIGN_OP_VAR, true);
7280
}
7381
}
82+
83+
private function markAssignVarInComparison(Expr $assignVar, Expr $comparedExpr): void
84+
{
85+
if (! $assignVar instanceof Variable || ! $comparedExpr instanceof Variable) {
86+
return;
87+
}
88+
89+
if (! is_string($assignVar->name) || $assignVar->name !== $comparedExpr->name) {
90+
return;
91+
}
92+
93+
$comparedExpr->setAttribute(AttributeKey::IS_ASSIGN_VAR_IN_COMPARISON, true);
94+
}
7495
}

0 commit comments

Comments
 (0)