From 7ec3ae3514546043861ab1615509c6c8ab9c26ff Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 Jan 2025 08:19:55 +0000 Subject: [PATCH] [removing] Add interface support to RemoveInterfaceRector (#6681) --- .../Fixture/remove_from_interface.php.inc | 23 ++++++++ .../Rector/Class_/RemoveInterfacesRector.php | 56 ++++++++++++++----- 2 files changed, 66 insertions(+), 13 deletions(-) create mode 100644 rules-tests/Removing/Rector/Class_/RemoveInterfacesRector/Fixture/remove_from_interface.php.inc diff --git a/rules-tests/Removing/Rector/Class_/RemoveInterfacesRector/Fixture/remove_from_interface.php.inc b/rules-tests/Removing/Rector/Class_/RemoveInterfacesRector/Fixture/remove_from_interface.php.inc new file mode 100644 index 00000000000..0e6d97547ba --- /dev/null +++ b/rules-tests/Removing/Rector/Class_/RemoveInterfacesRector/Fixture/remove_from_interface.php.inc @@ -0,0 +1,23 @@ + +----- + diff --git a/rules/Removing/Rector/Class_/RemoveInterfacesRector.php b/rules/Removing/Rector/Class_/RemoveInterfacesRector.php index e41d96202c0..f5226f8ce74 100644 --- a/rules/Removing/Rector/Class_/RemoveInterfacesRector.php +++ b/rules/Removing/Rector/Class_/RemoveInterfacesRector.php @@ -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; @@ -48,22 +49,42 @@ 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->implements === []) { + 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_ $class): ?Class_ + { + if ($class->implements === []) { return null; } $isInterfacesRemoved = false; - foreach ($node->implements as $key => $implement) { + foreach ($class->implements as $key => $implement) { if ($this->isNames($implement, $this->interfacesToRemove)) { - unset($node->implements[$key]); + unset($class->implements[$key]); $isInterfacesRemoved = true; } } @@ -72,17 +93,26 @@ public function refactor(Node $node): ?Node return null; } - return $node; + return $class; } - /** - * @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; } }