Skip to content

Commit

Permalink
[Naming] Remove parent lookup on VariableAndCallAssignMatcher (#4294)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jun 19, 2023
1 parent 70287f1 commit 9e3849f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 19 deletions.
16 changes: 1 addition & 15 deletions rules/Naming/Matcher/VariableAndCallAssignMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand All @@ -24,7 +23,7 @@ public function __construct(
) {
}

public function match(Assign $assign): ?VariableAndCallAssign
public function match(Assign $assign, ClassMethod|Closure|Function_ $functionLike): ?VariableAndCallAssign
{
$call = $this->callMatcher->matchCall($assign);
if ($call === null) {
Expand All @@ -40,11 +39,6 @@ public function match(Assign $assign): ?VariableAndCallAssign
return null;
}

$functionLike = $this->getFunctionLike($assign);
if (! $functionLike instanceof FunctionLike) {
return null;
}

$isVariableFoundInCallArgs = (bool) $this->betterNodeFinder->findFirst(
$call->isFirstClassCallable() ? [] : $call->getArgs(),
fn (Node $subNode): bool =>
Expand All @@ -57,12 +51,4 @@ public function match(Assign $assign): ?VariableAndCallAssign

return new VariableAndCallAssign($assign->var, $call, $assign, $variableName, $functionLike);
}

private function getFunctionLike(Assign $assign): ClassMethod | Function_ | Closure | null
{
return $this->betterNodeFinder->findParentByTypes(
$assign,
[Closure::class, ClassMethod::class, Function_::class]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Contract\PhpParser\Node\StmtsAwareInterface;
use PhpParser\Node\Stmt\Function_;
use Rector\Core\Rector\AbstractRector;
use Rector\Naming\Guard\BreakingVariableRenameGuard;
use Rector\Naming\Matcher\VariableAndCallAssignMatcher;
Expand Down Expand Up @@ -83,11 +85,11 @@ public function getRunner(): Runner
*/
public function getNodeTypes(): array
{
return [StmtsAwareInterface::class];
return [ClassMethod::class, Closure::class, Function_::class];
}

/**
* @param StmtsAwareInterface $node
* @param ClassMethod|Closure|Function_ $node
*/
public function refactor(Node $node): ?Node
{
Expand All @@ -106,7 +108,7 @@ public function refactor(Node $node): ?Node

$assign = $stmt->expr;

$variableAndCallAssign = $this->variableAndCallAssignMatcher->match($assign);
$variableAndCallAssign = $this->variableAndCallAssignMatcher->match($assign, $node);
if (! $variableAndCallAssign instanceof VariableAndCallAssign) {
return null;
}
Expand Down

0 comments on commit 9e3849f

Please sign in to comment.