Skip to content

Commit

Permalink
[DeadCode] Skip used in compact() on RemoveUnusedConstructorParamRect…
Browse files Browse the repository at this point in the history
…or (#2345)

* [DeadCode] Skip used in compact() on RemoveUnusedConstructorParamRector

* Fixed 🎉

* phpstan

* phpstan

* [ci-review] Rector Rectify

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed May 22, 2022
1 parent 20731ae commit 68906c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 39 deletions.
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

0 comments on commit 68906c7

Please sign in to comment.