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 — 0.14 (#851)
by Jérémiah
08:23
created

addExpressionVariableNameServiceId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\ExpressionLanguage;
6
7
use Overblog\GraphQLBundle\Generator\TypeGenerator;
8
use Symfony\Component\ExpressionLanguage\Expression;
9
use Symfony\Component\ExpressionLanguage\ExpressionLanguage as BaseExpressionLanguage;
10
use Symfony\Component\ExpressionLanguage\Lexer;
11
use Symfony\Component\ExpressionLanguage\SyntaxError;
12
use Symfony\Component\ExpressionLanguage\Token;
13
use function array_merge;
14
use function strlen;
15
use function strpos;
16
use function substr;
17
18
class ExpressionLanguage extends BaseExpressionLanguage
19
{
20
    // TODO (murtukov): make names conditional
21
    public const KNOWN_NAMES = ['value', 'args', 'context', 'info', 'object', 'validator', 'errors', 'childrenComplexity', 'typeName', 'fieldName'];
22
    public const EXPRESSION_TRIGGER = '@=';
23
24
    /** @var array<string, string> */
25
    public array $globalNames = [];
26
27
    /** @var array<string, string> */
28
    public array $expressionVariableServiceIds = [];
29
30 149
    public function addExpressionVariableNameServiceId(string $expressionVarName, string $serviceId): void
31
    {
32 149
        $this->expressionVariableServiceIds[$expressionVarName] = $serviceId;
33 149
        $this->addGlobalName(sprintf(TypeGenerator::GRAPHQL_SERVICES.'->get(\'%s\')', $serviceId), $expressionVarName);
34 149
    }
35
36
    /**
37
     * @return array<string, string>
38
     */
39 17
    public function getExpressionVariableServiceIds(): array
40
    {
41 17
        return $this->expressionVariableServiceIds;
42
    }
43
44 149
    public function addGlobalName(string $code, string $expressionVarName): void
45
    {
46 149
        $this->globalNames[$code] = $expressionVarName;
47 149
    }
48
49
    /**
50
     * @param string|Expression $expression
51
     * @param array             $names
52
     *
53
     * @return string
54
     */
55 55
    public function compile($expression, $names = [])
56
    {
57 55
        return parent::compile($expression, array_merge($names, $this->globalNames));
58
    }
59
60
    /**
61
     * Checks if expression string containst specific variable.
62
     *
63
     * Argument can be either an Expression object or a string with or
64
     * without a prefix
65
     *
66
     * @param string            $name       - name of the searched variable (needle)
67
     * @param string|Expression $expression - expression to search in (haystack)
68
     *
69
     * @throws SyntaxError
70
     */
71 39
    public static function expressionContainsVar(string $name, $expression): bool
72
    {
73 39
        foreach (static::extractExpressionVarNames($expression) as $varName) {
74 34
            if ($name === $varName) {
75 13
                return true;
76
            }
77
        }
78
79 34
        return false;
80
    }
81
82
    /**
83
     * @param string|Expression $expression - expression to search in (haystack)
84
     *
85
     * @throws SyntaxError
86
     */
87 60
    public static function extractExpressionVarNames($expression): iterable
88
    {
89 60
        if ($expression instanceof Expression) {
90 2
            $expression = $expression->__toString();
91 58
        } elseif (self::stringHasTrigger($expression)) {
92 9
            $expression = self::unprefixExpression($expression);
93
        }
94
95
        /** @var string $expression */
96 60
        $stream = (new Lexer())->tokenize($expression);
97 60
        $current = &$stream->current;
98 60
        $isProperty = false;
99 60
        $varNames = [];
100
101 60
        while (!$stream->isEOF()) {
102 60
            if ('.' === $current->value) {
103 30
                $isProperty = true;
104 60
            } elseif (Token::NAME_TYPE === $current->type) {
105 60
                if (!$isProperty) {
106 60
                    $name = $current->value;
107
                    // Also check that it's not a function's name
108 60
                    $stream->next();
109 60
                    if ('(' !== $current->value) {
110 54
                        $varNames[] = $name;
111
                    }
112 60
                    continue;
113
                } else {
114 30
                    $isProperty = false;
115
                }
116
            }
117
118 59
            $stream->next();
119
        }
120
121 60
        return $varNames;
122
    }
123
124
    /**
125
     * Checks if value is a string and has the expression trigger prefix.
126
     *
127
     * @param mixed $value
128
     */
129 35
    public static function isStringWithTrigger($value): bool
130
    {
131 35
        if (is_string($value)) {
132 35
            return self::stringHasTrigger($value);
133
        }
134
135 2
        return false;
136
    }
137
138
    /**
139
     * Checks if a string has the expression trigger prefix.
140
     */
141 68
    public static function stringHasTrigger(string $maybeExpression): bool
142
    {
143 68
        return 0 === strpos($maybeExpression, self::EXPRESSION_TRIGGER);
144
    }
145
146
    /**
147
     * Removes the expression trigger prefix from a string. If no prefix found,
148
     * returns the initial string.
149
     *
150
     * @param string $expression - String expression with a trigger prefix
151
     *
152
     * @return string
153
     */
154 33
    public static function unprefixExpression(string $expression)
155
    {
156 33
        $string = substr($expression, strlen(self::EXPRESSION_TRIGGER));
157
158 33
        return '' !== $string ? $string : $expression;
159
    }
160
}
161