-
-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c49f619
commit b91e6a9
Showing
2 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Nette\NodeAnalyzer; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr; | ||
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): int { | ||
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(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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters