Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions config/set/shopware/shopware56.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ services:
sendCookies: sendHeaders
setBody: setContent
Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~
Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php declare(strict_types=1);

namespace Rector\Shopware\Rector\MethodCall;

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
/**
* @see https://github.com/shopware/shopware/blob/5.6/UPGRADE-5.6.md
*/
final class ShopRegistrationServiceRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Replace $shop->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)]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Shopware\Tests\Rector\MethodCall\ShopRegistrationServiceRector\Fixture;

class SomeClass
{
public function run()
{
$shop = new \Shopware\Models\Shop\Shop();
$shop->registerResources();
}
}

?>
-----
<?php

namespace Rector\Shopware\Tests\Rector\MethodCall\ShopRegistrationServiceRector\Fixture;

class SomeClass
{
public function run()
{
$shop = new \Shopware\Models\Shop\Shop();
Shopware()->Container()->get('shopware.components.shop_registration_service')->registerShop($shop);
}
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);

namespace Rector\Shopware\Tests\Rector\MethodCall\ShopRegistrationServiceRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class ShopRegistrationServiceRectorTest extends AbstractRectorTestCase
{
public function test(): void
{
$this->doTestFiles([
__DIR__ . '/Fixture/fixture.php.inc'
]);
}

protected function getRectorClass(): string
{
return \Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector::class;
}
}