diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_break.php.inc b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_break.php.inc deleted file mode 100644 index 41442776eda..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/Fixture/skip_break.php.inc +++ /dev/null @@ -1,21 +0,0 @@ - ------ - 'no', - $value === 1 => 'yes', - $value === 2 => 'maybe', - default => 'nevermind', - }; - } -} - -?> diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/SwitchTrueToMatchRectorTest.php b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/SwitchTrueToMatchRectorTest.php deleted file mode 100644 index 41dbff0ec04..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/SwitchTrueToMatchRectorTest.php +++ /dev/null @@ -1,28 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/config/configured_rule.php deleted file mode 100644 index b864c1187a0..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector/config/configured_rule.php +++ /dev/null @@ -1,12 +0,0 @@ -phpVersion(PhpVersion::PHP_80); - $rectorConfig->rule(SwitchTrueToMatchRector::class); -}; diff --git a/rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php b/rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php index 36676573af8..8af3e5d7ecb 100644 --- a/rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php +++ b/rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php @@ -5,28 +5,18 @@ 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\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; -use Rector\ValueObject\PhpVersionFeature; -use Rector\VersionBonding\Contract\MinPhpVersionInterface; /** - * @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\SwitchTrueToMatchRectorTest + * @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. */ -final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface +final class SwitchTrueToMatchRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly ValueResolver $valueResolver, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [ @@ -79,60 +69,9 @@ public function getNodeTypes(): array */ 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; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as both switch (true) and match (true) are hacky constructions that need a human check', + self::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 0c7b6810cc4..0abd3f06e3c 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -60,7 +60,6 @@ use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector; use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector; use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector; -use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector; use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector; use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector; use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector; @@ -149,7 +148,6 @@ final class CodeQualityLevel SingularSwitchToIfRector::class, InlineIfToExplicitIfRector::class, TernaryFalseExpressionToIfRector::class, - SwitchTrueToMatchRector::class, SimplifyIfNullableReturnRector::class, FlipTypeControlToUseExclusiveTypeRector::class, InlineArrayReturnAssignRector::class,