Skip to content

Commit e4081fe

Browse files
authored
[Php71] Handle crash on partial destruct on assign var on ListToArrayDestructRector (#7909)
* [Php71] Handle crash on partial destruct on ListToArrayDestructRector * fix
1 parent 5ed1ae3 commit e4081fe

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;
4+
5+
final class SkipPartialDestruct
6+
{
7+
public function modify_shipping_rate_cost($cost, $rate) {
8+
list(, $instance_id) = explode(':', $rate->id);
9+
}
10+
}

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)