2020-01-03 23:31:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* @see https://stackoverflow.com/questions/559844/whats-better-to-use-in-php-array-value-or-array-pusharray-value
|
|
|
|
*
|
|
|
|
* @see \Rector\CodeQuality\Tests\Rector\FuncCall\ChangeArrayPushToArrayAssignRector\ChangeArrayPushToArrayAssignRectorTest
|
|
|
|
*/
|
|
|
|
final class ChangeArrayPushToArrayAssignRector extends AbstractRector
|
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2020-01-03 23:31:17 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition(
|
|
|
|
'Change array_push() to direct variable assign',
|
|
|
|
[
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
2020-01-03 23:31:17 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$items = [];
|
|
|
|
array_push($items, $item);
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-01-03 23:31:17 +00:00
|
|
|
,
|
2020-11-16 17:50:38 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2020-01-03 23:31:17 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$items = [];
|
|
|
|
$items[] = $item;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2020-11-16 17:50:38 +00:00
|
|
|
),
|
|
|
|
|
|
|
|
]);
|
2020-01-03 23:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
|
|
|
return [FuncCall::class];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param FuncCall $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
if (! $this->isName($node, 'array_push')) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-16 11:36:07 +00:00
|
|
|
if ($this->hasArraySpread($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-01-03 23:31:17 +00:00
|
|
|
$arrayDimFetch = new ArrayDimFetch($node->args[0]->value);
|
2020-01-04 23:12:57 +00:00
|
|
|
|
|
|
|
$position = 1;
|
|
|
|
while (isset($node->args[$position])) {
|
|
|
|
$assign = new Assign($arrayDimFetch, $node->args[$position]->value);
|
2020-12-04 23:22:12 +00:00
|
|
|
$assignExpression = new Expression($assign);
|
2020-01-05 12:10:01 +00:00
|
|
|
|
|
|
|
// keep comments of first line
|
|
|
|
if ($position === 1) {
|
2020-12-20 12:56:43 +00:00
|
|
|
$this->mirrorComments($assignExpression, $node);
|
2020-01-05 12:10:01 +00:00
|
|
|
}
|
|
|
|
|
2020-12-04 23:22:12 +00:00
|
|
|
$this->addNodeAfterNode($assignExpression, $node);
|
2020-01-04 23:12:57 +00:00
|
|
|
|
|
|
|
++$position;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->removeNode($node);
|
|
|
|
|
|
|
|
return null;
|
2020-01-03 23:31:17 +00:00
|
|
|
}
|
2020-04-16 11:36:07 +00:00
|
|
|
|
|
|
|
private function hasArraySpread(FuncCall $funcCall): bool
|
|
|
|
{
|
|
|
|
foreach ($funcCall->args as $arg) {
|
|
|
|
/** @var Arg $arg */
|
|
|
|
if ($arg->unpack) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-03 23:31:17 +00:00
|
|
|
}
|