-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #206 from Roave/return-nodes-getter
Added ReflectionFunctionAbstract::getReturnStatementsAst() method
- Loading branch information
Showing
4 changed files
with
191 additions
and
0 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
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,39 @@ | ||
<?php | ||
|
||
namespace BetterReflection\Util\Visitor; | ||
|
||
use PhpParser\NodeTraverser; | ||
use PhpParser\NodeVisitorAbstract; | ||
use PhpParser\Node; | ||
|
||
class ReturnNodeVisitor extends NodeVisitorAbstract | ||
{ | ||
/** | ||
* @var Node\Stmt\Return_[] | ||
*/ | ||
private $returnNodes = []; | ||
|
||
public function enterNode(Node $node) | ||
{ | ||
if ($this->isScopeChangingNode($node)) { | ||
return NodeTraverser::DONT_TRAVERSE_CHILDREN; | ||
} | ||
|
||
if ($node instanceof Node\Stmt\Return_) { | ||
array_push($this->returnNodes, $node); | ||
} | ||
} | ||
|
||
private function isScopeChangingNode(Node $node) | ||
{ | ||
return $node instanceof Node\FunctionLike || $node instanceof Node\Stmt\Class_; | ||
} | ||
|
||
/** | ||
* @return Node\Stmt\Return_[] | ||
*/ | ||
public function getReturnNodes() | ||
{ | ||
return $this->returnNodes; | ||
} | ||
} |
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
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,63 @@ | ||
<?php | ||
|
||
namespace BetterReflectionTest\Util\Visitor; | ||
|
||
use BetterReflection\Util\Visitor\ReturnNodeVisitor; | ||
use PhpParser\Node; | ||
use PhpParser\NodeTraverser; | ||
|
||
/** | ||
* @covers \BetterReflection\Util\Visitor\ReturnNodeVisitor | ||
*/ | ||
class ReturnNodeVisitorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function outOfScopeNodeTypeProvider() | ||
{ | ||
return [ | ||
'onlyExpectedNodesAdded' => [ | ||
[ | ||
new Node\Scalar\MagicConst\File(), | ||
new Node\Stmt\Return_(), | ||
], | ||
1 | ||
], | ||
'returnWithinClosureShouldNotBeReturned' => [ | ||
[ | ||
new Node\Expr\Closure([ | ||
new Node\Stmt\Return_(), | ||
]), | ||
new Node\Stmt\Return_(), | ||
], | ||
1 | ||
], | ||
'returnWithinAnonymousClassShouldNotBeReturned' => [ | ||
[ | ||
new Node\Stmt\Class_('', [ | ||
new Node\Stmt\Return_(), | ||
]), | ||
new Node\Stmt\Return_(), | ||
], | ||
1 | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @param Node[] $statements | ||
* @param int $expectedReturns | ||
* @dataProvider outOfScopeNodeTypeProvider | ||
*/ | ||
public function testOnlyExpectedReturnNodesAreReturned(array $statements, $expectedReturns) | ||
{ | ||
$visitor = new ReturnNodeVisitor(); | ||
|
||
$traverser = new NodeTraverser(); | ||
$traverser->addVisitor($visitor); | ||
|
||
$traverser->traverse($statements); | ||
|
||
$foundNodes = $visitor->getReturnNodes(); | ||
$this->assertCount($expectedReturns, $foundNodes); | ||
$this->assertContainsOnlyInstancesOf(Node\Stmt\Return_::class, $foundNodes); | ||
} | ||
} |