rector/rules/Naming/Matcher/VariableAndCallAssignMatcher.php

62 lines
2.2 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
namespace Rector\Naming\Matcher;
use PhpParser\Node\Expr\Assign;
2021-03-01 02:28:35 +01:00
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
2021-03-01 02:28:35 +01:00
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\Naming\ValueObject\VariableAndCallAssign;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeNestingScope\ParentFinder;
2021-03-01 02:28:35 +01:00
final class VariableAndCallAssignMatcher
{
/**
* @var \Rector\Naming\Matcher\CallMatcher
*/
2021-03-01 02:28:35 +01:00
private $callMatcher;
/**
* @var \Rector\NodeNameResolver\NodeNameResolver
2021-03-01 02:28:35 +01:00
*/
private $nodeNameResolver;
/**
* @var \Rector\NodeNestingScope\ParentFinder
*/
private $parentFinder;
public function __construct(\Rector\Naming\Matcher\CallMatcher $callMatcher, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\NodeNestingScope\ParentFinder $parentFinder)
{
2021-03-01 02:28:35 +01:00
$this->callMatcher = $callMatcher;
$this->nodeNameResolver = $nodeNameResolver;
$this->parentFinder = $parentFinder;
2021-03-01 02:28:35 +01:00
}
public function match(\PhpParser\Node\Expr\Assign $assign) : ?\Rector\Naming\ValueObject\VariableAndCallAssign
2021-03-01 02:28:35 +01:00
{
$call = $this->callMatcher->matchCall($assign);
if ($call === null) {
return null;
}
if (!$assign->var instanceof \PhpParser\Node\Expr\Variable) {
2021-03-01 02:28:35 +01:00
return null;
}
$variableName = $this->nodeNameResolver->getName($assign->var);
if ($variableName === null) {
return null;
}
$functionLike = $this->getFunctionLike($assign);
if (!$functionLike instanceof \PhpParser\Node\FunctionLike) {
return null;
}
return new \Rector\Naming\ValueObject\VariableAndCallAssign($assign->var, $call, $assign, $variableName, $functionLike);
}
/**
* @return \PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
*/
private function getFunctionLike(\PhpParser\Node\Expr\Assign $assign)
{
return $this->parentFinder->findByTypes($assign, [\PhpParser\Node\Expr\Closure::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class]);
}
}