From 776609debc74f16db490ec666cb699b531491df1 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Sun, 19 Jan 2025 07:59:00 +0000 Subject: [PATCH] [removing] Add interface support to RemoveInterfaceRector --- .../Fixture/remove_from_interface.php.inc | 23 +++++++++ .../Rector/Class_/RemoveInterfacesRector.php | 48 +++++++++++++++---- 2 files changed, 62 insertions(+), 9 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..ea6598dd6a2 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,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; @@ -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; } }