We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Promise; |
||
9 | use GraphQL\Executor\Promise\PromiseAdapter; |
||
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 Overblog\GraphQLBundle\Executor\Promise\PromiseAdapterInterface; |
||
21 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
22 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
23 | |||
24 | class Executor |
||
25 | { |
||
26 | public const PROMISE_ADAPTER_SERVICE_ID = 'overblog_graphql.promise_adapter'; |
||
27 | |||
28 | /** @var Schema[] */ |
||
29 | private $schemas = []; |
||
30 | |||
31 | /** @var EventDispatcherInterface|null */ |
||
32 | private $dispatcher; |
||
33 | |||
34 | /** @var ExecutorInterface */ |
||
35 | private $executor; |
||
36 | |||
37 | /** @var PromiseAdapter */ |
||
38 | private $promiseAdapter; |
||
39 | |||
40 | /** @var callable|null */ |
||
41 | private $defaultFieldResolver; |
||
42 | |||
43 | 96 | public function __construct( |
|
44 | ExecutorInterface $executor, |
||
45 | EventDispatcherInterface $dispatcher, |
||
46 | PromiseAdapter $promiseAdapter, |
||
47 | ?callable $defaultFieldResolver = null |
||
48 | ) { |
||
49 | 96 | $this->executor = $executor; |
|
50 | 96 | $this->dispatcher = $dispatcher; |
|
51 | 96 | $this->promiseAdapter = $promiseAdapter; |
|
52 | 96 | $this->defaultFieldResolver = $defaultFieldResolver; |
|
53 | 96 | } |
|
54 | |||
55 | 2 | public function setExecutor(ExecutorInterface $executor): self |
|
56 | { |
||
57 | 2 | $this->executor = $executor; |
|
58 | |||
59 | 2 | return $this; |
|
60 | } |
||
61 | |||
62 | 1 | public function setPromiseAdapter(PromiseAdapter $promiseAdapter): self |
|
63 | { |
||
64 | 1 | $this->promiseAdapter = $promiseAdapter; |
|
65 | |||
66 | 1 | return $this; |
|
67 | } |
||
68 | |||
69 | /** |
||
70 | * @param string $name |
||
71 | * @param Schema $schema |
||
72 | * |
||
73 | * @return self |
||
74 | */ |
||
75 | 88 | public function addSchema(string $name, Schema $schema): self |
|
76 | { |
||
77 | 88 | $this->schemas[$name] = $schema; |
|
78 | |||
79 | 88 | return $this; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param string|null $name |
||
84 | * |
||
85 | * @return Schema |
||
86 | */ |
||
87 | 84 | public function getSchema(?string $name = null): Schema |
|
88 | { |
||
89 | 84 | if (empty($this->schemas)) { |
|
90 | 1 | throw new \RuntimeException('At least one schema should be declare.'); |
|
91 | } |
||
92 | |||
93 | 83 | if (null === $name) { |
|
94 | 80 | $schema = \array_values($this->schemas)[0]; |
|
95 | } else { |
||
96 | 3 | if (!isset($this->schemas[$name])) { |
|
97 | 1 | throw new NotFoundHttpException(\sprintf('Could not found "%s" schema.', $name)); |
|
98 | } |
||
99 | 2 | $schema = $this->schemas[$name]; |
|
100 | } |
||
101 | |||
102 | 82 | return $schema; |
|
103 | } |
||
104 | |||
105 | 92 | public function setMaxQueryDepth($maxQueryDepth): void |
|
106 | { |
||
107 | /** @var QueryDepth $queryDepth */ |
||
108 | 92 | $queryDepth = DocumentValidator::getRule('QueryDepth'); |
|
109 | 92 | $queryDepth->setMaxQueryDepth($maxQueryDepth); |
|
110 | 92 | } |
|
111 | |||
112 | 92 | public function setMaxQueryComplexity($maxQueryComplexity): void |
|
113 | { |
||
114 | /** @var QueryComplexity $queryComplexity */ |
||
115 | 92 | $queryComplexity = DocumentValidator::getRule('QueryComplexity'); |
|
116 | 92 | $queryComplexity->setMaxQueryComplexity($maxQueryComplexity); |
|
117 | 92 | } |
|
118 | |||
119 | 1 | public function disableIntrospectionQuery(): void |
|
120 | { |
||
121 | 1 | DocumentValidator::addRule(new DisableIntrospection()); |
|
122 | 1 | } |
|
123 | |||
124 | /** |
||
125 | * @param null|string $schemaName |
||
126 | * @param array $request |
||
127 | * @param null|array|\ArrayObject|object $rootValue |
||
128 | * |
||
129 | * @return ExecutionResult |
||
130 | */ |
||
131 | 82 | public function execute(?string $schemaName = null, array $request, $rootValue = null): ExecutionResult |
|
132 | { |
||
133 | 82 | $executorArgumentsEvent = $this->preExecute( |
|
134 | 82 | $this->getSchema($schemaName), |
|
135 | 81 | $request[ParserInterface::PARAM_QUERY] ?? null, |
|
136 | 81 | new \ArrayObject(), |
|
137 | 81 | $rootValue, |
|
138 | 81 | $request[ParserInterface::PARAM_VARIABLES], |
|
139 | 81 | $request[ParserInterface::PARAM_OPERATION_NAME] ?? null |
|
140 | ); |
||
141 | |||
142 | 80 | $executorArgumentsEvent->getSchema()->processExtensions(); |
|
143 | |||
144 | 80 | $result = $this->executor->execute( |
|
145 | 80 | $executorArgumentsEvent->getSchema(), |
|
146 | 80 | $executorArgumentsEvent->getRequestString(), |
|
147 | 80 | $executorArgumentsEvent->getRootValue(), |
|
148 | 80 | $executorArgumentsEvent->getContextValue(), |
|
149 | 80 | $executorArgumentsEvent->getVariableValue(), |
|
150 | 80 | $executorArgumentsEvent->getOperationName() |
|
151 | ); |
||
152 | |||
153 | 80 | $result = $this->postExecute($result); |
|
154 | |||
155 | 76 | return $result; |
|
156 | } |
||
157 | |||
158 | 81 | private function preExecute( |
|
159 | Schema $schema, |
||
160 | ?string $requestString, |
||
161 | \ArrayObject $contextValue, |
||
162 | $rootValue = null, |
||
163 | ?array $variableValue = null, |
||
164 | ?string $operationName = null |
||
165 | ): ExecutorArgumentsEvent { |
||
166 | 81 | $this->checkPromiseAdapter(); |
|
167 | |||
168 | 80 | $this->executor->setPromiseAdapter($this->promiseAdapter); |
|
169 | // this is needed when not using only generated types |
||
170 | 80 | if ($this->defaultFieldResolver) { |
|
171 | 78 | $this->executor->setDefaultFieldResolver($this->defaultFieldResolver); |
|
172 | } |
||
173 | 80 | $this->dispatcher->dispatch(Events::EXECUTOR_CONTEXT, new ExecutorContextEvent($contextValue)); |
|
174 | |||
175 | 80 | return $this->dispatcher->dispatch( |
|
176 | 80 | Events::PRE_EXECUTOR, |
|
177 | 80 | ExecutorArgumentsEvent::create($schema, $requestString, $contextValue, $rootValue, $variableValue, $operationName) |
|
178 | ); |
||
179 | } |
||
180 | |||
181 | /** |
||
182 | * @param ExecutionResult|Promise $result |
||
183 | * |
||
184 | * @return ExecutionResult |
||
185 | */ |
||
186 | 80 | private function postExecute($result): ExecutionResult |
|
187 | { |
||
188 | 80 | if ($result instanceof Promise) { |
|
0 ignored issues
–
show
|
|||
189 | 78 | $result = $this->promiseAdapter->wait($result); |
|
190 | } |
||
191 | |||
192 | 80 | $this->checkExecutionResult($result); |
|
193 | |||
194 | 78 | return $this->dispatcher->dispatch(Events::POST_EXECUTOR, new ExecutorResultEvent($result))->getResult(); |
|
195 | } |
||
196 | |||
197 | 81 | private function checkPromiseAdapter(): void |
|
198 | { |
||
199 | 81 | if ($this->promiseAdapter && !$this->promiseAdapter instanceof PromiseAdapterInterface && !\is_callable([$this->promiseAdapter, 'wait'])) { |
|
200 | 1 | throw new \RuntimeException( |
|
201 | 1 | \sprintf( |
|
202 | 1 | 'PromiseAdapter should be an object instantiating "%s" or "%s" with a "wait" method.', |
|
203 | 1 | PromiseAdapterInterface::class, |
|
204 | 1 | PromiseAdapter::class |
|
205 | ) |
||
206 | ); |
||
207 | } |
||
208 | 80 | } |
|
209 | |||
210 | 80 | private function checkExecutionResult($result): void |
|
211 | { |
||
212 | 80 | if (!\is_object($result) || !$result instanceof ExecutionResult) { |
|
0 ignored issues
–
show
The class
GraphQL\Executor\ExecutionResult does not exist. Did you forget a USE statement, or did you not list all dependencies?
This error could be the result of: 1. Missing dependenciesPHP Analyzer uses your Are you sure this class is defined by one of your dependencies, or did you maybe
not list a dependency in either the 2. Missing use statementPHP does not complain about undefined classes in if ($x instanceof DoesNotExist) {
// Do something.
}
If you have not tested against this specific condition, such errors might go unnoticed.
Loading history...
|
|||
213 | 2 | throw new \RuntimeException( |
|
214 | 2 | \sprintf('Execution result should be an object instantiating "%s".', ExecutionResult::class) |
|
215 | ); |
||
216 | } |
||
217 | 78 | } |
|
218 | } |
||
219 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.