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..6e288253478 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', [ @@ -81,42 +72,12 @@ public function getNodeTypes(): array /** * @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,