Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Skip used in compact() on RemoveUnusedConstructorParamRector #2345

Merged
merged 5 commits into from
May 22, 2022
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,13 @@
<?php

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

final class SkipUsedInCompact
{
public $data;

public function __construct($hey, $man)
{
$this->data = compact('hey', 'man');
}
}
6 changes: 3 additions & 3 deletions rules/DeadCode/NodeCollector/UnusedParameterResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@

use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\NodeManipulator\ClassMethodManipulator;
use Rector\Core\NodeAnalyzer\ParamAnalyzer;

final class UnusedParameterResolver
{
public function __construct(
private readonly ClassMethodManipulator $classMethodManipulator
private readonly ParamAnalyzer $paramAnalyzer
) {
}

Expand All @@ -30,7 +30,7 @@ public function resolve(ClassMethod $classMethod): array
continue;
}

if ($this->classMethodManipulator->isParameterUsedInClassMethod($param, $classMethod)) {
if ($this->paramAnalyzer->isParamUsedInClassMethod($classMethod, $param)) {
continue;
}

Expand Down
33 changes: 26 additions & 7 deletions src/NodeAnalyzer/ParamAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,25 @@
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\NullableType;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\ClassMethod;
use Rector\Core\NodeManipulator\FuncCallManipulator;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\PhpParser\Node\Value\ValueResolver;
use Rector\NodeNameResolver\NodeNameResolver;

final class ParamAnalyzer
{
public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeComparator $nodeComparator,
private readonly ValueResolver $valueResolver
private readonly ValueResolver $valueResolver,
private readonly NodeNameResolver $nodeNameResolver,
private readonly FuncCallManipulator $funcCallManipulator
) {
}

Expand All @@ -29,21 +34,24 @@ public function isParamUsedInClassMethod(ClassMethod $classMethod, Param $param)
return (bool) $this->betterNodeFinder->findFirstInFunctionLikeScoped($classMethod, function (Node $node) use (
$param
): bool {
if (! $node instanceof Variable && ! $node instanceof Closure) {
if (! $node instanceof Variable && ! $node instanceof Closure && ! $node instanceof FuncCall) {
return false;
}

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;
}
if ($node instanceof Closure) {
return $this->isVariableInClosureUses($node, $param->var);
}

return false;
if (! $this->nodeNameResolver->isName($node, 'compact')) {
return false;
}

$arguments = $this->funcCallManipulator->extractArgumentsFromCompactFuncCalls([$node]);
return $this->nodeNameResolver->isNames($param, $arguments);
});
}

Expand Down Expand Up @@ -78,4 +86,15 @@ public function hasDefaultNull(Param $param): bool
{
return $param->default instanceof ConstFetch && $this->valueResolver->isNull($param->default);
}

private function isVariableInClosureUses(Closure $closure, Variable $variable): bool
{
foreach ($closure->uses as $use) {
if ($this->nodeComparator->areNodesEqual($use->var, $variable)) {
return true;
}
}

return false;
}
}
29 changes: 0 additions & 29 deletions src/NodeManipulator/ClassMethodManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Rector\Core\NodeManipulator;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
Expand All @@ -14,7 +13,6 @@
use PHPStan\Reflection\ClassReflection;
use PHPStan\Type\ObjectType;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\PhpParser\Comparing\NodeComparator;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
use Rector\Core\ValueObject\MethodName;
Expand All @@ -27,37 +25,10 @@ public function __construct(
private readonly BetterNodeFinder $betterNodeFinder,
private readonly NodeNameResolver $nodeNameResolver,
private readonly NodeTypeResolver $nodeTypeResolver,
private readonly NodeComparator $nodeComparator,
private readonly FuncCallManipulator $funcCallManipulator,
private readonly ReflectionResolver $reflectionResolver
) {
}

public function isParameterUsedInClassMethod(Param $param, ClassMethod $classMethod): bool
{
$isUsedDirectly = (bool) $this->betterNodeFinder->findFirst(
(array) $classMethod->stmts,
fn (Node $node): bool => $this->nodeComparator->areNodesEqual($node, $param->var)
);

if ($isUsedDirectly) {
return true;
}

/** @var FuncCall[] $compactFuncCalls */
$compactFuncCalls = $this->betterNodeFinder->find((array) $classMethod->stmts, function (Node $node): bool {
if (! $node instanceof FuncCall) {
return false;
}

return $this->nodeNameResolver->isName($node, 'compact');
});

$arguments = $this->funcCallManipulator->extractArgumentsFromCompactFuncCalls($compactFuncCalls);

return $this->nodeNameResolver->isNames($param, $arguments);
}

public function isNamedConstructor(ClassMethod $classMethod): bool
{
if (! $this->nodeNameResolver->isName($classMethod, MethodName::CONSTRUCT)) {
Expand Down