GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ExpressionEvaluator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 2
dl 0
loc 106
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setContextVariable() 0 4 1
A evaluateArray() 0 12 3
A __construct() 0 6 1
B evaluate() 0 30 6
A registerFunction() 0 14 2
1
<?php
2
3
namespace Hateoas\Expression;
4
5
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
6
7
/**
8
 * @author Adrien Brault <[email protected]>
9
 * @author William Durand <[email protected]>
10
 */
11
class ExpressionEvaluator
12
{
13
    const EXPRESSION_REGEX = '/expr\((?P<expression>.+)\)/';
14
15
    /**
16
     * @var ExpressionLanguage
17
     */
18
    private $expressionLanguage;
19
20
    /**
21
     * @var array
22
     */
23
    private $context;
24
25
    /**
26
     * @var array
27
     */
28
    private $cache;
29
30
    public function __construct(ExpressionLanguage $expressionLanguage, array $context = array(), array $cache = array())
31
    {
32
        $this->expressionLanguage = $expressionLanguage;
33
        $this->context            = $context;
34
        $this->cache              = $cache;
35
    }
36
37
    /**
38
     * @param string $name
39
     * @param mixed  $value
40
     */
41
    public function setContextVariable($name, $value)
42
    {
43
        $this->context[$name] = $value;
44
    }
45
46
    /**
47
     * @param  string $expression
48
     * @param  mixed  $data
49
     * @return mixed
50
     */
51
    public function evaluate($expression, $data)
52
    {
53
        if (!is_string($expression)) {
54
            return $expression;
55
        }
56
57
        $key = $expression;
58
59
        if (!array_key_exists($key, $this->cache)) {
60
            if (!preg_match(self::EXPRESSION_REGEX, $expression, $matches)) {
61
                $this->cache[$key] = false;
62
            } else {
63
                $expression = $matches['expression'];
64
                $context = $this->context;
65
                $context['object'] = $data;
66
                $this->cache[$key] = $this->expressionLanguage->parse($expression, array_keys($context));
67
            }
68
        }
69
70
        if (false !== $this->cache[$key]) {
71
            if (!isset($context)) {
72
                $context = $this->context;
73
                $context['object'] = $data;
74
            }
75
76
            return $this->expressionLanguage->evaluate($this->cache[$key], $context);
77
        }
78
79
        return $expression;
80
    }
81
82
    public function evaluateArray(array $array, $data)
83
    {
84
        $newArray = array();
85
        foreach ($array as $key => $value) {
86
            $key   = $this->evaluate($key, $data);
87
            $value = is_array($value) ? $this->evaluateArray($value, $data) : $this->evaluate($value, $data);
88
89
            $newArray[$key] = $value;
90
        }
91
92
        return $newArray;
93
    }
94
95
    /**
96
     * Register a new new ExpressionLanguage function.
97
     *
98
     * @param ExpressionFunctionInterface $function
99
     *
100
     * @return ExpressionEvaluator
101
     */
102
    public function registerFunction(ExpressionFunctionInterface $function)
103
    {
104
        $this->expressionLanguage->register(
105
            $function->getName(),
106
            $function->getCompiler(),
107
            $function->getEvaluator()
108
        );
109
110
        foreach ($function->getContextVariables() as $name => $value) {
111
            $this->setContextVariable($name, $value);
112
        }
113
114
        return $this;
115
    }
116
}
117