File tree Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Expand file tree Collapse file tree 3 files changed +34
-1
lines changed Original file line number Diff line number Diff line change 21
21
use PHPStan \Reflection \ClassReflection ;
22
22
use PHPStan \Reflection \ReflectionProvider ;
23
23
use PHPStan \Type \Type ;
24
+ use PHPStan \Type \TypeCombinator ;
24
25
use ShipMonk \PHPStan \DeadCode \Helper \DeadCodeHelper ;
25
26
26
27
/**
@@ -200,7 +201,9 @@ private function getMethodName(CallLike $call): ?string
200
201
*/
201
202
private function getReflectionsWithMethod (Type $ type , string $ methodName ): iterable
202
203
{
203
- $ classReflections = $ type ->getObjectTypeOrClassStringObjectType ()->getObjectClassReflections ();
204
+ // remove null to support nullsafe calls
205
+ $ typeNoNull = TypeCombinator::removeNull ($ type );
206
+ $ classReflections = $ typeNoNull ->getObjectTypeOrClassStringObjectType ()->getObjectClassReflections ();
204
207
205
208
foreach ($ classReflections as $ classReflection ) {
206
209
if ($ classReflection ->hasMethod ($ methodName )) {
Original file line number Diff line number Diff line change @@ -87,6 +87,7 @@ public static function provideFiles(): iterable
87
87
yield 'trait-1 ' => [__DIR__ . '/data/DeadMethodRule/traits-1.php ' ];
88
88
yield 'trait-2 ' => [__DIR__ . '/data/DeadMethodRule/traits-2.php ' ];
89
89
yield 'trait-3 ' => [__DIR__ . '/data/DeadMethodRule/traits-3.php ' ];
90
+ yield 'nullsafe ' => [__DIR__ . '/data/DeadMethodRule/nullsafe.php ' ];
90
91
yield 'dead-in-parent-1 ' => [__DIR__ . '/data/DeadMethodRule/dead-in-parent-1.php ' ];
91
92
yield 'indirect-interface ' => [__DIR__ . '/data/DeadMethodRule/indirect-interface.php ' ];
92
93
yield 'attribute ' => [__DIR__ . '/data/DeadMethodRule/attribute.php ' ];
Original file line number Diff line number Diff line change
1
+ <?php declare (strict_types = 1 );
2
+
3
+ namespace Nullsafe ;
4
+
5
+ class A {
6
+
7
+ public function first (): self {
8
+ return $ this ;
9
+ }
10
+
11
+ public function second (): self {
12
+ return $ this ;
13
+ }
14
+
15
+ public static function secondStatic (): self {
16
+ return new self ();
17
+ }
18
+ }
19
+
20
+ class B {
21
+
22
+ public function test (?A $ a ): void
23
+ {
24
+ $ a ?->first()->second ();
25
+ $ a ?->first()::secondStatic ();
26
+ }
27
+ }
28
+
29
+ (new B ())->test (null );
You can’t perform that action at this time.
0 commit comments