From 0301555a960f00672f04f3193933b500e6b12225 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Mon, 29 Jun 2026 11:22:37 +0200 Subject: [PATCH 1/2] [deprecation] Deprecate SwitchTrueToIfRector --- phpstan.neon | 1 + .../Fixture/skip_break.php.inc | 25 -------- .../Fixture/skip_empty_cases.php.inc | 12 ---- .../Fixture/skip_false.php.inc | 18 ------ .../Fixture/some_class.php.inc | 42 ------------- .../Fixture/with_default.php.inc | 45 -------------- .../SwitchTrueToIfRectorTest.php | 28 --------- .../config/configured_rule.php | 9 --- .../Rector/Switch_/SwitchTrueToIfRector.php | 60 +++---------------- src/Config/Level/CodeQualityLevel.php | 2 - 10 files changed, 10 insertions(+), 232 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_break.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_empty_cases.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_false.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/some_class.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/with_default.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/SwitchTrueToIfRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/config/configured_rule.php diff --git a/phpstan.neon b/phpstan.neon index 5a124796077..087f1bb2e4d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -419,6 +419,7 @@ parameters: - '#Class "Rector\\TypeDeclaration\\Rector\\ClassMethod\\StrictStringParamConcatRector" is missing @see annotation with test case class reference#' - '#Class "Rector\\CodingStyle\\Rector\\Enum_\\EnumCaseToPascalCaseRector" is missing @see annotation with test case class reference#' - '#Class "Rector\\CodeQuality\\Rector\\Concat\\JoinStringConcatRector" is missing @see annotation with test case class reference#' + - '#Class "Rector\\CodeQuality\\Rector\\Switch_\\SwitchTrueToIfRector" is missing @see annotation with test case class reference#' # false positive - diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_break.php.inc b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_break.php.inc deleted file mode 100644 index 84011f4ac76..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_break.php.inc +++ /dev/null @@ -1,25 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_empty_cases.php.inc b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_empty_cases.php.inc deleted file mode 100644 index 22cb8cc65be..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/skip_empty_cases.php.inc +++ /dev/null @@ -1,12 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/with_default.php.inc b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/with_default.php.inc deleted file mode 100644 index 0c9370d5266..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/Fixture/with_default.php.inc +++ /dev/null @@ -1,45 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/SwitchTrueToIfRectorTest.php b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/SwitchTrueToIfRectorTest.php deleted file mode 100644 index 84288f6b88e..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/SwitchTrueToIfRectorTest.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_/SwitchTrueToIfRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/config/configured_rule.php deleted file mode 100644 index 71414e8644e..00000000000 --- a/rules-tests/CodeQuality/Rector/Switch_/SwitchTrueToIfRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([SwitchTrueToIfRector::class]); diff --git a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php index c143fca8cc8..d9a4761f81e 100644 --- a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php +++ b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php @@ -5,27 +5,18 @@ namespace Rector\CodeQuality\Rector\Switch_; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Stmt; -use PhpParser\Node\Stmt\Case_; -use PhpParser\Node\Stmt\If_; -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 Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; /** - * @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToIfRector\SwitchTrueToIfRectorTest + * @deprecated as it worsens readability by expanding a compact `switch (true)` into many separate `if` statements. Use `match (true)` for the same logic instead. */ -final class SwitchTrueToIfRector extends AbstractRector +final class SwitchTrueToIfRector extends AbstractRector implements DeprecatedInterface { - public function __construct( - private readonly ValueResolver $valueResolver, - ) { - } - public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Change `switch (true)` to `if` statements', [ @@ -79,44 +70,11 @@ public function getNodeTypes(): array return [Switch_::class]; } - /** - * @param Switch_ $node - * @return Stmt[]|null - */ - public function refactor(Node $node): ?array + public function refactor(Node $node): ?Node { - if (! $this->valueResolver->isTrue($node->cond)) { - return null; - } - - $newStmts = []; - - $defaultCase = null; - - foreach ($node->cases as $case) { - if (! end($case->stmts) instanceof Return_) { - return null; - } - - if (! $case->cond instanceof Expr) { - $defaultCase = $case; - continue; - } - - $if = new If_($case->cond); - $if->stmts = $case->stmts; - - $newStmts[] = $if; - } - - if ($defaultCase instanceof Case_) { - $newStmts = array_merge($newStmts, $defaultCase->stmts); - } - - if ($newStmts === []) { - return null; - } - - return $newStmts; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated as it worsens readability. Use "match (true)" for the same logic instead', + self::class + )); } } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index e4d3b3954a4..3f1c9bb6e4f 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -75,7 +75,6 @@ use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector; use Rector\CodeQuality\Rector\StmtsAwareInterface\MoveInnerFunctionToTopLevelRector; use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector; -use Rector\CodeQuality\Rector\Switch_\SwitchTrueToIfRector; use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector; use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector; use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector; @@ -181,7 +180,6 @@ final class CodeQualityLevel TernaryEmptyArrayArrayDimFetchToCoalesceRector::class, OptionalParametersAfterRequiredRector::class, SimplifyEmptyCheckOnEmptyArrayRector::class, - SwitchTrueToIfRector::class, CleanupUnneededNullsafeOperatorRector::class, DisallowedEmptyRuleFixerRector::class, LocallyCalledStaticMethodToNonStaticRector::class, From d53de93d525ead9735cbfe8ddc99fa1915d28419 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Mon, 29 Jun 2026 09:24:20 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php index d9a4761f81e..6e288253478 100644 --- a/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php +++ b/rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php @@ -70,6 +70,9 @@ public function getNodeTypes(): array return [Switch_::class]; } + /** + * @param Switch_ $node + */ public function refactor(Node $node): ?Node { throw new ShouldNotHappenException(sprintf(