2020-01-04 13:56:07 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-01-04 13:56:07 +00:00
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
/**
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector\ForeachItemsAssignToEmptyArrayToAssignRectorTest
2020-01-04 13:56:07 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ForeachItemsAssignToEmptyArrayToAssignRector extends \Rector\Core\Rector\AbstractRector
2020-01-04 13:56:07 +00:00
{
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 ;
/**
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 ;
2021-05-10 22:23:08 +00:00
public function __construct ( \Rector\ReadWrite\NodeFinder\NodeUsageFinder $nodeUsageFinder , \Rector\CodeQuality\NodeAnalyzer\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
}
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-01-04 13:56:07 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change foreach() items assign to empty array to direct assign' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\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
2021-05-09 20:15:43 +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
2021-05-09 20:15:43 +00:00
)]);
2020-01-04 13:56:07 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-01-04 13:56:07 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-01-04 13:56:07 +00:00
{
2021-05-10 22:23:08 +00:00
return [ \PhpParser\Node\Stmt\Foreach_ :: class ];
2020-01-04 13:56:07 +00:00
}
/**
* @ param Foreach_ $node
*/
2021-05-10 22:23:08 +00:00
public function refactor ( \PhpParser\Node $node ) : ? \PhpParser\Node
2020-01-04 13:56:07 +00:00
{
2021-02-23 01:25:34 +00:00
$assignVariable = $this -> foreachAnalyzer -> matchAssignItemsOnlyForeachArrayVariable ( $node );
2021-05-10 22:23:08 +00:00
if ( ! $assignVariable instanceof \PhpParser\Node\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-05-10 22:23:08 +00:00
if ( ! $previousDeclaration instanceof \PhpParser\Node ) {
2020-01-08 13:36:53 +00:00
return null ;
}
2021-05-10 22:23:08 +00:00
$previousDeclarationParentNode = $previousDeclaration -> getAttribute ( \Rector\NodeTypeResolver\Node\AttributeKey :: PARENT_NODE );
if ( ! $previousDeclarationParentNode instanceof \PhpParser\Node\Expr\Assign ) {
2020-01-04 13:56:07 +00:00
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 ;
}
2021-05-10 22:23:08 +00:00
return new \PhpParser\Node\Expr\Assign ( $assignVariable , $node -> expr );
2020-01-04 13:56:07 +00:00
}
2021-05-10 22:23:08 +00:00
private function shouldSkipAsPartOfNestedForeach ( \PhpParser\Node\Stmt\Foreach_ $foreach ) : bool
2020-01-08 13:36:53 +00:00
{
2021-05-10 22:23:08 +00:00
$foreachParent = $this -> betterNodeFinder -> findParentType ( $foreach , \PhpParser\Node\Stmt\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
}