Skip to content

Commit

Permalink
[BUGFIX] Add check if rule must be skipped
Browse files Browse the repository at this point in the history
Resolves: #4461
Releases: 3, 2

(cherry picked from commit 2a50350)
  • Loading branch information
simonschaufi committed Dec 20, 2024
1 parent 543ca51 commit 628e4f5
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Ssch\TYPO3Rector\NodeFactory\GeneralUtilitySuperGlobalsToPsr7ServerRequestFactory;
Expand Down Expand Up @@ -94,6 +95,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

if ($this->shouldSkip($staticCall)) {
return null;
}

/** @var ArrayDimFetch $arrayDimFetch */
$arrayDimFetch = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall(
$scope->getClassReflection(),
Expand All @@ -106,6 +111,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return $node;
}

if ($this->shouldSkip($node)) {
return null;
}

$methodCall = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall(
$scope->getClassReflection(),
$node,
Expand All @@ -123,4 +132,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node

return new Coalesce($methodCall, $this->nodeFactory->createNull());
}

private function shouldSkip(StaticCall $staticMethodCall): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$staticMethodCall,
new ObjectType('TYPO3\CMS\Core\Utility\GeneralUtility')
)) {
return true;
}

return ! $this->isName($staticMethodCall->name, '_POST');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use PhpParser\Node\Expr\BinaryOp\Coalesce;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PHPStan\Type\ObjectType;
use PHPStan\Analyser\Scope;
use Rector\Rector\AbstractScopeAwareRector;
use Ssch\TYPO3Rector\NodeFactory\GeneralUtilitySuperGlobalsToPsr7ServerRequestFactory;
Expand Down Expand Up @@ -94,6 +95,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return null;
}

if ($this->shouldSkip($staticCall)) {
return null;
}

/** @var ArrayDimFetch $arrayDimFetch */
$arrayDimFetch = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall(
$scope->getClassReflection(),
Expand All @@ -106,6 +111,10 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node
return $node;
}

if ($this->shouldSkip($node)) {
return null;
}

$methodCall = $this->globalsToPsr7ServerRequestFactory->refactorToPsr7MethodCall(
$scope->getClassReflection(),
$node,
Expand All @@ -123,4 +132,16 @@ public function refactorWithScope(Node $node, Scope $scope): ?Node

return new Coalesce($methodCall, $this->nodeFactory->createNull());
}

private function shouldSkip(StaticCall $staticMethodCall): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$staticMethodCall,
new ObjectType('TYPO3\CMS\Core\Utility\GeneralUtility')
)) {
return true;
}

return ! $this->isName($staticMethodCall->name, '_GET');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\UseServerRequestInsteadOfGeneralU

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class MyController extends ActionController
{
public function myMethod(): void
{
$value = GeneralUtility::_POST('tx_scheduler');

$ll = LocalizationUtility::translate('foo', 'MyExtension') ?? '';
}
}

Expand All @@ -21,12 +24,15 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v0\UseServerRequestInsteadOfGeneralU

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class MyController extends ActionController
{
public function myMethod(): void
{
$value = $this->request->getParsedBody()['tx_scheduler'] ?? null;

$ll = LocalizationUtility::translate('foo', 'MyExtension') ?? '';
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v4\UseServerRequestInsteadOfGeneralU
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class MyController extends ActionController
{
public function myAction(): ResponseInterface
{
$value = GeneralUtility::_GET('tx_scheduler');

$ll = LocalizationUtility::translate('foo', 'MyExtension') ?? '';
}
}

Expand All @@ -23,12 +26,15 @@ namespace Ssch\TYPO3Rector\Tests\Rector\v12\v4\UseServerRequestInsteadOfGeneralU
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

class MyController extends ActionController
{
public function myAction(): ResponseInterface
{
$value = $this->request->getQueryParams()['tx_scheduler'] ?? null;

$ll = LocalizationUtility::translate('foo', 'MyExtension') ?? '';
}
}

Expand Down

0 comments on commit 628e4f5

Please sign in to comment.