From 9c023a2f85133d25c39c0802fdf22d9ea916a503 Mon Sep 17 00:00:00 2001 From: Sebastian Schreiber Date: Fri, 5 Jan 2024 14:04:38 +0100 Subject: [PATCH 1/2] TASK: Return nodes --- .../v7/RefactorPrintContentMethodsRector.php | 42 ++++++------------- ...thod_task_module_controller_rector.php.inc | 3 +- 2 files changed, 13 insertions(+), 32 deletions(-) diff --git a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php index 4fb49bf52..2c4efe5f6 100644 --- a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php +++ b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php @@ -8,10 +8,7 @@ use PhpParser\Node\Expr\MethodCall; use PhpParser\Node\Stmt\Echo_; 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 +18,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 [Node\Stmt\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(); ?> ----- From 20b1480c9205eb1658c5d808af7a0b38590f2f0e Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Fri, 5 Jan 2024 13:10:12 +0000 Subject: [PATCH 2/2] [ci-review] Rector Rectify --- src/Rector/v8/v7/RefactorPrintContentMethodsRector.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php index 2c4efe5f6..b4868eb66 100644 --- a/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php +++ b/src/Rector/v8/v7/RefactorPrintContentMethodsRector.php @@ -7,6 +7,7 @@ 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\Rector\AbstractRector; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; @@ -23,7 +24,7 @@ final class RefactorPrintContentMethodsRector extends AbstractRector */ public function getNodeTypes(): array { - return [Node\Stmt\Expression::class]; + return [Expression::class]; } /**