File tree Expand file tree Collapse file tree
rules-tests/DeadCode/Rector/Assign/RemoveDoubleSelfAssignRector
rules/DeadCode/Rector/Assign Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ ?>
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Rector \Tests \DeadCode \Rector \Assign \RemoveDoubleSelfAssignRector \Fixture ;
4+
5+ $ first = $ second = createValidator ();
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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]);
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 88use Rector \Contract \Rector \RectorInterface ;
99use Rector \DeadCode \Rector \Array_ \RemoveDuplicatedArrayKeyRector ;
1010use Rector \DeadCode \Rector \Assign \RemoveDoubleAssignRector ;
11+ use Rector \DeadCode \Rector \Assign \RemoveDoubleSelfAssignRector ;
1112use Rector \DeadCode \Rector \Assign \RemoveUnusedVariableAssignRector ;
1213use Rector \DeadCode \Rector \Block \ReplaceBlockToItsStmtsRector ;
1314use 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,
You can’t perform that action at this time.
0 commit comments