nodeUsageFinder = $nodeUsageFinder; $this->foreachAnalyzer = $foreachAnalyzer; } 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> */ 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; } // must be empty array, otherwise it will false override $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; } }