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: Refactor SubstituteBackendTemplateViewWithModuleTemplateRector #4012

Merged
merged 2 commits into from
Jan 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use PHPStan\Type\ObjectType;
use Rector\Core\NodeManipulator\ClassDependencyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\PostRector\Collector\NodesToAddCollector;
use Rector\PostRector\ValueObject\PropertyMetadata;
use Ssch\TYPO3Rector\NodeAnalyzer\ExtbaseControllerRedirectAnalyzer;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
Expand All @@ -46,11 +45,6 @@ final class SubstituteBackendTemplateViewWithModuleTemplateRector extends Abstra
*/
private const MODULE_TEMPLATE = 'moduleTemplate';

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

/**
* @readonly
*/
Expand All @@ -60,11 +54,9 @@ final class SubstituteBackendTemplateViewWithModuleTemplateRector extends Abstra

public function __construct(
ClassDependencyManipulator $classDependencyManipulator,
NodesToAddCollector $nodesToAddCollector,
ExtbaseControllerRedirectAnalyzer $extbaseControllerRedirectAnalyzer
) {
$this->classDependencyManipulator = $classDependencyManipulator;
$this->nodesToAddCollector = $nodesToAddCollector;
$this->extbaseControllerRedirectAnalyzer = $extbaseControllerRedirectAnalyzer;
}

Expand Down Expand Up @@ -281,37 +273,39 @@ private function callSetContentAndGetContent(ClassMethod $classMethod): void

$this->callModuleTemplateFactoryCreateIfNeeded($classMethod);

/** @var MethodCall[] $existingHtmlResponseMethodCallNodes */
$existingHtmlResponseMethodCallNodes = $this->betterNodeFinder->find(
(array) $classMethod->stmts,
function (Node $node) {
if (! $node instanceof MethodCall) {
return false;
}

if (! $this->isName($node->name, 'htmlResponse')) {
return false;
}

return $node->args === [];
}
);
$existingHtmlResponseMethodCallNodes = $this->findAllExistingHtmlResponseMethodCalls($classMethod->stmts ?? []);

if ($existingHtmlResponseMethodCallNodes === []) {
$classMethod->stmts[] = $callSetContentOnModuleTemplateVariable;
$classMethod->stmts[] = $htmlResponseMethodCallReturn;
return;
}

foreach ($existingHtmlResponseMethodCallNodes as $existingHtmlResponseMethodCallNode) {
$this->nodesToAddCollector->addNodeBeforeNode(
$callSetContentOnModuleTemplateVariable,
$existingHtmlResponseMethodCallNode
$classMethodStatements = [];
foreach ($classMethod->stmts ?? [] as $classMethodStatement) {
$existingHtmlResponseMethodCallNodes = $this->findAllExistingHtmlResponseMethodCalls(
[$classMethodStatement]
);
$existingHtmlResponseMethodCallNode->args = $this->nodeFactory->createArgs([
$moduleTemplateRenderContentMethodCall,
]);

if ($existingHtmlResponseMethodCallNodes === []) {
$classMethodStatements[] = $classMethodStatement;
continue;
}

foreach ($existingHtmlResponseMethodCallNodes as $existingHtmlResponseMethodCallNode) {
if (! $existingHtmlResponseMethodCallNode instanceof MethodCall) {
continue;
}

$classMethodStatements[] = $callSetContentOnModuleTemplateVariable;
$classMethodStatements[] = $classMethodStatement;
$existingHtmlResponseMethodCallNode->args = $this->nodeFactory->createArgs([
$moduleTemplateRenderContentMethodCall,
]);
}
}

$classMethod->stmts = $classMethodStatements;
}

private function callModuleTemplateFactoryCreateIfNeeded(ClassMethod $classMethod): void
Expand Down Expand Up @@ -345,4 +339,26 @@ function (Node $node) {
array_unshift($classMethod->stmts, $moduleTemplateFactoryAssignment);
}
}

/**
* @param Node[] $nodes
* @return Node[]
*/
private function findAllExistingHtmlResponseMethodCalls(array $nodes): array
{
return $this->betterNodeFinder->find(
$nodes,
function (Node $node) {
if (! $node instanceof MethodCall) {
return false;
}

if (! $this->isName($node->name, 'htmlResponse')) {
return false;
}

return $node->args === [];
}
);
}
}