|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Parser Reflection API |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* This source file is subject to the license that is bundled |
|
8
|
|
|
* with this source code in the file LICENSE. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Go\ParserReflection\NodeVisitor; |
|
12
|
|
|
|
|
13
|
|
|
use Go\ParserReflection\ValueResolver\NodeExpressionResolver; |
|
14
|
|
|
use PhpParser\Node; |
|
15
|
|
|
use PhpParser\NodeTraverser; |
|
16
|
|
|
use PhpParser\NodeVisitorAbstract; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Visitor to collect static variables in the method/function body and resove them |
|
20
|
|
|
*/ |
|
21
|
|
|
class StaticVariablesCollector extends NodeVisitorAbstract |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Reflection context, eg. ReflectionClass, ReflectionMethod, etc |
|
25
|
|
|
* |
|
26
|
|
|
* @var mixed |
|
27
|
|
|
*/ |
|
28
|
|
|
private $context; |
|
29
|
|
|
|
|
30
|
|
|
private $staticVariables = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Default constructor |
|
34
|
|
|
* |
|
35
|
|
|
* @param mixed $context Reflection context, eg. ReflectionClass, ReflectionMethod, etc |
|
36
|
|
|
*/ |
|
37
|
2 |
|
public function __construct($context) |
|
38
|
|
|
{ |
|
39
|
2 |
|
$this->context = $context; |
|
40
|
2 |
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* {@inheritDoc} |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function enterNode(Node $node) |
|
46
|
|
|
{ |
|
47
|
|
|
// There may be internal closures, we do not need to look at them |
|
48
|
2 |
|
if ($node instanceof Node\Expr\Closure) { |
|
49
|
2 |
|
return NodeTraverser::DONT_TRAVERSE_CHILDREN; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
2 |
|
if ($node instanceof Node\Stmt\Static_) { |
|
53
|
2 |
|
$expressionSolver = new NodeExpressionResolver($this->context); |
|
54
|
2 |
|
$staticVariables = $node->vars; |
|
55
|
2 |
|
foreach ($staticVariables as $staticVariable) { |
|
56
|
2 |
|
$expr = $staticVariable->default; |
|
57
|
2 |
|
if ($expr) { |
|
58
|
2 |
|
$expressionSolver->process($expr); |
|
59
|
2 |
|
$value = $expressionSolver->getValue(); |
|
60
|
|
|
} else { |
|
61
|
|
|
$value = null; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
2 |
|
$this->staticVariables[$staticVariable->name] = $value; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
2 |
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns an associative map of static variables in the method/function body |
|
73
|
|
|
* |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
2 |
|
public function getStaticVariables() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->staticVariables; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|