Skip to content

Commit

Permalink
Use getIndpEnv('HTTP_HOST') (#2817)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabbelasichon authored Feb 27, 2022
1 parent 85a72f9 commit 5948a1b
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/v9/typo3-94.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
71 changes: 71 additions & 0 deletions src/Rector/v9/v4/GeneralUtilityGetHostNameToGetIndpEnvRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Rector\v9\v4;

use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/master/en-us/Changelog/9.4/Deprecation-85759-GeneralUtilitygetHostName.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector\GeneralUtilityGetHostNameToGetIndpEnvRectorTest
*/
final class GeneralUtilityGetHostNameToGetIndpEnvRector extends AbstractRector
{
/**
* @return array<class-string<Node>>
*/
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
),

]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;

GeneralUtility::getHostName();

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;

GeneralUtility::getIndpEnv('HTTP_HOST');

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;

final class GeneralUtilityGetHostNameToGetIndpEnvRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

return static function (ContainerConfigurator $containerConfigurator): void {
$containerConfigurator->import(__DIR__ . '/../../../../../../config/config_test.php');

$services = $containerConfigurator->services();
$services->set(\Ssch\TYPO3Rector\Rector\v9\v4\GeneralUtilityGetHostNameToGetIndpEnvRector::class);
};

0 comments on commit 5948a1b

Please sign in to comment.