Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\DeadCode\Rector\Cast\RecastingRemovalRector\Fixture;

class SkipArrayDimFetch
{
public function run()
{
$list = ['id' => 1, 'name' => 'Tom'];

$id = (int) $list['id'];
$name = (string) $list['name'];

return [$id, $name];
}
}
6 changes: 6 additions & 0 deletions rules/DeadCode/Rector/Cast/RecastingRemovalRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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_;
Expand Down Expand Up @@ -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) {
Expand Down
Loading