From 7824b2eac55dd43499d8684afebc79778b634f63 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Mon, 31 Oct 2022 08:57:04 +0100 Subject: [PATCH] TASK: Add UseNormalizedParamsToGetRequestUrlRector Resolves: #2596 --- config/v11/typo3-113.php | 1 + ...eNormalizedParamsToGetRequestUrlRector.php | 70 +++++++++++++++++++ .../Http/Message/ServerRequestInterface.php | 4 ++ .../Mvc/Controller/ActionController.php | 7 ++ stubs/TYPO3/CMS/Extbase/Mvc/Request.php | 17 +++++ .../Fixture/fixture.php.inc | 31 ++++++++ ...malizedParamsToGetRequestUrlRectorTest.php | 32 +++++++++ .../config/configured_rule.php | 11 +++ 8 files changed, 173 insertions(+) create mode 100644 src/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector.php create mode 100644 tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/Fixture/fixture.php.inc create mode 100644 tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/UseNormalizedParamsToGetRequestUrlRectorTest.php create mode 100644 tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/config/configured_rule.php diff --git a/config/v11/typo3-113.php b/config/v11/typo3-113.php index a3105199a..fd7f899f5 100644 --- a/config/v11/typo3-113.php +++ b/config/v11/typo3-113.php @@ -12,4 +12,5 @@ $rectorConfig->rule(SubstituteMethodRmFromListOfGeneralUtilityRector::class); $rectorConfig->rule(SwitchBehaviorOfArrayUtilityMethodsRector::class); $rectorConfig->rule(SubstituteExtbaseRequestGetBaseUriRector::class); + $rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector::class); }; diff --git a/src/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector.php b/src/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector.php new file mode 100644 index 000000000..791774a0d --- /dev/null +++ b/src/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector.php @@ -0,0 +1,70 @@ +> + */ + public function getNodeTypes(): array + { + return [MethodCall::class]; + } + + /** + * @param MethodCall $node + */ + public function refactor(Node $node): ?Node + { + if ($this->shouldSkip($node)) { + return null; + } + + $node->name = new Node\Identifier('getAttribute'); + $node->args = $this->nodeFactory->createArgs(['normalizedParams']); + + return $this->nodeFactory->createMethodCall($node, 'getRequestUrl'); + } + + /** + * @codeCoverageIgnore + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition('Use normalized params to get the request url', [new CodeSample( + <<<'CODE_SAMPLE' +$requestUri = $this->request->getRequestUri(); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +$requestUri = $this->request->getAttribute('normalizedParams')->getRequestUrl(); +CODE_SAMPLE + )]); + } + + private function shouldSkip(MethodCall $node): bool + { + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( + $node, + new ObjectType('TYPO3\\CMS\\Extbase\\Mvc\\Request') + )) { + return true; + } + + return ! $this->isName($node->name, 'getRequestUri'); + } +} diff --git a/stubs/Psr/Http/Message/ServerRequestInterface.php b/stubs/Psr/Http/Message/ServerRequestInterface.php index e2b60526b..acb6921d0 100644 --- a/stubs/Psr/Http/Message/ServerRequestInterface.php +++ b/stubs/Psr/Http/Message/ServerRequestInterface.php @@ -8,4 +8,8 @@ interface ServerRequestInterface { + /** + * @return mixed + */ + public function getAttribute($name, $default); } diff --git a/stubs/TYPO3/CMS/Extbase/Mvc/Controller/ActionController.php b/stubs/TYPO3/CMS/Extbase/Mvc/Controller/ActionController.php index c8b37a118..8a3ffa64a 100644 --- a/stubs/TYPO3/CMS/Extbase/Mvc/Controller/ActionController.php +++ b/stubs/TYPO3/CMS/Extbase/Mvc/Controller/ActionController.php @@ -28,6 +28,13 @@ class ActionController extends AbstractController */ protected $objectManager; + /** + * The current request. + * + * @var \TYPO3\CMS\Extbase\Mvc\Request + */ + protected $request; + /** * @return void * @param string $actionName diff --git a/stubs/TYPO3/CMS/Extbase/Mvc/Request.php b/stubs/TYPO3/CMS/Extbase/Mvc/Request.php index 9bedc63a2..caefae3e3 100644 --- a/stubs/TYPO3/CMS/Extbase/Mvc/Request.php +++ b/stubs/TYPO3/CMS/Extbase/Mvc/Request.php @@ -2,12 +2,16 @@ namespace TYPO3\CMS\Extbase\Mvc; +use Psr\Http\Message\ServerRequestInterface; + if (class_exists('TYPO3\CMS\Extbase\Mvc\Request')) { return; } class Request { + protected ServerRequestInterface $request; + /** * @return string */ @@ -15,4 +19,17 @@ public function getControllerExtensionName() { return 'extensionName'; } + + public function getRequestUri(): string + { + return 'uri'; + } + + /** + * @return mixed + */ + public function getAttribute($name, $default = null) + { + return $this->request->getAttribute($name, $default); + } } diff --git a/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/Fixture/fixture.php.inc b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/Fixture/fixture.php.inc new file mode 100644 index 000000000..f30dfb2a0 --- /dev/null +++ b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/Fixture/fixture.php.inc @@ -0,0 +1,31 @@ +request->getRequestUri(); + } +} + +?> +----- +request->getAttribute('normalizedParams')->getRequestUrl(); + } +} + +?> diff --git a/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/UseNormalizedParamsToGetRequestUrlRectorTest.php b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/UseNormalizedParamsToGetRequestUrlRectorTest.php new file mode 100644 index 000000000..cf377d3a4 --- /dev/null +++ b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/UseNormalizedParamsToGetRequestUrlRectorTest.php @@ -0,0 +1,32 @@ +doTestFile($filePath); + } + + /** + * @return Iterator> + */ + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + public function provideConfigFilePath(): string + { + return __DIR__ . '/config/configured_rule.php'; + } +} diff --git a/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/config/configured_rule.php b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/config/configured_rule.php new file mode 100644 index 000000000..4a3d7cc8e --- /dev/null +++ b/tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/config/configured_rule.php @@ -0,0 +1,11 @@ +import(__DIR__ . '/../../../../../../../config/config_test.php'); + $rectorConfig->rule(UseNormalizedParamsToGetRequestUrlRector::class); +};