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
2 changes: 2 additions & 0 deletions config/v14/typo3-143.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\TYPO314\v3\MigrateCacheActionHrefToEndpointRector;
use Ssch\TYPO3Rector\TYPO314\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector;
use Ssch\TYPO3Rector\TYPO314\v3\MigrateGeneralUtilityGetIndpEnvRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../config.php');
$rectorConfig->rule(MigrateGeneralUtilityGetIndpEnvRector::class);
$rectorConfig->rule(MigrateCacheActionHrefToEndpointRector::class);
$rectorConfig->rule(AddServerRequestArgumentToGeneralUtilityMethodsRector::class);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO314\v3;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PHPStan\Type\ObjectType;
use Rector\PHPStan\ScopeFetcher;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\Contract\DocumentedRuleInterface;
use Rector\RuleDoc\RuleDefinition;
use Ssch\TYPO3Rector\NodeFactory\Typo3RequestNodeFactory;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/14.3/Deprecation-109523-GeneralUtilityIsOnCurrentHostWithoutRequest.html
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/14.3/Deprecation-109544-GeneralUtilitySanitizeLocalUrlWithoutRequest.html
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/14.3/Deprecation-109548-GeneralUtilityLocationHeaderUrlWithoutRequest.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\AddServerRequestArgumentToGeneralUtilityMethodsRectorTest
*/
final class AddServerRequestArgumentToGeneralUtilityMethodsRector extends AbstractRector implements DocumentedRuleInterface
{
/**
* @var string[]
*/
private const METHOD_NAMES = ['isOnCurrentHost', 'sanitizeLocalUrl', 'locationHeaderUrl'];

/**
* @readonly
*/
private Typo3RequestNodeFactory $typo3RequestNodeFactory;

public function __construct(Typo3RequestNodeFactory $typo3RequestNodeFactory)
{
$this->typo3RequestNodeFactory = $typo3RequestNodeFactory;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Pass the PSR-7 request as second argument to `GeneralUtility::isOnCurrentHost()`, `GeneralUtility::sanitizeLocalUrl()` and `GeneralUtility::locationHeaderUrl()`',
[
new CodeSample(
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\GeneralUtility;

$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url);
$url = GeneralUtility::sanitizeLocalUrl($url);
$url = GeneralUtility::locationHeaderUrl($path);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\GeneralUtility;

$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url, $GLOBALS['TYPO3_REQUEST']);
$url = GeneralUtility::sanitizeLocalUrl($url, $GLOBALS['TYPO3_REQUEST']);
$url = GeneralUtility::locationHeaderUrl($path, $GLOBALS['TYPO3_REQUEST']);
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class MyController extends ActionController
{
public function myAction(string $url): void
{
$url = GeneralUtility::sanitizeLocalUrl($url);
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class MyController extends ActionController
{
public function myAction(string $url): void
{
$url = GeneralUtility::sanitizeLocalUrl($url, $this->request);
}
}
CODE_SAMPLE
),
]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [StaticCall::class];
}

/**
* @param StaticCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->isNames($node->name, self::METHOD_NAMES)) {
return null;
}

if (! $this->isObjectType($node->class, new ObjectType('TYPO3\CMS\Core\Utility\GeneralUtility'))) {
return null;
}

if ($node->isFirstClassCallable() || count($node->args) !== 1) {
return null;
}

$firstArg = $node->args[0];
if (! $firstArg instanceof Arg || $firstArg->unpack || $firstArg->name instanceof Identifier) {
return null;
}

$scope = ScopeFetcher::fetch($node);
$node->args[] = new Arg($this->typo3RequestNodeFactory->getServerRequestInScope($scope));

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AddServerRequestArgumentToGeneralUtilityMethodsRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

/**
* @return \Iterator<array<string>>
*/
public static function provideData(): \Iterator
{
return self::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,37 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class MyExtbaseController extends ActionController
{
public function myAction(string $url): void
{
$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url);
$absoluteUrl = GeneralUtility::locationHeaderUrl($url);
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;

class MyExtbaseController extends ActionController
{
public function myAction(string $url): void
{
$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url, $this->request);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url, $this->request);
$absoluteUrl = GeneralUtility::locationHeaderUrl($url, $this->request);
}
}

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

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;

$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url);
$absoluteUrl = GeneralUtility::locationHeaderUrl('/path');

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;

$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url, $GLOBALS['TYPO3_REQUEST']);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url, $GLOBALS['TYPO3_REQUEST']);
$absoluteUrl = GeneralUtility::locationHeaderUrl('/path', $GLOBALS['TYPO3_REQUEST']);

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

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MyMiddleware
{
public function process(ServerRequestInterface $request, string $url): void
{
$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url);
$absoluteUrl = GeneralUtility::locationHeaderUrl($url);
}
}

?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use Psr\Http\Message\ServerRequestInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;

class MyMiddleware
{
public function process(ServerRequestInterface $request, string $url): void
{
$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url, $request);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url, $request);
$absoluteUrl = GeneralUtility::locationHeaderUrl($url, $request);
}
}

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

namespace Ssch\TYPO3Rector\Tests\Rector\v14\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector\Fixture;

use Foo\Bar;
use TYPO3\CMS\Core\Utility\GeneralUtility;

$isOnCurrentHost = GeneralUtility::isOnCurrentHost($url, $request);
$sanitizedUrl = GeneralUtility::sanitizeLocalUrl($url, $GLOBALS['TYPO3_REQUEST']);
$absoluteUrl = GeneralUtility::locationHeaderUrl(...$arguments);
$namedUrl = GeneralUtility::sanitizeLocalUrl(url: $url);
$callable = GeneralUtility::locationHeaderUrl(...);
$other = GeneralUtility::getIndpEnv('HTTP_HOST');
$otherClass = Bar::sanitizeLocalUrl($url);
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Ssch\TYPO3Rector\TYPO314\v3\AddServerRequestArgumentToGeneralUtilityMethodsRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../../../config/config_test.php');
$rectorConfig->rule(AddServerRequestArgumentToGeneralUtilityMethodsRector::class);
};