Skip to content

Commit

Permalink
[DeadCode] Remove parent lookup on RemoveUnusedPrivateMethodRector (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed May 28, 2023
1 parent cc6dc93 commit 95d3dc9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
7 changes: 1 addition & 6 deletions rules/DeadCode/NodeAnalyzer/IsClassMethodUsedAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,8 @@ public function __construct(
) {
}

public function isClassMethodUsed(ClassMethod $classMethod, Scope $scope): bool
public function isClassMethodUsed(Class_ $class, ClassMethod $classMethod, Scope $scope): bool
{
$class = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
if (! $class instanceof Class_) {
return true;
}

$classMethodName = $this->nodeNameResolver->getName($classMethod);

// 1. direct normal calls
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,35 @@ public function run()
*/
public function getNodeTypes(): array
{
return [ClassMethod::class];
return [Class_::class];
}

/**
* @param ClassMethod $node
* @param Class_ $node
*/
public function refactorWithScope(Node $node, Scope $scope): ?Node
{
if ($this->shouldSkip($node)) {
if ($this->hasDynamicMethodCallOnFetchThis($node)) {
return null;
}

if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $scope)) {
return null;
}
$hasChanged = false;
foreach ($node->getMethods() as $classMethod) {
if ($this->shouldSkip($classMethod)) {
continue;
}

if ($this->hasDynamicMethodCallOnFetchThis($node)) {
return null;
if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $classMethod, $scope)) {
continue;
}

$this->removeNode($classMethod);
$hasChanged = true;
}

$this->removeNode($node);
if ($hasChanged) {
return $node;
}

return null;
}
Expand Down Expand Up @@ -124,16 +132,11 @@ private function shouldSkip(ClassMethod $classMethod): bool
return $classReflection->hasMethod(MethodName::CALL);
}

private function hasDynamicMethodCallOnFetchThis(ClassMethod $classMethod): bool
private function hasDynamicMethodCallOnFetchThis(Class_ $class): bool
{
$class = $this->betterNodeFinder->findParentType($classMethod, Class_::class);
if (! $class instanceof Class_) {
return false;
}

foreach ($class->getMethods() as $method) {
foreach ($class->getMethods() as $classMethod) {
$isFound = (bool) $this->betterNodeFinder->findFirst(
(array) $method->getStmts(),
(array) $classMethod->getStmts(),
function (Node $subNode): bool {
if (! $subNode instanceof MethodCall) {
return false;
Expand Down

0 comments on commit 95d3dc9

Please sign in to comment.