From 11e74f233b8dd0a0b362ea73badd8cfd1f775b2f Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sat, 25 May 2024 23:38:01 +0900 Subject: [PATCH] Remove NoVoidGetterMethodRule as not that practical --- docs/rules_overview.md | 36 +---- src/Rules/NoVoidGetterMethodRule.php | 142 ------------------ .../Fixture/SkipAbstractGetter.php | 10 -- .../Fixture/SkipGetterWithReturn.php | 13 -- .../Fixture/SkipIfElseReturn.php | 17 --- .../Fixture/SkipInterfaceContractGetter.php | 10 -- .../Fixture/SkipNoThrows.php | 16 -- .../Fixture/SkipSetter.php | 12 -- .../Fixture/SkipYieldFrom.php | 15 -- .../Fixture/SkipYielder.php | 15 -- .../Fixture/SomeGetterVoid.php | 12 -- .../Fixture/SomeGetterWithNoReturn.php | 12 -- .../NoVoidGetterMethodRuleTest.php | 51 ------- .../config/configured_rule.neon | 5 - 14 files changed, 1 insertion(+), 365 deletions(-) delete mode 100644 src/Rules/NoVoidGetterMethodRule.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipAbstractGetter.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipGetterWithReturn.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipIfElseReturn.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipInterfaceContractGetter.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipNoThrows.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipSetter.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipYieldFrom.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SkipYielder.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SomeGetterVoid.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/Fixture/SomeGetterWithNoReturn.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/NoVoidGetterMethodRuleTest.php delete mode 100644 tests/Rules/NoVoidGetterMethodRule/config/configured_rule.neon diff --git a/docs/rules_overview.md b/docs/rules_overview.md index 6fd187b7..3d4ef6ed 100644 --- a/docs/rules_overview.md +++ b/docs/rules_overview.md @@ -1,4 +1,4 @@ -# 41 Rules Overview +# 40 Rules Overview ## AnnotateRegexClassConstWithRegexLinkRule @@ -1100,40 +1100,6 @@ final class SkipApiMock extends TestCase
-## NoVoidGetterMethodRule - -Getter method must return something, not void - -- class: [`Symplify\PHPStanRules\Rules\NoVoidGetterMethodRule`](../src/Rules/NoVoidGetterMethodRule.php) - -```php -final class SomeClass -{ - public function getData(): void - { - // ... - } -} -``` - -:x: - -
- -```php -final class SomeClass -{ - public function getData(): array - { - // ... - } -} -``` - -:+1: - -
- ## PreferredClassRule Instead of "%s" class/interface use "%s" diff --git a/src/Rules/NoVoidGetterMethodRule.php b/src/Rules/NoVoidGetterMethodRule.php deleted file mode 100644 index 3510722a..00000000 --- a/src/Rules/NoVoidGetterMethodRule.php +++ /dev/null @@ -1,142 +0,0 @@ -> - */ - private const STOPPING_TYPES = [ - Return_::class, - Yield_::class, - YieldFrom::class, - // possibly unneeded contract override - Throw_::class, - Node\Stmt\Throw_::class, - ]; - - public function __construct( - private readonly TypeAwareNodeFinder $typeAwareNodeFinder - ) { - } - - /** - * @return class-string - */ - public function getNodeType(): string - { - return ClassMethod::class; - } - - /** - * @param ClassMethod $node - * @return string[] - */ - public function processNode(Node $node, Scope $scope): array - { - $classReflection = $scope->getClassReflection(); - if (! $classReflection instanceof ClassReflection) { - return []; - } - - if (! $classReflection->isClass()) { - return []; - } - - if ($node->isAbstract()) { - return []; - } - - if (! str_starts_with($node->name->toString(), 'get')) { - return []; - } - - if (! $this->isVoidReturnClassMethod($node)) { - return []; - } - - return [self::ERROR_MESSAGE]; - } - - public function getRuleDefinition(): RuleDefinition - { - return new RuleDefinition(self::ERROR_MESSAGE, [ - new CodeSample( - <<<'CODE_SAMPLE' -final class SomeClass -{ - public function getData(): void - { - // ... - } -} -CODE_SAMPLE - , - <<<'CODE_SAMPLE' -final class SomeClass -{ - public function getData(): array - { - // ... - } -} -CODE_SAMPLE - ), - ]); - } - - private function isVoidReturnClassMethod(ClassMethod $classMethod): bool - { - if ($this->hasClassMethodVoidReturnType($classMethod)) { - return true; - } - - foreach (self::STOPPING_TYPES as $stoppingType) { - $foundNode = $this->typeAwareNodeFinder->findFirstInstanceOf($classMethod, $stoppingType); - if ($foundNode instanceof Node) { - return false; - } - } - - return true; - } - - private function hasClassMethodVoidReturnType(ClassMethod $classMethod): bool - { - if ($classMethod->returnType === null) { - return false; - } - - if (! $classMethod->returnType instanceof Identifier) { - return false; - } - - return $classMethod->returnType->toString() === 'void'; - } -} diff --git a/tests/Rules/NoVoidGetterMethodRule/Fixture/SkipAbstractGetter.php b/tests/Rules/NoVoidGetterMethodRule/Fixture/SkipAbstractGetter.php deleted file mode 100644 index de011697..00000000 --- a/tests/Rules/NoVoidGetterMethodRule/Fixture/SkipAbstractGetter.php +++ /dev/null @@ -1,10 +0,0 @@ -analyse([$filePath], $expectedErrorsWithLines); - } - - public static function provideData(): Iterator - { - yield [__DIR__ . '/Fixture/SomeGetterVoid.php', [[NoVoidGetterMethodRule::ERROR_MESSAGE, 9]]]; - yield [__DIR__ . '/Fixture/SomeGetterWithNoReturn.php', [[NoVoidGetterMethodRule::ERROR_MESSAGE, 9]]]; - - yield [__DIR__ . '/Fixture/SkipAbstractGetter.php', []]; - yield [__DIR__ . '/Fixture/SkipIfElseReturn.php', []]; - yield [__DIR__ . '/Fixture/SkipGetterWithReturn.php', []]; - yield [__DIR__ . '/Fixture/SkipSetter.php', []]; - yield [__DIR__ . '/Fixture/SkipYielder.php', []]; - yield [__DIR__ . '/Fixture/SkipYieldFrom.php', []]; - yield [__DIR__ . '/Fixture/SkipInterfaceContractGetter.php', []]; - yield [__DIR__ . '/Fixture/SkipNoThrows.php', []]; - } - - /** - * @return string[] - */ - public static function getAdditionalConfigFiles(): array - { - return [__DIR__ . '/config/configured_rule.neon']; - } - - protected function getRule(): Rule - { - return self::getContainer()->getByType(NoVoidGetterMethodRule::class); - } -} diff --git a/tests/Rules/NoVoidGetterMethodRule/config/configured_rule.neon b/tests/Rules/NoVoidGetterMethodRule/config/configured_rule.neon deleted file mode 100644 index 2d48f314..00000000 --- a/tests/Rules/NoVoidGetterMethodRule/config/configured_rule.neon +++ /dev/null @@ -1,5 +0,0 @@ -includes: - - ../../../config/included_services.neon - -rules: - - Symplify\PHPStanRules\Rules\NoVoidGetterMethodRule