Skip to content

Commit

Permalink
[removing] Add interface support to RemoveInterfaceRector
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 19, 2025
1 parent 8da1bfe commit 776609d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\Fixture;

use Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\Source\SomeInterface;

interface RemoveFromInterface extends SomeInterface
{
}

?>
-----
<?php

namespace Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\Fixture;

use Rector\Tests\Removing\Rector\Class_\RemoveInterfacesRector\Source\SomeInterface;

interface RemoveFromInterface
{
}

?>
48 changes: 39 additions & 9 deletions rules/Removing/Rector/Class_/RemoveInterfacesRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Interface_;
use Rector\Contract\Rector\ConfigurableRectorInterface;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
Expand Down Expand Up @@ -48,13 +49,33 @@ class SomeClass
*/
public function getNodeTypes(): array
{
return [Class_::class];
return [Class_::class, Interface_::class];
}

/**
* @param Class_ $node
* @param Class_|Interface_ $node
*/
public function refactor(Node $node): ?Node
{
if ($node instanceof Class_) {
return $this->refactorClass($node);
}

return $this->refactorInterface($node);
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
Assert::allString($configuration);

/** @var string[] $configuration */
$this->interfacesToRemove = $configuration;
}

private function refactorClass(Class_ $node): ?Class_
{
if ($node->implements === []) {
return null;
Expand All @@ -75,14 +96,23 @@ public function refactor(Node $node): ?Node
return $node;
}

/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
private function refactorInterface(Interface_ $interface): Interface_|null
{
Assert::allString($configuration);
$isInterfacesRemoved = false;

/** @var string[] $configuration */
$this->interfacesToRemove = $configuration;
foreach ($interface->extends as $key => $extend) {
if (! $this->isNames($extend, $this->interfacesToRemove)) {
continue;
}

unset($interface->extends[$key]);
$isInterfacesRemoved = true;
}

if (! $isInterfacesRemoved) {
return null;
}

return $interface;
}
}

0 comments on commit 776609d

Please sign in to comment.