Skip to content

Commit

Permalink
TASK: Return nodes directly (#3996)
Browse files Browse the repository at this point in the history
* TASK: Return nodes directly

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
sabbelasichon and actions-user authored Jan 9, 2024
1 parent 3a60eee commit 19fef07
Showing 1 changed file with 34 additions and 42 deletions.
76 changes: 34 additions & 42 deletions src/Rector/v8/v5/ContentObjectRendererFileResourceRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
use PhpParser\Node\Stmt\If_;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PostRector\Collector\NodesToAddCollector;
use Ssch\TYPO3Rector\Helper\Typo3NodeResolver;
use Ssch\TYPO3Rector\NodeFactory\Typo3GlobalsFactory;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -34,11 +32,6 @@ final class ContentObjectRendererFileResourceRector extends AbstractRector
*/
private const PATH = 'path';

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

/**
* @readonly
*/
Expand All @@ -51,11 +44,9 @@ final class ContentObjectRendererFileResourceRector extends AbstractRector

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

Expand All @@ -64,36 +55,39 @@ public function __construct(
*/
public function getNodeTypes(): array
{
return [MethodCall::class];
return [Expression::class];
}

/**
* @param MethodCall $node
* @param Expression $node
* @return Node[]
*/
public function refactor(Node $node): ?Node
public function refactor(Node $node): ?array
{
if ($this->shouldSkip($node)) {
if (! $node->expr instanceof Assign) {
return null;
}

if (! $this->isName($node->name, 'fileResource')) {
$methodCall = $node->expr->expr;

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

$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);

if (! $parentNode instanceof Assign) {
if ($this->shouldSkip($methodCall)) {
return null;
}

$this->addInitializeVariableNode($node);
$this->addTypoScriptFrontendControllerAssignmentNode($node);
$this->addFileNameNode($node);
$this->addIfNode($node);

$this->removeNode($parentNode);
if (! $this->isName($methodCall->name, 'fileResource')) {
return null;
}

return $node;
return array_filter([
$this->addInitializeVariableNode($methodCall),
$this->addTypoScriptFrontendControllerAssignmentNode(),
$this->addFileNameNode($methodCall),
$this->addIfNode($methodCall),
]);
}

/**
Expand Down Expand Up @@ -133,42 +127,40 @@ private function shouldSkip(MethodCall $methodCall): bool
);
}

private function addInitializeVariableNode(MethodCall $methodCall): void
private function addInitializeVariableNode(MethodCall $methodCall): ?Node
{
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
$parentNode = $methodCall->getAttribute('parent');

if (! $parentNode->var instanceof PropertyFetch) {
$initializeVariable = new Expression(new Assign($parentNode->var, new String_('')));
$this->nodesToAddCollector->addNodeBeforeNode($initializeVariable, $methodCall);
if ($parentNode->var instanceof PropertyFetch) {
return null;
}

return new Expression(new Assign($parentNode->var, new String_('')));
}

private function addTypoScriptFrontendControllerAssignmentNode(MethodCall $methodCall): void
private function addTypoScriptFrontendControllerAssignmentNode(): Node
{
$typoscriptFrontendControllerVariable = new Variable('typoscriptFrontendController');
$typoscriptFrontendControllerAssign = new Assign(
$typoscriptFrontendControllerVariable,
return new Expression(new Assign(
new Variable('typoscriptFrontendController'),
$this->typo3GlobalsFactory->create(Typo3NodeResolver::TYPO_SCRIPT_FRONTEND_CONTROLLER)
);
$this->nodesToAddCollector->addNodeBeforeNode($typoscriptFrontendControllerAssign, $methodCall);
));
}

private function addFileNameNode(MethodCall $methodCall): void
private function addFileNameNode(MethodCall $methodCall): Node
{
$fileNameAssign = new Assign(
return new Expression(new Assign(
new Variable(self::PATH),
$this->nodeFactory->createMethodCall(
$this->nodeFactory->createPropertyFetch(new Variable('typoscriptFrontendController'), 'tmpl'),
'getFileName',
$methodCall->args
)
);
$this->nodesToAddCollector->addNodeBeforeNode($fileNameAssign, $methodCall);
));
}

private function addIfNode(MethodCall $methodCall): void
private function addIfNode(MethodCall $methodCall): Node
{
$parentNode = $methodCall->getAttribute(AttributeKey::PARENT_NODE);
$parentNode = $methodCall->getAttribute('parent');

$if = new If_(new BooleanAnd(
new NotIdentical(new Variable(self::PATH), $this->nodeFactory->createNull()),
Expand All @@ -181,6 +173,6 @@ private function addIfNode(MethodCall $methodCall): void
));
$if->stmts[] = new Expression($templateAssignment);

$this->nodesToAddCollector->addNodeBeforeNode($if, $methodCall);
return $if;
}
}

0 comments on commit 19fef07

Please sign in to comment.