|
5 | 5 | namespace Rector\CodeQuality\Rector\Switch_; |
6 | 6 |
|
7 | 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 | 8 | use PhpParser\Node\Stmt\Switch_; |
13 | | -use Rector\PhpParser\Node\Value\ValueResolver; |
| 9 | +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; |
| 10 | +use Rector\Exception\ShouldNotHappenException; |
14 | 11 | use Rector\Rector\AbstractRector; |
15 | 12 | use Rector\RuleDoc\CodeSample\CodeSample; |
16 | 13 | use Rector\RuleDoc\RuleDefinition; |
17 | | -use Rector\ValueObject\PhpVersionFeature; |
18 | | -use Rector\VersionBonding\Contract\MinPhpVersionInterface; |
19 | 14 |
|
20 | 15 | /** |
21 | | - * @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\SwitchTrueToMatchRectorTest |
| 16 | + * @deprecated This rule is deprecated, as both `switch (true)` and `match (true)` are hacky constructions; a constant list or explicit mapping is clearer and the change needs a human check. |
22 | 17 | */ |
23 | | -final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface |
| 18 | +final class SwitchTrueToMatchRector extends AbstractRector implements DeprecatedInterface |
24 | 19 | { |
25 | | - public function __construct( |
26 | | - private readonly ValueResolver $valueResolver, |
27 | | - ) { |
28 | | - } |
29 | | - |
30 | 20 | public function getRuleDefinition(): RuleDefinition |
31 | 21 | { |
32 | 22 | return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [ |
@@ -79,60 +69,9 @@ public function getNodeTypes(): array |
79 | 69 | */ |
80 | 70 | public function refactor(Node $node): ?Node |
81 | 71 | { |
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; |
| 72 | + throw new ShouldNotHappenException(sprintf( |
| 73 | + '"%s" rule is deprecated, as both switch (true) and match (true) are hacky constructions that need a human check', |
| 74 | + self::class |
| 75 | + )); |
137 | 76 | } |
138 | 77 | } |
0 commit comments