2020-01-03 23:31:17 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\CodeQuality\Rector\FuncCall;
|
2020-01-03 23:31:17 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
|
|
|
use PhpParser\Node\Expr\Assign;
|
|
|
|
use PhpParser\Node\Expr\FuncCall;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2022-06-07 09:18:30 +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
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class ChangeArrayPushToArrayAssignRector extends AbstractRector
|
2020-01-03 23:31:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2020-01-03 23:31:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Change array_push() to direct variable assign', [new 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-06-07 08:22:29 +00:00
|
|
|
return [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-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?array
|
2020-01-03 23:31:17 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$node->expr instanceof 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-11-07 17:28:06 +00:00
|
|
|
if ($funcCall->isFirstClassCallable()) {
|
|
|
|
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();
|
2022-05-08 11:21:43 +00:00
|
|
|
if ($args === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-05-08 10:54:09 +00:00
|
|
|
$firstArg = \array_shift($args);
|
2022-05-08 11:21:43 +00:00
|
|
|
if ($args === []) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
$arrayDimFetch = new ArrayDimFetch($firstArg->value);
|
2022-05-08 10:54:09 +00:00
|
|
|
$newStmts = [];
|
|
|
|
foreach ($args as $key => $arg) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$assign = new Assign($arrayDimFetch, $arg->value);
|
|
|
|
$assignExpression = new 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
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasArraySpread(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
|
|
|
}
|