-
-
Notifications
You must be signed in to change notification settings - Fork 699
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DeadCode] Fix binary different nesting in RemoveOverriddenValuesRect…
…or (#4422) * decopule NodeByTypeAndPositionCollector * decouple VariableUseFinder
- Loading branch information
1 parent
09dc6ce
commit 16031da
Showing
7 changed files
with
260 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\FlowControl; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use Rector\Core\PhpParser\Node\BetterNodeFinder; | ||
use Rector\Core\PhpParser\Printer\BetterStandardPrinter; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class VariableUseFinder | ||
{ | ||
/** | ||
* @var BetterNodeFinder | ||
*/ | ||
private $betterNodeFinder; | ||
|
||
/** | ||
* @var NodeNameResolver | ||
*/ | ||
private $nodeNameResolver; | ||
|
||
/** | ||
* @var BetterStandardPrinter | ||
*/ | ||
private $betterStandardPrinter; | ||
|
||
public function __construct( | ||
BetterNodeFinder $betterNodeFinder, | ||
NodeNameResolver $nodeNameResolver, | ||
BetterStandardPrinter $betterStandardPrinter | ||
) { | ||
$this->betterNodeFinder = $betterNodeFinder; | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
$this->betterStandardPrinter = $betterStandardPrinter; | ||
} | ||
|
||
/** | ||
* @param Variable[] $assignedVariables | ||
* @return Variable[] | ||
*/ | ||
public function resolveUsedVariables(Node $node, array $assignedVariables): array | ||
{ | ||
return $this->betterNodeFinder->find($node, function (Node $node) use ($assignedVariables): bool { | ||
if (! $node instanceof Variable) { | ||
return false; | ||
} | ||
|
||
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); | ||
// is the left assign - not use of one | ||
if ($parentNode instanceof Assign && ($parentNode->var instanceof Variable && $parentNode->var === $node)) { | ||
return false; | ||
} | ||
|
||
// simple variable only | ||
if ($this->nodeNameResolver->getName($node) === null) { | ||
return false; | ||
} | ||
|
||
return $this->betterStandardPrinter->isNodeEqual($node, $assignedVariables); | ||
}); | ||
} | ||
} |
99 changes: 99 additions & 0 deletions
99
rules/dead-code/src/NodeCollector/NodeByTypeAndPositionCollector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\DeadCode\NodeCollector; | ||
|
||
use PhpParser\Node\Expr\Assign; | ||
use PhpParser\Node\Expr\Variable; | ||
use PhpParser\Node\FunctionLike; | ||
use Rector\DeadCode\ValueObject\VariableNodeUse; | ||
use Rector\NodeNameResolver\NodeNameResolver; | ||
use Rector\NodeNestingScope\FlowOfControlLocator; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
|
||
final class NodeByTypeAndPositionCollector | ||
{ | ||
/** | ||
* @var FlowOfControlLocator | ||
*/ | ||
private $flowOfControlLocator; | ||
|
||
/** | ||
* @var NodeNameResolver | ||
*/ | ||
private $nodeNameResolver; | ||
|
||
public function __construct(FlowOfControlLocator $flowOfControlLocator, NodeNameResolver $nodeNameResolver) | ||
{ | ||
$this->flowOfControlLocator = $flowOfControlLocator; | ||
$this->nodeNameResolver = $nodeNameResolver; | ||
} | ||
|
||
/** | ||
* @param Variable[] $assignedVariables | ||
* @param Variable[] $assignedVariablesUse | ||
* @return VariableNodeUse[] | ||
*/ | ||
public function collectNodesByTypeAndPosition( | ||
array $assignedVariables, | ||
array $assignedVariablesUse, | ||
FunctionLike $functionLike | ||
): array { | ||
$nodesByTypeAndPosition = []; | ||
|
||
foreach ($assignedVariables as $assignedVariable) { | ||
/** @var int $startTokenPos */ | ||
$startTokenPos = $assignedVariable->getAttribute(AttributeKey::START_TOKEN_POSITION); | ||
|
||
// not in different scope, than previous one - e.g. if/while/else... | ||
// get nesting level to $classMethodNode | ||
/** @var Assign $assign */ | ||
$assign = $assignedVariable->getAttribute(AttributeKey::PARENT_NODE); | ||
$nestingHash = $this->flowOfControlLocator->resolveNestingHashFromFunctionLike($functionLike, $assign); | ||
|
||
/** @var string $variableName */ | ||
$variableName = $this->nodeNameResolver->getName($assignedVariable); | ||
|
||
$nodesByTypeAndPosition[] = new VariableNodeUse( | ||
$startTokenPos, | ||
$variableName, | ||
VariableNodeUse::TYPE_ASSIGN, | ||
$assignedVariable, | ||
$nestingHash | ||
); | ||
} | ||
|
||
foreach ($assignedVariablesUse as $assignedVariableUse) { | ||
/** @var int $startTokenPos */ | ||
$startTokenPos = $assignedVariableUse->getAttribute(AttributeKey::START_TOKEN_POSITION); | ||
|
||
/** @var string $variableName */ | ||
$variableName = $this->nodeNameResolver->getName($assignedVariableUse); | ||
|
||
$nodesByTypeAndPosition[] = new VariableNodeUse( | ||
$startTokenPos, | ||
$variableName, | ||
VariableNodeUse::TYPE_USE, | ||
$assignedVariableUse | ||
); | ||
} | ||
|
||
return $this->sortByStart($nodesByTypeAndPosition); | ||
} | ||
|
||
/** | ||
* @param VariableNodeUse[] $nodesByTypeAndPosition | ||
* @return VariableNodeUse[] | ||
*/ | ||
private function sortByStart(array $nodesByTypeAndPosition): array | ||
{ | ||
usort( | ||
$nodesByTypeAndPosition, | ||
function (VariableNodeUse $firstVariableNodeUse, VariableNodeUse $secondVariableNodeUse): int { | ||
return $firstVariableNodeUse->getStartTokenPosition() <=> $secondVariableNodeUse->getStartTokenPosition(); | ||
} | ||
); | ||
return $nodesByTypeAndPosition; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.