diff --git a/config/set/shopware/shopware55.yaml b/config/set/shopware/shopware55.yaml index a3bd46b8e52c..c22149113a15 100644 --- a/config/set/shopware/shopware55.yaml +++ b/config/set/shopware/shopware55.yaml @@ -17,3 +17,4 @@ services: Shopware_Components_Config: isset: 'offsetExists' unset: 'offsetUnset' + Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~ \ No newline at end of file diff --git a/config/set/shopware/shopware56.yaml b/config/set/shopware/shopware56.yaml index 701da7421ecb..7040d01f49eb 100644 --- a/config/set/shopware/shopware56.yaml +++ b/config/set/shopware/shopware56.yaml @@ -7,3 +7,4 @@ services: setHttpResponseCode: setStatusCode sendCookies: sendHeaders setBody: setContent + Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector: ~ diff --git a/packages/Shopware/src/Rector/ClassConstFetch/ShopwareVersionConstsRector.php b/packages/Shopware/src/Rector/ClassConstFetch/ShopwareVersionConstsRector.php new file mode 100644 index 000000000000..b22b49d722b7 --- /dev/null +++ b/packages/Shopware/src/Rector/ClassConstFetch/ShopwareVersionConstsRector.php @@ -0,0 +1,84 @@ +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))]); + } +} diff --git a/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/Fixture/fixture.php.inc b/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..f9dc526c3080 --- /dev/null +++ b/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/Fixture/fixture.php.inc @@ -0,0 +1,27 @@ + +----- +Container()->getParameter('shopware.release.version'); + } +} + +?> diff --git a/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/ShopwareVersionConstsRectorTest.php b/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/ShopwareVersionConstsRectorTest.php new file mode 100644 index 000000000000..8a71739ac489 --- /dev/null +++ b/packages/Shopware/tests/Rector/ClassConstFetch/ShopwareVersionConstsRector/ShopwareVersionConstsRectorTest.php @@ -0,0 +1,20 @@ +doTestFiles([ + __DIR__ . '/Fixture/fixture.php.inc' + ]); + } + + protected function getRectorClass(): string + { + return \Rector\Shopware\Rector\ClassConstFetch\ShopwareVersionConstsRector::class; + } +}