-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add MigrateHttpUtilityRedirectRector (#4154)
[FEATURE] Add MigrateHttpUtilityRedirectRector Relates: #2603
- Loading branch information
Showing
11 changed files
with
247 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...tor/v11/v3/MigrateHttpUtilityRedirectRector/Fixture/RedirectCallWithOneParameters.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
?> |
21 changes: 21 additions & 0 deletions
21
...tor/v11/v3/MigrateHttpUtilityRedirectRector/Fixture/RedirectCallWithTwoParameters.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
?> |
8 changes: 8 additions & 0 deletions
8
...teHttpUtilityRedirectRector/Fixture/RedirectCallWithTwoParametersNotInHttpUtility.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
?> |
32 changes: 32 additions & 0 deletions
32
...s/Rector/v11/v3/MigrateHttpUtilityRedirectRector/MigrateHttpUtilityRedirectRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
tests/Rector/v11/v3/MigrateHttpUtilityRedirectRector/Source/Foo.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
tests/Rector/v11/v3/MigrateHttpUtilityRedirectRector/config/configured_rule.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |