Passed
Push — master ( 55d5d2...562140 )
by Yaroslav
08:11
created

ExpressionAnalyzer::getUsedVariables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/*
4
 *
5
 * (c) Yaroslav Honcharuk <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Yarhon\RouteGuardBundle\ExpressionLanguage;
12
13
use Symfony\Component\ExpressionLanguage\ParsedExpression;
14
use Symfony\Component\ExpressionLanguage\Node\Node;
15
use Symfony\Component\ExpressionLanguage\Node\NameNode;
16
use Symfony\Component\ExpressionLanguage\Node\GetAttrNode;
17
use Symfony\Component\ExpressionLanguage\Node\ConstantNode;
18
19
/**
20
 * @author Yaroslav Honcharuk <[email protected]>
21
 */
22
class ExpressionAnalyzer
23
{
24
    /**
25
     * @param ParsedExpression $expression
26
     *
27
     * @return string[]
28
     */
29 15
    public function getUsedVariables(ParsedExpression $expression)
30
    {
31 15
        $variables = [];
32 15
        $this->collectVariables($expression->getNodes(), $variables);
33
34 15
        return array_unique($variables);
35
    }
36
37
    /**
38
     * @param ParsedExpression $expression
39
     * @param string           $variable
40
     *
41
     * @return string[]
42
     */
43 5
    public function getVariableAttributesCalls(ParsedExpression $expression, $variable)
44
    {
45 5
        $attributes = [];
46 5
        $this->collectVariableGetAttr($expression->getNodes(), $variable, $attributes);
47
48 5
        return array_unique($attributes);
49
    }
50
51
    /**
52
     * @param Node  $node
53
     * @param array $variables
54
     */
55 15
    private function collectVariables(Node $node, array &$variables)
56
    {
57 15
        if ($node instanceof NameNode) {
58 13
            $variables[] = $node->attributes['name'];
59
        }
60
61 15
        foreach ($node->nodes as $innerNode) {
62 15
            $this->collectVariables($innerNode, $variables);
63
        }
64 15
    }
65
66
    /**
67
     * @param Node   $node
68
     * @param string $variable
69
     * @param array  $attributes
70
     */
71 5
    private function collectVariableGetAttr(Node $node, $variable, array &$attributes)
72
    {
73 5
        if ($node instanceof GetAttrNode) {
74 4
            $nameNode = $node->nodes['node'];
75 4
            $attrNode = $node->nodes['attribute'];
76
77 4
            if ($nameNode->attributes['name'] === $variable && $attrNode instanceof ConstantNode) {
78 3
                $attributes[] = $attrNode->attributes['value'];
79
            }
80
        }
81
82 5
        foreach ($node->nodes as $innerNode) {
83 5
            $this->collectVariableGetAttr($innerNode, $variable, $attributes);
84
        }
85 5
    }
86
}
87