Skip to content

Commit

Permalink
skip nested property fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Jan 31, 2021
1 parent c49f619 commit cc8fc35
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 2 deletions.
87 changes: 87 additions & 0 deletions rules/nette/src/NodeAnalyzer/RenderMethodAnalyzer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

declare(strict_types=1);

namespace Rector\Nette\NodeAnalyzer;

use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeTraverser;
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;

public function __construct(
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
NodeNameResolver $nodeNameResolver,
ScopeNestingComparator $scopeNestingComparator
) {
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->nodeNameResolver = $nodeNameResolver;
$this->scopeNestingComparator = $scopeNestingComparator;
}

public function hasConditionalTemplateAssigns(ClassMethod $classMethod): bool
{
$hasConditionalAssigns = false;

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$classMethod,
function (Node $node) use (&$hasConditionalAssigns) {
if (! $node instanceof Assign) {
return null;
}

if (! $this->isThisTemplatePropertyFetch($node->var)) {
return null;
}

if ($this->scopeNestingComparator->isNodeConditionallyScoped($node)) {
$hasConditionalAssigns = true;
return NodeTraverser::STOP_TRAVERSAL;
}

return null;
}
);

return $hasConditionalAssigns;
}

private function isThisTemplatePropertyFetch(Node $node): bool
{
if (! $node instanceof PropertyFetch) {
return false;
}

if (! $node->var instanceof PropertyFetch) {
return false;
}

$nestedPropertyFetch = $node->var;
if (! $this->nodeNameResolver->isName($nestedPropertyFetch->var, 'this')) {
return false;
}

return $this->nodeNameResolver->isName($nestedPropertyFetch->name, 'template');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Rector\AbstractRector;
use Rector\Nette\NodeAnalyzer\RenderMethodAnalyzer;
use Rector\Nette\NodeFactory\ActionRenderFactory;
use Rector\Nette\TemplatePropertyAssignCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
Expand All @@ -31,12 +32,19 @@ final class TemplateMagicAssignToExplicitVariableArrayRector extends AbstractRec
*/
private $actionRenderFactory;

/**
* @var RenderMethodAnalyzer
*/
private $renderMethodAnalyzer;

public function __construct(
ActionRenderFactory $actionRenderFactory,
TemplatePropertyAssignCollector $templatePropertyAssignCollector
TemplatePropertyAssignCollector $templatePropertyAssignCollector,
RenderMethodAnalyzer $renderMethodAnalyzer
) {
$this->templatePropertyAssignCollector = $templatePropertyAssignCollector;
$this->actionRenderFactory = $actionRenderFactory;
$this->renderMethodAnalyzer = $renderMethodAnalyzer;
}

public function getRuleDefinition(): RuleDefinition
Expand Down Expand Up @@ -126,6 +134,6 @@ private function shouldSkip(ClassMethod $classMethod): bool
return true;
}

return ! $classMethod->isPublic();
return $this->renderMethodAnalyzer->hasConditionalTemplateAssigns($classMethod);
}
}

0 comments on commit cc8fc35

Please sign in to comment.