Skip to content

Commit d504e5f

Browse files
committed
fix
1 parent 2d05f43 commit d504e5f

1 file changed

Lines changed: 19 additions & 4 deletions

File tree

rules/Php71/Rector/List_/ListToArrayDestructRector.php

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Rector\Php71\Rector\List_;
66

77
use PhpParser\Node;
8+
use PhpParser\Node\ArrayItem;
89
use PhpParser\Node\Expr\Array_;
910
use PhpParser\Node\Expr\Assign;
1011
use PhpParser\Node\Expr\List_;
@@ -78,6 +79,11 @@ public function refactor(Node $node): ?Node
7879

7980
$list = $node->var;
8081

82+
// all list items must be set
83+
if ($this->hasPartialDestruct($list)) {
84+
return null;
85+
}
86+
8187
$node->var = new Array_($list->items);
8288
return $node;
8389
}
@@ -93,17 +99,26 @@ public function refactor(Node $node): ?Node
9399
$list = $node->valueVar;
94100

95101
// all list items must be set
96-
foreach ($list->items as $listItem) {
97-
if ($listItem === null) {
98-
return null;
99-
}
102+
if ($this->hasPartialDestruct($list)) {
103+
return null;
100104
}
101105

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

104108
return $node;
105109
}
106110

111+
private function hasPartialDestruct(List_ $list): bool
112+
{
113+
foreach ($list->items as $listItem) {
114+
if (! $listItem instanceof ArrayItem) {
115+
return true;
116+
}
117+
}
118+
119+
return false;
120+
}
121+
107122
public function provideMinPhpVersion(): int
108123
{
109124
return PhpVersionFeature::ARRAY_DESTRUCT;

0 commit comments

Comments
 (0)