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/shopware55.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ services:
Shopware_Components_Config:
isset: 'offsetExists'
unset: 'offsetUnset'
Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~
1 change: 1 addition & 0 deletions config/set/shopware/shopware56.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ services:
setHttpResponseCode: setStatusCode
sendCookies: sendHeaders
setBody: setContent
Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php declare(strict_types=1);

namespace Rector\Shopware\Rector\ClassConstFetch;

use PhpParser\Node;
use PhpParser\Node\Expr\ClassConstFetch;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
use spec\Rector\PhpSpecToPHPUnit\Tests\Rector\Class_\PhpSpecToPHPUnitRector\Fixture\BlablaSpec;

/**
* @see https://github.com/shopware/shopware/blob/5.6/UPGRADE-5.6.md
*/
final class ShopwareVersionConstsRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
return new RectorDefinition('Use version from di parameter', [
new CodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
echo \Shopware::VERSION;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
echo Shopware()->Container()->getParameter('shopware.release.version');
}
}
CODE_SAMPLE

)
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [ClassConstFetch::class];
}

/**
* @param ClassConstFetch $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isType($node, 'Shopware')) {
return null;
}

if (! $node->name instanceof Node\Identifier) {
return null;
}

switch ($node->name->name) {
case 'VERSION':
return $this->buildParameterCall('shopware.release.version');
case 'VERSION_TEXT':
return $this->buildParameterCall('shopware.release.version_text');
case 'REVISION':
return $this->buildParameterCall('shopware.release.revision');
default:
return null;
}
}

private function buildParameterCall(string $paramterName): Node\Expr\MethodCall
{
$shopwareFunction = new Node\Expr\FuncCall(new Node\Name('Shopware'));
$containerCall = new Node\Expr\MethodCall($shopwareFunction, new Node\Identifier('Container'));
return new Node\Expr\MethodCall($containerCall, new Node\Identifier('getParameter'), [new Node\Arg(new Node\Scalar\String_($paramterName))]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Shopware\Tests\Rector\ClassConstFetch\ShopwareVersionConstsRector\Fixture;

class SomeClass
{
public function run()
{
echo \Shopware::VERSION;
}
}

?>
-----
<?php

namespace Rector\Shopware\Tests\Rector\ClassConstFetch\ShopwareVersionConstsRector\Fixture;

class SomeClass
{
public function run()
{
echo Shopware()->Container()->getParameter('shopware.release.version');
}
}

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

namespace Rector\Shopware\Tests\Rector\ClassConstFetch\ShopwareVersionConstsRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

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

protected function getRectorClass(): string
{
return \Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector::class;
}
}