Skip to content

Commit

Permalink
[FEATURE] Add MigrateHttpUtilityRedirectRector (#4154)
Browse files Browse the repository at this point in the history
[FEATURE] Add MigrateHttpUtilityRedirectRector

Relates: #2603
  • Loading branch information
maddy2101 authored Mar 8, 2024
1 parent d9d7933 commit ed3ffab
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 10 deletions.
1 change: 1 addition & 0 deletions config/v11/typo3-113.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
$rectorConfig->rule(SwitchBehaviorOfArrayUtilityMethodsRector::class);
$rectorConfig->rule(SubstituteExtbaseRequestGetBaseUriRector::class);
$rectorConfig->rule(UseNormalizedParamsToGetRequestUrlRector::class);
$rectorConfig->rule(\Ssch\TYPO3Rector\TYPO311\v3\MigrateHttpUtilityRedirectRector::class);
};
121 changes: 121 additions & 0 deletions rules/TYPO311/v3/MigrateHttpUtilityRedirectRector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\TYPO311\v3;

use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Throw_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;

/**
* @changelog https://docs.typo3.org/c/typo3/cms-core/main/en-us/Changelog/11.3/Deprecation-94316-DeprecatedHTTPHeaderManipulatingMethodsFromHttpUtility.html
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\MigrateHttpUtilityRedirectRectorTest
*/
final class MigrateHttpUtilityRedirectRector extends AbstractRector
{
private BuilderFactory $builderFactory;

public function __construct(BuilderFactory $builderFactory)
{
$this->builderFactory = $builderFactory;
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Migrate HttpUtilty::redirect() to responseFactory', [new CodeSample(
<<<'CODE_SAMPLE'
use TYPO3\CMS\Core\Utility\HttpUtility;
HttpUtility::redirect('https://example.com', HttpUtility::HTTP_STATUS_303);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Psr\Http\Message\ResponseFactoryInterface;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
$response = GeneralUtility::makeInstance(ResponseFactoryInterface::class)
->createResponse(HttpUtility::HTTP_STATUS_303)
->withAddedHeader('location', 'https://example.com');
throw new PropagateResponseException($response);
CODE_SAMPLE
),
]);
}

public function getNodeTypes(): array
{
return [Expression::class];
}

/**
* @param Expression $node
* @return Node[]|null
*/
public function refactor(Node $node): ?array
{
$staticCall = $node->expr;
if (! $staticCall instanceof StaticCall) {
return null;
}

if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$staticCall,
new ObjectType(HttpUtility::class)
)) {
return null;
}

if (! $this->isName($staticCall->name, 'redirect')) {
return null;
}

$target = $staticCall->args[0];
$httpStatusCode = $staticCall->args[1] ?? $this->nodeFactory->createClassConstFetch(
HttpUtility::class,
'HTTP_STATUS_303'
);

$createResponseCallNode = $this->nodeFactory->createMethodCall(
$this->createResponseFactory(),
'createResponse',
[$httpStatusCode]
);
$withHeaderCallNode = $this->nodeFactory->createMethodCall($createResponseCallNode, 'withAddedHeader', [
'location',
$target,
]);

$responseVariable = new Variable('response');
$assignment = new Expression(new Assign($responseVariable, $withHeaderCallNode));

$exception = new Throw_($this->builderFactory->new(
'\\TYPO3\\CMS\\Core\\Http\\PropagateResponseException',
[$responseVariable]
));

return [new Nop(), $assignment, $exception];
}

private function createResponseFactory(): StaticCall
{
return $this->nodeFactory->createStaticCall(
GeneralUtility::class,
'makeInstance',
[$this->nodeFactory->createClassConstReference('Psr\\Http\\Message\\ResponseFactoryInterface')]
);
}
}
23 changes: 14 additions & 9 deletions rules/TYPO311/v4/MigrateRootUidToStartingPointsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Name;
use PhpParser\Node\Scalar\String_;
use Ssch\TYPO3Rector\Rector\AbstractTcaRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -22,8 +20,11 @@ final class MigrateRootUidToStartingPointsRector extends AbstractTcaRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('If a column has [treeConfig][rootUid] defined, migrate to [treeConfig][startingPoints] on the same level.', [new CodeSample(
<<<'CODE_SAMPLE'
return new RuleDefinition(
'If a column has [treeConfig][rootUid] defined, migrate to [treeConfig][startingPoints] on the same level.',
[
new CodeSample(
<<<'CODE_SAMPLE'
return [
'columns' => [
'aField' => [
Expand All @@ -38,8 +39,8 @@ public function getRuleDefinition(): RuleDefinition
],
];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
,
<<<'CODE_SAMPLE'
return [
'columns' => [
'aField' => [
Expand All @@ -54,7 +55,9 @@ public function getRuleDefinition(): RuleDefinition
],
];
CODE_SAMPLE
)]);
),

]);
}

protected function refactorColumn(Expr $columnName, Expr $columnTca): void
Expand All @@ -65,7 +68,7 @@ protected function refactorColumn(Expr $columnName, Expr $columnTca): void
}

// Fetch type
if (!$this->isConfigType($configArray, 'select') && !$this->isConfigType($configArray, 'category')) {
if (! $this->isConfigType($configArray, 'select') && ! $this->isConfigType($configArray, 'category')) {
return;
}

Expand All @@ -91,7 +94,9 @@ protected function refactorColumn(Expr $columnName, Expr $columnTca): void
return;
}

$treeConfigArray->items[] = new ArrayItem(new String_((string)$rootUidValue), new String_('startingPoints'));
$treeConfigArray->items[] = new ArrayItem(new String_((string) $rootUidValue), new String_(
'startingPoints'
));
}

$this->removeArrayItemFromArrayByKey($treeConfigArray, 'rootUid');
Expand Down
2 changes: 1 addition & 1 deletion stubs/TYPO3/CMS/Core/Http/PropagateResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

final class PropagateResponseException extends ImmediateResponseException
{
public function __construct(ResponseInterface $response, int $code)
public function __construct(ResponseInterface $response, int $code = 0)
{
parent::__construct();
}
Expand Down
5 changes: 5 additions & 0 deletions stubs/TYPO3/CMS/Core/Utility/HttpUtility.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class HttpUtility
{

const HTTP_STATUS_400 = 'HTTP/1.1 400 Bad Request';
public const HTTP_STATUS_303 = 'HTTP/1.1 303 See Other';

/**
* @return string
Expand All @@ -26,4 +27,8 @@ public static function setResponseCode($httpStatus)
{

}

public static function redirect($url, $httpStatus = self::HTTP_STATUS_303)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Fixture;

use TYPO3\CMS\Core\Utility\HttpUtility;

HttpUtility::redirect('https://example.com');
?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use Psr\Http\Message\ResponseFactoryInterface;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Utility\HttpUtility;

$response = GeneralUtility::makeInstance(ResponseFactoryInterface::class)->createResponse(HttpUtility::HTTP_STATUS_303)->withAddedHeader('location', 'https://example.com');
throw new PropagateResponseException($response);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Fixture;

use TYPO3\CMS\Core\Utility\HttpUtility;

HttpUtility::redirect('https://example.com', HttpUtility::HTTP_STATUS_303);
?>
-----
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Fixture;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use Psr\Http\Message\ResponseFactoryInterface;
use TYPO3\CMS\Core\Http\PropagateResponseException;
use TYPO3\CMS\Core\Utility\HttpUtility;

$response = GeneralUtility::makeInstance(ResponseFactoryInterface::class)->createResponse(HttpUtility::HTTP_STATUS_303)->withAddedHeader('location', 'https://example.com');
throw new PropagateResponseException($response);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Fixture;

use Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Source\Foo;

Foo::redirect('https://example.com', 0);
?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class MigrateHttpUtilityRedirectRectorTest 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,12 @@
<?php

declare(strict_types=1);

namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\MigrateHttpUtilityRedirectRector\Source;

class Foo
{
public static function redirect(string $input, int $code)
{
}
}
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\TYPO311\v3\MigrateHttpUtilityRedirectRector;

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

0 comments on commit ed3ffab

Please sign in to comment.