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 Timur
06:41
created

ExpressionLanguage::expressionContainsVar()   B

Complexity

Conditions 7
Paths 12

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7

Importance

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