2020-01-03 23:31:17 +00:00
< ? php
2021-05-09 20:15:43 +00:00
declare ( strict_types = 1 );
2020-01-03 23:31:17 +00:00
namespace Rector\CodeQuality\Rector\FuncCall ;
use PhpParser\Node ;
2020-04-16 11:36:07 +00:00
use PhpParser\Node\Arg ;
2020-01-03 23:31:17 +00:00
use PhpParser\Node\Expr\ArrayDimFetch ;
use PhpParser\Node\Expr\Assign ;
use PhpParser\Node\Expr\FuncCall ;
2020-01-05 12:10:01 +00:00
use PhpParser\Node\Stmt\Expression ;
2020-02-06 21:48:18 +00:00
use Rector\Core\Rector\AbstractRector ;
2020-11-16 17:50:38 +00:00
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition ;
2020-01-03 23:31:17 +00:00
/**
2021-04-10 18:47:17 +00:00
* @ changelog https :// stackoverflow . com / questions / 559844 / whats - better - to - use - in - php - array - value - or - array - pusharray - value
2020-01-03 23:31:17 +00:00
*
2021-03-12 22:20:25 +00:00
* @ see \Rector\Tests\CodeQuality\Rector\FuncCall\ChangeArrayPushToArrayAssignRector\ChangeArrayPushToArrayAssignRectorTest
2020-01-03 23:31:17 +00:00
*/
2021-05-10 22:23:08 +00:00
final class ChangeArrayPushToArrayAssignRector extends \Rector\Core\Rector\AbstractRector
2020-01-03 23:31:17 +00:00
{
2021-05-10 22:23:08 +00:00
public function getRuleDefinition () : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
2020-01-03 23:31:17 +00:00
{
2021-05-10 22:23:08 +00:00
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition ( 'Change array_push() to direct variable assign' , [ new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample ( <<< 'CODE_SAMPLE'
2022-05-08 10:54:09 +00:00
$items = [];
array_push ( $items , $item );
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
, <<< 'CODE_SAMPLE'
2022-05-08 10:54:09 +00:00
$items = [];
$items [] = $item ;
2020-09-15 08:23:13 +00:00
CODE_SAMPLE
2021-05-09 20:15:43 +00:00
)]);
2020-01-03 23:31:17 +00:00
}
/**
2021-02-27 00:06:15 +00:00
* @ return array < class - string < Node >>
2020-01-03 23:31:17 +00:00
*/
2021-05-09 20:15:43 +00:00
public function getNodeTypes () : array
2020-01-03 23:31:17 +00:00
{
2022-05-08 10:54:09 +00:00
return [ \PhpParser\Node\Stmt\Expression :: class ];
2020-01-03 23:31:17 +00:00
}
/**
2022-05-08 10:54:09 +00:00
* @ param Expression [] $node
* @ param Expression [] | null $node
2020-01-03 23:31:17 +00:00
*/
2022-05-08 10:54:09 +00:00
public function refactor ( \PhpParser\Node $node ) : ? array
2020-01-03 23:31:17 +00:00
{
2022-05-08 10:54:09 +00:00
if ( ! $node -> expr instanceof \PhpParser\Node\Expr\FuncCall ) {
2021-03-14 16:15:38 +00:00
return null ;
}
2022-05-08 10:54:09 +00:00
$funcCall = $node -> expr ;
if ( ! $this -> isName ( $funcCall , 'array_push' )) {
2021-09-27 15:43:15 +00:00
return null ;
}
2022-05-08 10:54:09 +00:00
if ( $this -> hasArraySpread ( $funcCall )) {
2021-09-27 15:43:15 +00:00
return null ;
}
2022-05-08 10:54:09 +00:00
$args = $funcCall -> getArgs ();
/** @var Arg $firstArg */
$firstArg = \array_shift ( $args );
$arrayDimFetch = new \PhpParser\Node\Expr\ArrayDimFetch ( $firstArg -> value );
$newStmts = [];
foreach ( $args as $key => $arg ) {
$assign = new \PhpParser\Node\Expr\Assign ( $arrayDimFetch , $arg -> value );
2021-05-10 22:23:08 +00:00
$assignExpression = new \PhpParser\Node\Stmt\Expression ( $assign );
2022-05-08 10:54:09 +00:00
$newStmts [] = $assignExpression ;
2020-01-05 12:10:01 +00:00
// keep comments of first line
2022-05-08 10:54:09 +00:00
if ( $key === 0 ) {
2020-12-20 12:56:43 +00:00
$this -> mirrorComments ( $assignExpression , $node );
2020-01-05 12:10:01 +00:00
}
2020-01-04 23:12:57 +00:00
}
2022-05-08 10:54:09 +00:00
return $newStmts ;
2020-01-03 23:31:17 +00:00
}
2021-05-10 22:23:08 +00:00
private function hasArraySpread ( \PhpParser\Node\Expr\FuncCall $funcCall ) : bool
2020-04-16 11:36:07 +00:00
{
2022-05-08 10:54:09 +00:00
foreach ( $funcCall -> getArgs () as $arg ) {
2020-04-16 11:36:07 +00:00
if ( $arg -> unpack ) {
2021-05-09 20:15:43 +00:00
return \true ;
2020-04-16 11:36:07 +00:00
}
}
2021-05-09 20:15:43 +00:00
return \false ;
2020-04-16 11:36:07 +00:00
}
2020-01-03 23:31:17 +00:00
}