Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TASK: Return nodes instead of using NodesToAddCollector #3988

Merged
merged 2 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 13 additions & 30 deletions src/Rector/v8/v7/RefactorPrintContentMethodsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,58 +19,43 @@
*/
final class RefactorPrintContentMethodsRector 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 [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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

?>
-----
Expand Down