From 5948a1b11f3ef8e2131ac3bbf2395aaad4ec6205 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Sun, 27 Feb 2022 21:48:15 +0100 Subject: [PATCH] Use getIndpEnv('HTTP_HOST') (#2817) --- config/v9/typo3-94.php | 1 + ...alUtilityGetHostNameToGetIndpEnvRector.php | 71 +++++++++++++++++++ .../get_host_name_to_get_indp_env.php.inc | 19 +++++ ...ilityGetHostNameToGetIndpEnvRectorTest.php | 33 +++++++++ .../config/configured_rule.php | 12 ++++ 5 files changed, 136 insertions(+) create mode 100644 src/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector.php create mode 100644 tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/Fixture/get_host_name_to_get_indp_env.php.inc create mode 100644 tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/GeneralUtilityGetHostNameToGetIndpEnvRectorTest.php create mode 100644 tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/config/configured_rule.php diff --git a/config/v9/typo3-94.php b/config/v9/typo3-94.php index 0728ee69a..1f56fd59e 100644 --- a/config/v9/typo3-94.php +++ b/config/v9/typo3-94.php @@ -52,4 +52,5 @@ $services->set(UseClassSchemaInsteadReflectionServiceMethodsRector::class); $services->set(RemoveMethodsFromEidUtilityAndTsfeRector::class); $services->set(AdditionalFieldProviderRector::class); + $services->set(\Ssch\TYPO3Rector\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector::class); }; diff --git a/src/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector.php b/src/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector.php new file mode 100644 index 000000000..79c930f25 --- /dev/null +++ b/src/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector.php @@ -0,0 +1,71 @@ +> + */ + public function getNodeTypes(): array + { + return [StaticCall::class]; + } + + /** + * @param StaticCall $node + */ + public function refactor(Node $node): ?Node + { + if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( + $node, + new ObjectType('TYPO3\CMS\Core\Utility\GeneralUtility') + )) { + return null; + } + + if (! $this->isName($node->name, 'getHostname')) { + return null; + } + + $node->name = new Node\Identifier('getIndpEnv'); + $node->args = $this->nodeFactory->createArgs(['HTTP_HOST']); + + return $node; + } + + /** + * @codeCoverageIgnore + */ + public function getRuleDefinition(): RuleDefinition + { + return new RuleDefinition( + 'Migrating method call GeneralUtility::getHostname() to GeneralUtility::getIndpEnv(\'HTTP_HOST\')', + [ + new CodeSample( + <<<'CODE_SAMPLE' +\TYPO3\CMS\Core\Utility\GeneralUtility::getHostname(); +CODE_SAMPLE + , + <<<'CODE_SAMPLE' +\TYPO3\CMS\Core\Utility\GeneralUtility::getIndpEnv('HTTP_HOST') +CODE_SAMPLE + ), + + ] + ); + } +} diff --git a/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/Fixture/get_host_name_to_get_indp_env.php.inc b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/Fixture/get_host_name_to_get_indp_env.php.inc new file mode 100644 index 000000000..f95fff040 --- /dev/null +++ b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/Fixture/get_host_name_to_get_indp_env.php.inc @@ -0,0 +1,19 @@ + +----- + diff --git a/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/GeneralUtilityGetHostNameToGetIndpEnvRectorTest.php b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/GeneralUtilityGetHostNameToGetIndpEnvRectorTest.php new file mode 100644 index 000000000..347f7e397 --- /dev/null +++ b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/GeneralUtilityGetHostNameToGetIndpEnvRectorTest.php @@ -0,0 +1,33 @@ +doTestFileInfo($fileInfo); + } + + /** + * @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/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/config/configured_rule.php b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/config/configured_rule.php new file mode 100644 index 000000000..e307fecb0 --- /dev/null +++ b/tests/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector/config/configured_rule.php @@ -0,0 +1,12 @@ +import(__DIR__ . '/../../../../../../config/config_test.php'); + + $services = $containerConfigurator->services(); + $services->set(\Ssch\TYPO3Rector\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector::class); +};