forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemoveUnreachableStatementRector.php
188 lines (158 loc) · 4.77 KB
/
RemoveUnreachableStatementRector.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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Stmt;
use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Namespace_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\While_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://github.com/phpstan/phpstan/blob/83078fe308a383c618b8c1caec299e5765d9ac82/src/Node/UnreachableStatementNode.php
*
* @see \Rector\Tests\DeadCode\Rector\Stmt\RemoveUnreachableStatementRector\RemoveUnreachableStatementRectorTest
*/
final class RemoveUnreachableStatementRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unreachable statements', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return 5;
$removeMe = 10;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
return 5;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Stmt::class];
}
/**
* @param Stmt $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkipNode($node)) {
return null;
}
// might be PHPStan false positive, better skip
$previousStatement = $node->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
if ($previousStatement instanceof If_) {
$node->setAttribute(
AttributeKey::IS_UNREACHABLE,
$previousStatement->getAttribute(AttributeKey::IS_UNREACHABLE)
);
}
if ($previousStatement instanceof While_) {
$node->setAttribute(
AttributeKey::IS_UNREACHABLE,
$previousStatement->getAttribute(AttributeKey::IS_UNREACHABLE)
);
}
if (! $this->isUnreachable($node)) {
return null;
}
if ($this->isAfterMarkTestSkippedMethodCall($node)) {
$node->setAttribute(AttributeKey::IS_UNREACHABLE, false);
return null;
}
$this->removeNode($node);
return null;
}
private function shouldSkipNode(Stmt $stmt): bool
{
if ($stmt instanceof Nop) {
return true;
}
if ($stmt instanceof ClassLike) {
return true;
}
return $stmt instanceof FunctionLike;
}
private function isUnreachable(Stmt $stmt): bool
{
$isUnreachable = $stmt->getAttribute(AttributeKey::IS_UNREACHABLE);
if ($isUnreachable === true) {
return true;
}
// traverse up for unreachable node in the same scope
$previousNode = $stmt->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
while ($previousNode instanceof Stmt && ! $this->isBreakingScopeNode($previousNode)) {
$isUnreachable = $previousNode->getAttribute(AttributeKey::IS_UNREACHABLE);
if ($isUnreachable === true) {
return true;
}
$previousNode = $previousNode->getAttribute(AttributeKey::PREVIOUS_STATEMENT);
}
return false;
}
/**
* Keep content after markTestSkipped(), intentional temporary
*/
private function isAfterMarkTestSkippedMethodCall(Stmt $stmt): bool
{
return (bool) $this->betterNodeFinder->findFirstPrevious($stmt, function (Node $node): bool {
if (! $node instanceof MethodCall) {
return false;
}
if ($node->name instanceof MethodCall) {
return false;
}
if ($node->name instanceof StaticCall) {
return false;
}
return $this->isName($node->name, 'markTestSkipped');
});
}
/**
* Check nodes that breaks scope while traversing up
*/
private function isBreakingScopeNode(Stmt $stmt): bool
{
if ($stmt instanceof ClassLike) {
return true;
}
if ($stmt instanceof ClassMethod) {
return true;
}
if ($stmt instanceof Function_) {
return true;
}
if ($stmt instanceof Namespace_) {
return true;
}
return $stmt instanceof Else_;
}
}