From 6dde3477991c5d2a9fa818ed0cf4cba797d686cd Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Tue, 4 Aug 2026 10:53:00 +0200 Subject: [PATCH] [CodeQuality] Deprecate SwitchNegatedTernaryRector --- .../Fixture/fixture.php.inc | 31 ------------- .../Fixture/nested_ternary.php.inc | 45 ------------------- .../Fixture/skip_elvis.php.inc | 13 ------ .../Fixture/skip_truthy.php.inc | 15 ------- .../SwitchNegatedTernaryRectorTest.php | 28 ------------ .../config/configured_rule.php | 9 ---- .../Ternary/SwitchNegatedTernaryRector.php | 40 ++++------------- .../Ternary/RemoveUselessTernaryRector.php | 6 +-- src/Config/Level/CodeQualityLevel.php | 2 - .../Issues/Issue6480/Fixture/fixture.php.inc | 31 ------------- .../NegatedTernaryInsideDeadInstanceTest.php | 31 ------------- .../Issues/Issue6480/Source/ExistingClass.php | 10 ----- .../Issue6480/config/configured_rule.php | 10 ----- 13 files changed, 9 insertions(+), 262 deletions(-) delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/fixture.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/nested_ternary.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_elvis.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_truthy.php.inc delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/SwitchNegatedTernaryRectorTest.php delete mode 100644 rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/config/configured_rule.php delete mode 100644 tests/Issues/Issue6480/Fixture/fixture.php.inc delete mode 100644 tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php delete mode 100644 tests/Issues/Issue6480/Source/ExistingClass.php delete mode 100644 tests/Issues/Issue6480/config/configured_rule.php diff --git a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/fixture.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/fixture.php.inc deleted file mode 100644 index 6ec928f1b0f..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/nested_ternary.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/nested_ternary.php.inc deleted file mode 100644 index f49d44fe594..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/nested_ternary.php.inc +++ /dev/null @@ -1,45 +0,0 @@ - ------ - diff --git a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_elvis.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_elvis.php.inc deleted file mode 100644 index 7134bdced13..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_elvis.php.inc +++ /dev/null @@ -1,13 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_truthy.php.inc b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_truthy.php.inc deleted file mode 100644 index 20cf678e8fd..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_truthy.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - diff --git a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/SwitchNegatedTernaryRectorTest.php b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/SwitchNegatedTernaryRectorTest.php deleted file mode 100644 index 64b4f14bcb6..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/SwitchNegatedTernaryRectorTest.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/Ternary/SwitchNegatedTernaryRector/config/configured_rule.php b/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/config/configured_rule.php deleted file mode 100644 index 29d85602baa..00000000000 --- a/rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([SwitchNegatedTernaryRector::class]); diff --git a/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php b/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php index 9f1d1c5ce63..3b7a520853f 100644 --- a/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php +++ b/rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php @@ -5,18 +5,17 @@ namespace Rector\CodeQuality\Rector\Ternary; use PhpParser\Node; -use PhpParser\Node\Expr; -use PhpParser\Node\Expr\BooleanNot; use PhpParser\Node\Expr\Ternary; -use Rector\NodeTypeResolver\Node\AttributeKey; +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\Ternary\SwitchNegatedTernaryRector\SwitchNegatedTernaryRectorTest + * @deprecated This rule is deprecated, as it is a personal preference. Swapping the branches keeps the condition positive, but moves the values out of their logical order, e.g. a fallback value ahead of the main one. */ -final class SwitchNegatedTernaryRector extends AbstractRector +final class SwitchNegatedTernaryRector extends AbstractRector implements DeprecatedInterface { public function getRuleDefinition(): RuleDefinition { @@ -65,32 +64,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if (! $node->cond instanceof BooleanNot) { - return null; - } - - if (! $node->if instanceof Expr) { - return null; - } - - $node->cond = $node->cond->expr; - $else = clone $node->else; - $if = clone $node->if; - - $node->else = $if; - $node->if = $else; - - if ($node->if instanceof Ternary) { - $ternary = $node->if; - $ternary->setAttribute(AttributeKey::KIND, AttributeKey::WRAPPED_IN_PARENTHESES); - $ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null); - } - - if ($node->else instanceof Ternary) { - $ternary = $node->else; - $ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null); - } - - return $node; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated, as it is a personal preference that can move ternary values out of their logical order', + self::class + )); } } diff --git a/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php b/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php index 7d30ee12c63..23b6266a1d8 100644 --- a/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php +++ b/rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php @@ -62,11 +62,7 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - /** - * if condition is negated, skip - * switch negated ternary condition early via SwitchNegatedTernaryRector for that - * if needed - */ + // if condition is negated, skip if ($node->cond instanceof BooleanNot) { return null; } diff --git a/src/Config/Level/CodeQualityLevel.php b/src/Config/Level/CodeQualityLevel.php index 3265c4da8d4..6c4bee0d7f6 100644 --- a/src/Config/Level/CodeQualityLevel.php +++ b/src/Config/Level/CodeQualityLevel.php @@ -70,7 +70,6 @@ use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector; use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector; use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector; -use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector; use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector; use Rector\CodeQuality\Rector\Ternary\TernaryImplodeToImplodeRector; use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector; @@ -155,7 +154,6 @@ final class CodeQualityLevel NewStaticToNewSelfRector::class, UnwrapSprintfOneArgumentRector::class, VariableConstFetchToClassConstFetchRector::class, - SwitchNegatedTernaryRector::class, SingularSwitchToIfRector::class, SwitchTrueToMatchRector::class, SimplifyIfNullableReturnRector::class, diff --git a/tests/Issues/Issue6480/Fixture/fixture.php.inc b/tests/Issues/Issue6480/Fixture/fixture.php.inc deleted file mode 100644 index 2cdf0985bdf..00000000000 --- a/tests/Issues/Issue6480/Fixture/fixture.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - diff --git a/tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php b/tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php deleted file mode 100644 index 2d1a402270f..00000000000 --- a/tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php +++ /dev/null @@ -1,31 +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/tests/Issues/Issue6480/Source/ExistingClass.php b/tests/Issues/Issue6480/Source/ExistingClass.php deleted file mode 100644 index 1b1cea4afcd..00000000000 --- a/tests/Issues/Issue6480/Source/ExistingClass.php +++ /dev/null @@ -1,10 +0,0 @@ -withRules([RemoveDeadInstanceOfRector::class, SwitchNegatedTernaryRector::class]);