Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Add UseNormalizedParamsToGetRequestUrlRector #3200

Merged
merged 1 commit into from
Oct 31, 2022
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
1 change: 1 addition & 0 deletions config/v11/typo3-113.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
$rectorConfig->rule(SubstituteMethodRmFromListOfGeneralUtilityRector::class);
$rectorConfig->rule(SwitchBehaviorOfArrayUtilityMethodsRector::class);
$rectorConfig->rule(SubstituteExtbaseRequestGetBaseUriRector::class);
$rectorConfig->rule(\Ssch\TYPO3Rector\Rector\v11\v3\typo3\UseNormalizedParamsToGetRequestUrlRector::class);
};
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');
}
}
4 changes: 4 additions & 0 deletions stubs/Psr/Http/Message/ServerRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
interface ServerRequestInterface
{

/**
* @return mixed
*/
public function getAttribute($name, $default);
}
7 changes: 7 additions & 0 deletions stubs/TYPO3/CMS/Extbase/Mvc/Controller/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ class ActionController extends AbstractController
*/
protected $objectManager;

/**
* The current request.
*
* @var \TYPO3\CMS\Extbase\Mvc\Request
*/
protected $request;

/**
* @return void
* @param string $actionName
Expand Down
17 changes: 17 additions & 0 deletions stubs/TYPO3/CMS/Extbase/Mvc/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,34 @@

namespace TYPO3\CMS\Extbase\Mvc;

use Psr\Http\Message\ServerRequestInterface;

if (class_exists('TYPO3\CMS\Extbase\Mvc\Request')) {
return;
}

class Request
{
protected ServerRequestInterface $request;

/**
* @return string
*/
public function getControllerExtensionName()
{
return 'extensionName';
}

public function getRequestUri(): string
{
return 'uri';
}

/**
* @return mixed
*/
public function getAttribute($name, $default = null)
{
return $this->request->getAttribute($name, $default);
}
}
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();
}
}

?>
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';
}
}
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);
};