StaticVariablesCollector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 86.96%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 0
loc 66
ccs 20
cts 23
cp 0.8696
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B enterNode() 0 31 6
A getStaticVariables() 0 4 1
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