From 4e5f4c8535062ac072e3a39484de82f579abf062 Mon Sep 17 00:00:00 2001 From: jkindly Date: Thu, 5 Sep 2024 14:05:27 +0200 Subject: [PATCH] CMS Plugin namespace/use change --- config/sets/sylius/cms/cms-plugin.php | 12 +++ .../Namespace/ChangeNamespaceRector.php | 82 +++++++++++++++++++ src/Set/SyliusCms.php | 10 +++ tests/Set/CmsPlugin/CmsPluginTest.php | 32 ++++++++ .../Fixture/class_namespace_change.php.inc | 25 ++++++ .../Set/CmsPlugin/config/configured_rule.php | 11 +++ 6 files changed, 172 insertions(+) create mode 100644 config/sets/sylius/cms/cms-plugin.php create mode 100644 src/Rector/Namespace/ChangeNamespaceRector.php create mode 100644 src/Set/SyliusCms.php create mode 100644 tests/Set/CmsPlugin/CmsPluginTest.php create mode 100644 tests/Set/CmsPlugin/Fixture/class_namespace_change.php.inc create mode 100644 tests/Set/CmsPlugin/config/configured_rule.php diff --git a/config/sets/sylius/cms/cms-plugin.php b/config/sets/sylius/cms/cms-plugin.php new file mode 100644 index 0000000..a59bc7e --- /dev/null +++ b/config/sets/sylius/cms/cms-plugin.php @@ -0,0 +1,12 @@ +ruleWithConfiguration(ChangeNamespaceRector::class, [ + 'BitBag\SyliusCmsPlugin' => 'Sylius\CmsPlugin', + ]); +}; diff --git a/src/Rector/Namespace/ChangeNamespaceRector.php b/src/Rector/Namespace/ChangeNamespaceRector.php new file mode 100644 index 0000000..03af90e --- /dev/null +++ b/src/Rector/Namespace/ChangeNamespaceRector.php @@ -0,0 +1,82 @@ + Array where the key is the old namespace, and the value is the new namespace + */ + private array $namespaceChanges = []; + + public function configure(array $configuration): void + { + $this->namespaceChanges = $configuration; + } + + public function getNodeTypes(): array + { + return [Namespace_::class, Use_::class]; + } + + public function refactor(Node $node): ?Node + { + if ($node instanceof Namespace_) { + $namespaceName = $this->getName($node); + if ($namespaceName === null) { + return null; + } + + foreach ($this->namespaceChanges as $oldNamespace => $newNamespace) { + if (str_starts_with($namespaceName, $oldNamespace)) { + $node->name = new Name( + str_replace($oldNamespace, $newNamespace, $namespaceName) + ); + + break; + } + } + + return $node; + } + + // Handle "use" statements + if ($node instanceof Use_) { + /** @var UseUse $use */ + foreach ($node->uses as $use) { + $useName = $this->getName($use->name); + + foreach ($this->namespaceChanges as $oldNamespace => $newNamespace) { + if (str_starts_with($useName, $oldNamespace)) { + $use->name = new Name( + str_replace($oldNamespace, $newNamespace, $useName) + ); + } + } + } + + return $node; + } + + return null; + } + + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Change specified namespaces and use statements', + [] + ); + } +} diff --git a/src/Set/SyliusCms.php b/src/Set/SyliusCms.php new file mode 100644 index 0000000..3f55607 --- /dev/null +++ b/src/Set/SyliusCms.php @@ -0,0 +1,10 @@ +doTestFile($file); + } + + /** + * @return Iterator + */ + public function provideData(): Iterator + { + return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Set/CmsPlugin/Fixture/class_namespace_change.php.inc b/tests/Set/CmsPlugin/Fixture/class_namespace_change.php.inc new file mode 100644 index 0000000..b3b24e2 --- /dev/null +++ b/tests/Set/CmsPlugin/Fixture/class_namespace_change.php.inc @@ -0,0 +1,25 @@ + +----- + diff --git a/tests/Set/CmsPlugin/config/configured_rule.php b/tests/Set/CmsPlugin/config/configured_rule.php new file mode 100644 index 0000000..97bdeaf --- /dev/null +++ b/tests/Set/CmsPlugin/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../config/config.php'); + $rectorConfig->sets([SyliusCms::CMS_PLUGIN]); +};