2020-01-04 13:56:07 +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\CodeQuality\Rector\Foreach_;
|
2020-01-04 13:56:07 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
2023-06-02 22:04:35 +00:00
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2023-05-25 09:21:23 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2023-05-25 13:47:07 +00:00
|
|
|
use PhpParser\Node\Stmt;
|
2023-05-25 09:21:23 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
2023-06-02 22:04:35 +00:00
|
|
|
use PhpParser\NodeTraverser;
|
2022-06-06 17:12:56 +00:00
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Contract\PhpParser\Node\StmtsAwareInterface;
|
|
|
|
use Rector\PhpParser\Node\Value\ValueResolver;
|
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-01-04 13:56:07 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
|
2020-01-04 13:56:07 +00:00
|
|
|
*/
|
2023-06-24 09:24:54 +00:00
|
|
|
final class ForeachItemsAssignToEmptyArrayToAssignRector extends AbstractRector
|
2020-01-04 13:56:07 +00:00
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer
|
2020-10-20 14:24:32 +00:00
|
|
|
*/
|
2021-02-23 01:25:34 +00:00
|
|
|
private $foreachAnalyzer;
|
2023-09-20 12:53:23 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\Node\Value\ValueResolver
|
2023-09-20 12:53:23 +00:00
|
|
|
*/
|
|
|
|
private $valueResolver;
|
|
|
|
public function __construct(ForeachAnalyzer $foreachAnalyzer, ValueResolver $valueResolver)
|
2020-04-01 01:55:44 +00:00
|
|
|
{
|
2021-02-23 01:25:34 +00:00
|
|
|
$this->foreachAnalyzer = $foreachAnalyzer;
|
2023-09-20 12:53:23 +00:00
|
|
|
$this->valueResolver = $valueResolver;
|
2020-04-01 01:55:44 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-04 13:56:07 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change foreach() items assign to empty array to direct assign', [new CodeSample(<<<'CODE_SAMPLE'
|
2020-01-04 13:56:07 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
$collectedItems = [];
|
|
|
|
|
2020-01-04 13:56:07 +00:00
|
|
|
foreach ($items as $item) {
|
2020-10-20 14:24:32 +00:00
|
|
|
$collectedItems[] = $item;
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2020-01-04 13:56:07 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
$collectedItems = [];
|
|
|
|
|
|
|
|
$collectedItems = $items;
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2020-01-04 13:56:07 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2020-01-04 13:56:07 +00:00
|
|
|
{
|
2023-05-25 09:21:23 +00:00
|
|
|
return [StmtsAwareInterface::class];
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
|
|
|
/**
|
2023-05-25 09:21:23 +00:00
|
|
|
* @param StmtsAwareInterface $node
|
2020-01-04 13:56:07 +00:00
|
|
|
*/
|
2023-06-24 09:24:54 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2020-01-04 13:56:07 +00:00
|
|
|
{
|
2023-05-25 09:21:23 +00:00
|
|
|
if ($node->stmts === null) {
|
2021-08-22 17:58:54 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
$emptyArrayVariables = [];
|
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
$variableName = $this->matchEmptyArrayVariableAssign($stmt);
|
|
|
|
if (\is_string($variableName)) {
|
|
|
|
$emptyArrayVariables[] = $variableName;
|
|
|
|
}
|
|
|
|
if (!$stmt instanceof Foreach_) {
|
2023-06-03 00:54:04 +00:00
|
|
|
if ($this->isAppend($stmt, $emptyArrayVariables)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ($this->shouldSkip($stmt, $emptyArrayVariables)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$assignVariable = $this->foreachAnalyzer->matchAssignItemsOnlyForeachArrayVariable($stmt);
|
|
|
|
if (!$assignVariable instanceof Expr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$directAssign = new Assign($assignVariable, $stmt->expr);
|
|
|
|
$node->stmts[$key] = new Expression($directAssign);
|
|
|
|
return $node;
|
2023-05-06 22:11:26 +00:00
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
return null;
|
2021-08-22 17:58:54 +00:00
|
|
|
}
|
2023-06-02 22:04:35 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $emptyArrayVariables
|
|
|
|
*/
|
|
|
|
private function isAppend(Stmt $stmt, array $emptyArrayVariables) : bool
|
|
|
|
{
|
|
|
|
$isAppend = \false;
|
|
|
|
$this->traverseNodesWithCallable($stmt, function (Node $subNode) use($emptyArrayVariables, &$isAppend) : ?int {
|
|
|
|
if ($subNode instanceof Assign && $subNode->var instanceof ArrayDimFetch) {
|
|
|
|
$isAppend = $this->isNames($subNode->var->var, $emptyArrayVariables);
|
|
|
|
if ($isAppend) {
|
|
|
|
return NodeTraverser::STOP_TRAVERSAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
return $isAppend;
|
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
/**
|
|
|
|
* @param string[] $emptyArrayVariables
|
|
|
|
*/
|
|
|
|
private function shouldSkip(Foreach_ $foreach, array $emptyArrayVariables) : bool
|
2021-08-22 17:58:54 +00:00
|
|
|
{
|
2023-05-26 14:00:49 +00:00
|
|
|
$assignVariableExpr = $this->foreachAnalyzer->matchAssignItemsOnlyForeachArrayVariable($foreach);
|
|
|
|
if (!$assignVariableExpr instanceof Expr) {
|
2021-08-22 17:58:54 +00:00
|
|
|
return \true;
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
$foreachedExprType = $this->getType($foreach->expr);
|
|
|
|
// only arrays, not traversable/iterable
|
|
|
|
if (!$foreachedExprType->isArray()->yes()) {
|
2021-08-22 17:58:54 +00:00
|
|
|
return \true;
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
2023-05-26 14:00:49 +00:00
|
|
|
return !$this->isNames($assignVariableExpr, $emptyArrayVariables);
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|
2023-05-25 09:21:23 +00:00
|
|
|
private function matchEmptyArrayVariableAssign(Stmt $stmt) : ?string
|
|
|
|
{
|
|
|
|
if (!$stmt instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$stmt->expr instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$assign = $stmt->expr;
|
|
|
|
if (!$assign->var instanceof Variable) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// must be assign of empty array
|
|
|
|
if (!$this->valueResolver->isValue($assign->expr, [])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return $this->getName($assign->var);
|
|
|
|
}
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|