Completed
Pull Request — master (#92)
by Dmitriy
02:24
created

StaticVariablesCollector::enterNode()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6.1666

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 15
cts 18
cp 0.8333
rs 8.8017
c 0
b 0
f 0
cc 6
nc 3
nop 1
crap 6.1666
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 65
    public function __construct($context)
38
    {
39 65
        $this->context = $context;
40 65
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 10
    public function enterNode(Node $node)
46
    {
47
        // There may be internal closures, we do not need to look at them
48 10
        if ($node instanceof Node\Expr\Closure) {
49 2
            return NodeTraverser::DONT_TRAVERSE_CHILDREN;
50
        }
51
52 10
        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
                if ($staticVariable->var->name instanceof Node\Expr) {
65
                    $expressionSolver->process($staticVariable->var->name);
66
                    $name = $expressionSolver->getValue();
67
                } else {
68 2
                    $name = $staticVariable->var->name;
69
                }
70 2
                $this->staticVariables[$name] = $value;
71
            }
72
        }
73
74 10
        return null;
75
    }
76
77
    /**
78
     * Returns an associative map of static variables in the method/function body
79
     *
80
     * @return array
81
     */
82 65
    public function getStaticVariables()
83
    {
84 65
        return $this->staticVariables;
85
    }
86
}
87