-
-
Notifications
You must be signed in to change notification settings - Fork 431
[coding-style] Add NestedTernaryToMatchRector #7572
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93a1109
[coding-style] Add NestedTernaryToMatchTrueRector
TomasVotruba c5a1bb8
fixes
TomasVotruba 3668f10
add identical support
TomasVotruba 927bba4
rename to NestedTernaryToMatchRector
TomasVotruba 9ae7e91
add fixture with different identical varible
TomasVotruba e57a74f
[ci-review] Rector Rectify
actions-user File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
...ts/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/negated_identical.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NegatedIdentical | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = $value !== 10 ? 'equal to 10' : ($value !== 5 ? 'equal to 5' : 'other value'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NegatedIdentical | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = match (true) { | ||
| $value !== 10 => 'equal to 10', | ||
| $value !== 5 => 'equal to 5', | ||
| default => 'other value', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryToMatchRector | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = $value > 10 ? 'greater than 10' : ($value === 10 ? 'equal to 10' : 'less than 10'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryToMatchRector | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = match (true) { | ||
| $value > 10 => 'greater than 10', | ||
| $value === 10 => 'equal to 10', | ||
| default => 'less than 10', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...le/Rector/Assign/NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryWithIdentical | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = $value === 10 ? 'equal to 10' : ($value === 5 ? 'equal to 5' : 'other value'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryWithIdentical | ||
| { | ||
| public function run($value) | ||
| { | ||
| $result = match ($value) { | ||
| 10 => 'equal to 10', | ||
| 5 => 'equal to 5', | ||
| default => 'other value', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| ?> |
31 changes: 31 additions & 0 deletions
31
...NestedTernaryToMatchRector/Fixture/nested_ternary_with_identical_different_values.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryWithIdenticalDifferentValues | ||
| { | ||
| public function run($value, $nextValue) | ||
| { | ||
| $result = $value === 10 ? 'equal to 10' : ($nextValue === 5 ? 'equal to 5' : 'other value'); | ||
| } | ||
| } | ||
|
|
||
| ?> | ||
| ----- | ||
| <?php | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class NestedTernaryWithIdenticalDifferentValues | ||
| { | ||
| public function run($value, $nextValue) | ||
| { | ||
| $result = match (true) { | ||
| $value === 10 => 'equal to 10', | ||
| $nextValue === 5 => 'equal to 5', | ||
| default => 'other value', | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| ?> |
15 changes: 15 additions & 0 deletions
15
...dingStyle/Rector/Assign/NestedTernaryToMatchRector/Fixture/skip_silent_conditions.php.inc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\Fixture; | ||
|
|
||
| final class SkipSilentConditions | ||
| { | ||
| public function run($value) | ||
| { | ||
| $value = 5; | ||
|
|
||
| $result = $value > 10 ? 'greater than 10' : ($value === 10 ?: 'less than 10'); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
...s/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/NestedTernaryToMatchRectorTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector; | ||
|
|
||
| use Iterator; | ||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
|
||
| final class NestedTernaryToMatchRectorTest extends AbstractRectorTestCase | ||
| { | ||
| #[DataProvider('provideData')] | ||
| public function test(string $filePath): void | ||
| { | ||
| $this->doTestFile($filePath); | ||
| } | ||
|
|
||
| public static function provideData(): Iterator | ||
| { | ||
| return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
| } | ||
|
|
||
| public function provideConfigFilePath(): string | ||
| { | ||
| return __DIR__ . '/config/configured_rule.php'; | ||
| } | ||
| } |
9 changes: 9 additions & 0 deletions
9
rules-tests/CodingStyle/Rector/Assign/NestedTernaryToMatchRector/config/configured_rule.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use Rector\CodingStyle\Rector\Assign\NestedTernaryToMatchRector; | ||
| use Rector\Config\RectorConfig; | ||
|
|
||
| return RectorConfig::configure() | ||
| ->withRules([NestedTernaryToMatchRector::class]); |
161 changes: 161 additions & 0 deletions
161
rules/CodingStyle/Rector/Assign/NestedTernaryToMatchRector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| 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\Rector\AbstractRector; | ||
| use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; | ||
| use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
|
|
||
| /** | ||
| * @see \Rector\Tests\CodingStyle\Rector\Assign\NestedTernaryToMatchRector\NestedTernaryToMatchRectorTest | ||
| */ | ||
| final class NestedTernaryToMatchRector extends AbstractRector | ||
| { | ||
| public function getRuleDefinition(): RuleDefinition | ||
| { | ||
| return new RuleDefinition('Convert nested ternary expressions to match(true) statements', [ | ||
| new CodeSample( | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function getValue($input) | ||
| { | ||
| return $input > 100 ? 'more than 100' : ($input > 5 ? 'more than 5' : 'less'); | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| , | ||
| <<<'CODE_SAMPLE' | ||
| class SomeClass | ||
| { | ||
| public function getValue($input) | ||
| { | ||
| return match (true) { | ||
| $input > 100 => 'more than 100', | ||
| $input > 5 => 'more than 5', | ||
| default => 'less', | ||
| }; | ||
| } | ||
| } | ||
| CODE_SAMPLE | ||
| ), | ||
|
|
||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return array<class-string<Node>> | ||
| */ | ||
| public function getNodeTypes(): array | ||
| { | ||
| return [Assign::class]; | ||
| } | ||
|
|
||
| /** | ||
| * @param Assign $node | ||
| */ | ||
| 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; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.