File tree Expand file tree Collapse file tree 2 files changed +38
-1
lines changed
tests/Rule/data/ForbidUselessNullableReturnRule Expand file tree Collapse file tree 2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change 6
6
use PHPStan \Analyser \Scope ;
7
7
use PHPStan \Node \ClosureReturnStatementsNode ;
8
8
use PHPStan \Node \ReturnStatementsNode ;
9
+ use PHPStan \Reflection \MethodReflection ;
9
10
use PHPStan \Rules \IdentifierRuleError ;
10
11
use PHPStan \Rules \Rule ;
11
12
use PHPStan \Rules \RuleErrorBuilder ;
@@ -38,6 +39,10 @@ public function processNode(
38
39
$ verbosity = VerbosityLevel::precise ();
39
40
$ methodReflection = $ scope ->getFunction ();
40
41
42
+ if ($ methodReflection instanceof MethodReflection && $ this ->isOverridable ($ methodReflection )) {
43
+ return [];
44
+ }
45
+
41
46
if ($ node instanceof ClosureReturnStatementsNode) {
42
47
$ declaredType = $ scope ->getFunctionType ($ node ->getClosureExpr ()->getReturnType (), false , false );
43
48
} elseif ($ methodReflection !== null ) {
@@ -80,4 +85,9 @@ public function processNode(
80
85
return [];
81
86
}
82
87
88
+ private function isOverridable (MethodReflection $ methodReflection ): bool
89
+ {
90
+ return !$ methodReflection ->isFinal ()->yes () && !$ methodReflection ->isPrivate () && !$ methodReflection ->isStatic () && !$ methodReflection ->getDeclaringClass ()->isFinalByKeyword ();
91
+ }
92
+
83
93
}
Original file line number Diff line number Diff line change 2
2
3
3
namespace ForbidUselessNullableReturnRule ;
4
4
5
- class ExampleClass {
5
+ final class ExampleClass {
6
6
7
7
private ?int $ foo ;
8
8
@@ -87,3 +87,30 @@ function nullableFunction(int $int): ?int // error: Declared return type int|nul
87
87
$ globalFn = static function (int $ int ): ?int { // error: Declared return type int|null contains null, but it is never returned. Returned types: int.
88
88
return $ int ;
89
89
};
90
+
91
+ class NonFinalClass {
92
+ public static function staticMethod (): ?int // error: Declared return type int|null contains null, but it is never returned. Returned types: 1.
93
+ {
94
+ return 1 ;
95
+ }
96
+
97
+ final public function finalMethod (): ?int // error: Declared return type int|null contains null, but it is never returned. Returned types: 1.
98
+ {
99
+ return 1 ;
100
+ }
101
+
102
+ private function privateMethod (): ?int // error: Declared return type int|null contains null, but it is never returned. Returned types: 1.
103
+ {
104
+ return 1 ;
105
+ }
106
+
107
+ public function publicMethod (): ?int
108
+ {
109
+ return 1 ;
110
+ }
111
+
112
+ protected function protectedMethod (): ?int
113
+ {
114
+ return 1 ;
115
+ }
116
+ }
You can’t perform that action at this time.
0 commit comments