-
Notifications
You must be signed in to change notification settings - Fork 89
/
ClosureFinderVisitor.php
49 lines (43 loc) · 1.23 KB
/
ClosureFinderVisitor.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Jeremeamia\SuperClosure;
/**
* This is a visitor that extends the nikic/php-parser library and looks for a closure node.
*/
class ClosureFinderVisitor extends \PHPParser_NodeVisitorAbstract
{
/**
* @var \ReflectionFunction
*/
protected $reflection;
/**
* @var \PHPParser_Node_Expr_Closure
*/
protected $closureNode;
/**
* @param \ReflectionFunction $reflection
*/
public function __construct(\ReflectionFunction $reflection)
{
$this->reflection = $reflection;
}
/**
* Identifies Closure nodes and holds onto the first closure it finds that matches the line number of the closure
* specified in the constructor by its reflection
*
* {@inheritdoc}
*/
public function leaveNode(\PHPParser_Node $node)
{
if (!$this->closureNode && $node instanceof \PHPParser_Node_Expr_Closure) {
$nodeStartLine = $node->getAttribute('startLine');
$closureStartLine = $this->reflection->getStartLine();
if ($nodeStartLine == $closureStartLine) {
$this->closureNode = $node;
}
}
}
public function getClosureNode()
{
return $this->closureNode;
}
}