Skip to content

Commit

Permalink
Remove method calls as well
Browse files Browse the repository at this point in the history
  • Loading branch information
ViniTou committed Jun 18, 2024
1 parent f4b8e49 commit f4d9f5f
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 4 deletions.
54 changes: 50 additions & 4 deletions src/lib/Rule/Internal/RemoveInterfaceWithMethods.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@
namespace Ibexa\Rector\Rule\Internal;

use PhpParser\Node;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\Rector\AbstractRector;
use Rector\Removing\Rector\Class_\RemoveInterfacesRector;
use ReflectionClass;
Expand Down Expand Up @@ -78,17 +82,59 @@ public function refactor(Node $node): ?int
foreach ($this->interfacesToRemove as $interface) {
$ref = new ReflectionClass($interface);
$methods = array_map(static fn (ReflectionMethod $reflectionMethod) => $reflectionMethod->getName(), $ref->getMethods());
foreach ($methods as $methodToRemove) {
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof ClassMethod && $this->isName($stmt, $methodToRemove)) {
unset($node->stmts[$key]);

// Remove method definition
foreach ($node->stmts as $key => $stmt) {
if ($stmt instanceof ClassMethod && $this->isNames($stmt, $methods)) {
unset($node->stmts[$key]);
}
}

// Remove method calls, if one of the arguments was removed method
foreach ($node->getMethods() as $classMethod) {
$nodeTraverser = new NodeTraverser();
$nodeTraverser->addVisitor(new class($this->nodeNameResolver, $methods) extends NodeVisitorAbstract {
private NodeNameResolver $nodeNameResolver;

/** @var string[] */
private array $methods;

/**
* @param string[] $methods
*/
public function __construct(NodeNameResolver $nodeNameResolver, array $methods)
{
$this->nodeNameResolver = $nodeNameResolver;
$this->methods = $methods;
}

public function leaveNode(Node $node)
{
if ($node instanceof MethodCall) {
foreach ($node->getArgs() as $arg) {
$argValue = $arg->value;
if ($argValue instanceof MethodCall && $this->nodeNameResolver->isNames($argValue->name, $this->methods)) {
return $node->var;
}
}
}

return null;
}
});

if ($classMethod->stmts !== null) {
/** @var array<\PhpParser\Node\Stmt>|null $traversedStmts */

$traversedStmts = $nodeTraverser->traverse($classMethod->stmts);
$classMethod->stmts = $traversedStmts;
}
}
}
}
}

// Remove interface
$this->removeInterfacesRector->refactor($node);

return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php /** @noinspection ALL */

use Ibexa\Rector\Tests\Rule\Internal\RemoveInterfaceWithMethods\Fixture\SomeInterface;
use Symfony\Component\Console\Command\Command;

class SomeClass implements SomeInterface
{
public function firstMethod(): void
{

}

public function configure()
{
$this->firstMethod()->firstMethod($this->someMethod())->thirdMethod();
}

public function someMethod(): array
{
return ['ezplatform:some_old:command_with_interface'];
}

public function thirdMethod(): void
{

}
}

?>
-----
<?php /** @noinspection ALL */

use Ibexa\Rector\Tests\Rule\Internal\RemoveInterfaceWithMethods\Fixture\SomeInterface;
use Symfony\Component\Console\Command\Command;

class SomeClass
{
public function firstMethod(): void
{

}
public function configure()
{
$this->firstMethod()->thirdMethod();
}
public function thirdMethod(): void
{

}
}

?>

0 comments on commit f4d9f5f

Please sign in to comment.