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 (#682)
by
unknown
24:37
created

ExpressionLanguage::expressionContainsVar()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 15
c 0
b 0
f 0
dl 0
loc 27
ccs 0
cts 0
cp 0
rs 8.8333
cc 7
nc 12
nop 2
crap 56
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 143
    private array $globalNames = [];
20
21 143
    /**
22 143
     * @param $index
23
     * @param $name
24 2
     */
25
    public function addGlobalName($index, $name): void
26 2
    {
27
        $this->globalNames[$index] = $name;
28
    }
29 51
30
    public function getGlobalNames()
31 51
    {
32
        return $this->globalNames;
33
    }
34
35
    public function compile($expression, $names = [])
36
    {
37
        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
    public static function expressionContainsVar(string $name, $expression): bool
52
    {
53
        if ($expression instanceof Expression) {
54
            $expression = $expression->__toString();
55
        } elseif (self::stringHasTrigger($expression)) {
56
            $expression = self::unprefixExpression($expression);
57
        }
58
59
        /** @var string $expression */
60
        $stream = (new Lexer())->tokenize($expression);
61
        $current = &$stream->current;
62
63
        while (!$stream->isEOF()) {
64
            if ($name === $current->value && Token::NAME_TYPE === $current->type) {
65
                // Also check that it's not a function's name
66
                $stream->next();
67
                if ('(' !== $current->value) {
68
                    $contained = true;
69
                    break;
70
                }
71
                continue;
72
            }
73
74
            $stream->next();
75
        }
76
77
        return $contained ?? false;
78
    }
79
80
    /**
81
     * Checks if a string has the expression trigger prefix.
82
     *
83
     * @param string $maybeExpression
84
     */
85
    public static function stringHasTrigger(string $maybeExpression): bool
86
    {
87
        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
    public static function unprefixExpression(string $expression)
99
    {
100
        $string = \substr($expression, \strlen(self::EXPRESSION_TRIGGER));
101
102
        return $string ?: $expression;
103
    }
104
}
105