Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/Collector/MethodCallCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use ShipMonk\PHPStan\DeadCode\Helper\DeadCodeHelper;

/**
Expand Down Expand Up @@ -200,7 +201,9 @@ private function getMethodName(CallLike $call): ?string
*/
private function getReflectionsWithMethod(Type $type, string $methodName): iterable
{
$classReflections = $type->getObjectTypeOrClassStringObjectType()->getObjectClassReflections();
// remove null to support nullsafe calls
$typeNoNull = TypeCombinator::removeNull($type);
$classReflections = $typeNoNull->getObjectTypeOrClassStringObjectType()->getObjectClassReflections();

foreach ($classReflections as $classReflection) {
if ($classReflection->hasMethod($methodName)) {
Expand Down
1 change: 1 addition & 0 deletions tests/Rule/DeadMethodRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public static function provideFiles(): iterable
yield 'trait-1' => [__DIR__ . '/data/DeadMethodRule/traits-1.php'];
yield 'trait-2' => [__DIR__ . '/data/DeadMethodRule/traits-2.php'];
yield 'trait-3' => [__DIR__ . '/data/DeadMethodRule/traits-3.php'];
yield 'nullsafe' => [__DIR__ . '/data/DeadMethodRule/nullsafe.php'];
yield 'dead-in-parent-1' => [__DIR__ . '/data/DeadMethodRule/dead-in-parent-1.php'];
yield 'indirect-interface' => [__DIR__ . '/data/DeadMethodRule/indirect-interface.php'];
yield 'attribute' => [__DIR__ . '/data/DeadMethodRule/attribute.php'];
Expand Down
29 changes: 29 additions & 0 deletions tests/Rule/data/DeadMethodRule/nullsafe.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php declare(strict_types = 1);

namespace Nullsafe;

class A {

public function first(): self {
return $this;
}

public function second(): self {
return $this;
}

public static function secondStatic(): self {
return new self();
}
}

class B {

public function test(?A $a): void
{
$a?->first()->second();
$a?->first()::secondStatic();
}
}

(new B())->test(null);
Loading