Skip to content

Commit 1b106d2

Browse files
authored
[CodeQuality] Deprecate SwitchNegatedTernaryRector (#8283)
1 parent 40943e4 commit 1b106d2

13 files changed

Lines changed: 9 additions & 262 deletions

File tree

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/fixture.php.inc‎

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

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/nested_ternary.php.inc‎

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

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_elvis.php.inc‎

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

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/Fixture/skip_truthy.php.inc‎

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

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/SwitchNegatedTernaryRectorTest.php‎

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

‎rules-tests/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector/config/configured_rule.php‎

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

‎rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php‎

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

77
use PhpParser\Node;
8-
use PhpParser\Node\Expr;
9-
use PhpParser\Node\Expr\BooleanNot;
108
use PhpParser\Node\Expr\Ternary;
11-
use Rector\NodeTypeResolver\Node\AttributeKey;
9+
use Rector\Configuration\Deprecation\Contract\DeprecatedInterface;
10+
use Rector\Exception\ShouldNotHappenException;
1211
use Rector\Rector\AbstractRector;
1312
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
1413
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
1514

1615
/**
17-
* @see \Rector\Tests\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector\SwitchNegatedTernaryRectorTest
16+
* @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.
1817
*/
19-
final class SwitchNegatedTernaryRector extends AbstractRector
18+
final class SwitchNegatedTernaryRector extends AbstractRector implements DeprecatedInterface
2019
{
2120
public function getRuleDefinition(): RuleDefinition
2221
{
@@ -65,32 +64,9 @@ public function getNodeTypes(): array
6564
*/
6665
public function refactor(Node $node): ?Node
6766
{
68-
if (! $node->cond instanceof BooleanNot) {
69-
return null;
70-
}
71-
72-
if (! $node->if instanceof Expr) {
73-
return null;
74-
}
75-
76-
$node->cond = $node->cond->expr;
77-
$else = clone $node->else;
78-
$if = clone $node->if;
79-
80-
$node->else = $if;
81-
$node->if = $else;
82-
83-
if ($node->if instanceof Ternary) {
84-
$ternary = $node->if;
85-
$ternary->setAttribute(AttributeKey::KIND, AttributeKey::WRAPPED_IN_PARENTHESES);
86-
$ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null);
87-
}
88-
89-
if ($node->else instanceof Ternary) {
90-
$ternary = $node->else;
91-
$ternary->setAttribute(AttributeKey::ORIGINAL_NODE, null);
92-
}
93-
94-
return $node;
67+
throw new ShouldNotHappenException(sprintf(
68+
'"%s" rule is deprecated, as it is a personal preference that can move ternary values out of their logical order',
69+
self::class
70+
));
9571
}
9672
}

‎rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,7 @@ public function getNodeTypes(): array
6262
*/
6363
public function refactor(Node $node): ?Node
6464
{
65-
/**
66-
* if condition is negated, skip
67-
* switch negated ternary condition early via SwitchNegatedTernaryRector for that
68-
* if needed
69-
*/
65+
// if condition is negated, skip
7066
if ($node->cond instanceof BooleanNot) {
7167
return null;
7268
}

‎src/Config/Level/CodeQualityLevel.php‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
7171
use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector;
7272
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
73-
use Rector\CodeQuality\Rector\Ternary\SwitchNegatedTernaryRector;
7473
use Rector\CodeQuality\Rector\Ternary\TernaryEmptyArrayArrayDimFetchToCoalesceRector;
7574
use Rector\CodeQuality\Rector\Ternary\TernaryImplodeToImplodeRector;
7675
use Rector\CodeQuality\Rector\Ternary\UnnecessaryTernaryExpressionRector;
@@ -155,7 +154,6 @@ final class CodeQualityLevel
155154
NewStaticToNewSelfRector::class,
156155
UnwrapSprintfOneArgumentRector::class,
157156
VariableConstFetchToClassConstFetchRector::class,
158-
SwitchNegatedTernaryRector::class,
159157
SingularSwitchToIfRector::class,
160158
SwitchTrueToMatchRector::class,
161159
SimplifyIfNullableReturnRector::class,

‎tests/Issues/Issue6480/Fixture/fixture.php.inc‎

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

0 commit comments

Comments
 (0)