-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathRemoveDeadIfBlockRector.php
More file actions
158 lines (138 loc) · 4.14 KB
/
RemoveDeadIfBlockRector.php
File metadata and controls
158 lines (138 loc) · 4.14 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
154
155
156
157
158
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\If_;
use PhpParser\Node;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\If_;
use PhpParser\NodeVisitor;
use Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use Rector\EarlyReturn\NodeTransformer\ConditionInverter;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\If_\RemoveDeadIfBlockRector\RemoveDeadIfBlockRectorTest
*/
final class RemoveDeadIfBlockRector extends AbstractRector
{
public function __construct(
private readonly ConditionInverter $conditionInverter,
private readonly SideEffectNodeDetector $sideEffectNodeDetector,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Remove if, elseif, and else blocks that do not do anything',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value, $differentValue)
{
if ($value) {
} elseif ($differentValue) {
} else {
}
if ($differentValue) {
echo 'different';
} elseif ($value) {
} else {
}
if ($differentValue) {
} elseif ($value) {
echo 'value';
} else {
}
return $differentValue;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value, $differentValue)
{
if ($differentValue) {
echo 'different';
}
if (!$differentValue && $value) {
echo 'value';
}
return $differentValue;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [If_::class];
}
/**
* @param If_ $node
* @return NodeVisitor::REMOVE_NODE|null|If_
*/
public function refactor(Node $node): int|null|If_
{
if ($node->else instanceof Else_ && $node->else->stmts === []) {
$node->else = null;
return $this->refactor($node) ?? $node;
}
foreach ($node->elseifs as $elseif) {
$keep_elseifs = array_values(array_filter(
$node->elseifs,
fn (ElseIf_ $elseif): bool => $elseif->stmts !== [] || $this->sideEffectNodeDetector->detect(
$elseif->cond
)
));
if (count($node->elseifs) !== count($keep_elseifs)) {
$node->elseifs = $keep_elseifs;
return $this->refactor($node) ?? $node;
}
}
if ($node->stmts !== []) {
return null;
}
// Skip commented blocks because we can't know
// if the comment will make sense after merging.
if ($node->getComments() !== []) {
return null;
}
if ($this->sideEffectNodeDetector->detect($node->cond)) {
return null;
}
// When the if body is blank but it has an elseif,
// merge the negated if condition with the elseif condition
if ($node->elseifs !== []) {
$firstElseIf = $node->elseifs[0];
$cond = new BooleanAnd(
$this->conditionInverter->createInvertedCondition($node->cond),
$firstElseIf->cond
);
$if = new If_($cond, [
'stmts' => $firstElseIf->stmts,
]);
if (count($node->elseifs) > 1) {
$if->elseifs = \array_slice($node->elseifs, 1);
}
return $this->refactor($if) ?? $if;
}
if ($node->else instanceof Else_) {
$node->cond = $this->conditionInverter->createInvertedCondition($node->cond);
$node->stmts = $node->else->stmts;
$node->else = null;
return $node;
}
return NodeVisitor::REMOVE_NODE;
}
}