Skip to content

Commit

Permalink
[Performance] Combine repetitive find() usage on RemoveUnusedVariable…
Browse files Browse the repository at this point in the history
…AssignRector (#5686)
  • Loading branch information
samsonasik committed Mar 4, 2024
1 parent b523b73 commit b4212ed
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions rules/DeadCode/Rector/Assign/RemoveUnusedVariableAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ public function refactorWithScope(Node $node, Scope $scope): null|ClassMethod|Fu
}

// we cannot be sure here
if ($this->containsCompactFuncCall($node)) {
return null;
}

if ($this->containsFileIncludes($node)) {
if ($this->shouldSkip($stmts)) {
return null;
}

Expand Down Expand Up @@ -173,22 +169,22 @@ private function isVariableUsedInFollowingStmts(
return false;
}

private function containsCompactFuncCall(ClassMethod|Function_ $functionLike): bool
/**
* @param Stmt[] $stmts
*/
private function shouldSkip(array $stmts): bool
{
$compactFuncCall = $this->betterNodeFinder->findFirst($functionLike, function (Node $node): bool {
return (bool) $this->betterNodeFinder->findFirst($stmts, function (Node $node): bool {
if ($node instanceof Include_) {
return true;
}

if (! $node instanceof FuncCall) {
return false;
}

return $this->isName($node, 'compact');
});

return $compactFuncCall instanceof FuncCall;
}

private function containsFileIncludes(ClassMethod|Function_ $functionLike): bool
{
return (bool) $this->betterNodeFinder->findInstancesOf($functionLike, [Include_::class]);
}

/**
Expand Down

0 comments on commit b4212ed

Please sign in to comment.