Skip to content

Commit 9a7d321

Browse files
authored
[Php71] Convert partial list() destruct in ListToArrayDestructRector (#9028)
1 parent c6678ce commit 9a7d321

5 files changed

Lines changed: 66 additions & 36 deletions

File tree

‎phpstan.neon‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,6 @@ parameters:
130130
identifier: argument.type
131131
message: '#Parameter \#1 \$name of method PHPStan\\BetterReflection\\Reflection\\Adapter\\ReflectionClass\:\:getAttributes\(\) expects class\-string\|null, string given#'
132132

133-
# false positive, checked above
134-
-
135-
identifier: argument.type
136-
message: '#Parameter \#1 \$items of class PhpParser\\Node\\Expr\\Array_ constructor expects array<PhpParser\\Node\\ArrayItem>, array<PhpParser\\Node\\ArrayItem\|null> given#'
137-
path: rules/Php71/Rector/List_/ListToArrayDestructRector.php
138-
139133
# scope vs mutating scope
140134
-
141135
identifier: argument.type
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;
4+
5+
final class PartialDestruct
6+
{
7+
public function modify_shipping_rate_cost($cost, $rate) {
8+
list(, $instance_id) = explode(':', $rate->id);
9+
}
10+
}
11+
12+
?>
13+
-----
14+
<?php
15+
16+
namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;
17+
18+
final class PartialDestruct
19+
{
20+
public function modify_shipping_rate_cost($cost, $rate) {
21+
[, $instance_id] = explode(':', $rate->id);
22+
}
23+
}
24+
25+
?>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;
4+
5+
final class PartialDestructInForeach
6+
{
7+
public function run(array $rows)
8+
{
9+
foreach ($rows as list(, $second)) {
10+
echo $second;
11+
}
12+
13+
list($first, , $third) = $rows;
14+
}
15+
}
16+
17+
?>
18+
-----
19+
<?php
20+
21+
namespace Rector\Tests\Php71\Rector\List_\ListToArrayDestructRector\Fixture;
22+
23+
final class PartialDestructInForeach
24+
{
25+
public function run(array $rows)
26+
{
27+
foreach ($rows as [, $second]) {
28+
echo $second;
29+
}
30+
31+
[$first, , $third] = $rows;
32+
}
33+
}
34+
35+
?>

‎rules-tests/Php71/Rector/List_/ListToArrayDestructRector/Fixture/skip_partial_destruct.php.inc‎

Lines changed: 0 additions & 10 deletions
This file was deleted.

‎rules/Php71/Rector/List_/ListToArrayDestructRector.php‎

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

77
use PhpParser\Node;
8-
use PhpParser\Node\ArrayItem;
9-
use PhpParser\Node\Expr\Array_;
108
use PhpParser\Node\Expr\Assign;
119
use PhpParser\Node\Expr\List_;
1210
use PhpParser\Node\Stmt\Foreach_;
@@ -77,14 +75,7 @@ public function refactor(Node $node): ?Node
7775
return null;
7876
}
7977

80-
$list = $node->var;
81-
82-
// all list items must be set
83-
if ($this->hasPartialDestruct($list)) {
84-
return null;
85-
}
86-
87-
$node->var = new Array_($list->items);
78+
$node->var = $this->createShortList($node->var);
8879
return $node;
8980
}
9081

@@ -96,14 +87,7 @@ public function refactor(Node $node): ?Node
9687
return null;
9788
}
9889

99-
$list = $node->valueVar;
100-
101-
// all list items must be set
102-
if ($this->hasPartialDestruct($list)) {
103-
return null;
104-
}
105-
106-
$node->valueVar = new Array_($list->items);
90+
$node->valueVar = $this->createShortList($node->valueVar);
10791

10892
return $node;
10993
}
@@ -113,8 +97,10 @@ public function provideMinPhpVersion(): int
11397
return PhpVersion::PHP_71;
11498
}
11599

116-
private function hasPartialDestruct(List_ $list): bool
100+
private function createShortList(List_ $list): List_
117101
{
118-
return array_any($list->items, fn (?ArrayItem $arrayItem): bool => ! $arrayItem instanceof ArrayItem);
102+
return new List_($list->items, [
103+
AttributeKey::KIND => List_::KIND_ARRAY,
104+
]);
119105
}
120106
}

0 commit comments

Comments
 (0)