Skip to content

Commit

Permalink
[Php71] Skip already array destruct on ListToArrayDestructRector (#6460)
Browse files Browse the repository at this point in the history
* [Php71] Skip already array destruct on ListToArrayDestructRector

* fix
  • Loading branch information
samsonasik authored Nov 21, 2024
1 parent 20cc149 commit 57b9ccf
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;

final class SkipAlreadyArrayDestruct
{
public function run($line, $file, $trace)
{
[
'line' => $line,
'file' => $file,
] = $trace;
}
}
9 changes: 9 additions & 0 deletions rules/Php71/Rector/List_/ListToArrayDestructRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Stmt\Foreach_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
Expand Down Expand Up @@ -71,6 +72,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->var->getAttribute(AttributeKey::KIND) === List_::KIND_ARRAY) {
return null;
}

$list = $node->var;

$node->var = new Array_($list->items);
Expand All @@ -81,6 +86,10 @@ public function refactor(Node $node): ?Node
return null;
}

if ($node->valueVar->getAttribute(AttributeKey::KIND) === List_::KIND_ARRAY) {
return null;
}

$list = $node->valueVar;
$node->valueVar = new Array_($list->items);

Expand Down

0 comments on commit 57b9ccf

Please sign in to comment.