-
-
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.
TASK: Add UseNormalizedParamsToGetRequestUrlRector (#3200)
Resolves: #2596
- Loading branch information
1 parent
c5b1157
commit af05745
Showing
8 changed files
with
173 additions
and
0 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
70 changes: 70 additions & 0 deletions
70
src/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector.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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Ssch\TYPO3Rector\Rector\v11\v3\typo3; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\MethodCall; | ||
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/11.3/Deprecation-94228-DeprecateExtbaseRequestGetRequestUri.html | ||
* @see \Ssch\TYPO3Rector\Tests\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector\UseNormalizedParamsToGetRequestUrlRectorTest | ||
*/ | ||
final class UseNormalizedParamsToGetRequestUrlRector extends AbstractRector | ||
{ | ||
/** | ||
* @return array<class-string<Node>> | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [MethodCall::class]; | ||
} | ||
|
||
/** | ||
* @param MethodCall $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($this->shouldSkip($node)) { | ||
return null; | ||
} | ||
|
||
$node->name = new Node\Identifier('getAttribute'); | ||
$node->args = $this->nodeFactory->createArgs(['normalizedParams']); | ||
|
||
return $this->nodeFactory->createMethodCall($node, 'getRequestUrl'); | ||
} | ||
|
||
/** | ||
* @codeCoverageIgnore | ||
*/ | ||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Use normalized params to get the request url', [new CodeSample( | ||
<<<'CODE_SAMPLE' | ||
$requestUri = $this->request->getRequestUri(); | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$requestUri = $this->request->getAttribute('normalizedParams')->getRequestUrl(); | ||
CODE_SAMPLE | ||
)]); | ||
} | ||
|
||
private function shouldSkip(MethodCall $node): bool | ||
{ | ||
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType( | ||
$node, | ||
new ObjectType('TYPO3\\CMS\\Extbase\\Mvc\\Request') | ||
)) { | ||
return true; | ||
} | ||
|
||
return ! $this->isName($node->name, 'getRequestUri'); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -8,4 +8,8 @@ | |
interface ServerRequestInterface | ||
{ | ||
|
||
/** | ||
* @return mixed | ||
*/ | ||
public function getAttribute($name, $default); | ||
} |
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
31 changes: 31 additions & 0 deletions
31
tests/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/Fixture/fixture.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,31 @@ | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
class MyController extends ActionController | ||
{ | ||
public function myAction() | ||
{ | ||
$requestUri = $this->request->getRequestUri(); | ||
} | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Ssch\TYPO3Rector\Tests\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector\Fixture; | ||
|
||
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController; | ||
|
||
class MyController extends ActionController | ||
{ | ||
public function myAction() | ||
{ | ||
$requestUri = $this->request->getAttribute('normalizedParams')->getRequestUrl(); | ||
} | ||
} | ||
|
||
?> |
32 changes: 32 additions & 0 deletions
32
...UseNormalizedParamsToGetRequestUrlRector/UseNormalizedParamsToGetRequestUrlRectorTest.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\typo3\UseNormalizedParamsToGetRequestUrlRector; | ||
|
||
use Iterator; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
|
||
final class UseNormalizedParamsToGetRequestUrlRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(string $filePath): void | ||
{ | ||
$this->doTestFile($filePath); | ||
} | ||
|
||
/** | ||
* @return Iterator<array<string>> | ||
*/ | ||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
public function provideConfigFilePath(): string | ||
{ | ||
return __DIR__ . '/config/configured_rule.php'; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...s/Rector/v11/v3/typo3/UseNormalizedParamsToGetRequestUrlRector/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\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->import(__DIR__ . '/../../../../../../../config/config_test.php'); | ||
$rectorConfig->rule(UseNormalizedParamsToGetRequestUrlRector::class); | ||
}; |