Skip to content

Commit a4e6a8c

Browse files
committed
[CodeQuality] Deprecate SwitchTrueToMatchRector as hacky switch/match (true) construction, needs human check
1 parent 5911691 commit a4e6a8c

9 files changed

Lines changed: 8 additions & 221 deletions

File tree

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_break.php.inc‎

Lines changed: 0 additions & 21 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_default_not_last.php.inc‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_false.php.inc‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_no_default.php.inc‎

Lines changed: 0 additions & 16 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/with_default.php.inc‎

Lines changed: 0 additions & 41 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/SwitchTrueToMatchRectorTest.php‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/config/configured_rule.php‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php‎

Lines changed: 8 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,18 @@
55
namespace Rector\CodeQuality\Rector\Switch_;
66

77
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_;
128
use PhpParser\Node\Stmt\Switch_;
13-
use Rector\PhpParser\Node\Value\ValueResolver;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
1411
use Rector\Rector\AbstractRector;
1512
use Rector\RuleDoc\CodeSample\CodeSample;
1613
use Rector\RuleDoc\RuleDefinition;
17-
use Rector\ValueObject\PhpVersionFeature;
18-
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
1914

2015
/**
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.
2217
*/
23-
final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface
18+
final class SwitchTrueToMatchRector extends AbstractRector implements DeprecatedInterface
2419
{
25-
public function __construct(
26-
private readonly ValueResolver $valueResolver,
27-
) {
28-
}
29-
3020
public function getRuleDefinition(): RuleDefinition
3121
{
3222
return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [
@@ -79,60 +69,9 @@ public function getNodeTypes(): array
7969
*/
8070
public function refactor(Node $node): ?Node
8171
{
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+
));
13776
}
13877
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
6363
use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector;
6464
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
65-
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
6665
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
6766
use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector;
6867
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
@@ -153,7 +152,6 @@ final class CodeQualityLevel
153152
SingularSwitchToIfRector::class,
154153
InlineIfToExplicitIfRector::class,
155154
TernaryFalseExpressionToIfRector::class,
156-
SwitchTrueToMatchRector::class,
157155
SimplifyIfNullableReturnRector::class,
158156
FlipTypeControlToUseExclusiveTypeRector::class,
159157
InlineArrayReturnAssignRector::class,

0 commit comments

Comments
 (0)