diff --git a/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_array_dim_fetch.php.inc b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_array_dim_fetch.php.inc new file mode 100644 index 00000000000..cddd11fa0f6 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Cast/RecastingRemovalRector/Fixture/skip_array_dim_fetch.php.inc @@ -0,0 +1,16 @@ + 1, 'name' => 'Tom']; + + $id = (int) $list['id']; + $name = (string) $list['name']; + + return [$id, $name]; + } +} diff --git a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php index 1d8f6184cc5..b675b2f3e13 100644 --- a/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php +++ b/rules/DeadCode/Rector/Cast/RecastingRemovalRector.php @@ -6,6 +6,7 @@ use PhpParser\Node; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\ArrayDimFetch; use PhpParser\Node\Expr\Cast; use PhpParser\Node\Expr\Cast\Array_; use PhpParser\Node\Expr\Cast\Bool_; @@ -143,6 +144,11 @@ private function shouldSkipCall(Expr $expr): bool private function shouldSkip(Expr $expr): bool { + // array dim fetch value can be anything, the type is often inaccurate + if ($expr instanceof ArrayDimFetch) { + return true; + } + $type = $this->getType($expr); if ($type instanceof UnionType) {