[DeadCode] Skip used in Closure use on RemoveUnusedConstructorParamRector (#2341)

This commit is contained in:
Abdul Malik Ikhsan 2022-05-21 23:24:10 +07:00 committed by GitHub
parent b4120a8850
commit fef1a03955
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -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;
})();
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
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;
@ -28,11 +29,21 @@ final class ParamAnalyzer
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;
});
}