diff --git a/rules/TYPO312/v0/UseServerRequestInsteadOfGeneralUtilityPostRector.php b/rules/TYPO312/v0/UseServerRequestInsteadOfGeneralUtilityPostRector.php index b757a9469..5f252f27a 100644 --- a/rules/TYPO312/v0/UseServerRequestInsteadOfGeneralUtilityPostRector.php +++ b/rules/TYPO312/v0/UseServerRequestInsteadOfGeneralUtilityPostRector.php @@ -5,6 +5,9 @@ namespace Ssch\TYPO3Rector\TYPO312\v0; use PhpParser\Node; +use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\BinaryOp\Coalesce; +use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; use Rector\Rector\AbstractScopeAwareRector; @@ -41,7 +44,7 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' use TYPO3\CMS\Core\Utility\GeneralUtility; -$value = $GLOBALS['TYPO3_REQUEST']->getParsedBody()['tx_scheduler']; +$value = $GLOBALS['TYPO3_REQUEST']->getParsedBody()['tx_scheduler'] ?? null; CODE_SAMPLE ), new CodeSample( @@ -66,7 +69,7 @@ class MyActionController extends ActionController { public function myMethod() { - $value = $this->request->getParsedBody()['tx_scheduler']; + $value = $this->request->getParsedBody()['tx_scheduler'] ?? null; } } CODE_SAMPLE @@ -76,19 +79,48 @@ public function myMethod() public function getNodeTypes(): array { - return [StaticCall::class]; + return [Coalesce::class, StaticCall::class]; } /** - * @param StaticCall $node + * @param Coalesce|StaticCall $node */ public function refactorWithScope(Node $node, Scope $scope): ?Node { - return $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( + if ($node instanceof Coalesce) { + $staticCall = $node->left; + + if (! $staticCall instanceof StaticCall) { + return null; + } + + /** @var ArrayDimFetch $arrayDimFetch */ + $arrayDimFetch = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( + $scope->getClassReflection(), + $staticCall, + 'getParsedBody', + '_POST' + ); + + $node->left = $arrayDimFetch; + return $node; + } + + $methodCall = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( $scope->getClassReflection(), $node, 'getParsedBody', '_POST' ); + + if ($methodCall instanceof MethodCall) { + return $methodCall; + } + + if (! $methodCall instanceof ArrayDimFetch) { + return null; + } + + return new Coalesce($methodCall, $this->nodeFactory->createNull()); } } diff --git a/rules/TYPO312/v4/UseServerRequestInsteadOfGeneralUtilityGetRector.php b/rules/TYPO312/v4/UseServerRequestInsteadOfGeneralUtilityGetRector.php index a84a24ce3..51638f1c2 100644 --- a/rules/TYPO312/v4/UseServerRequestInsteadOfGeneralUtilityGetRector.php +++ b/rules/TYPO312/v4/UseServerRequestInsteadOfGeneralUtilityGetRector.php @@ -5,6 +5,9 @@ namespace Ssch\TYPO3Rector\TYPO312\v4; use PhpParser\Node; +use PhpParser\Node\Expr\ArrayDimFetch; +use PhpParser\Node\Expr\BinaryOp\Coalesce; +use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Expr\StaticCall; use PHPStan\Analyser\Scope; use Rector\Rector\AbstractScopeAwareRector; @@ -41,7 +44,7 @@ public function getRuleDefinition(): RuleDefinition <<<'CODE_SAMPLE' use TYPO3\CMS\Core\Utility\GeneralUtility; -$value = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_scheduler']; +$value = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_scheduler'] ?? null; CODE_SAMPLE ), new CodeSample( @@ -66,7 +69,7 @@ class MyActionController extends ActionController { public function myMethod() { - $value = $this->request->getQueryParams()['tx_scheduler']; + $value = $this->request->getQueryParams()['tx_scheduler'] ?? null; } } CODE_SAMPLE @@ -76,19 +79,48 @@ public function myMethod() public function getNodeTypes(): array { - return [StaticCall::class]; + return [Coalesce::class, StaticCall::class]; } /** - * @param StaticCall $node + * @param Coalesce|StaticCall $node */ public function refactorWithScope(Node $node, Scope $scope): ?Node { - return $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( + if ($node instanceof Coalesce) { + $staticCall = $node->left; + + if (! $staticCall instanceof StaticCall) { + return null; + } + + /** @var ArrayDimFetch $arrayDimFetch */ + $arrayDimFetch = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( + $scope->getClassReflection(), + $staticCall, + 'getQueryParams', + '_GET' + ); + + $node->left = $arrayDimFetch; + return $node; + } + + $methodCall = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall( $scope->getClassReflection(), $node, 'getQueryParams', '_GET' ); + + if ($methodCall instanceof MethodCall) { + return $methodCall; + } + + if (! $methodCall instanceof ArrayDimFetch) { + return null; + } + + return new Coalesce($methodCall, $this->nodeFactory->createNull()); } } diff --git a/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/action_controller_context.php.inc b/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/action_controller_context.php.inc index b343d7f70..7442457b4 100644 --- a/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/action_controller_context.php.inc +++ b/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/action_controller_context.php.inc @@ -26,7 +26,7 @@ class MyController extends ActionController { public function myMethod(): void { - $value = $this->request->getParsedBody()['tx_scheduler']; + $value = $this->request->getParsedBody()['tx_scheduler'] ?? null; } } diff --git a/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/standalone_class.php.inc b/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/standalone_class.php.inc index a210209dc..c0b4065ba 100644 --- a/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/standalone_class.php.inc +++ b/tests/Rector/v12/v0/UseServerRequestInsteadOfGeneralUtilityPostRector/Fixture/standalone_class.php.inc @@ -27,7 +27,7 @@ class MyClass { public function myMethod(): void { - $value = $GLOBALS['TYPO3_REQUEST']->getParsedBody()['tx_scheduler']; + $value = $GLOBALS['TYPO3_REQUEST']->getParsedBody()['tx_scheduler'] ?? null; $anotherValue = $GLOBALS['TYPO3_REQUEST']->getParsedBody(); $anotherValue2 = $GLOBALS['TYPO3_REQUEST']->getParsedBody(); diff --git a/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/action_controller_context.php.inc b/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/action_controller_context.php.inc index 259e51bd8..9faa56900 100644 --- a/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/action_controller_context.php.inc +++ b/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/action_controller_context.php.inc @@ -28,7 +28,7 @@ class MyController extends ActionController { public function myAction(): ResponseInterface { - $value = $this->request->getQueryParams()['tx_scheduler']; + $value = $this->request->getQueryParams()['tx_scheduler'] ?? null; } } diff --git a/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/standalone_class.php.inc b/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/standalone_class.php.inc index ee5f5f62f..fc2822ae4 100644 --- a/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/standalone_class.php.inc +++ b/tests/Rector/v12/v4/UseServerRequestInsteadOfGeneralUtilityGetRector/Fixture/standalone_class.php.inc @@ -27,7 +27,7 @@ class MyClass { public function myMethod(): void { - $value = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_scheduler']; + $value = $GLOBALS['TYPO3_REQUEST']->getQueryParams()['tx_scheduler'] ?? null; $anotherValue = $GLOBALS['TYPO3_REQUEST']->getQueryParams(); $anotherValue2 = $GLOBALS['TYPO3_REQUEST']->getQueryParams();