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 (#682)
by
unknown
26:17 queued 01:19
created

ExpressionLanguage::expressionContainsVar()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 26
ccs 0
cts 0
cp 0
rs 8.8333
c 0
b 0
f 0
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
        $stream = (new Lexer())->tokenize($expression);
60
        $current = &$stream->current;
61
62
        while (!$stream->isEOF()) {
63
            if ($name === $current->value && Token::NAME_TYPE === $current->type) {
64
                // Also check that it's not a function's name
65
                $stream->next();
66
                if ('(' !== $current->value) {
67
                    $contained = true;
68
                    break;
69
                }
70
                continue;
71
            }
72
73
            $stream->next();
74
        }
75
76
        return $contained ?? false;
77
    }
78
79
    /**
80
     * Checks if a string has the expression trigger prefix.
81
     *
82
     * @param string $maybeExpression
83
     */
84
    public static function stringHasTrigger(string $maybeExpression): bool
85
    {
86
        return 0 === \strpos($maybeExpression, self::EXPRESSION_TRIGGER);
87
    }
88
89
    /**
90
     * Removes the expression trigger prefix from a string. If no prefix found,
91
     * returns the initial string.
92
     *
93
     * @param string $expression - String expression with a trigger prefix
94
     *
95
     * @return false|string
96
     */
97
    public static function unprefixExpression(string $expression)
98
    {
99
        $string = \substr($expression, \strlen(self::EXPRESSION_TRIGGER));
100
101
        return $string ?: $expression;
102
    }
103
}
104