Repository navigation
Expand file tree
/
Copy pathSingularSwitchToIfRector.php
More file actions
154 lines (130 loc) · 3.96 KB
/
Copy pathSingularSwitchToIfRector.php
File metadata and controls
154 lines (130 loc) · 3.96 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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Switch_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\BinaryOp\Equal;
use PhpParser\Node\Expr\BinaryOp\Identical;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Continue_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\NodeVisitor;
use Rector\Rector\AbstractRector;
use Rector\Renaming\NodeManipulator\SwitchManipulator;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SingularSwitchToIfRector\SingularSwitchToIfRectorTest
*/
final class SingularSwitchToIfRector extends AbstractRector
{
public function __construct(
private readonly SwitchManipulator $switchManipulator
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change `switch` with only 1 check to `if`', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeObject
{
public function run($value)
{
$result = 1;
switch ($value) {
case 100:
$result = 1000;
}
return $result;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeObject
{
public function run($value)
{
$result = 1;
if ($value == 100) {
$result = 1000;
}
return $result;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Switch_::class];
}
/**
* @param Switch_ $node
* @return Node\Stmt[]|If_|null
*/
public function refactor(Node $node): array|If_|null
{
if (count($node->cases) !== 1) {
return null;
}
$onlyCase = $node->cases[0];
// nested break/continue would lose the switch to target and cause a fatal error
if ($this->hasNestedBreakOrContinue($onlyCase->stmts)) {
return null;
}
// only default → basically unwrap
if (! $onlyCase->cond instanceof Expr) {
// remove default clause because it cause syntax error
return array_filter($onlyCase->stmts, static fn (Stmt $stmt): bool => ! $stmt instanceof Break_);
}
$if = new If_($this->createComparison($node->cond, $onlyCase->cond));
$if->stmts = $this->switchManipulator->removeBreakNodes($onlyCase->stmts);
return $if;
}
/**
* switch compares loosely, so strict comparison is safe only for 2 integers
*/
private function createComparison(Expr $leftExpr, Expr $rightExpr): Equal|Identical
{
$leftType = $this->getType($leftExpr);
$rightType = $this->getType($rightExpr);
if ($leftType->isInteger()->yes() && $rightType->isInteger()->yes()) {
return new Identical($leftExpr, $rightExpr);
}
return new Equal($leftExpr, $rightExpr);
}
/**
* @param Stmt[] $stmts
*/
private function hasNestedBreakOrContinue(array $stmts): bool
{
$hasNested = false;
foreach ($stmts as $stmt) {
// top level break is removed by SwitchManipulator
if ($stmt instanceof Break_) {
continue;
}
$this->traverseNodesWithCallable($stmt, static function (Node $subNode) use (&$hasNested): ?int {
if ($subNode instanceof Class_ || $subNode instanceof FunctionLike) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($subNode instanceof Break_ || $subNode instanceof Continue_) {
$hasNested = true;
return NodeVisitor::STOP_TRAVERSAL;
}
return null;
});
}
return $hasNested;
}
}