Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Passed
Pull Request — master (#770)
by Timur
46:36 queued 22:02
created

ExpressionLanguage   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 99
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 14
eloc 26
c 0
b 0
f 0
dl 0
loc 99
ccs 26
cts 26
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addGlobalName() 0 3 1
B expressionContainsVar() 0 27 7
A compile() 0 3 1
A stringHasTrigger() 0 3 1
A unprefixExpression() 0 5 2
A isStringWithTrigger() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\ExpressionLanguage;
6
7
use Symfony\Component\ExpressionLanguage\Expression;
8
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;
9
use Symfony\Component\ExpressionLanguage\Lexer;
10
use Symfony\Component\ExpressionLanguage\SyntaxError;
11
use Symfony\Component\ExpressionLanguage\Token;
12
use function array_merge;
13
use function strlen;
14
use function strpos;
15
use function substr;
16
17
class ExpressionLanguage extends BaseExpressionLanguage
18
{
19
    // TODO (murtukov): make names conditional
20
    public const KNOWN_NAMES = ['value', 'args', 'context', 'info', 'object', 'validator', 'errors', 'childrenComplexity', 'typeName', 'fieldName'];
21
    public const EXPRESSION_TRIGGER = '@=';
22
23
    public array $globalNames = [];
24
25 144
    public function addGlobalName(string $index, string $name): void
26
    {
27 144
        $this->globalNames[$index] = $name;
28 144
    }
29
30
    /**
31
     * @param string|Expression $expression
32
     * @param array             $names
33
     *
34
     * @return string
35
     */
36 51
    public function compile($expression, $names = [])
37
    {
38 51
        return parent::compile($expression, array_merge($names, $this->globalNames));
39
    }
40
41
    /**
42
     * Checks if expression string containst specific variable.
43
     *
44
     * Argument can be either an Expression object or a string with or
45
     * without a prefix
46
     *
47
     * @param string            $name       - name of the searched variable (needle)
48
     * @param string|Expression $expression - expression to search in (haystack)
49
     *
50
     * @throws SyntaxError
51
     */
52 34
    public static function expressionContainsVar(string $name, $expression): bool
53
    {
54 34
        if ($expression instanceof Expression) {
55 2
            $expression = $expression->__toString();
56 32
        } elseif (self::stringHasTrigger($expression)) {
57 31
            $expression = self::unprefixExpression($expression);
58
        }
59
60
        /** @var string $expression */
61 34
        $stream = (new Lexer())->tokenize($expression);
62 34
        $current = &$stream->current;
63
64 34
        while (!$stream->isEOF()) {
65 34
            if ($name === $current->value && Token::NAME_TYPE === $current->type) {
66
                // Also check that it's not a function's name
67 14
                $stream->next();
68 14
                if ('(' !== $current->value) {
69 11
                    $contained = true;
70 11
                    break;
71
                }
72 3
                continue;
73
            }
74
75 33
            $stream->next();
76
        }
77
78 34
        return $contained ?? false;
79
    }
80
81
    /**
82
     * Checks if value is a string and has the expression trigger prefix.
83
     *
84 40
     * @param mixed $value
85
     */
86 40
    public static function isStringWithTrigger($value): bool
87
    {
88
        if (is_string($value)) {
89
            return self::stringHasTrigger($value);
90
        }
91
92
        return false;
93
    }
94
95
    /**
96
     * Checks if a string has the expression trigger prefix.
97 31
     */
98
    public static function stringHasTrigger(string $maybeExpression): bool
99 31
    {
100
        return 0 === strpos($maybeExpression, self::EXPRESSION_TRIGGER);
101 31
    }
102
103
    /**
104
     * Removes the expression trigger prefix from a string. If no prefix found,
105
     * returns the initial string.
106
     *
107
     * @param string $expression - String expression with a trigger prefix
108
     *
109
     * @return string
110
     */
111
    public static function unprefixExpression(string $expression)
112
    {
113
        $string = substr($expression, strlen(self::EXPRESSION_TRIGGER));
114
115
        return '' !== $string ? $string : $expression;
116
    }
117
}
118