diff --git a/config/set/shopware/shopware56.yaml b/config/set/shopware/shopware56.yaml index 7040d01f49eb..862d4ec1dcf3 100644 --- a/config/set/shopware/shopware56.yaml +++ b/config/set/shopware/shopware56.yaml @@ -8,3 +8,4 @@ services: sendCookies: sendHeaders setBody: setContent Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~ + Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector: ~ diff --git a/packages/Shopware/src/Rector/MethodCall/ShopRegistrationServiceRector.php b/packages/Shopware/src/Rector/MethodCall/ShopRegistrationServiceRector.php new file mode 100644 index 000000000000..c081c50c445f --- /dev/null +++ b/packages/Shopware/src/Rector/MethodCall/ShopRegistrationServiceRector.php @@ -0,0 +1,73 @@ +registerResources() with ShopRegistrationService', [ + new CodeSample( + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run() + { + $shop = new \Shopware\Models\Shop\Shop(); + $shop->registerResources(); + } +} +CODE_SAMPLE +, + <<<'CODE_SAMPLE' +class SomeClass +{ + public function run() + { + $shop = new \Shopware\Models\Shop\Shop(); + Shopware()->Container()->get('shopware.components.shop_registration_service')->registerShop($shop); + } +} +CODE_SAMPLE + + ) + ]); + } + + /** + * @return string[] + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->isName($node, 'registerResources')) { + return null; + } + + if (! $this->isType($node, 'Shopware\Models\Shop\Shop')) { + return null; + } + + $shopwareFunction = new FuncCall(new Node\Name('Shopware')); + $containerCall = new MethodCall($shopwareFunction, new Node\Identifier('Container')); + $methodCall = new MethodCall($containerCall, new Node\Identifier('get'), [new Node\Arg(new Node\Scalar\String_('shopware.components.shop_registration_service'))]); + + return new MethodCall($methodCall, new Node\Identifier('registerShop'), [new Node\Arg($node->var)]); + } +} diff --git a/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/Fixture/fixture.php.inc b/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..dc953839759e --- /dev/null +++ b/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/Fixture/fixture.php.inc @@ -0,0 +1,29 @@ +registerResources(); + } +} + +?> +----- +Container()->get('shopware.components.shop_registration_service')->registerShop($shop); + } +} + +?> diff --git a/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/ShopRegistrationServiceRectorTest.php b/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/ShopRegistrationServiceRectorTest.php new file mode 100644 index 000000000000..2908dfedae90 --- /dev/null +++ b/packages/Shopware/tests/Rector/MethodCall/ShopRegistrationServiceRector/ShopRegistrationServiceRectorTest.php @@ -0,0 +1,20 @@ +doTestFiles([ + __DIR__ . '/Fixture/fixture.php.inc' + ]); + } + + protected function getRectorClass(): string + { + return \Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector::class; + } +}