File tree Expand file tree Collapse file tree 2 files changed +29
-4
lines changed
rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture Expand file tree Collapse file tree 2 files changed +29
-4
lines changed Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55namespace Rector \Php71 \Rector \List_ ;
66
77use PhpParser \Node ;
8+ use PhpParser \Node \ArrayItem ;
89use PhpParser \Node \Expr \Array_ ;
910use PhpParser \Node \Expr \Assign ;
1011use 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 ;
You can’t perform that action at this time.
0 commit comments