diff --git a/src/Rules/Playground/MethodNeverRule.php b/src/Rules/Playground/MethodNeverRule.php index fdef4e9396..342f009d67 100644 --- a/src/Rules/Playground/MethodNeverRule.php +++ b/src/Rules/Playground/MethodNeverRule.php @@ -33,6 +33,10 @@ public function processNode(Node $node, Scope $scope): array $method = $node->getMethodReflection(); + if (!$method->isPrivate() && !$node->getClassReflection()->isFinal()) { + return []; + } + $returnType = $method->getReturnType(); $helperResult = $this->helper->shouldReturnNever($node, $returnType); if ($helperResult === false) { diff --git a/tests/PHPStan/Rules/Playground/MethodNeverRuleTest.php b/tests/PHPStan/Rules/Playground/MethodNeverRuleTest.php index 35b3d20463..4bdf930786 100644 --- a/tests/PHPStan/Rules/Playground/MethodNeverRuleTest.php +++ b/tests/PHPStan/Rules/Playground/MethodNeverRuleTest.php @@ -36,4 +36,18 @@ public function testRule(): void ]); } + public function testBug10820(): void + { + $this->analyse([__DIR__ . '/data/bug-10820.php'], [ + [ + 'Method Bug10820\NonFinalBase::doPrivate() always throws an exception, it should have return type "never".', + 28, + ], + [ + 'Method Bug10820\FinalClass::doSomething() always throws an exception, it should have return type "never".', + 36, + ], + ]); + } + } diff --git a/tests/PHPStan/Rules/Playground/data/bug-10820.php b/tests/PHPStan/Rules/Playground/data/bug-10820.php new file mode 100644 index 0000000000..b2efca740a --- /dev/null +++ b/tests/PHPStan/Rules/Playground/data/bug-10820.php @@ -0,0 +1,40 @@ +