2020-09-09 08:52:43 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\Naming\Matcher;
|
2020-09-09 08:52:43 +00:00
|
|
|
|
2023-09-17 00:30:04 +00:00
|
|
|
use PhpParser\Node;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use Rector\Naming\ValueObject\VariableAndCallForeach;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2021-03-01 01:28:35 +00:00
|
|
|
final class ForeachMatcher
|
2020-09-09 08:52:43 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2020-09-09 08:52:43 +00:00
|
|
|
*/
|
2021-03-01 01:28:35 +00:00
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Naming\Matcher\CallMatcher
|
2021-03-01 01:28:35 +00:00
|
|
|
*/
|
|
|
|
private $callMatcher;
|
2023-06-19 19:32:10 +00:00
|
|
|
public function __construct(NodeNameResolver $nodeNameResolver, \Rector\Naming\Matcher\CallMatcher $callMatcher)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-03-01 01:28:35 +00:00
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->callMatcher = $callMatcher;
|
|
|
|
}
|
2023-06-19 19:32:10 +00:00
|
|
|
/**
|
|
|
|
* @param \PhpParser\Node\Stmt\ClassMethod|\PhpParser\Node\Expr\Closure|\PhpParser\Node\Stmt\Function_ $functionLike
|
|
|
|
*/
|
|
|
|
public function match(Foreach_ $foreach, $functionLike) : ?VariableAndCallForeach
|
2021-03-01 01:28:35 +00:00
|
|
|
{
|
|
|
|
$call = $this->callMatcher->matchCall($foreach);
|
2023-09-17 00:30:04 +00:00
|
|
|
if (!$call instanceof Node) {
|
2021-03-01 01:28:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$foreach->valueVar instanceof Variable) {
|
2021-03-01 01:28:35 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$variableName = $this->nodeNameResolver->getName($foreach->valueVar);
|
|
|
|
if ($variableName === null) {
|
2020-09-09 08:52:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
return new VariableAndCallForeach($foreach->valueVar, $call, $variableName, $functionLike);
|
2020-09-09 08:52:43 +00:00
|
|
|
}
|
|
|
|
}
|