-
Notifications
You must be signed in to change notification settings - Fork 576
add a new rule to detect duplicate switch cases #5849
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
Open
canvural
wants to merge
2
commits into
phpstan:2.2.x
Choose a base branch
from
canvural:duplicate-switch-cases
base: 2.2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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
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
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
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,83 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Comparison; | ||
|
|
||
| use PhpParser\Node; | ||
| use PhpParser\Node\Stmt\Switch_; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\Node\Printer\ExprPrinter; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Rules\RuleErrorBuilder; | ||
| use function count; | ||
| use function sprintf; | ||
|
|
||
| /** | ||
| * @implements Rule<Switch_> | ||
| */ | ||
| final class DuplicateCasesInSwitchRule implements Rule | ||
| { | ||
|
|
||
| public function __construct(private ExprPrinter $exprPrinter) | ||
| { | ||
| } | ||
|
|
||
| public function getNodeType(): string | ||
| { | ||
| return Switch_::class; | ||
| } | ||
|
|
||
| public function processNode(Node $node, Scope $scope): array | ||
| { | ||
| $errors = []; | ||
| $seenCases = []; | ||
|
|
||
| foreach ($node->cases as $case) { | ||
| if ($case->cond === null) { | ||
| continue; | ||
| } | ||
|
|
||
| $condType = $scope->getType($case->cond); | ||
| $scalarValues = $condType->getConstantScalarValues(); | ||
|
|
||
| if (count($scalarValues) === 1) { | ||
| $key = ['scalar', $scalarValues[0]]; | ||
| } else { | ||
| $enumCases = $condType->getEnumCases(); | ||
|
|
||
| if (count($enumCases) !== 1) { | ||
| continue; | ||
| } | ||
|
|
||
| $key = ['enum', $enumCases[0]->getClassName(), $enumCases[0]->getEnumCaseName()]; | ||
| } | ||
|
|
||
| $firstSeen = null; | ||
|
|
||
| foreach ($seenCases as $seenCase) { | ||
| if ($seenCase['key'] === $key) { | ||
| $firstSeen = $seenCase; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if ($firstSeen === null) { | ||
| $seenCases[] = [ | ||
| 'key' => $key, | ||
| 'printed' => $this->exprPrinter->printExpr($case->cond), | ||
| 'line' => $case->cond->getStartLine(), | ||
| ]; | ||
| continue; | ||
| } | ||
|
|
||
| $errors[] = RuleErrorBuilder::message(sprintf( | ||
| 'Case %s in switch is a duplicate of case %s on line %d.', | ||
| $this->exprPrinter->printExpr($case->cond), | ||
| $firstSeen['printed'], | ||
| $firstSeen['line'], | ||
| ))->identifier('switch.duplicateCase')->line($case->getStartLine())->build(); | ||
| } | ||
|
|
||
| return $errors; | ||
| } | ||
|
|
||
| } | ||
85 changes: 85 additions & 0 deletions
85
tests/PHPStan/Rules/Comparison/DuplicateCasesInSwitchRuleTest.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,85 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace PHPStan\Rules\Comparison; | ||
|
|
||
| use PHPStan\Node\Printer\ExprPrinter; | ||
| use PHPStan\Node\Printer\Printer; | ||
| use PHPStan\Rules\Rule; | ||
| use PHPStan\Testing\RuleTestCase; | ||
| use function define; | ||
| use function defined; | ||
|
|
||
| /** | ||
| * @extends RuleTestCase<DuplicateCasesInSwitchRule> | ||
| */ | ||
| class DuplicateCasesInSwitchRuleTest extends RuleTestCase | ||
| { | ||
|
|
||
| protected function getRule(): Rule | ||
| { | ||
| return new DuplicateCasesInSwitchRule( | ||
| new ExprPrinter(new Printer()), | ||
| ); | ||
| } | ||
|
|
||
| public function testDuplicateCases(): void | ||
| { | ||
| if (!defined('DUPLICATE_SWITCH_CASE_CONST')) { | ||
| define('DUPLICATE_SWITCH_CASE_CONST', 'unknown'); | ||
| } | ||
|
|
||
| $this->analyse([__DIR__ . '/data/duplicate-cases-in-switch.php'], [ | ||
| [ | ||
| 'Case \'lb\' in switch is a duplicate of case \'lb\' on line 24.', | ||
| 30, | ||
| ], | ||
| [ | ||
| 'Case \'oz\' in switch is a duplicate of case \'oz\' on line 27.', | ||
| 33, | ||
| ], | ||
| [ | ||
| 'Case 1 in switch is a duplicate of case 1 on line 42.', | ||
| 46, | ||
| ], | ||
| [ | ||
| 'Case \'x\' in switch is a duplicate of case \'x\' on line 54.', | ||
| 58, | ||
| ], | ||
| [ | ||
| 'Case \'x\' in switch is a duplicate of case \'x\' on line 54.', | ||
| 60, | ||
| ], | ||
| [ | ||
| 'Case self::EQ in switch is a duplicate of case \'=\' on line 68.', | ||
| 72, | ||
| ], | ||
| [ | ||
| 'Case DUPLICATE_SWITCH_CASE_CONST in switch is a duplicate of case \'unknown\' on line 80.', | ||
| 82, | ||
| ], | ||
| [ | ||
| 'Case \'a\' in switch is a duplicate of case \'a\' on line 90.', | ||
| 94, | ||
| ], | ||
| [ | ||
| 'Case true in switch is a duplicate of case true on line 103.', | ||
| 107, | ||
| ], | ||
| [ | ||
| 'Case null in switch is a duplicate of case null on line 105.', | ||
| 109, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| public function testDuplicateCasesEnum(): void | ||
| { | ||
| $this->analyse([__DIR__ . '/data/duplicate-cases-in-switch-enum.php'], [ | ||
| [ | ||
| 'Case \DuplicateCasesInSwitchEnum\Status::Active in switch is a duplicate of case \DuplicateCasesInSwitchEnum\Status::Active on line 20.', | ||
| 24, | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| } |
55 changes: 55 additions & 0 deletions
55
tests/PHPStan/Rules/Comparison/data/duplicate-cases-in-switch-enum.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,55 @@ | ||
| <?php // lint >= 8.1 | ||
|
|
||
| namespace DuplicateCasesInSwitchEnum; | ||
|
|
||
| enum Status: string | ||
| { | ||
|
|
||
| case Active = 'active'; | ||
| case Inactive = 'inactive'; | ||
| case Pending = 'pending'; | ||
|
|
||
| } | ||
|
|
||
| class Foo | ||
| { | ||
|
|
||
| public function doFoo(Status $status): void | ||
| { | ||
| switch ($status) { | ||
|
staabm marked this conversation as resolved.
|
||
| case Status::Active: | ||
| break; | ||
| case Status::Inactive: | ||
| break; | ||
| case Status::Active: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public function noDuplicates(Status $status): void | ||
| { | ||
| switch ($status) { | ||
| case Status::Active: | ||
| break; | ||
| case Status::Inactive: | ||
| break; | ||
| case Status::Pending: | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| public function backedEnumCaseIsNotADuplicateOfItsBackingValue(Status|string $value): void | ||
| { | ||
| switch ($value) { | ||
| case 'active': | ||
| break; | ||
| case Status::Active: | ||
| break; | ||
| case Status::Inactive: | ||
| break; | ||
| case 'inactive': | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expr printing can be expensive. maybe only remember the
$case->condand print the condition only when building the error?