Skip to content

Commit

Permalink
Make ChangeArrayPushToArrayAssignRector use stmts (#2265)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed May 8, 2022
1 parent e341cc2 commit 88c9b8e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 44 deletions.
15 changes: 15 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -595,3 +595,18 @@ parameters:

# known value
- '#Method Rector\\Core\\Php\\PhpVersionProvider\:\:provide\(\) should return 50200\|50300\|50400\|50500\|50600\|70000\|70100\|70200\|70300\|70400\|80000\|80100\|80200\|100000 but returns int#'

-
message: '#PHPDoc tag @param for parameter \$node with type array<PhpParser\\Node\\Stmt\\Expression>\|null is incompatible with native type PhpParser\\Node#'
paths:
- rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php

-
message: '#Method Rector\\CodeQuality\\Rector\\FuncCall\\ChangeArrayPushToArrayAssignRector\:\:refactor\(\) return type has no value type specified in iterable type array#'
paths:
- rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php

-
message: '#Access to an undefined property PhpParser\\Node\:\:\$expr#'
paths:
- rules/CodeQuality/Rector/FuncCall/ChangeArrayPushToArrayAssignRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -29,25 +28,13 @@ public function getRuleDefinition(): RuleDefinition
[
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$items = [];
array_push($items, $item);
}
}
$items = [];
array_push($items, $item);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$items = [];
$items[] = $item;
}
}
$items = [];
$items[] = $item;
CODE_SAMPLE
),
]
Expand All @@ -59,61 +46,54 @@ public function run()
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
return [Expression::class];
}

/**
* @param FuncCall $node
* @param Expression[] $node
* @param Expression[]|null $node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?array
{
if (! $this->isName($node, 'array_push')) {
if (! $node->expr instanceof FuncCall) {
return null;
}

if ($this->hasArraySpread($node)) {
$funcCall = $node->expr;
if (! $this->isName($funcCall, 'array_push')) {
return null;
}

$parent = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parent instanceof Expression) {
if ($this->hasArraySpread($funcCall)) {
return null;
}

if (! isset($node->args[0])) {
return null;
}
$args = $funcCall->getArgs();

if (! $node->args[0] instanceof Arg) {
return null;
}
/** @var Arg $firstArg */
$firstArg = array_shift($args);

$arrayDimFetch = new ArrayDimFetch($node->args[0]->value);
$arrayDimFetch = new ArrayDimFetch($firstArg->value);

$position = 1;
while (isset($node->args[$position]) && $node->args[$position] instanceof Arg) {
$assign = new Assign($arrayDimFetch, $node->args[$position]->value);
$newStmts = [];

foreach ($args as $key => $arg) {
$assign = new Assign($arrayDimFetch, $arg->value);
$assignExpression = new Expression($assign);
$newStmts[] = $assignExpression;

// keep comments of first line
if ($position === 1) {
if ($key === 0) {
$this->mirrorComments($assignExpression, $node);
}

$this->nodesToAddCollector->addNodeAfterNode($assignExpression, $node);

++$position;
}

$this->removeNode($node);

return null;
return $newStmts;
}

private function hasArraySpread(FuncCall $funcCall): bool
{
foreach ($funcCall->args as $arg) {
/** @var Arg $arg */
foreach ($funcCall->getArgs() as $arg) {
if ($arg->unpack) {
return true;
}
Expand Down

0 comments on commit 88c9b8e

Please sign in to comment.