Skip to content

Commit

Permalink
Support NullSafeMethod calls in unused-code analysis (#5838)
Browse files Browse the repository at this point in the history
* Support NullSafeMethod calls in unused-code analysis

* fix phpstan
  • Loading branch information
staabm committed Apr 21, 2024
1 parent 068799f commit 4cd2622
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPrivateMethodRector\Fixture;

final class KeepNullSafeCalledMethod
{
private function privateMethod():void {}

public function pubMethod() {
$this->getX()?->privateMethod();
}

public function getX(): KeepNullSafeCalledMethod|null {
return rand(0,1) ? new KeepNullSafeCalledMethod() : null;
}
}
7 changes: 4 additions & 3 deletions rules/DeadCode/NodeAnalyzer/CallCollectionAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\DeadCode\NodeAnalyzer;

use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\NullsafeMethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
Expand All @@ -22,7 +23,7 @@ public function __construct(
}

/**
* @param StaticCall[]|MethodCall[] $calls
* @param StaticCall[]|MethodCall[]|NullsafeMethodCall[] $calls
*/
public function isExists(array $calls, string $classMethodName, string $className): bool
{
Expand Down Expand Up @@ -50,7 +51,7 @@ public function isExists(array $calls, string $classMethodName, string $classNam
return false;
}

private function isSelfStatic(MethodCall|StaticCall $call): bool
private function isSelfStatic(MethodCall|StaticCall|NullsafeMethodCall $call): bool
{
return $call instanceof StaticCall && $call->class instanceof Name && in_array(
$call->class->toString(),
Expand All @@ -59,7 +60,7 @@ private function isSelfStatic(MethodCall|StaticCall $call): bool
);
}

private function shouldSkip(StaticCall|MethodCall $call, string $classMethodName): bool
private function shouldSkip(StaticCall|MethodCall|NullsafeMethodCall $call, string $classMethodName): bool
{
if (! $call->name instanceof Identifier) {
return true;
Expand Down
18 changes: 16 additions & 2 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,17 @@ public function isClassMethodUsed(Class_ $class, ClassMethod $classMethod, Scope
return true;
}

// 2. direct static calls
// 2. direct null-safe calls
if ($this->isClassMethodCalledInLocalNullsafeMethodCall($class, $classMethodName)) {
return true;
}

// 3. direct static calls
if ($this->isClassMethodUsedInLocalStaticCall($class, $classMethodName)) {
return true;
}

// 3. magic array calls!
// 4. magic array calls!
if ($this->isClassMethodCalledInLocalArrayCall($class, $classMethod, $scope)) {
return true;
}
Expand All @@ -85,6 +90,15 @@ private function isClassMethodCalledInLocalMethodCall(Class_ $class, string $cla
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
}

private function isClassMethodCalledInLocalNullsafeMethodCall(Class_ $class, string $classMethodName): bool
{
$className = (string) $this->nodeNameResolver->getName($class);

/** @var Node\Expr\NullsafeMethodCall[] $methodCalls */
$methodCalls = $this->betterNodeFinder->findInstanceOf($class, Node\Expr\NullsafeMethodCall::class);
return $this->callCollectionAnalyzer->isExists($methodCalls, $classMethodName, $className);
}

private function isInArrayMap(Class_ $class, Array_ $array): bool
{
if (! $array->getAttribute(ArrayMapArgVisitor::ATTRIBUTE_NAME) instanceof Arg) {
Expand Down

0 comments on commit 4cd2622

Please sign in to comment.