rector/rules/code-quality/src/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php

122 lines
3.1 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Foreach_;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Stmt\Foreach_;
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ReadWrite\NodeFinder\NodeUsageFinder;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
*/
final class ForeachItemsAssignToEmptyArrayToAssignRector extends AbstractRector
{
2020-04-01 01:55:44 +00:00
/**
* @var NodeUsageFinder
2020-04-01 01:55:44 +00:00
*/
private $nodeUsageFinder;
2020-04-01 01:55:44 +00:00
/**
* @var ForeachAnalyzer
*/
private $foreachAnalyzer;
public function __construct(NodeUsageFinder $nodeUsageFinder, ForeachAnalyzer $foreachAnalyzer)
2020-04-01 01:55:44 +00:00
{
$this->nodeUsageFinder = $nodeUsageFinder;
$this->foreachAnalyzer = $foreachAnalyzer;
2020-04-01 01:55:44 +00:00
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Change foreach() items assign to empty array to direct assign',
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
$collectedItems = [];
foreach ($items as $item) {
$collectedItems[] = $item;
}
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run($items)
{
$collectedItems = [];
$collectedItems = $items;
}
}
CODE_SAMPLE
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [Foreach_::class];
}
/**
* @param Foreach_ $node
*/
public function refactor(Node $node): ?Node
{
$assignVariable = $this->foreachAnalyzer->matchAssignItemsOnlyForeachArrayVariable($node);
if (! $assignVariable instanceof Expr) {
return null;
}
if ($this->shouldSkipAsPartOfNestedForeach($node)) {
return null;
}
$previousDeclaration = $this->nodeUsageFinder->findPreviousForeachNodeUsage($node, $assignVariable);
if (! $previousDeclaration instanceof Node) {
return null;
}
$previousDeclarationParentNode = $previousDeclaration->getAttribute(AttributeKey::PARENT_NODE);
if (! $previousDeclarationParentNode instanceof Assign) {
return null;
}
2020-04-01 01:55:44 +00:00
// must be empty array, otherwise it will false override
2021-01-30 23:20:05 +00:00
$defaultValue = $this->valueResolver->getValue($previousDeclarationParentNode->expr);
if ($defaultValue !== []) {
return null;
}
return new Assign($assignVariable, $node->expr);
}
private function shouldSkipAsPartOfNestedForeach(Foreach_ $foreach): bool
{
2021-01-08 22:30:33 +00:00
$foreachParent = $this->betterNodeFinder->findParentType($foreach, Foreach_::class);
return $foreachParent !== null;
2020-02-01 16:04:38 +00:00
}
}