Skip to content

Commit

Permalink
[BUGFIX] Avoid notices and use null coalesce
Browse files Browse the repository at this point in the history
Resolves: #4392
  • Loading branch information
simonschaufi committed Oct 25, 2024
1 parent 8bb7cb4 commit 1b07af9
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 1b07af9

Please sign in to comment.