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.

This file was deleted.

77 changes: 8 additions & 69 deletions rules/CodeQuality/Rector/Switch_/SwitchTrueToMatchRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,18 @@
namespace Rector\CodeQuality\Rector\Switch_;

use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Match_;
use PhpParser\Node\MatchArm;
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 Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;

/**
* @see \Rector\Tests\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector\SwitchTrueToMatchRectorTest
* @deprecated This rule is deprecated, as both `switch (true)` and `match (true)` are hacky constructions; a constant list or explicit mapping is clearer and the change needs a human check.
*/
final class SwitchTrueToMatchRector extends AbstractRector implements MinPhpVersionInterface
final class SwitchTrueToMatchRector extends AbstractRector implements DeprecatedInterface
{
public function __construct(
private readonly ValueResolver $valueResolver,
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change `switch (true)` of returning cases to `match (true)` expression', [
Expand Down Expand Up @@ -79,60 +69,9 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if (! $this->valueResolver->isTrue($node->cond)) {
return null;
}

if ($node->cases === []) {
return null;
}

$matchArms = [];
$hasDefault = false;

$lastCaseKey = array_key_last($node->cases);

foreach ($node->cases as $key => $case) {
// a match arm can only carry a single return expression
if (count($case->stmts) !== 1) {
return null;
}

$onlyStmt = $case->stmts[0];
if (! $onlyStmt instanceof Return_) {
return null;
}

if (! $onlyStmt->expr instanceof Expr) {
return null;
}

if ($case->cond instanceof Expr) {
$matchArms[] = new MatchArm([$case->cond], $onlyStmt->expr);
continue;
}

// default case must be the last one, as a match default arm catches everything
if ($key !== $lastCaseKey) {
return null;
}

$hasDefault = true;
$matchArms[] = new MatchArm(null, $onlyStmt->expr);
}

// require a default arm, otherwise match (true) throws UnhandledMatchError where switch (true) falls through
if (! $hasDefault) {
return null;
}

$match = new Match_($this->nodeFactory->createTrue(), $matchArms);

return new Return_($match);
}

public function provideMinPhpVersion(): int
{
return PhpVersionFeature::MATCH_EXPRESSION;
throw new ShouldNotHappenException(sprintf(
'"%s" rule is deprecated, as both switch (true) and match (true) are hacky constructions that need a human check',
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 @@ -60,7 +60,6 @@
use Rector\CodeQuality\Rector\NullsafeMethodCall\CleanupUnneededNullsafeOperatorRector;
use Rector\CodeQuality\Rector\Property\FixClassCaseSensitivityVarDocblockRector;
use Rector\CodeQuality\Rector\Switch_\SingularSwitchToIfRector;
use Rector\CodeQuality\Rector\Switch_\SwitchTrueToMatchRector;
use Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector;
use Rector\CodeQuality\Rector\Ternary\NumberCompareToMaxFuncCallRector;
use Rector\CodeQuality\Rector\Ternary\SimplifyTautologyTernaryRector;
Expand Down Expand Up @@ -149,7 +148,6 @@ final class CodeQualityLevel
SingularSwitchToIfRector::class,
InlineIfToExplicitIfRector::class,
TernaryFalseExpressionToIfRector::class,
SwitchTrueToMatchRector::class,
SimplifyIfNullableReturnRector::class,
FlipTypeControlToUseExclusiveTypeRector::class,
InlineArrayReturnAssignRector::class,
Expand Down
Loading