Skip to content

Commit 209ae32

Browse files
authored
[DeadCode] Skip RecastingRemovalRector on array dim fetch value (#8262)
1 parent 98e63f8 commit 209ae32

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Fixture;
4+
5+
class SkipArrayDimFetch
6+
{
7+
public function run()
8+
{
9+
$list = ['id' => 1, 'name' => 'Tom'];
10+
11+
$id = (int) $list['id'];
12+
$name = (string) $list['name'];
13+
14+
return [$id, $name];
15+
}
16+
}

rules/DeadCode/Rector/Cast/RecastingRemovalRector.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Expr;
9+
use PhpParser\Node\Expr\ArrayDimFetch;
910
use PhpParser\Node\Expr\Cast;
1011
use PhpParser\Node\Expr\Cast\Array_;
1112
use PhpParser\Node\Expr\Cast\Bool_;
@@ -143,6 +144,11 @@ private function shouldSkipCall(Expr $expr): bool
143144

144145
private function shouldSkip(Expr $expr): bool
145146
{
147+
// array dim fetch value can be anything, the type is often inaccurate
148+
if ($expr instanceof ArrayDimFetch) {
149+
return true;
150+
}
151+
146152
$type = $this->getType($expr);
147153

148154
if ($type instanceof UnionType) {

0 commit comments

Comments
 (0)