2021-02-23 02:25:34 +01:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\NodeAnalyzer;
|
2021-02-23 02:25:34 +01:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
|
|
|
use Rector\Core\PhpParser\Comparing\NodeComparator;
|
|
|
|
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
|
|
|
use Rector\NodeNameResolver\NodeNameResolver;
|
2022-08-29 20:49:28 +00:00
|
|
|
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
|
2021-02-23 02:25:34 +01:00
|
|
|
final class ForeachAnalyzer
|
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Comparing\NodeComparator
|
2021-02-23 02:25:34 +01:00
|
|
|
*/
|
|
|
|
private $nodeComparator;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\ForAnalyzer
|
2021-02-23 02:25:34 +01:00
|
|
|
*/
|
|
|
|
private $forAnalyzer;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\NodeNameResolver\NodeNameResolver
|
2021-02-23 02:25:34 +01:00
|
|
|
*/
|
|
|
|
private $nodeNameResolver;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2022-08-29 20:49:28 +00:00
|
|
|
* @var \Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser
|
2021-02-23 02:25:34 +01:00
|
|
|
*/
|
|
|
|
private $simpleCallableNodeTraverser;
|
2021-04-20 19:21:02 +07:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
|
2021-04-20 19:21:02 +07:00
|
|
|
*/
|
|
|
|
private $betterNodeFinder;
|
2022-09-02 18:47:27 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\VariableNameUsedNextAnalyzer
|
|
|
|
*/
|
|
|
|
private $variableNameUsedNextAnalyzer;
|
|
|
|
public function __construct(NodeComparator $nodeComparator, \Rector\CodeQuality\NodeAnalyzer\ForAnalyzer $forAnalyzer, NodeNameResolver $nodeNameResolver, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, BetterNodeFinder $betterNodeFinder, \Rector\CodeQuality\NodeAnalyzer\VariableNameUsedNextAnalyzer $variableNameUsedNextAnalyzer)
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2021-02-23 02:25:34 +01:00
|
|
|
$this->nodeComparator = $nodeComparator;
|
|
|
|
$this->forAnalyzer = $forAnalyzer;
|
|
|
|
$this->nodeNameResolver = $nodeNameResolver;
|
|
|
|
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
2021-04-20 19:21:02 +07:00
|
|
|
$this->betterNodeFinder = $betterNodeFinder;
|
2022-09-02 18:47:27 +00:00
|
|
|
$this->variableNameUsedNextAnalyzer = $variableNameUsedNextAnalyzer;
|
2021-02-23 02:25:34 +01:00
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Matches$
|
|
|
|
* foreach ($values as $value) {
|
|
|
|
* <$assigns[]> = $value;
|
|
|
|
* }
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function matchAssignItemsOnlyForeachArrayVariable(Foreach_ $foreach) : ?Expr
|
2021-02-23 02:25:34 +01:00
|
|
|
{
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($foreach->stmts) !== 1) {
|
2021-02-23 02:25:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$onlyStatement = $foreach->stmts[0];
|
2022-06-07 08:22:29 +00:00
|
|
|
if ($onlyStatement instanceof Expression) {
|
2021-02-23 02:25:34 +01:00
|
|
|
$onlyStatement = $onlyStatement->expr;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$onlyStatement instanceof Assign) {
|
2021-02-23 02:25:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$onlyStatement->var instanceof ArrayDimFetch) {
|
2021-02-23 02:25:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($onlyStatement->var->dim instanceof Expr) {
|
2021-02-23 02:25:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (!$this->nodeComparator->areNodesEqual($foreach->valueVar, $onlyStatement->expr)) {
|
2021-02-23 02:25:34 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $onlyStatement->var->var;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @param Stmt[] $stmts
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function useForeachVariableInStmts(Expr $foreachedValue, Expr $singleValue, array $stmts, string $keyValueName) : void
|
2021-05-09 20:15:43 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($stmts, function (Node $node) use($foreachedValue, $singleValue, $keyValueName) : ?Expr {
|
|
|
|
if (!$node instanceof ArrayDimFetch) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// must be the same as foreach value
|
|
|
|
if (!$this->nodeComparator->areNodesEqual($node->var, $foreachedValue)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->forAnalyzer->isArrayDimFetchPartOfAssignOrArgParentCount($node)) {
|
|
|
|
return null;
|
2021-02-23 02:25:34 +01:00
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
// is dim same as key value name, ...[$i]
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->dim instanceof Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$this->nodeNameResolver->isName($node->dim, $keyValueName)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $singleValue;
|
|
|
|
});
|
2021-02-23 02:25:34 +01:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function isValueVarUsed(Foreach_ $foreach, string $singularValueVarName) : bool
|
2021-04-20 19:21:02 +07:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
$isUsedInStmts = (bool) $this->betterNodeFinder->findFirst($foreach->stmts, function (Node $node) use($singularValueVarName) : bool {
|
|
|
|
if (!$node instanceof Variable) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \false;
|
2021-04-20 19:21:02 +07:00
|
|
|
}
|
|
|
|
return $this->nodeNameResolver->isName($node, $singularValueVarName);
|
|
|
|
});
|
|
|
|
if ($isUsedInStmts) {
|
2021-05-09 20:15:43 +00:00
|
|
|
return \true;
|
2021-04-20 19:21:02 +07:00
|
|
|
}
|
2022-09-02 18:47:27 +00:00
|
|
|
if ($this->variableNameUsedNextAnalyzer->isValueVarUsedNext($foreach, $singularValueVarName)) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $this->variableNameUsedNextAnalyzer->isValueVarUsedNext($foreach, (string) $this->nodeNameResolver->getName($foreach->valueVar));
|
2021-04-20 19:21:02 +07:00
|
|
|
}
|
2021-02-23 02:25:34 +01:00
|
|
|
}
|