diff --git a/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc new file mode 100644 index 00000000000..2a0790987ab --- /dev/null +++ b/rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc @@ -0,0 +1,10 @@ +id); + } +} diff --git a/rules/Php71/Rector/List_/ListToArrayDestructRector.php b/rules/Php71/Rector/List_/ListToArrayDestructRector.php index ce7d469a1a6..00f97b8ab16 100644 --- a/rules/Php71/Rector/List_/ListToArrayDestructRector.php +++ b/rules/Php71/Rector/List_/ListToArrayDestructRector.php @@ -5,6 +5,7 @@ namespace Rector\Php71\Rector\List_; use PhpParser\Node; +use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\Assign; use PhpParser\Node\Expr\List_; @@ -78,6 +79,11 @@ public function refactor(Node $node): ?Node $list = $node->var; + // all list items must be set + if ($this->hasPartialDestruct($list)) { + return null; + } + $node->var = new Array_($list->items); return $node; } @@ -93,10 +99,8 @@ public function refactor(Node $node): ?Node $list = $node->valueVar; // all list items must be set - foreach ($list->items as $listItem) { - if ($listItem === null) { - return null; - } + if ($this->hasPartialDestruct($list)) { + return null; } $node->valueVar = new Array_($list->items); @@ -104,6 +108,17 @@ public function refactor(Node $node): ?Node return $node; } + private function hasPartialDestruct(List_ $list): bool + { + foreach ($list->items as $listItem) { + if (! $listItem instanceof ArrayItem) { + return true; + } + } + + return false; + } + public function provideMinPhpVersion(): int { return PhpVersionFeature::ARRAY_DESTRUCT;