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

Completed
Pull Request — master (#684)
by
unknown
06:07
created

ExpressionLanguage::expressionContainsVar()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 7.0957

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 27
ccs 14
cts 16
cp 0.875
rs 8.8333
c 0
b 0
f 0
cc 7
nc 12
nop 2
crap 7.0957
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
13
class ExpressionLanguage extends BaseExpressionLanguage
14
{
15
    // TODO (murtukov): make names conditional
16
    public const KNOWN_NAMES = ['value', 'args', 'context', 'info', 'object', 'validator', 'errors', 'childrenComplexity', 'typeName', 'fieldName'];
17
    public const EXPRESSION_TRIGGER = '@=';
18
19
    private array $globalNames = [];
20
21
    /**
22
     * @param $index
23
     * @param $name
24
     */
25 143
    public function addGlobalName($index, $name): void
26
    {
27 143
        $this->globalNames[$index] = $name;
28 143
    }
29
30
    public function getGlobalNames()
31
    {
32
        return $this->globalNames;
33
    }
34
35 51
    public function compile($expression, $names = [])
36
    {
37 51
        return parent::compile($expression, \array_merge($names, $this->globalNames));
38
    }
39
40
    /**
41
     * Checks if expression string containst specific variable.
42
     *
43
     * Argument can be either an Expression object or a string with or
44
     * without a prefix
45
     *
46
     * @param string            $name       - Name of the searched variable
47
     * @param string|Expression $expression - Expression to search in
48
     *
49
     * @throws SyntaxError
50
     */
51 29
    public static function expressionContainsVar(string $name, $expression): bool
52
    {
53 29
        if ($expression instanceof Expression) {
54
            $expression = $expression->__toString();
55 29
        } elseif (self::stringHasTrigger($expression)) {
56 29
            $expression = self::unprefixExpression($expression);
57
        }
58
59
        /** @var string $expression */
60 29
        $stream = (new Lexer())->tokenize($expression);
61 29
        $current = &$stream->current;
62
63 29
        while (!$stream->isEOF()) {
64 29
            if ($name === $current->value && Token::NAME_TYPE === $current->type) {
65
                // Also check that it's not a function's name
66 8
                $stream->next();
67 8
                if ('(' !== $current->value) {
68 8
                    $contained = true;
69 8
                    break;
70
                }
71
                continue;
72
            }
73
74 29
            $stream->next();
75
        }
76
77 29
        return $contained ?? false;
78
    }
79
80
    /**
81
     * Checks if a string has the expression trigger prefix.
82
     *
83
     * @param string $maybeExpression
84
     */
85 37
    public static function stringHasTrigger(string $maybeExpression): bool
86
    {
87 37
        return 0 === \strpos($maybeExpression, self::EXPRESSION_TRIGGER);
88
    }
89
90
    /**
91
     * Removes the expression trigger prefix from a string. If no prefix found,
92
     * returns the initial string.
93
     *
94
     * @param string $expression - String expression with a trigger prefix
95
     *
96
     * @return string
97
     */
98 29
    public static function unprefixExpression(string $expression)
99
    {
100 29
        $string = \substr($expression, \strlen(self::EXPRESSION_TRIGGER));
101
102 29
        return $string ?: $expression;
103
    }
104
}
105