diff --git a/src/Rules/ThrowsPhpDocRule.php b/src/Rules/ThrowsPhpDocRule.php index d61b023..84926d7 100644 --- a/src/Rules/ThrowsPhpDocRule.php +++ b/src/Rules/ThrowsPhpDocRule.php @@ -465,7 +465,11 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array return []; } - $classReflection = $scope->getClassReflection(); + $classReflection = $scope->getTraitReflection(); + if ($classReflection === null) { + $classReflection = $scope->getClassReflection(); + } + if ($classReflection === null) { try { $methodReflection = $this->broker->getFunction(new Name($node->name->toString()), $scope); @@ -489,7 +493,7 @@ private function processFunction(Node\FunctionLike $node, Scope $scope): array if (!$node->hasAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END)) { $node->setAttribute(self::ATTRIBUTE_HAS_CLASS_METHOD_END, true); if ($node->stmts === null) { - $node->stmts = []; + throw new ShouldNotHappenException(); } $node->stmts[] = new FunctionEnd($node); } diff --git a/tests/src/Rules/Bug113Test.php b/tests/src/Rules/Bug113Test.php new file mode 100644 index 0000000..7238497 --- /dev/null +++ b/tests/src/Rules/Bug113Test.php @@ -0,0 +1,61 @@ + + */ +class Bug113Test extends RuleTestCase +{ + + protected function getRule(): Rule + { + $defaultThrowTypeService = new DefaultThrowTypeService([], []); + + $extensions = [ + new DynamicExtension(), + new DefaultThrowTypeExtension($defaultThrowTypeService), + ]; + + return new ThrowsPhpDocRule( + new CheckedExceptionService( + [ + RuntimeException::class, + CheckedException::class, + ReflectionException::class, + ] + ), + new DynamicThrowTypeService( + $extensions, + $extensions, + $extensions, + $extensions + ), + $defaultThrowTypeService, + $this->createThrowsAnnotationReader(), + $this->createBroker(), + false, + true, + false, + false, + [] + ); + } + + public function testBasicThrows(): void + { + $this->analyse(__DIR__ . '/data/bug113.php'); + } + +} diff --git a/tests/src/Rules/data/bug113.php b/tests/src/Rules/data/bug113.php new file mode 100644 index 0000000..466061f --- /dev/null +++ b/tests/src/Rules/data/bug113.php @@ -0,0 +1,22 @@ +