diff --git a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php index 4fb49bf52..b4868eb66 100644 --- a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php +++ b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php @@ -7,11 +7,9 @@ use PhpParser\Node; use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Stmt\Echo_; +use PhpParser\Node\Stmt\Expression; use PHPStan\Type\ObjectType; -use Rector\Core\Exception\ShouldNotHappenException; use Rector\Core\Rector\AbstractRector; -use Rector\NodeTypeResolver\Node\AttributeKey; -use Rector\PostRector\Collector\NodesToAddCollector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; @@ -21,58 +19,43 @@ */ final class RefactorPrintContentMethodsRector extends AbstractRector { - /** - * @readonly - */ - public NodesToAddCollector $nodesToAddCollector; - - public function __construct(NodesToAddCollector $nodesToAddCollector) - { - $this->nodesToAddCollector = $nodesToAddCollector; - } - /** * @return array> */ public function getNodeTypes(): array { - return [MethodCall::class]; + return [Expression::class]; } /** - * @param MethodCall $node + * @param Node\Stmt\Expression $node */ public function refactor(Node $node): ?Node { - if ($this->shouldSkip($node)) { + if (! $node->expr instanceof MethodCall) { return null; } - if (! $this->isName($node->name, 'printContent')) { + if ($this->shouldSkip($node->expr)) { return null; } - if ($this->isPageLayoutControllerClass($node)) { + if (! $this->isName($node->expr->name, 'printContent')) { + return null; + } + + if ($this->isPageLayoutControllerClass($node->expr)) { $echo = new Echo_([ $this->nodeFactory->createMethodCall( - $this->nodeFactory->createMethodCall($node->var, 'getModuleTemplate'), + $this->nodeFactory->createMethodCall($node->expr->var, 'getModuleTemplate'), 'renderContent' ), ]); } else { - $echo = new Echo_([$this->nodeFactory->createPropertyFetch($node->var, 'content')]); + $echo = new Echo_([$this->nodeFactory->createPropertyFetch($node->expr->var, 'content')]); } - try { - $this->removeNode($node); - } catch (ShouldNotHappenException $shouldNotHappenException) { - $parentNode = $node->getAttribute(AttributeKey::PARENT_NODE); - $this->removeNode($parentNode); - } - - $this->nodesToAddCollector->addNodeBeforeNode($echo, $node); - - return $node; + return $echo; } /** diff --git a/tests/Rector/v8/v7/RefactorPrintContentMethodsRector/Fixture/refactor_print_method_task_module_controller_rector.php.inc b/tests/Rector/v8/v7/RefactorPrintContentMethodsRector/Fixture/refactor_print_method_task_module_controller_rector.php.inc index 51478f425..2d3c2bcb1 100644 --- a/tests/Rector/v8/v7/RefactorPrintContentMethodsRector/Fixture/refactor_print_method_task_module_controller_rector.php.inc +++ b/tests/Rector/v8/v7/RefactorPrintContentMethodsRector/Fixture/refactor_print_method_task_module_controller_rector.php.inc @@ -6,8 +6,7 @@ use TYPO3\CMS\Taskcenter\Controller\TaskModuleController; $taskLayoutController = GeneralUtility::makeInstance(TaskModuleController::class); $taskLayoutController->printContent(); -/** This is stupid, but could break our rector */ -$assign = $taskLayoutController->printContent(); +$taskLayoutController->printContent(); ?> -----