rector/rules/CodeQuality/Rector/Foreach_/ForeachItemsAssignToEmptyArrayToAssignRector.php

102 lines
3.4 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\Tests\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
*/
final class ForeachItemsAssignToEmptyArrayToAssignRector extends \Rector\Core\Rector\AbstractRector
{
2020-04-01 01:55:44 +00:00
/**
* @var NodeUsageFinder
2020-04-01 01:55:44 +00:00
*/
private $nodeUsageFinder;
/**
* @var ForeachAnalyzer
*/
private $foreachAnalyzer;
public function __construct(\Rector\ReadWrite\NodeFinder\NodeUsageFinder $nodeUsageFinder, \Rector\CodeQuality\NodeAnalyzer\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() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Change foreach() items assign to empty array to direct assign', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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 array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Stmt\Foreach_::class];
}
/**
* @param Foreach_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
$assignVariable = $this->foreachAnalyzer->matchAssignItemsOnlyForeachArrayVariable($node);
if (!$assignVariable instanceof \PhpParser\Node\Expr) {
return null;
}
if ($this->shouldSkipAsPartOfNestedForeach($node)) {
return null;
}
$previousDeclaration = $this->nodeUsageFinder->findPreviousForeachNodeUsage($node, $assignVariable);
if (!$previousDeclaration instanceof \PhpParser\Node) {
return null;
}
$previousDeclarationParentNode = $previousDeclaration->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::PARENT_NODE);
if (!$previousDeclarationParentNode instanceof \PhpParser\Node\Expr\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 \PhpParser\Node\Expr\Assign($assignVariable, $node->expr);
}
private function shouldSkipAsPartOfNestedForeach(\PhpParser\Node\Stmt\Foreach_ $foreach) : bool
{
$foreachParent = $this->betterNodeFinder->findParentType($foreach, \PhpParser\Node\Stmt\Foreach_::class);
return $foreachParent !== null;
2020-02-01 16:04:38 +00:00
}
}