diff --git a/docs/rules_overview.md b/docs/rules_overview.md index 0f9432ac..d1648c7b 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -1,4 +1,4 @@ -# 60 Rules Overview +# 59 Rules Overview ## AnnotateRegexClassConstWithRegexLinkRule @@ -2182,39 +2182,6 @@ class SomeClass extends Rule
-## SwitchToMatchSpotterRule - -Switch construction can be replace with more robust `match()` - -- class: [`Symplify\PHPStanRules\Rules\Spotter\SwitchToMatchSpotterRule`](../src/Rules/Spotter/SwitchToMatchSpotterRule.php) - -```php -switch ($key) { - case 1: - return 100; - case 2: - return 200; - default: - return 300; -}; -``` - -:x: - -
- -```php -return match($key) { - 1 => 100, - 2 => 200, - default => 300, -}; -``` - -:+1: - -
- ## TwigPublicCallableExistsRule The callable method [$this, "%s"] was not found diff --git a/src/Rules/Spotter/SwitchToMatchSpotterRule.php b/src/Rules/Spotter/SwitchToMatchSpotterRule.php deleted file mode 100644 index e13babc0..00000000 --- a/src/Rules/Spotter/SwitchToMatchSpotterRule.php +++ /dev/null @@ -1,123 +0,0 @@ - - */ - public function getNodeType(): string - { - return Switch_::class; - } - - /** - * @param Switch_ $node - * @return string[] - */ - public function processNode(Node $node, Scope $scope): array - { - if (! $this->hasDefaultCase($node)) { - return []; - } - - if (! $this->isMatchingSwitch($node)) { - return []; - } - - return [self::ERROR_MESSAGE]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new CodeSample( - <<<'CODE_SAMPLE' -switch ($key) { - case 1: - return 100; - case 2: - return 200; - default: - return 300; -}; -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -return match($key) { - 1 => 100, - 2 => 200, - default => 300, -}; -CODE_SAMPLE - ), - ]); - } - - private function hasDefaultCase(Switch_ $switch): bool - { - foreach ($switch->cases as $case) { - if (! $case->cond instanceof Expr) { - return true; - } - } - - return false; - } - - private function isMatchingSwitch(Switch_ $switch): bool - { - foreach ($switch->cases as $case) { - if (! $case->cond instanceof Expr) { - continue; - } - - // no stmts, merged with another case - if ($case->stmts === []) { - continue; - } - - // must be exact 1 stmts - if (count($case->stmts) !== 1) { - continue; - } - - $onlyStmt = $case->stmts[0]; - if ($onlyStmt instanceof Return_ && $onlyStmt->expr instanceof Expr) { - continue; - } - - if ($onlyStmt instanceof Throw_) { - continue; - } - - return false; - } - - return true; - } -} diff --git a/tests/Rules/Spotter/SwitchToMatchSpotterRule/Fixture/ReturnAndException.php b/tests/Rules/Spotter/SwitchToMatchSpotterRule/Fixture/ReturnAndException.php deleted file mode 100644 index 3bf38fcf..00000000 --- a/tests/Rules/Spotter/SwitchToMatchSpotterRule/Fixture/ReturnAndException.php +++ /dev/null @@ -1,18 +0,0 @@ -analyse([$filePath], $expectedErrorMessagesWithLines); - } - - public static function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/SkipNoDefault.php', []]; - yield [__DIR__ . '/Fixture/SimpleSwitch.php', [[SwitchToMatchSpotterRule::ERROR_MESSAGE, 11]]]; - yield [__DIR__ . '/Fixture/ReturnAndException.php', [[SwitchToMatchSpotterRule::ERROR_MESSAGE, 11]]]; - } - - /** - * @return string[] - */ - public static function getAdditionalConfigFiles(): array - { - return [__DIR__ . '/config/configured_rule.neon']; - } - - protected function getRule(): Rule - { - return self::getContainer()->getByType(SwitchToMatchSpotterRule::class); - } -} diff --git a/tests/Rules/Spotter/SwitchToMatchSpotterRule/config/configured_rule.neon b/tests/Rules/Spotter/SwitchToMatchSpotterRule/config/configured_rule.neon deleted file mode 100644 index e48eec5d..00000000 --- a/tests/Rules/Spotter/SwitchToMatchSpotterRule/config/configured_rule.neon +++ /dev/null @@ -1,5 +0,0 @@ -includes: - - ../../../../config/included_services.neon - -rules: - - Symplify\PHPStanRules\Rules\Spotter\SwitchToMatchSpotterRule