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