Skip to content

Commit

Permalink
TASK: Return nodes for RefactorDeprecatedConcatenateMethodsPageRender…
Browse files Browse the repository at this point in the history
…erRector (#3998)

* TASK: Remove NodesToAddCollector. Not used

* TASK: Return nodes RefactorDeprecatedConcatenateMethodsPageRendererRector
  • Loading branch information
sabbelasichon authored Jan 9, 2024
1 parent 19fef07 commit 8e35fd5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,34 +53,34 @@ public function change(string $condition): ?string
}

switch ($type) {
case 'TSFE' :
case 'TSFE':
$conditions[$key][] = $this->refactorTsfe($property, $operator, $value);
break;
case 'GP' :
case 'GP':
$conditions[$key][] = $this->refactorGetPost($property, $operator, $value);
break;
case 'LIT' :
case 'LIT':
$conditions[$key][] = sprintf('"%s" %s "%s"', $value, self::OPERATOR_MAPPING[$operator], $property);
break;
case 'ENV' :
case 'ENV':
$conditions[$key][] = $this->createEnvCondition($property, $operator, $value);
break;
case 'IENV' :
case 'IENV':
$conditions[$key][] = $this->createIndependentCondition($property, $operator, $value);
break;
case 'BE_USER' :
case 'BE_USER':
$conditions[$key][] = $this->createBackendUserCondition($property, $operator, $value);
break;
case '_GET' :
case '_GET':
$conditions[$key][] = $this->refactorGet($property, $operator, $value);
break;
case 'GPmerged' :
case 'GPmerged':
$conditions[$key][] = $this->refactorGetPost($property, $operator, $value);
break;
case '_POST' :
case '_POST':
$conditions[$key][] = $this->refactorPost($property, $operator, $value);
break;
default :
default:
$conditions[$key][] = $condition;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
use PhpParser\Node\Name;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand All @@ -33,20 +32,14 @@ final class ForceTemplateParsingInTsfeAndTemplateServiceRector extends AbstractR
*/
private const TYPOSCRIPT = 'typoscript';

/**
* @readonly
*/
public NodesToAddCollector $nodesToAddCollector;

/**
* @readonly
*/
private Typo3NodeResolver $typo3NodeResolver;

public function __construct(Typo3NodeResolver $typo3NodeResolver, NodesToAddCollector $nodesToAddCollector)
public function __construct(Typo3NodeResolver $typo3NodeResolver)
{
$this->typo3NodeResolver = $typo3NodeResolver;
$this->nodesToAddCollector = $nodesToAddCollector;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -21,46 +21,44 @@
*/
final class RefactorDeprecatedConcatenateMethodsPageRendererRector extends AbstractRector
{
/**
* @readonly
*/
public NodesToAddCollector $nodesToAddCollector;

public function __construct(NodesToAddCollector $nodesToAddCollector)
{
$this->nodesToAddCollector = $nodesToAddCollector;
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
return [MethodCall::class, Expression::class];
}

/**
* @param MethodCall $node
* @param MethodCall|Expression $node
* @return Node[]|null|Node
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node)
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Page\PageRenderer')
)) {
if ($node instanceof MethodCall) {
if ($this->shouldSkip($node)) {
return null;
}

if ($this->isName($node->name, 'getConcatenateFiles')) {
return $this->createArrayMergeCall($node);
}

return null;
}

if ($this->isName($node->name, 'getConcatenateFiles')) {
return $this->createArrayMergeCall($node);
$methodCall = $node->expr;

if (! $methodCall instanceof MethodCall) {
return null;
}

if ($this->isName($node->name, 'enableConcatenateFiles')) {
return $this->splitMethodCall($node, 'enableConcatenateJavascript', 'enableConcatenateCss');
if ($this->isName($methodCall->name, 'enableConcatenateFiles')) {
return $this->splitMethodCall($methodCall, 'enableConcatenateJavascript', 'enableConcatenateCss');
}

if ($this->isName($node->name, 'disableConcatenateFiles')) {
return $this->splitMethodCall($node, 'disableConcatenateJavascript', 'disableConcatenateCss');
if ($this->isName($methodCall->name, 'disableConcatenateFiles')) {
return $this->splitMethodCall($methodCall, 'disableConcatenateJavascript', 'disableConcatenateCss');
}

return null;
Expand Down Expand Up @@ -93,14 +91,31 @@ private function createArrayMergeCall(MethodCall $methodCall): FuncCall
return $this->nodeFactory->createFuncCall('array_merge', [new Arg($node1), new Arg($node2)]);
}

private function splitMethodCall(MethodCall $methodCall, string $firstMethod, string $secondMethod): MethodCall
/**
* @return Node[]
*/
private function splitMethodCall(MethodCall $methodCall, string $firstMethod, string $secondMethod): array
{
$methodCall->name = new Identifier($firstMethod);

$node1 = clone $methodCall;
$node1->name = new Identifier($secondMethod);
$this->nodesToAddCollector->addNodeBeforeNode($node1, $methodCall);

return $methodCall;
return [new Expression($node1), new Expression($methodCall)];
}

private function shouldSkip(MethodCall $node): bool
{
if (! $this->nodeTypeResolver->isMethodStaticCallOrClassMethodObjectType(
$node,
new ObjectType('TYPO3\CMS\Core\Page\PageRenderer')
)) {
return true;
}

return ! $this->isNames(
$node->name,
['getConcatenateFiles', 'enableConcatenateFiles', 'disableConcatenateFiles']
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ final class ClassWithOldConcatenateMethodsPageRenderer
$pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
$files = array_merge($pageRenderer->getConcatenateCss(), $pageRenderer->getConcatenateJavascript());
$this->pageRenderer->enableConcatenateCss();

$this->pageRenderer->enableConcatenateJavascript();
$this->pageRenderer->disableConcatenateCss();
$this->pageRenderer->disableConcatenateJavascript();
Expand Down

0 comments on commit 8e35fd5

Please sign in to comment.