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 (#836)
by Jérémiah
02:45
created

ExpressionValidator::addGlobalVariables()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 8
nc 3
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Validator\Constraints;
6
7
use Overblog\GraphQLBundle\Definition\GraphQLServices;
8
use Overblog\GraphQLBundle\ExpressionLanguage\ExpressionLanguage;
9
use Overblog\GraphQLBundle\Generator\TypeGenerator;
10
use Overblog\GraphQLBundle\Validator\ValidationNode;
11
use Symfony\Component\Validator\Constraint;
12
use Symfony\Component\Validator\Constraints\Expression;
13
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
14
15
class ExpressionValidator extends \Symfony\Component\Validator\Constraints\ExpressionValidator
16
{
17
    private ExpressionLanguage $expressionLanguage;
18
19
    private GraphQLServices $graphQLServices;
20
21 16
    public function __construct(ExpressionLanguage $expressionLanguage, GraphQLServices $graphQLServices)
22
    {
23 16
        $this->expressionLanguage = $expressionLanguage;
24 16
        $this->graphQLServices = $graphQLServices;
25 16
        parent::__construct($expressionLanguage);
26 16
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 16
    public function validate($value, Constraint $constraint): void
32
    {
33 16
        if (!$constraint instanceof Expression) {
34
            throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
35
        }
36
37 16
        $variables = $constraint->values;
38 16
        $variables['value'] = $value;
39 16
        $variables[TypeGenerator::GRAPHQL_SERVICES] = $this->graphQLServices;
40
41 16
        $object = $this->context->getObject();
42
43 16
        $variables['this'] = $object;
44
45 16
        if ($object instanceof ValidationNode) {
46 16
            $variables['parentValue'] = $object->getResolverArg('value');
47 16
            $variables['context'] = $object->getResolverArg('context');
48 16
            $variables['args'] = $object->getResolverArg('args');
49 16
            $variables['info'] = $object->getResolverArg('info');
50
        }
51
52
        // Make all tagged GraphQL public services available in the expression constraint
53 16
        $this->addGlobalVariables($constraint->expression, $variables);
54
55 16
        if (!$this->expressionLanguage->evaluate($constraint->expression, $variables)) {
56 4
            $this->context->buildViolation($constraint->message)
57 4
                          ->setParameter('{{ value }}', $this->formatValue($value, self::OBJECT_TO_STRING))
58 4
                          ->setCode(Expression::EXPRESSION_FAILED_ERROR)
59 4
                          ->addViolation();
60
        }
61 16
    }
62
63
    /**
64
     * @param string|\Symfony\Component\ExpressionLanguage\Expression $expression
65
     */
66 16
    private function addGlobalVariables($expression, array &$variables): void
67
    {
68 16
        $globalVariables = $this->expressionLanguage->getGlobalNames();
69 16
        foreach (ExpressionLanguage::extractExpressionVarNames($expression) as $extractExpressionVarName) {
70
            if (
71 16
                isset($variables[$extractExpressionVarName])
72 16
                || !$this->graphQLServices->has($extractExpressionVarName)
73 16
                || !in_array($extractExpressionVarName, $globalVariables)
74
            ) {
75 16
                continue;
76
            }
77
78 7
            $variables[$extractExpressionVarName] = $this->graphQLServices->get($extractExpressionVarName);
79
        }
80 16
    }
81
}
82