Repository navigation
Expand file tree
/
Copy pathSwitchTrueToMatchRector.php
More file actions
138 lines (117 loc) · 3.43 KB
/
Copy pathSwitchTrueToMatchRector.php
File metadata and controls
138 lines (117 loc) · 3.43 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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Switch_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\MatchArm;
use PhpParser\Node\Stmt\Return_;
use PhpParser\Node\Stmt\Switch_;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\SwitchTrueToMatchRectorTest
*/
final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
) {
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
switch (true) {
case $value === 0:
return 'no';
case $value === 1:
return 'yes';
default:
return 'maybe';
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($value)
{
return match (true) {
$value === 0 => 'no',
$value === 1 => 'yes',
default => 'maybe',
};
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Switch_::class];
}
/**
* @param Switch_ $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->valueResolver->isTrue($node->cond)) {
return null;
}
if ($node->cases === []) {
return null;
}
$matchArms = [];
$hasDefault = false;
$lastCaseKey = array_key_last($node->cases);
foreach ($node->cases as $key => $case) {
// a match arm can only carry a single return expression
if (count($case->stmts) !== 1) {
return null;
}
$onlyStmt = $case->stmts[0];
if (! $onlyStmt instanceof Return_) {
return null;
}
if (! $onlyStmt->expr instanceof Expr) {
return null;
}
if ($case->cond instanceof Expr) {
$matchArms[] = new MatchArm([$case->cond], $onlyStmt->expr);
continue;
}
// default case must be the last one, as a match default arm catches everything
if ($key !== $lastCaseKey) {
return null;
}
$hasDefault = true;
$matchArms[] = new MatchArm(null, $onlyStmt->expr);
}
// require a default arm, otherwise match (true) throws UnhandledMatchError where switch (true) falls through
if (! $hasDefault) {
return null;
}
$match = new Match_($this->nodeFactory->createTrue(), $matchArms);
return new Return_($match);
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::MATCH_EXPRESSION;
}
}