Skip to content
This repository has been archived by the owner on Sep 24, 2020. It is now read-only.

Commit

Permalink
[Analyzer] Helper\ResolveExpressionTrait - rewrite code to find any N…
Browse files Browse the repository at this point in the history
…ode, refs #185

- We didnt implement CFG right now, but I added yet another simple way to find Statement or Expression by Node::class name, it's a help commit for hacktoberfest contributers
  • Loading branch information
ovr committed Oct 5, 2016
1 parent 348c992 commit 39148e0
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/Analyzer/Helper/ResolveExpressionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Yield_;
use PhpParser\Node\Stmt\Return_;
use PHPSA\Context;

Expand Down Expand Up @@ -34,13 +35,36 @@ public function resolveFunctionName(FuncCall $funcCall, Context $context)
}

/**
* Return \Generator with Return_ statement(s)
*
* @param \PhpParser\Node[] $nodes
* @return \PhpParser\Node\Stmt\Return_
* @return \Generator
*/
protected function findReturnStatement(array $nodes)
{
return $this->findNode($nodes, Return_::class);
}

/**
* Return \Generator with Yield_ expression(s)
*
* @param \PhpParser\Node[] $nodes
* @return \Generator
*/
protected function findYieldExpression(array $nodes)
{
return $this->findNode($nodes, Yield_::class);
}

/**
* @param array $nodes
* @param string $nodeName Class name of Node(s) what We should return
* @return \Generator
*/
protected function findNode(array $nodes, $nodeName)
{
foreach ($this->traverseArray($nodes) as $node) {
if ($node instanceof Return_) {
if (get_class($node) === $nodeName) {
yield $node;
}
}
Expand All @@ -50,11 +74,11 @@ protected function findReturnStatement(array $nodes)
* For the code above
* Я атеист, но когда я начинал это писать, только Бог и я понимали, что я делаю
* Сейчас остался только Бог
*/

/**
*
* @param Node $node
* @return \Generator
*
* @todo After move to PHP 7.0+ use yield from
*/
protected function traverseNode(Node $node)
{
Expand Down

0 comments on commit 39148e0

Please sign in to comment.