-
-
Notifications
You must be signed in to change notification settings - Fork 688
/
SimplifyMirrorAssignRector.php
52 lines (50 loc) · 1.31 KB
/
SimplifyMirrorAssignRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare (strict_types=1);
namespace Rector\DeadCode\Rector\Expression;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Expression;
use PhpParser\NodeVisitor;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Expression\SimplifyMirrorAssignRector\SimplifyMirrorAssignRectorTest
*/
final class SimplifyMirrorAssignRector extends AbstractRector
{
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Removes unneeded $value = $value assigns', [new CodeSample(<<<'CODE_SAMPLE'
function run() {
$result = $result;
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
function run() {
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Expression::class];
}
/**
* @param Expression $node
*/
public function refactor(Node $node) : ?int
{
if (!$node->expr instanceof Assign) {
return null;
}
$assign = $node->expr;
if (!$this->nodeComparator->areNodesEqual($assign->var, $assign->expr)) {
return null;
}
return NodeVisitor::REMOVE_NODE;
}
}