Skip to content

Commit

Permalink
Cleanup removeNodeFromStatements(), removeArg(), removeParam(), remov…
Browse files Browse the repository at this point in the history
…eStmt() methods and remove nodes directly (#4012)

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
TomasVotruba and actions-user committed May 29, 2023
1 parent 8f24c73 commit cd48bfc
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 83 deletions.
77 changes: 0 additions & 77 deletions packages/NodeRemoval/NodeRemover.php
Expand Up @@ -5,16 +5,7 @@
namespace Rector\NodeRemoval;

use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Function_;
use Rector\ChangesReporting\Collector\RectorChangeCollector;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToRemoveCollector;

Expand Down Expand Up @@ -42,24 +33,6 @@ public function removeNode(Node $node): void
$this->rectorChangeCollector->notifyNodeFileInfo($node);
}

/**
* @api used in rector-doctrine
*/
public function removeNodeFromStatements(
Class_ | ClassMethod | Function_ $nodeWithStatements,
Node $toBeRemovedNode
): void {
foreach ((array) $nodeWithStatements->stmts as $key => $stmt) {
if ($toBeRemovedNode !== $stmt) {
continue;
}

$this->removeNode($stmt);
unset($nodeWithStatements->stmts[$key]);
break;
}
}

/**
* @param Node[] $nodes
*/
Expand All @@ -69,54 +42,4 @@ public function removeNodes(array $nodes): void
$this->removeNode($node);
}
}

public function removeParam(ClassMethod $classMethod, int | Param $keyOrParam): void
{
$key = $keyOrParam instanceof Param ? $keyOrParam->getAttribute(AttributeKey::PARAMETER_POSITION) : $keyOrParam;

if ($classMethod->params === null) {
throw new ShouldNotHappenException();
}

// already removed
if (! isset($classMethod->params[$key])) {
return;
}

$this->removeNode($classMethod->params[$key]);
unset($classMethod->params[$key]);
}

public function removeArg(FuncCall | MethodCall | StaticCall $node, int $key): void
{
if ($node->getArgs() === []) {
throw new ShouldNotHappenException();
}

// already removed
if (! isset($node->args[$key])) {
return;
}

$this->removeNode($node->args[$key]);
unset($node->args[$key]);
}

/**
* @api phpunit
*/
public function removeStmt(Closure | ClassMethod | Function_ $functionLike, int $key): void
{
if ($functionLike->stmts === null) {
throw new ShouldNotHappenException();
}

// already removed
if (! isset($functionLike->stmts[$key])) {
return;
}

$this->removeNode($functionLike->stmts[$key]);
unset($functionLike->stmts[$key]);
}
}
Expand Up @@ -140,7 +140,7 @@ private function processClassMethod(Class_ $class, ClassMethod $classMethod): vo
$propertyMetadata = new PropertyMetadata($paramName, $paramType, Class_::MODIFIER_PRIVATE);
$this->propertyToAddCollector->addPropertyToClass($class, $propertyMetadata);

$this->nodeRemover->removeParam($classMethod, $key);
unset($classMethod->params[$key]);

$this->variablesToPropertyFetchCollection->addVariableNameAndType($paramName, $paramType);
}
Expand Down
10 changes: 5 additions & 5 deletions rules/Removing/Rector/ClassMethod/ArgumentRemoverRector.php
Expand Up @@ -103,9 +103,9 @@ private function processPosition(
): void {
if ($argumentRemover->getValue() === null) {
if ($node instanceof MethodCall || $node instanceof StaticCall) {
$this->nodeRemover->removeArg($node, $argumentRemover->getPosition());
unset($node->args[$argumentRemover->getPosition()]);
} else {
$this->nodeRemover->removeParam($node, $argumentRemover->getPosition());
unset($node->params[$argumentRemover->getPosition()]);
}

return;
Expand All @@ -128,15 +128,15 @@ private function processPosition(

if ($this->isArgumentValueMatch($node->args[$argumentRemover->getPosition()], $match)) {
$this->hasChanged = true;
$this->nodeRemover->removeArg($node, $argumentRemover->getPosition());
unset($node->args[$argumentRemover->getPosition()]);
}
}

private function removeByName(ClassMethod | StaticCall | MethodCall $node, int $position, string $name): void
{
if ($node instanceof MethodCall || $node instanceof StaticCall) {
if (isset($node->args[$position]) && $this->isName($node->args[$position], $name)) {
$this->nodeRemover->removeArg($node, $position);
unset($node->args[$position]);
}

return;
Expand All @@ -146,7 +146,7 @@ private function removeByName(ClassMethod | StaticCall | MethodCall $node, int $
return;
}

$this->nodeRemover->removeParam($node, $position);
unset($node->params[$position]);
}

/**
Expand Down

0 comments on commit cd48bfc

Please sign in to comment.