2020-01-04 13:56:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\Foreach_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
2020-10-20 14:24:32 +00:00
|
|
|
use PhpParser\Node\Expr\Variable;
|
2020-01-04 13:56:07 +00:00
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
2020-10-20 14:24:32 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ForeachNodeAnalyzer;
|
|
|
|
use Rector\Core\NodeFinder\NodeUsageFinder;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\RectorDefinition\CodeSample;
|
|
|
|
use Rector\Core\RectorDefinition\RectorDefinition;
|
2020-01-04 13:56:07 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
|
|
|
|
*/
|
|
|
|
final class ForeachItemsAssignToEmptyArrayToAssignRector extends AbstractRector
|
|
|
|
{
|
2020-04-01 01:55:44 +00:00
|
|
|
/**
|
2020-10-20 14:24:32 +00:00
|
|
|
* @var NodeUsageFinder
|
2020-04-01 01:55:44 +00:00
|
|
|
*/
|
2020-10-20 14:24:32 +00:00
|
|
|
private $nodeUsageFinder;
|
2020-04-01 01:55:44 +00:00
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
/**
|
|
|
|
* @var ForeachNodeAnalyzer
|
|
|
|
*/
|
|
|
|
private $foreachNodeAnalyzer;
|
|
|
|
|
|
|
|
public function __construct(NodeUsageFinder $nodeUsageFinder, ForeachNodeAnalyzer $foreachNodeAnalyzer)
|
2020-04-01 01:55:44 +00:00
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
$this->nodeUsageFinder = $nodeUsageFinder;
|
|
|
|
$this->foreachNodeAnalyzer = $foreachNodeAnalyzer;
|
2020-04-01 01:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-01-04 13:56:07 +00:00
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Change foreach() items assign to empty array to direct assign', [
|
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +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 = [];
|
|
|
|
|
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
|
2020-01-04 13:56:07 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +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
|
2020-01-04 13:56:07 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Foreach_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Foreach_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
$assignVariable = $this->foreachNodeAnalyzer->matchAssignItemsOnlyForeachArrayVariable($node);
|
2020-01-04 13:56:07 +00:00
|
|
|
if ($assignVariable === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
/** @var Scope $scope */
|
|
|
|
$scope = $node->getAttribute(AttributeKey::SCOPE);
|
|
|
|
$previousDeclaration = $this->nodeUsageFinder->findPreviousForeachNodeUsage($node, $assignVariable);
|
2020-01-04 13:56:07 +00:00
|
|
|
if ($previousDeclaration === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:36:53 +00:00
|
|
|
if ($this->shouldSkipAsPartOfNestedForeach($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:56:07 +00:00
|
|
|
$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
|
2020-01-04 13:56:07 +00:00
|
|
|
$defaultValue = $this->getValue($previousDeclarationParentNode->expr);
|
|
|
|
if ($defaultValue !== []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Assign($assignVariable, $node->expr);
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:36:53 +00:00
|
|
|
private function shouldSkipAsPartOfNestedForeach(Foreach_ $foreach): bool
|
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
/** @var Variable|null $foreachedVariable */
|
|
|
|
$foreachedVariable = $this->betterNodeFinder->findFirstInstanceOf($foreach->expr, Variable::class);
|
|
|
|
if ($foreachedVariable === null) {
|
2020-01-08 13:36:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
$previousForeachVariableUsage = $this->nodeUsageFinder->findPreviousForeachNodeUsage(
|
|
|
|
$foreach,
|
|
|
|
$foreachedVariable
|
2020-01-08 13:36:53 +00:00
|
|
|
);
|
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
if ($previousForeachVariableUsage === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
$parent = $previousForeachVariableUsage->getAttribute(AttributeKey::PARENT_NODE);
|
2020-02-01 16:04:38 +00:00
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
/** @var Foreach_ $previousForeachVariableUsageParentNode */
|
|
|
|
$previousForeachVariableUsageParentNode = $parent;
|
2020-02-01 16:04:38 +00:00
|
|
|
|
2020-10-20 14:24:32 +00:00
|
|
|
return $this->areNodesEqual($previousForeachVariableUsageParentNode->valueVar, $foreachedVariable);
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|