Skip to content

Commit 91df931

Browse files
[Php84] Skip value extraction on ForeachToArray* rules (#7193)
* [Php84] Skip value extraction on ForeachToArray* rules * [ci-review] Rector Rectify --------- Co-authored-by: GitHub Action <actions@github.com>
1 parent 81fb372 commit 91df931

5 files changed

Lines changed: 37 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php84\Rector\Foreach_\ForeachToArrayAnyRector\Fixture;
4+
5+
class SkipValueExtraction
6+
{
7+
public function check($type)
8+
{
9+
$data = [
10+
['type' => 'foo'],
11+
];
12+
13+
foreach ($data as ['type' => $currentType]) {
14+
if ($currentType === $type) {
15+
return true;
16+
}
17+
}
18+
19+
return false;
20+
}
21+
}

rules/Php84/Rector/Foreach_/ForeachToArrayAllRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,10 @@ private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVa
198198
return false;
199199
}
200200

201+
if (! $foreach->valueVar instanceof Variable) {
202+
return false;
203+
}
204+
201205
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
202206
return $type->isArray()
203207
->yes();

rules/Php84/Rector/Foreach_/ForeachToArrayAnyRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,10 @@ private function isValidEarlyReturnForeachStructure(Foreach_ $foreach): bool
312312
return false;
313313
}
314314

315+
if (! $foreach->valueVar instanceof Variable) {
316+
return false;
317+
}
318+
315319
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
316320

317321
return $type->isArray()

rules/Php84/Rector/Foreach_/ForeachToArrayFindKeyRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVa
203203
return false;
204204
}
205205

206+
if (! $foreach->valueVar instanceof Variable) {
207+
return false;
208+
}
209+
206210
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
207211
return $type->isArray()
208212
->yes();

rules/Php84/Rector/Foreach_/ForeachToArrayFindRector.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,10 @@ private function isValidForeachStructure(Foreach_ $foreach, Variable $assignedVa
195195
return false;
196196
}
197197

198+
if (! $foreach->valueVar instanceof Variable) {
199+
return false;
200+
}
201+
198202
$type = $this->nodeTypeResolver->getNativeType($foreach->expr);
199203
return $type->isArray()
200204
->yes();

0 commit comments

Comments
 (0)