Skip to content

Commit

Permalink
[TypeDeclaration] Skip echo-ed on StrictArrayParamDimFetchRector (#5260)
Browse files Browse the repository at this point in the history
* [TypeDeclaration] Skip echo-ed on StrictArrayParamDimFetchRector

* Fixed 🎉

* fix
  • Loading branch information
samsonasik authored Nov 18, 2023
1 parent 03c7ab7 commit 6a2af18
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

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

final class SkipEchoed
{
public function run($param)
{
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ final class WithDefaultArrayType
{
public function run($param = [])
{
if (isset($param['bar'])) {
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}

Expand All @@ -24,11 +22,9 @@ final class WithDefaultArrayType
{
public function run(array $param = [])
{
if (isset($param['bar'])) {
if (isset($param['bar'])) {
echo $param['bar'];
}

echo $param;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Echo_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\NodeTraverser;
Expand Down Expand Up @@ -155,6 +156,21 @@ private function isParamAccessedArrayDimFetch(Param $param, ClassMethod|Function
return $isParamAccessedArrayDimFetch;
}

private function isEchoed(Node $node, string $paramName): bool
{
if (! $node instanceof Echo_) {
return false;
}

foreach ($node->exprs as $expr) {
if ($expr instanceof Variable && $this->isName($expr, $paramName)) {
return true;
}
}

return false;
}

private function shouldStop(Node $node, string $paramName): bool
{
$nodeToCheck = null;
Expand All @@ -178,14 +194,27 @@ private function shouldStop(Node $node, string $paramName): bool
$nodeToCheck = $node->var;
}

if ($nodeToCheck instanceof MethodCall) {
return $nodeToCheck->var instanceof Variable && $this->isName($nodeToCheck->var, $paramName);
if ($this->isMethodCallOrArrayDimFetch($paramName, $nodeToCheck)) {
return true;
}

if ($nodeToCheck instanceof Variable && $this->isName($nodeToCheck, $paramName)) {
return true;
}

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

private function isMethodCallOrArrayDimFetch(string $paramName, ?Node $node): bool
{
if ($node instanceof MethodCall) {
return $node->var instanceof Variable && $this->isName($node->var, $paramName);
}

if ($nodeToCheck instanceof ArrayDimFetch) {
return $nodeToCheck->var instanceof Variable && $this->isName($nodeToCheck->var, $paramName);
if ($node instanceof ArrayDimFetch) {
return $node->var instanceof Variable && $this->isName($node->var, $paramName);
}

return $nodeToCheck instanceof Variable && $this->isName($nodeToCheck, $paramName);
return false;
}
}

0 comments on commit 6a2af18

Please sign in to comment.