mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-21 01:41:00 +01:00
[Nette] Allow optional case to render() parameters (#5538)
This commit is contained in:
parent
6072b261f5
commit
768e1ad381
@ -4,49 +4,26 @@ declare(strict_types=1);
|
||||
|
||||
namespace Rector\Nette\NodeAnalyzer;
|
||||
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr;
|
||||
use PhpParser\Node\Expr\Assign;
|
||||
use PhpParser\Node\Expr\MethodCall;
|
||||
use PhpParser\Node\Expr\PropertyFetch;
|
||||
use PhpParser\Node\Stmt\ClassMethod;
|
||||
use PhpParser\NodeTraverser;
|
||||
use Rector\Core\PhpParser\Node\BetterNodeFinder;
|
||||
use Rector\NodeNameResolver\NodeNameResolver;
|
||||
use Rector\NodeNestingScope\ScopeNestingComparator;
|
||||
use Symplify\Astral\NodeTraverser\SimpleCallableNodeTraverser;
|
||||
|
||||
final class RenderMethodAnalyzer
|
||||
{
|
||||
/**
|
||||
* @var SimpleCallableNodeTraverser
|
||||
*/
|
||||
private $simpleCallableNodeTraverser;
|
||||
|
||||
/**
|
||||
* @var NodeNameResolver
|
||||
*/
|
||||
private $nodeNameResolver;
|
||||
|
||||
/**
|
||||
* @var ScopeNestingComparator
|
||||
*/
|
||||
private $scopeNestingComparator;
|
||||
|
||||
/**
|
||||
* @var BetterNodeFinder
|
||||
*/
|
||||
private $betterNodeFinder;
|
||||
|
||||
public function __construct(
|
||||
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
|
||||
NodeNameResolver $nodeNameResolver,
|
||||
ScopeNestingComparator $scopeNestingComparator,
|
||||
BetterNodeFinder $betterNodeFinder
|
||||
) {
|
||||
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
|
||||
public function __construct(NodeNameResolver $nodeNameResolver, BetterNodeFinder $betterNodeFinder)
|
||||
{
|
||||
$this->nodeNameResolver = $nodeNameResolver;
|
||||
$this->scopeNestingComparator = $scopeNestingComparator;
|
||||
$this->betterNodeFinder = $betterNodeFinder;
|
||||
}
|
||||
|
||||
@ -63,49 +40,4 @@ final class RenderMethodAnalyzer
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function hasConditionalTemplateAssigns(ClassMethod $classMethod): bool
|
||||
{
|
||||
$hasConditionalAssigns = false;
|
||||
|
||||
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
|
||||
$classMethod,
|
||||
function (Node $node) use (&$hasConditionalAssigns): ?int {
|
||||
if (! $node instanceof Assign) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (! $this->isThisTemplatePropertyFetch($node->var)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($this->scopeNestingComparator->isNodeConditionallyScoped($node->var)) {
|
||||
$hasConditionalAssigns = true;
|
||||
return NodeTraverser::STOP_TRAVERSAL;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
);
|
||||
|
||||
return $hasConditionalAssigns;
|
||||
}
|
||||
|
||||
private function isThisTemplatePropertyFetch(Expr $expr): bool
|
||||
{
|
||||
if (! $expr instanceof PropertyFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! $expr->var instanceof PropertyFetch) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$nestedPropertyFetch = $expr->var;
|
||||
if (! $this->nodeNameResolver->isName($nestedPropertyFetch->var, 'this')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->nodeNameResolver->isName($nestedPropertyFetch->name, 'template');
|
||||
}
|
||||
}
|
||||
|
@ -145,10 +145,6 @@ CODE_SAMPLE
|
||||
return true;
|
||||
}
|
||||
|
||||
if (! $this->netteClassAnalyzer->isInComponent($classMethod)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return $this->renderMethodAnalyzer->hasConditionalTemplateAssigns($classMethod);
|
||||
return ! $this->netteClassAnalyzer->isInComponent($classMethod);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\TemplateMagicAssignToExplicitVariableArrayRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class ConditionalAndOneNormalVariables extends Control
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
if (mt_rand(0, 1000)) {
|
||||
$this->template->yes = 'true';
|
||||
}
|
||||
|
||||
$this->template->normalVariable = 'include me!';
|
||||
$this->template->render(__DIR__ . '/someFile.latte');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\Nette\Tests\Rector\ClassMethod\TemplateMagicAssignToExplicitVariableArrayRector\Fixture;
|
||||
|
||||
use Nette\Application\UI\Control;
|
||||
|
||||
final class ConditionalAndOneNormalVariables extends Control
|
||||
{
|
||||
public function render()
|
||||
{
|
||||
if (mt_rand(0, 1000)) {
|
||||
$this->template->yes = 'true';
|
||||
}
|
||||
$this->template->render(__DIR__ . '/someFile.latte', ['normalVariable' => 'include me!']);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user