Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

40 changes: 8 additions & 32 deletions rules/CodeQuality/Rector/Ternary/SwitchNegatedTernaryRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
));
}
}
6 changes: 1 addition & 5 deletions rules/DeadCode/Rector/Ternary/RemoveUselessTernaryRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -155,7 +154,6 @@ final class CodeQualityLevel
NewStaticToNewSelfRector::class,
UnwrapSprintfOneArgumentRector::class,
VariableConstFetchToClassConstFetchRector::class,
SwitchNegatedTernaryRector::class,
SingularSwitchToIfRector::class,
SwitchTrueToMatchRector::class,
SimplifyIfNullableReturnRector::class,
Expand Down
31 changes: 0 additions & 31 deletions tests/Issues/Issue6480/Fixture/fixture.php.inc

This file was deleted.

31 changes: 0 additions & 31 deletions tests/Issues/Issue6480/NegatedTernaryInsideDeadInstanceTest.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/Issues/Issue6480/Source/ExistingClass.php

This file was deleted.

10 changes: 0 additions & 10 deletions tests/Issues/Issue6480/config/configured_rule.php

This file was deleted.

Loading