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 (#602)
by
unknown
22:29
created

Executor::getSchemaNameOrDefault()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Overblog\GraphQLBundle\Request;
6
7
use GraphQL\Executor\ExecutionResult;
8
use GraphQL\Executor\Promise\PromiseAdapter;
9
use GraphQL\GraphQL;
10
use GraphQL\Type\Schema;
11
use GraphQL\Validator\DocumentValidator;
12
use GraphQL\Validator\Rules\DisableIntrospection;
13
use GraphQL\Validator\Rules\QueryComplexity;
14
use GraphQL\Validator\Rules\QueryDepth;
15
use Overblog\GraphQLBundle\Event\Events;
16
use Overblog\GraphQLBundle\Event\ExecutorArgumentsEvent;
17
use Overblog\GraphQLBundle\Event\ExecutorContextEvent;
18
use Overblog\GraphQLBundle\Event\ExecutorResultEvent;
19
use Overblog\GraphQLBundle\Executor\ExecutorInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
23
class Executor
24
{
25
    public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter';
26
27
    private $schemas = [];
28
29
    private $dispatcher;
30
31
    private $promiseAdapter;
32
33
    private $executor;
34
35
    private $defaultFieldResolver;
36
37
    private $useExperimentalExecutor;
38
39 123
    public function __construct(
40
        ExecutorInterface $executor,
41
        PromiseAdapter $promiseAdapter,
42
        EventDispatcherInterface $dispatcher,
43
        ?callable $defaultFieldResolver = null,
44
        bool $useExperimental = false
45
    ) {
46 123
        $this->executor = $executor;
47 123
        $this->promiseAdapter = $promiseAdapter;
48 123
        $this->dispatcher = $dispatcher;
49 123
        $this->defaultFieldResolver = $defaultFieldResolver;
50 123
        $this->useExperimentalExecutor = $useExperimental;
51 123
    }
52
53
    public function setExecutor(ExecutorInterface $executor): self
54
    {
55
        $this->executor = $executor;
56
57
        return $this;
58
    }
59
60 115
    public function addSchemaBuilder(string $name, callable $builder): self
61
    {
62 115
        $this->schemas[$name] = $builder;
63
64 115
        return $this;
65
    }
66
67
    /**
68
     * @param string $name
69
     * @param Schema $schema
70
     *
71
     * @return self
72
     */
73 106
    public function addSchema(string $name, Schema $schema): self
74
    {
75 106
        $this->schemas[$name] = $schema;
76
77 106
        return $this;
78
    }
79
80
    /**
81
     * @param string|null $name
82
     *
83
     * @return Schema
84
     */
85 109
    public function getSchema(?string $name = null): Schema
86
    {
87 109
        if (empty($this->schemas)) {
88 1
            throw new \RuntimeException('At least one schema should be declare.');
89
        }
90
91 108
        $name = $this->getSchemaNameOrDefault($name);
92
93 102
        if (!isset($this->schemas[$name])) {
94 102
            throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name));
95
        }
96
97 108
        $schema = $this->schemas[$name];
98 1
        if (\is_callable($schema)) {
99
            $schema = $schema();
100 107
            $this->addSchema($name, $schema);
101 107
        }
102 107
103 106
        return $schema;
104
    }
105
106 106
    public function setMaxQueryDepth($maxQueryDepth): void
107
    {
108
        /** @var QueryDepth $queryDepth */
109 122
        $queryDepth = DocumentValidator::getRule('QueryDepth');
110
        $queryDepth->setMaxQueryDepth($maxQueryDepth);
111
    }
112 122
113 122
    public function setMaxQueryComplexity($maxQueryComplexity): void
114 122
    {
115
        /** @var QueryComplexity $queryComplexity */
116 122
        $queryComplexity = DocumentValidator::getRule('QueryComplexity');
117
        $queryComplexity->setMaxQueryComplexity($maxQueryComplexity);
118
    }
119 122
120 122
    public function enableIntrospectionQuery(): void
121 122
    {
122
        DocumentValidator::addRule(new DisableIntrospection(DisableIntrospection::DISABLED));
123 121
    }
124
125 121
    public function disableIntrospectionQuery(): void
126 121
    {
127
        DocumentValidator::addRule(new DisableIntrospection());
128 1
    }
129
130 1
    /**
131 1
     * @param string|null                    $schemaName
132
     * @param array                          $request
133
     * @param array|\ArrayObject|object|null $rootValue
134
     *
135
     * @return ExecutionResult
136
     */
137
    public function execute(?string $schemaName, array $request, $rootValue = null): ExecutionResult
138
    {
139
        $this->useExperimentalExecutor ? GraphQL::useExperimentalExecutor() : GraphQL::useReferenceExecutor();
140 105
141
        $schemaName = $this->getSchemaNameOrDefault($schemaName);
142 105
        $executorArgumentsEvent = $this->preExecute(
143
            $schemaName,
144 105
            $this->getSchema($schemaName),
145 105
            $request[ParserInterface::PARAM_QUERY] ?? null,
146 103
            new \ArrayObject(),
147 103
            $rootValue,
148 103
            $request[ParserInterface::PARAM_VARIABLES],
149 103
            $request[ParserInterface::PARAM_OPERATION_NAME] ?? null
150 103
        );
151
152
        $executorArgumentsEvent->getSchema()->processExtensions();
153 103
154
        $result = $this->executor->execute(
155 103
            $this->promiseAdapter,
156 103
            $executorArgumentsEvent->getSchema(),
157 103
            $executorArgumentsEvent->getRequestString(),
158 103
            $executorArgumentsEvent->getRootValue(),
159 103
            $executorArgumentsEvent->getContextValue(),
0 ignored issues
show
Bug introduced by
$executorArgumentsEvent->getContextValue() of type ArrayObject is incompatible with the type array|null expected by parameter $contextValue of Overblog\GraphQLBundle\E...torInterface::execute(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
            /** @scrutinizer ignore-type */ $executorArgumentsEvent->getContextValue(),
Loading history...
160 103
            $executorArgumentsEvent->getVariableValue(),
161 103
            $executorArgumentsEvent->getOperationName(),
162 103
            $this->defaultFieldResolver
163 103
        );
164
165
        $result = $this->postExecute($result);
166 103
167
        return $result;
168 102
    }
169
170
    /**
171 103
     * @param string|null $name
172
     *
173
     * @return string
174
     */
175
    private function getSchemaNameOrDefault(?string $name)
176
    {
177
        if (null === $name) {
178
            // TODO(mcg-web): Replace by array_key_first PHP 7 >= 7.3.0.
179 103
            foreach ($this->schemas as $name => $schema) {
180 103
                break;
181 103
            }
182
        }
183
184 103
        return $name;
185 103
    }
186 103
187
    private function preExecute(
188
        string $schemaName,
189
        Schema $schema,
190 103
        ?string $requestString,
191
        \ArrayObject $contextValue,
192 103
        $rootValue = null,
193 103
        ?array $variableValue = null,
194 103
        ?string $operationName = null
195 102
    ): ExecutorArgumentsEvent {
196
        $this->dispatcher->dispatch(
197
            new ExecutorContextEvent($contextValue),
198
            Events::EXECUTOR_CONTEXT
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with Overblog\GraphQLBundle\E...vents::EXECUTOR_CONTEXT. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
        $this->dispatcher->/** @scrutinizer ignore-call */ 
199
                           dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
199
        );
200
201
        return $this->dispatcher->dispatch(
202
            ExecutorArgumentsEvent::create($schemaName, $schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName),
203
            Events::PRE_EXECUTOR
204
        );
205
    }
206
207
    private function postExecute(ExecutionResult $result): ExecutionResult
208
    {
209
        return $this->dispatcher->dispatch(
210
            new ExecutorResultEvent($result),
211
            Events::POST_EXECUTOR
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with Overblog\GraphQLBundle\Event\Events::POST_EXECUTOR. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
        return $this->dispatcher->/** @scrutinizer ignore-call */ dispatch(

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
212
        )->getResult();
213
    }
214
}
215