2020-01-04 13:56:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\Foreach_;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
2021-01-20 11:41:35 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2020-01-04 13:56:07 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Stmt\Foreach_;
|
2021-02-23 01:25:34 +00:00
|
|
|
use Rector\CodeQuality\NodeAnalyzer\ForeachAnalyzer;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2020-01-04 13:56:07 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2021-02-08 12:33:17 +00:00
|
|
|
use Rector\ReadWrite\NodeFinder\NodeUsageFinder;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2020-01-04 13:56:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
/**
|
2021-02-23 01:25:34 +00:00
|
|
|
* @var ForeachAnalyzer
|
2020-10-20 14:24:32 +00:00
|
|
|
*/
|
2021-02-23 01:25:34 +00:00
|
|
|
private $foreachAnalyzer;
|
2020-10-20 14:24:32 +00:00
|
|
|
|
2021-02-23 01:25:34 +00:00
|
|
|
public function __construct(NodeUsageFinder $nodeUsageFinder, ForeachAnalyzer $foreachAnalyzer)
|
2020-04-01 01:55:44 +00:00
|
|
|
{
|
2020-10-20 14:24:32 +00:00
|
|
|
$this->nodeUsageFinder = $nodeUsageFinder;
|
2021-02-23 01:25:34 +00:00
|
|
|
$this->foreachAnalyzer = $foreachAnalyzer;
|
2020-04-01 01:55:44 +00:00
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-01-04 13:56:07 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +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
|
2020-01-04 13:56:07 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +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-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
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
|
|
|
|
{
|
2021-02-23 01:25:34 +00:00
|
|
|
$assignVariable = $this->foreachAnalyzer->matchAssignItemsOnlyForeachArrayVariable($node);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $assignVariable instanceof Expr) {
|
2020-01-04 13:56:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:07:01 +00:00
|
|
|
if ($this->shouldSkipAsPartOfNestedForeach($node)) {
|
2020-01-04 13:56:07 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-10-20 15:07:01 +00:00
|
|
|
$previousDeclaration = $this->nodeUsageFinder->findPreviousForeachNodeUsage($node, $assignVariable);
|
2021-01-20 11:41:35 +00:00
|
|
|
if (! $previousDeclaration instanceof Node) {
|
2020-01-08 13:36:53 +00:00
|
|
|
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
|
2021-01-30 23:20:05 +00:00
|
|
|
$defaultValue = $this->valueResolver->getValue($previousDeclarationParentNode->expr);
|
2020-01-04 13:56:07 +00:00
|
|
|
if ($defaultValue !== []) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Assign($assignVariable, $node->expr);
|
|
|
|
}
|
|
|
|
|
2020-01-08 13:36:53 +00:00
|
|
|
private function shouldSkipAsPartOfNestedForeach(Foreach_ $foreach): bool
|
|
|
|
{
|
2021-01-08 22:30:33 +00:00
|
|
|
$foreachParent = $this->betterNodeFinder->findParentType($foreach, Foreach_::class);
|
2020-10-20 15:07:01 +00:00
|
|
|
return $foreachParent !== null;
|
2020-02-01 16:04:38 +00:00
|
|
|
}
|
2020-01-04 13:56:07 +00:00
|
|
|
}
|