2020-07-19 18:11:59 +02:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2020-07-19 18:11:59 +02:00
|
|
|
namespace Rector\Naming\Matcher;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2021-03-01 02:28:35 +01:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
2020-07-19 18:11:59 +02:00
|
|
|
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;
|
2021-07-15 03:40:51 +00:00
|
|
|
use Rector\NodeNestingScope\ParentFinder;
|
2021-03-01 02:28:35 +01:00
|
|
|
final class VariableAndCallAssignMatcher
|
2020-07-19 18:11:59 +02:00
|
|
|
{
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Matcher\CallMatcher
|
2020-07-19 18:11:59 +02:00
|
|
|
*/
|
2021-03-01 02:28:35 +01:00
|
|
|
private $callMatcher;
|
|
|
|
/**
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2021-03-01 02:28:35 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
2021-07-15 03:40:51 +00:00
|
|
|
/**
|
|
|
|
* @var \Rector\NodeNestingScope\ParentFinder
|
|
|
|
*/
|
|
|
|
private $parentFinder;
|
2021-07-18 02:11:18 +00:00
|
|
|
public function __construct(\Rector\Naming\Matcher\CallMatcher $callMatcher, \Rector\NodeNameResolver\NodeNameResolver $nodeNameResolver, \Rector\NodeNestingScope\ParentFinder $parentFinder)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-01 02:28:35 +01:00
|
|
|
$this->callMatcher = $callMatcher;
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
2021-07-15 03:40:51 +00:00
|
|
|
$this->parentFinder = $parentFinder;
|
2021-03-01 02:28:35 +01:00
|
|
|
}
|
2021-05-10 22:23:08 +00: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;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
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);
|
2021-05-10 22:23:08 +00:00
|
|
|
if (!$functionLike instanceof \PhpParser\Node\FunctionLike) {
|
2020-07-19 18:11:59 +02:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-10 22:23:08 +00:00
|
|
|
return new \Rector\Naming\ValueObject\VariableAndCallAssign($assign->var, $call, $assign, $variableName, $functionLike);
|
2020-07-19 18:11:59 +02:00
|
|
|
}
|
2020-08-05 22:45:36 +02:00
|
|
|
/**
|
2021-10-30 14:18:31 +00:00
|
|
|
* @return \PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Stmt\Function_|null
|
2020-08-05 22:45:36 +02:00
|
|
|
*/
|
2021-06-29 13:37:16 +00:00
|
|
|
private function getFunctionLike(\PhpParser\Node\Expr\Assign $assign)
|
2020-08-05 22:45:36 +02:00
|
|
|
{
|
2021-07-15 03:40:51 +00:00
|
|
|
return $this->parentFinder->findByTypes($assign, [\PhpParser\Node\Expr\Closure::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class]);
|
2020-08-05 22:45:36 +02:00
|
|
|
}
|
2020-07-19 18:11:59 +02:00
|
|
|
}
|