rector/rules/Removing/NodeManipulator/ComplexNodeRemover.php

229 lines
9.2 KiB
PHP
Raw Normal View History

<?php
declare (strict_types=1);
2022-06-06 16:43:29 +00:00
namespace RectorPrefix20220606\Rector\Removing\NodeManipulator;
2022-06-06 16:43:29 +00:00
use RectorPrefix20220606\PhpParser\Node;
use RectorPrefix20220606\PhpParser\Node\Expr;
use RectorPrefix20220606\PhpParser\Node\Expr\ArrayDimFetch;
use RectorPrefix20220606\PhpParser\Node\Expr\Assign;
use RectorPrefix20220606\PhpParser\Node\Expr\PropertyFetch;
use RectorPrefix20220606\PhpParser\Node\Expr\StaticPropertyFetch;
use RectorPrefix20220606\PhpParser\Node\Expr\Variable;
use RectorPrefix20220606\PhpParser\Node\Param;
use RectorPrefix20220606\PhpParser\Node\Stmt\Class_;
use RectorPrefix20220606\PhpParser\Node\Stmt\ClassMethod;
use RectorPrefix20220606\PhpParser\Node\Stmt\Expression;
use RectorPrefix20220606\PhpParser\Node\Stmt\Property;
use RectorPrefix20220606\Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
use RectorPrefix20220606\Rector\Core\PhpParser\Node\BetterNodeFinder;
use RectorPrefix20220606\Rector\Core\ValueObject\MethodName;
use RectorPrefix20220606\Rector\DeadCode\SideEffect\SideEffectNodeDetector;
use RectorPrefix20220606\Rector\NodeNameResolver\NodeNameResolver;
use RectorPrefix20220606\Rector\NodeRemoval\NodeRemover;
use RectorPrefix20220606\Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
final class ComplexNodeRemover
{
/**
* @readonly
* @var \Rector\NodeNameResolver\NodeNameResolver
*/
private $nodeNameResolver;
/**
* @readonly
* @var \Rector\Core\PhpParser\Node\BetterNodeFinder
*/
private $betterNodeFinder;
/**
* @readonly
* @var \Rector\NodeRemoval\NodeRemover
*/
private $nodeRemover;
/**
* @readonly
* @var \Rector\DeadCode\SideEffect\SideEffectNodeDetector
*/
private $sideEffectNodeDetector;
/**
* @readonly
* @var \Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser
*/
private $simpleCallableNodeTraverser;
/**
* @readonly
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
*/
private $propertyFetchAnalyzer;
2022-06-06 16:43:29 +00:00
public function __construct(NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder, NodeRemover $nodeRemover, SideEffectNodeDetector $sideEffectNodeDetector, SimpleCallableNodeTraverser $simpleCallableNodeTraverser, PropertyFetchAnalyzer $propertyFetchAnalyzer)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->betterNodeFinder = $betterNodeFinder;
$this->nodeRemover = $nodeRemover;
$this->sideEffectNodeDetector = $sideEffectNodeDetector;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
}
2022-06-06 16:43:29 +00:00
public function removePropertyAndUsages(Class_ $class, Property $property, bool $removeAssignSideEffect) : void
2020-04-29 13:42:11 +02:00
{
$propertyName = $this->nodeNameResolver->getName($property);
$hasSideEffect = \false;
$isPartOfAnotherAssign = \false;
2022-06-06 16:43:29 +00:00
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($class->stmts, function (Node $node) use($removeAssignSideEffect, $propertyName, &$hasSideEffect, &$isPartOfAnotherAssign) {
// here should be checked all expr like stmts that can hold assign, e.f. if, foreach etc. etc.
2022-06-06 16:43:29 +00:00
if (!$node instanceof Expression) {
return null;
}
$nodeExpr = $node->expr;
// remove direct assigns
2022-06-06 16:43:29 +00:00
if (!$nodeExpr instanceof Assign) {
return null;
}
$assign = $nodeExpr;
// skip double assigns
2022-06-06 16:43:29 +00:00
if ($assign->expr instanceof Assign) {
$isPartOfAnotherAssign = \true;
return null;
}
$propertyFetches = $this->resolvePropertyFetchFromDimFetch($assign->var);
if ($propertyFetches === []) {
return null;
}
foreach ($propertyFetches as $propertyFetch) {
if ($this->nodeNameResolver->isName($propertyFetch->name, $propertyName)) {
if (!$removeAssignSideEffect && $this->sideEffectNodeDetector->detect($assign->expr)) {
$hasSideEffect = \true;
return null;
}
$this->nodeRemover->removeNode($node);
}
}
return null;
});
// do not remove anyhting in case of side-effect
if ($hasSideEffect) {
return;
}
if ($isPartOfAnotherAssign) {
return;
}
$this->removeConstructorDependency($class, $propertyName);
$this->nodeRemover->removeNode($property);
}
/**
* @param Param[] $params
* @param int[] $paramKeysToBeRemoved
* @return int[]
*/
public function processRemoveParamWithKeys(array $params, array $paramKeysToBeRemoved) : array
{
$totalKeys = \count($params) - 1;
$removedParamKeys = [];
foreach ($paramKeysToBeRemoved as $paramKeyToBeRemoved) {
$startNextKey = $paramKeyToBeRemoved + 1;
for ($nextKey = $startNextKey; $nextKey <= $totalKeys; ++$nextKey) {
if (!isset($params[$nextKey])) {
// no next param, break the inner loop, remove the param
break;
}
if (\in_array($nextKey, $paramKeysToBeRemoved, \true)) {
// keep searching next key not in $paramKeysToBeRemoved
continue;
}
return [];
}
$this->nodeRemover->removeNode($params[$paramKeyToBeRemoved]);
$removedParamKeys[] = $paramKeyToBeRemoved;
}
return $removedParamKeys;
}
2022-06-06 16:43:29 +00:00
private function removeConstructorDependency(Class_ $class, string $propertyName) : void
{
2022-06-06 16:43:29 +00:00
$classMethod = $class->getMethod(MethodName::CONSTRUCT);
if (!$classMethod instanceof ClassMethod) {
return;
}
$stmts = (array) $classMethod->stmts;
$paramKeysToBeRemoved = [];
foreach ($stmts as $key => $stmt) {
2022-06-06 16:43:29 +00:00
if (!$stmt instanceof Expression) {
continue;
}
$stmtExpr = $stmt->expr;
2022-06-06 16:43:29 +00:00
if (!$stmtExpr instanceof Assign) {
continue;
}
if (!$this->propertyFetchAnalyzer->isLocalPropertyFetch($stmtExpr->var)) {
continue;
}
/** @var StaticPropertyFetch|PropertyFetch $propertyFetch */
$propertyFetch = $stmtExpr->var;
if (!$this->nodeNameResolver->isName($propertyFetch, $propertyName)) {
continue;
}
unset($classMethod->stmts[$key]);
2022-06-06 16:43:29 +00:00
if (!$stmtExpr->expr instanceof Variable) {
continue;
}
$key = $this->resolveToBeClearedParamFromConstructor($classMethod, $stmtExpr->expr);
if (\is_int($key)) {
$paramKeysToBeRemoved[] = $key;
}
}
if ($paramKeysToBeRemoved === []) {
return;
}
$this->processRemoveParamWithKeys($classMethod->getParams(), $paramKeysToBeRemoved);
}
/**
* @return StaticPropertyFetch[]|PropertyFetch[]
*/
2022-06-06 16:43:29 +00:00
private function resolvePropertyFetchFromDimFetch(Expr $expr) : array
{
// unwrap array dim fetch, till we get to parent too caller node
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = [];
2022-06-06 16:43:29 +00:00
while ($expr instanceof ArrayDimFetch) {
$propertyFetches = $this->collectPropertyFetches($expr->dim, $propertyFetches);
$expr = $expr->var;
}
return $this->collectPropertyFetches($expr, $propertyFetches);
}
/**
* @param StaticPropertyFetch[]|PropertyFetch[] $propertyFetches
* @return PropertyFetch[]|StaticPropertyFetch[]
*/
2022-06-06 16:43:29 +00:00
private function collectPropertyFetches(?Expr $expr, array $propertyFetches) : array
{
2022-06-06 16:43:29 +00:00
if (!$expr instanceof Expr) {
return $propertyFetches;
}
if ($this->propertyFetchAnalyzer->isLocalPropertyFetch($expr)) {
/** @var StaticPropertyFetch|PropertyFetch $expr */
return \array_merge($propertyFetches, [$expr]);
}
return $propertyFetches;
}
2022-06-06 16:43:29 +00:00
private function resolveToBeClearedParamFromConstructor(ClassMethod $classMethod, Variable $assignedVariable) : ?int
{
// is variable used somewhere else? skip it
2022-06-06 16:43:29 +00:00
$variables = $this->betterNodeFinder->findInstanceOf($classMethod, Variable::class);
$paramNamedVariables = \array_filter($variables, function (Variable $variable) use($assignedVariable) : bool {
return $this->nodeNameResolver->areNamesEqual($variable, $assignedVariable);
});
// there is more than 1 use, keep it in the constructor
if (\count($paramNamedVariables) > 1) {
return null;
}
$paramName = $this->nodeNameResolver->getName($assignedVariable);
if (!\is_string($paramName)) {
return null;
}
foreach ($classMethod->params as $paramKey => $param) {
if ($this->nodeNameResolver->isName($param->var, $paramName)) {
return $paramKey;
}
}
return null;
}
}