Skip to content

Commit

Permalink
[DeadCode] Skip used in Closure use on RemoveUnusedConstructorParamRe…
Browse files Browse the repository at this point in the history
…ctor (#2341)
  • Loading branch information
samsonasik committed May 21, 2022
1 parent b4120a8 commit fef1a03
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedConstructorParamRector\Fixture;

final class SkipUsedInClosureUse
{
public function __construct($hey, $man)
{
echo $hey . ' ';

(function () use ($man) {
echo $man;
})();
}
}
15 changes: 13 additions & 2 deletions src/NodeAnalyzer/ParamAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Rector\Core\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\NullableType;
Expand All @@ -28,11 +29,21 @@ public function isParamUsedInClassMethod(ClassMethod $classMethod, Param $param)
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($classMethod, function (Node $node) use (
$param
): bool {
if (! $node instanceof Variable) {
if (! $node instanceof Variable && ! $node instanceof Closure) {
return false;
}

return $this->nodeComparator->areNodesEqual($node, $param->var);
if ($node instanceof Variable) {
return $this->nodeComparator->areNodesEqual($node, $param->var);
}

foreach ($node->uses as $use) {
if ($this->nodeComparator->areNodesEqual($use->var, $param->var)) {
return true;
}
}

return false;
});
}

Expand Down

0 comments on commit fef1a03

Please sign in to comment.