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;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
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
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Change foreach() items assign to empty array to direct assign', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
|
|
|
$items2 = [];
|
|
|
|
foreach ($items as $item) {
|
|
|
|
$items2[] = $item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
,
|
|
|
|
<<<'PHP'
|
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run($items)
|
|
|
|
{
|
|
|
|
$items2 = [];
|
|
|
|
$items2 = $items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PHP
|
|
|
|
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [Foreach_::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Foreach_ $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
$assignVariable = $this->matchAssignItemsOnlyForeachArrayVariable($node);
|
|
|
|
if ($assignVariable === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$previousDeclaration = $this->findPreviousNodeUsage($node, $assignVariable);
|
|
|
|
if ($previousDeclaration === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:36:53 +00:00
|
|
|
// covers https://github.com/Symplify/Symplify/pull/1733/checks?check_run_id=379368887#step:5:64
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// must be empty array, othewise it will false override
|
|
|
|
$defaultValue = $this->getValue($previousDeclarationParentNode->expr);
|
|
|
|
if ($defaultValue !== []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Assign($assignVariable, $node->expr);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function matchAssignItemsOnlyForeachArrayVariable(Foreach_ $foreach): ?Expr
|
|
|
|
{
|
|
|
|
if (count($foreach->stmts) !== 1) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$onlyStatement = $foreach->stmts[0];
|
|
|
|
if ($onlyStatement instanceof Expression) {
|
|
|
|
$onlyStatement = $onlyStatement->expr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $onlyStatement instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $onlyStatement->var instanceof ArrayDimFetch) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-11 00:56:26 +00:00
|
|
|
if ($onlyStatement->var->dim !== null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-04 13:56:07 +00:00
|
|
|
if (! $this->areNodesEqual($foreach->valueVar, $onlyStatement->expr)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $onlyStatement->var->var;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function findPreviousNodeUsage(Node $node, Expr $expr): ?Node
|
|
|
|
{
|
|
|
|
return $this->betterNodeFinder->findFirstPrevious($node, function (Node $node) use ($expr) {
|
|
|
|
if ($node === $expr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->areNodesEqual($node, $expr);
|
|
|
|
});
|
|
|
|
}
|
2020-01-08 13:36:53 +00:00
|
|
|
|
|
|
|
private function shouldSkipAsPartOfNestedForeach(Foreach_ $foreach): bool
|
|
|
|
{
|
2020-01-08 14:03:09 +00:00
|
|
|
$previousForeachVariableUsage = $this->findPreviousNodeUsageInForeach($foreach, $foreach->expr);
|
2020-01-08 13:36:53 +00:00
|
|
|
if ($previousForeachVariableUsage === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-01-08 14:03:09 +00:00
|
|
|
/** @var Foreach_ $previousForeachVariableUsageParentNode */
|
2020-01-08 13:36:53 +00:00
|
|
|
$previousForeachVariableUsageParentNode = $previousForeachVariableUsage->getAttribute(
|
|
|
|
AttributeKey::PARENT_NODE
|
|
|
|
);
|
|
|
|
|
|
|
|
return $this->areNodesEqual($previousForeachVariableUsageParentNode->valueVar, $foreach->expr);
|
|
|
|
}
|
2020-02-01 16:04:38 +00:00
|
|
|
|
|
|
|
private function findPreviousNodeUsageInForeach(Node $node, Expr $expr): ?Node
|
|
|
|
{
|
|
|
|
return $this->betterNodeFinder->findFirstPrevious($node, function (Node $node) use ($expr) {
|
|
|
|
if ($node === $expr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (! $this->areNodesEqual($node, $expr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $node->getAttribute(AttributeKey::PARENT_NODE) instanceof Foreach_;
|
|
|
|
});
|
|
|
|
}
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|