Repository navigation
Expand file tree
/
Copy pathRemoveDeadStmtRector.php
More file actions
153 lines (128 loc) · 4.31 KB
/
Copy pathRemoveDeadStmtRector.php
File metadata and controls
153 lines (128 loc) · 4.31 KB
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Expression;
use PhpParser\Comment\Doc;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Cast\Void_;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\NodeVisitor;
use PHPStan\Reflection\Php\PhpPropertyReflection;
use Rector\DeadCode\NodeAnalyzer\NoDiscardCallAnalyzer;
use Rector\DeadCode\NodeManipulator\LivingCodeManipulator;
use Rector\NodeAnalyzer\PropertyFetchAnalyzer;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Expression\RemoveDeadStmtRector\RemoveDeadStmtRectorTest
*/
final class RemoveDeadStmtRector extends AbstractRector
{
public function __construct(
private readonly LivingCodeManipulator $livingCodeManipulator,
private readonly NoDiscardCallAnalyzer $noDiscardCallAnalyzer,
private readonly PropertyFetchAnalyzer $propertyFetchAnalyzer,
private readonly ReflectionResolver $reflectionResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove dead code statements', [
new CodeSample(
<<<'CODE_SAMPLE'
$value = 5;
$value;
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$value = 5;
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
* @return Node[]|Node|null|NodeVisitor::REMOVE_NODE
*/
public function refactor(Node $node): array|Node|null|int
{
if ($this->hasGetMagic($node)) {
return null;
}
if ($this->isVoidCastNoDiscardCall($node)) {
return null;
}
if ($this->isShortCircuitIssetWithSideEffect($node)) {
return null;
}
$livingCode = $this->livingCodeManipulator->keepLivingCodeFromExpr($node->expr);
if ($livingCode === []) {
return $this->removeNodeAndKeepComments($node);
}
if ($livingCode === [$node->expr]) {
return null;
}
$newNode = clone $node;
$newNode->expr = array_shift($livingCode);
$newNodes = [$newNode];
foreach ($livingCode as $singleLivingCode) {
$newNodes[] = new Expression($singleLivingCode);
}
return $newNodes;
}
private function hasGetMagic(Expression $expression): bool
{
if (! $this->propertyFetchAnalyzer->isPropertyFetch($expression->expr)) {
return false;
}
$propertyFetch = $expression->expr;
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($propertyFetch);
/**
* property not found assume has class has __get method
* that can call non-defined property, that can have some special handling, eg: throw on special case
*/
return ! $phpPropertyReflection instanceof PhpPropertyReflection;
}
/**
* isset() with more args stops on first unset one, so side effects in later args are conditional
*/
private function isShortCircuitIssetWithSideEffect(Expression $expression): bool
{
if (! $expression->expr instanceof Isset_) {
return false;
}
$vars = $expression->expr->vars;
array_shift($vars);
return array_any($vars, fn (Expr $var): bool => $this->livingCodeManipulator->keepLivingCodeFromExpr($var) !== []);
}
private function isVoidCastNoDiscardCall(Expression $expression): bool
{
if (! $expression->expr instanceof Void_) {
return false;
}
return $this->noDiscardCallAnalyzer->isNoDiscardCall($expression->expr->expr);
}
/**
* @return NodeVisitor::REMOVE_NODE|Node
*/
private function removeNodeAndKeepComments(Expression $expression): int|Node
{
if ($expression->getDocComment() instanceof Doc) {
$nop = new Nop();
$nop->setDocComment($expression->getDocComment());
return $nop;
}
return NodeVisitor::REMOVE_NODE;
}
}