diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/negated_identical.php.inc b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/negated_identical.php.inc deleted file mode 100644 index 1a54b23ec21..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/negated_identical.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - 'equal to 10', - $value !== 5 => 'equal to 5', - default => 'other value', - }; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary.php.inc b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary.php.inc deleted file mode 100644 index afd96295397..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - 10 ? 'greater than 10' : ($value === 10 ? 'equal to 10' : 'less than 10'); - } -} - -?> ------ - 10 => 'greater than 10', - $value === 10 => 'equal to 10', - default => 'less than 10', - }; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical.php.inc b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical.php.inc deleted file mode 100644 index 4bf88c20e59..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - 'equal to 10', - 5 => 'equal to 5', - default => 'other value', - }; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical_different_values.php.inc b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical_different_values.php.inc deleted file mode 100644 index 7c0eb13788a..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical_different_values.php.inc +++ /dev/null @@ -1,31 +0,0 @@ - ------ - 'equal to 10', - $nextValue === 5 => 'equal to 5', - default => 'other value', - }; - } -} - -?> diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/skip_silent_conditions.php.inc b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/skip_silent_conditions.php.inc deleted file mode 100644 index 3c88aefc254..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/skip_silent_conditions.php.inc +++ /dev/null @@ -1,15 +0,0 @@ - 10 ? 'greater than 10' : ($value === 10 ?: 'less than 10'); - } -} diff --git a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/NestedTernaryToMatchRectorTest.php b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/NestedTernaryToMatchRectorTest.php deleted file mode 100644 index dd6a3af3fa1..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/NestedTernaryToMatchRectorTest.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/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/config/configured_rule.php b/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/config/configured_rule.php deleted file mode 100644 index 9e294c27119..00000000000 --- a/rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([NestedTernaryToMatchRector::class]); diff --git a/rules/CodingStyle/Rector/Assign/NestedTernaryToMatchRector.php b/rules/CodingStyle/Rector/Assign/NestedTernaryToMatchRector.php index dc73567fbd7..1adcac3f394 100644 --- a/rules/CodingStyle/Rector/Assign/NestedTernaryToMatchRector.php +++ b/rules/CodingStyle/Rector/Assign/NestedTernaryToMatchRector.php @@ -5,21 +5,17 @@ namespace Rector\CodingStyle\Rector\Assign; use PhpParser\Node; -use PhpParser\Node\Expr; use PhpParser\Node\Expr\Assign; -use PhpParser\Node\Expr\Match_; -use PhpParser\Node\Expr\Ternary; -use PhpParser\Node\Expr\Variable; -use PhpParser\Node\MatchArm; -use Rector\CodingStyle\ValueObject\ConditionAndResult; +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\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\NestedTernaryToMatchRectorTest + * @deprecated This rule is deprecated, as match(true) with nested conditions is often less readable and more confusing than the original ternary. Refactor to an explicit intent instead, e.g. early returns or a named method. */ -final class NestedTernaryToMatchRector extends AbstractRector +final class NestedTernaryToMatchRector extends AbstractRector implements DeprecatedInterface { public function getRuleDefinition(): RuleDefinition { @@ -66,96 +62,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Assign { - if (! $node->expr instanceof Ternary) { - return null; - } - - $ternary = $node->expr; - - // traverse nested ternaries to collect them all - $currentTernary = $ternary; - - /** @var ConditionAndResult[] $conditionsAndResults */ - $conditionsAndResults = []; - $defaultExpr = null; - - while ($currentTernary instanceof Ternary) { - if (! $currentTernary->if instanceof Expr) { - // short ternary, skip - return null; - } - - $conditionsAndResults[] = new ConditionAndResult($currentTernary->cond, $currentTernary->if); - - $currentTernary = $currentTernary->else; - - if (! $currentTernary instanceof Ternary) { - $defaultExpr = $currentTernary; - } - } - - // nothing long enough - if (count($conditionsAndResults) < 2 || ! $defaultExpr instanceof Expr) { - return null; - } - - $match = $this->createMatch($conditionsAndResults, $defaultExpr); - $node->expr = $match; - - return $node; - } - - /** - * @param ConditionAndResult[] $conditionsAndResults - */ - private function createMatch(array $conditionsAndResults, Expr $defaultExpr): Match_ - { - $singleVariableName = $this->matchAlwaysIdenticalVariableName($conditionsAndResults); - if (is_string($singleVariableName)) { - $isVariableIdentical = true; - $match = new Match_(new Variable($singleVariableName)); - } else { - $isVariableIdentical = false; - $match = new Match_($this->nodeFactory->createTrue()); - } - - foreach ($conditionsAndResults as $conditionAndResult) { - $match->arms[] = new MatchArm([ - $isVariableIdentical ? $conditionAndResult->getIdenticalExpr() : $conditionAndResult->getConditionExpr(), - ], $conditionAndResult->getResultExpr()); - } - - $match->arms[] = new MatchArm(null, $defaultExpr); - - return $match; - } - - /** - * @param ConditionAndResult[] $conditionsAndResults - */ - private function matchAlwaysIdenticalVariableName(array $conditionsAndResults): mixed - { - $identicalVariableNames = []; - foreach ($conditionsAndResults as $conditionAndResult) { - if (! $conditionAndResult->isIdenticalCompare()) { - return null; - } - - $variableName = $conditionAndResult->getIdenticalVariableName(); - if (! is_string($variableName)) { - return null; - } - - $identicalVariableNames[] = $variableName; - } - - $uniqueIdenticalVariableNames = array_unique($identicalVariableNames); - $uniqueIdenticalVariableNames = array_values($uniqueIdenticalVariableNames); - - if (count($uniqueIdenticalVariableNames) === 1) { - return $uniqueIdenticalVariableNames[0]; - } - - return null; + throw new ShouldNotHappenException(sprintf( + '"%s" is deprecated, as match(true) with nested conditions is often less readable than the original ternary', + self::class + )); } } diff --git a/rules/CodingStyle/ValueObject/ConditionAndResult.php b/rules/CodingStyle/ValueObject/ConditionAndResult.php deleted file mode 100644 index d15655e6c19..00000000000 --- a/rules/CodingStyle/ValueObject/ConditionAndResult.php +++ /dev/null @@ -1,64 +0,0 @@ -conditionExpr; - } - - public function isIdenticalCompare(): bool - { - return $this->conditionExpr instanceof Identical; - } - - public function getIdenticalVariableName(): ?string - { - $identical = $this->getConditionIdentical(); - if (! $identical->left instanceof Variable) { - return null; - } - - $variable = $identical->left; - if ($variable->name instanceof Expr) { - return null; - } - - return $variable->name; - } - - public function getResultExpr(): Expr - { - return $this->resultExpr; - } - - public function getIdenticalExpr(): Expr - { - /** @var Identical $identical */ - $identical = $this->conditionExpr; - - return $identical->right; - } - - private function getConditionIdentical(): Identical - { - Assert::isInstanceOf($this->conditionExpr, Identical::class); - - return $this->conditionExpr; - } -}