Skip to content

Commit 919fc0e

Browse files
authored
[DeadCode] Add RemoveDoubleSelfAssignRector (#8203)
1 parent 696058c commit 919fc0e

6 files changed

Lines changed: 120 additions & 0 deletions

File tree

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\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;
4+
5+
$validator = $validator = createValidator();
6+
7+
?>
8+
-----
9+
<?php
10+
11+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;
12+
13+
$validator = createValidator();
14+
15+
?>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\Fixture;
4+
5+
$first = $second = createValidator();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class RemoveDoubleSelfAssignRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\Config\RectorConfig;
6+
use Rector\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector;
7+
8+
return RectorConfig::configure()
9+
->withRules([RemoveDoubleSelfAssignRector::class]);
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\DeadCode\Rector\Assign;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\Assign;
9+
use PhpParser\Node\Expr\Variable;
10+
use Rector\Rector\AbstractRector;
11+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
12+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
13+
14+
/**
15+
* @see \Rector\Tests\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector\RemoveDoubleSelfAssignRectorTest
16+
*/
17+
final class RemoveDoubleSelfAssignRector extends AbstractRector
18+
{
19+
public function getRuleDefinition(): RuleDefinition
20+
{
21+
return new RuleDefinition('Remove duplicated self assign of the same variable', [
22+
new CodeSample(
23+
'$validator = $validator = Validation::createValidator();',
24+
'$validator = Validation::createValidator();'
25+
),
26+
]);
27+
}
28+
29+
/**
30+
* @return array<class-string<Node>>
31+
*/
32+
public function getNodeTypes(): array
33+
{
34+
return [Assign::class];
35+
}
36+
37+
/**
38+
* @param Assign $node
39+
*/
40+
public function refactor(Node $node): ?Node
41+
{
42+
if (! $node->var instanceof Variable) {
43+
return null;
44+
}
45+
46+
if (! $node->expr instanceof Assign) {
47+
return null;
48+
}
49+
50+
$innerAssign = $node->expr;
51+
if (! $innerAssign->var instanceof Variable) {
52+
return null;
53+
}
54+
55+
if (! $this->nodeComparator->areNodesEqual($node->var, $innerAssign->var)) {
56+
return null;
57+
}
58+
59+
return $innerAssign;
60+
}
61+
}

src/Config/Level/DeadCodeLevel.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Rector\Contract\Rector\RectorInterface;
99
use Rector\DeadCode\Rector\Array_\RemoveDuplicatedArrayKeyRector;
1010
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
11+
use Rector\DeadCode\Rector\Assign\RemoveDoubleSelfAssignRector;
1112
use Rector\DeadCode\Rector\Assign\RemoveUnusedVariableAssignRector;
1213
use Rector\DeadCode\Rector\Block\ReplaceBlockToItsStmtsRector;
1314
use Rector\DeadCode\Rector\BooleanAnd\RemoveAndTrueRector;
@@ -109,6 +110,7 @@ final class DeadCodeLevel
109110
TernaryToBooleanOrFalseToBooleanAndRector::class,
110111
RemoveUselessTernaryRector::class,
111112
RemoveDoubleAssignRector::class,
113+
RemoveDoubleSelfAssignRector::class,
112114
RemoveUselessAssignFromPropertyPromotionRector::class,
113115
RemoveConcatAutocastRector::class,
114116
SimplifyIfElseWithSameContentRector::class,

0 commit comments

Comments
 (0)