Skip to content

Commit 0ad4f12

Browse files
github-actions[bot]phpstan-bot
authored andcommitted
Fix phpstan/phpstan#10820: playground never rule false positive for overridable methods
- MethodNeverRule now skips non-private methods in non-final classes - Private methods and methods in final classes still get reported - Added regression test in tests/PHPStan/Rules/Playground/data/bug-10820.php - Updated existing test data to use final class to preserve coverage
1 parent d8f5be7 commit 0ad4f12

File tree

4 files changed

+59
-1
lines changed

4 files changed

+59
-1
lines changed

src/Rules/Playground/MethodNeverRule.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public function processNode(Node $node, Scope $scope): array
3333

3434
$method = $node->getMethodReflection();
3535

36+
if (!$method->isPrivate() && !$node->getClassReflection()->isFinal()) {
37+
return [];
38+
}
39+
3640
$returnType = $method->getReturnType();
3741
$helperResult = $this->helper->shouldReturnNever($node, $returnType);
3842
if ($helperResult === false) {

tests/PHPStan/Rules/Playground/MethodNeverRuleTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,18 @@ public function testRule(): void
3636
]);
3737
}
3838

39+
public function testBug10820(): void
40+
{
41+
$this->analyse([__DIR__ . '/data/bug-10820.php'], [
42+
[
43+
'Method Bug10820\NonFinalBase::doPrivate() always throws an exception, it should have return type "never".',
44+
28,
45+
],
46+
[
47+
'Method Bug10820\FinalClass::doSomething() always throws an exception, it should have return type "never".',
48+
36,
49+
],
50+
]);
51+
}
52+
3953
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug10820;
4+
5+
abstract class Value
6+
{
7+
public function assertString(): SassString
8+
{
9+
throw new \Exception('this is not a string');
10+
}
11+
}
12+
13+
final class SassString extends Value
14+
{
15+
public function assertString(): SassString
16+
{
17+
return $this;
18+
}
19+
}
20+
21+
class NonFinalBase
22+
{
23+
protected function doSomething(): int
24+
{
25+
throw new \Exception('not implemented');
26+
}
27+
28+
private function doPrivate(): int
29+
{
30+
throw new \Exception('not implemented');
31+
}
32+
}
33+
34+
final class FinalClass
35+
{
36+
public function doSomething(): int
37+
{
38+
throw new \Exception('not implemented');
39+
}
40+
}

tests/PHPStan/Rules/Playground/data/method-never.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace MethodNever;
44

5-
class Foo
5+
final class Foo
66
{
77

88
public function doFoo(): never

0 commit comments

Comments
 (0)