Skip to content

Commit

Permalink
[TypeDeclaration] Skip casted to (array) on StrictArrayParamDimFetchR…
Browse files Browse the repository at this point in the history
…ector (#5356)

* [TypeDeclaration] Skip casted to (array) on StrictArrayParamDimFetchRector

* fix
  • Loading branch information
samsonasik authored Dec 12, 2023
1 parent dc9573d commit fea5d6c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector\Fixture;

final class SkipCastedToArray
{
public function resolve($params = [])
{
$params = (array) $params;

foreach ($params as $key => $param) {
echo $params[$key];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\AssignOp\Coalesce as AssignOpCoalesce;
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\Cast\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
Expand Down Expand Up @@ -202,7 +203,16 @@ private function shouldStop(Node $node, string $paramName): bool
return true;
}

return $this->isEchoed($node, $paramName);
return $this->isEchoedOrCasted($node, $paramName);
}

private function isEchoedOrCasted(Node $node, string $paramName): bool
{
if ($this->isEchoed($node, $paramName)) {
return true;
}

return $node instanceof Array_ && $node->expr instanceof Variable && $node->expr->name === $paramName;
}

private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bool
Expand Down

0 comments on commit fea5d6c

Please sign in to comment.