Skip to content

Commit e12a222

Browse files
authored
[CodeQuality] Add SwitchTrueToMatchRector as replacement for deprecated SwitchTrueToIfRector (#8113)
1 parent c9ba93d commit e12a222

9 files changed

Lines changed: 290 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
4+
5+
class SkipBreak
6+
{
7+
public function run(int $int): string
8+
{
9+
$str = 'error';
10+
switch (true) {
11+
case $int === 0:
12+
$str = 'zero';
13+
break;
14+
default:
15+
$str = 'other';
16+
break;
17+
}
18+
19+
return $str;
20+
}
21+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
4+
5+
class SkipDefaultNotLast
6+
{
7+
public function run($value)
8+
{
9+
switch (true) {
10+
default:
11+
return 'maybe';
12+
case $value === 0:
13+
return 'no';
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
4+
5+
class SkipFalse
6+
{
7+
public function run($value)
8+
{
9+
switch (false) {
10+
case $value === 0:
11+
return 'no';
12+
default:
13+
return 'maybe';
14+
}
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
4+
5+
class SkipNoDefault
6+
{
7+
public function run($value)
8+
{
9+
switch (true) {
10+
case $value === 0:
11+
return 'no';
12+
case $value === 1:
13+
return 'yes';
14+
}
15+
}
16+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
4+
5+
class WithDefault
6+
{
7+
public function run($value)
8+
{
9+
switch (true) {
10+
case $value === 0:
11+
return 'no';
12+
case $value === 1:
13+
return 'yes';
14+
case $value === 2:
15+
return 'maybe';
16+
default:
17+
return 'nevermind';
18+
}
19+
}
20+
}
21+
22+
?>
23+
-----
24+
<?php
25+
26+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\Fixture;
27+
28+
class WithDefault
29+
{
30+
public function run($value)
31+
{
32+
return match (true) {
33+
$value === 0 => 'no',
34+
$value === 1 => 'yes',
35+
$value === 2 => 'maybe',
36+
default => 'nevermind',
37+
};
38+
}
39+
}
40+
41+
?>
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
6+
7+
use Iterator;
8+
use PHPUnit\Framework\Attributes\DataProvider;
9+
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
10+
11+
final class SwitchTrueToMatchRectorTest extends AbstractRectorTestCase
12+
{
13+
#[DataProvider('provideData')]
14+
public function test(string $filePath): void
15+
{
16+
$this->doTestFile($filePath);
17+
}
18+
19+
public static function provideData(): Iterator
20+
{
21+
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
22+
}
23+
24+
public function provideConfigFilePath(): string
25+
{
26+
return __DIR__ . '/config/configured_rule.php';
27+
}
28+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
6+
use Rector\Config\RectorConfig;
7+
use Rector\ValueObject\PhpVersion;
8+
9+
return static function (RectorConfig $rectorConfig): void {
10+
$rectorConfig->phpVersion(PhpVersion::PHP_80);
11+
$rectorConfig->rule(SwitchTrueToMatchRector::class);
12+
};
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\CodeQuality\Rector\Switch_;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\Match_;
10+
use PhpParser\Node\MatchArm;
11+
use PhpParser\Node\Stmt\Return_;
12+
use PhpParser\Node\Stmt\Switch_;
13+
use Rector\PhpParser\Node\Value\ValueResolver;
14+
use Rector\Rector\AbstractRector;
15+
use Rector\ValueObject\PhpVersionFeature;
16+
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
17+
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
18+
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
19+
20+
/**
21+
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\SwitchTrueToMatchRectorTest
22+
*/
23+
final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface
24+
{
25+
public function __construct(
26+
private readonly ValueResolver $valueResolver,
27+
) {
28+
}
29+
30+
public function getRuleDefinition(): RuleDefinition
31+
{
32+
return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [
33+
new CodeSample(
34+
<<<'CODE_SAMPLE'
35+
class SomeClass
36+
{
37+
public function run($value)
38+
{
39+
switch (true) {
40+
case $value === 0:
41+
return 'no';
42+
case $value === 1:
43+
return 'yes';
44+
default:
45+
return 'maybe';
46+
}
47+
}
48+
}
49+
CODE_SAMPLE
50+
51+
,
52+
<<<'CODE_SAMPLE'
53+
class SomeClass
54+
{
55+
public function run($value)
56+
{
57+
return match (true) {
58+
$value === 0 => 'no',
59+
$value === 1 => 'yes',
60+
default => 'maybe',
61+
};
62+
}
63+
}
64+
CODE_SAMPLE
65+
),
66+
]);
67+
}
68+
69+
/**
70+
* @return array<class-string<Node>>
71+
*/
72+
public function getNodeTypes(): array
73+
{
74+
return [Switch_::class];
75+
}
76+
77+
/**
78+
* @param Switch_ $node
79+
*/
80+
public function refactor(Node $node): ?Node
81+
{
82+
if (! $this->valueResolver->isTrue($node->cond)) {
83+
return null;
84+
}
85+
86+
if ($node->cases === []) {
87+
return null;
88+
}
89+
90+
$matchArms = [];
91+
$hasDefault = false;
92+
93+
$lastCaseKey = array_key_last($node->cases);
94+
95+
foreach ($node->cases as $key => $case) {
96+
// a match arm can only carry a single return expression
97+
if (count($case->stmts) !== 1) {
98+
return null;
99+
}
100+
101+
$onlyStmt = $case->stmts[0];
102+
if (! $onlyStmt instanceof Return_) {
103+
return null;
104+
}
105+
106+
if (! $onlyStmt->expr instanceof Expr) {
107+
return null;
108+
}
109+
110+
if ($case->cond instanceof Expr) {
111+
$matchArms[] = new MatchArm([$case->cond], $onlyStmt->expr);
112+
continue;
113+
}
114+
115+
// default case must be the last one, as a match default arm catches everything
116+
if ($key !== $lastCaseKey) {
117+
return null;
118+
}
119+
120+
$hasDefault = true;
121+
$matchArms[] = new MatchArm(null, $onlyStmt->expr);
122+
}
123+
124+
// require a default arm, otherwise match (true) throws UnhandledMatchError where switch (true) falls through
125+
if (! $hasDefault) {
126+
return null;
127+
}
128+
129+
$match = new Match_($this->nodeFactory->createTrue(), $matchArms);
130+
131+
return new Return_($match);
132+
}
133+
134+
public function provideMinPhpVersion(): int
135+
{
136+
return PhpVersionFeature::MATCH_EXPRESSION;
137+
}
138+
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector;
7676
use Rector\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector;
7777
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
78+
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
7879
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
7980
use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector;
8081
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
@@ -170,6 +171,7 @@ final class CodeQualityLevel
170171
VariableConstFetchToClassConstFetchRector::class,
171172
SwitchNegatedTernaryRector::class,
172173
SingularSwitchToIfRector::class,
174+
SwitchTrueToMatchRector::class,
173175
SimplifyIfNullableReturnRector::class,
174176
CallUserFuncWithArrowFunctionToInlineRector::class,
175177
FlipTypeControlToUseExclusiveTypeRector::class,

0 commit comments

Comments
 (0)