Nextrek
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
<?php
namespace Jeremeamia\SuperClosure\Visitor;
use Jeremeamia\SuperClosure\ClosureLocation;
/**
* This is a visitor that extends the nikic/php-parser library and looks for a closure node and its location
*
* @copyright Jeremy Lindblom 2010-2013
*/
class ClosureFinderVisitor extends \PHPParser_NodeVisitorAbstract
{
/**
* @var \ReflectionFunction
*/
protected $reflection;
/**
* @var \PHPParser_Node_Expr_Closure
*/
protected $closureNode;
/**
* @var ClosureLocation
*/
protected $location;
/**
* @param \ReflectionFunction $reflection
*/
public function __construct(\ReflectionFunction $reflection)
{
$this->reflection = $reflection;
$this->location = new ClosureLocation;
}
public function beforeTraverse(array $nodes)
{
$this->location = ClosureLocation::fromReflection($this->reflection);
}
public function afterTraverse(array $nodes)
{
$this->location->finalize();
}
public function enterNode(\PHPParser_Node $node)
{
// Determine information about the closure's location
if (!$this->closureNode) {
if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
$this->location->namespace = is_array($node->name->parts) ? implode('\\', $node->name->parts) : null;
}
if ($node instanceof \PHPParser_Node_Stmt_Trait) {
$this->location->trait = $this->location->namespace . '\\' . $node->name;
$this->location->class = null;
}
elseif ($node instanceof \PHPParser_Node_Stmt_Class) {
$this->location->class = $this->location->namespace . '\\' . $node->name;
$this->location->trait = null;
}
}
// Locate the node of the closure
if ($node instanceof \PHPParser_Node_Expr_Closure) {
if ($node->getAttribute('startLine') == $this->reflection->getStartLine()) {
if ($this->closureNode) {
throw new \RuntimeException('Two closures were declared on the same line of code. Cannot determine '
. 'which closure was the intended target.');
} else {
$this->closureNode = $node;
}
}
}
}
public function leaveNode(\PHPParser_Node $node)
{
// Determine information about the closure's location
if (!$this->closureNode) {
if ($node instanceof \PHPParser_Node_Stmt_Namespace) {
$this->location->namespace = null;
}
if ($node instanceof \PHPParser_Node_Stmt_Trait) {
$this->location->trait = null;
}
elseif ($node instanceof \PHPParser_Node_Stmt_Class) {
$this->location->class = null;
}
}
}
/**
* @return \PHPParser_Node_Expr_Closure
*/
public function getClosureNode()
{
return $this->closureNode;
}
/**
* @return ClosureLocation
*/
public function getLocation()
{
return $this->location;
}
}
|
Revision |
Author |
Commited |
Message |
1464
|
MOliva
|
Tue 13 Oct, 2020 11:16:56 +0000 |
|