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
1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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
-
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

57 changes: 9 additions & 48 deletions rules/CodeQuality/Rector/Switch_/SwitchTrueToIfRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -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
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/CodeQualityLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -181,7 +180,6 @@ final class CodeQualityLevel
TernaryEmptyArrayArrayDimFetchToCoalesceRector::class,
OptionalParametersAfterRequiredRector::class,
SimplifyEmptyCheckOnEmptyArrayRector::class,
SwitchTrueToIfRector::class,
CleanupUnneededNullsafeOperatorRector::class,
DisallowedEmptyRuleFixerRector::class,
LocallyCalledStaticMethodToNonStaticRector::class,
Expand Down
Loading