Add rector for ShopRegistration

This commit is contained in:
Soner Sayakci 2019-06-12 22:19:37 +02:00
parent a48fe8cd98
commit 787a401fbe
4 changed files with 123 additions and 0 deletions

View File

@ -8,3 +8,4 @@ services:
sendCookies: sendHeaders
setBody: setContent
Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~
Rector\Shopware\Rector\MethodCall\ShopRegistrationServiceRector: ~

View File

@ -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)]);
}
}

View File

@ -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);
}
}
?>

View File

@ -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;
}
}