Complex classes like Processor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Processor, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
37 | class Processor |
||
38 | { |
||
39 | |||
40 | const TYPE_NAME_QUERY = '__typename'; |
||
41 | |||
42 | /** @var array */ |
||
43 | protected $data; |
||
44 | |||
45 | /** @var ResolveValidatorInterface */ |
||
46 | protected $resolveValidator; |
||
47 | |||
48 | /** @var ExecutionContext */ |
||
49 | protected $executionContext; |
||
50 | |||
51 | 23 | public function __construct(AbstractSchema $schema) |
|
61 | |||
62 | |||
63 | 22 | public function processPayload($payload, $variables = []) |
|
64 | { |
||
65 | 22 | if ($this->executionContext->hasErrors()) { |
|
66 | 4 | $this->executionContext->clearErrors(); |
|
67 | } |
||
68 | |||
69 | 22 | $this->data = []; |
|
70 | |||
71 | try { |
||
72 | 22 | $this->parseAndCreateRequest($payload, $variables); |
|
73 | |||
74 | 22 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
75 | 22 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
76 | 22 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
77 | 22 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
78 | 22 | $this->data = array_merge($this->data, $operationResult); |
|
79 | }; |
||
80 | } |
||
81 | |||
82 | 3 | } catch (\Exception $e) { |
|
83 | 3 | $this->executionContext->addError($e); |
|
84 | } |
||
85 | |||
86 | 22 | return $this; |
|
87 | } |
||
88 | |||
89 | 22 | protected function parseAndCreateRequest($payload, $variables = []) |
|
99 | |||
100 | /** |
||
101 | * @param Query|Field $query |
||
102 | * @param AbstractObjectType $currentLevelSchema |
||
103 | * @return array|bool|mixed |
||
104 | */ |
||
105 | 22 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
121 | |||
122 | /** |
||
123 | * @param Query $query |
||
124 | * @param AbstractField $field |
||
125 | * @param $contextValue |
||
126 | * @return array|mixed|null |
||
127 | */ |
||
128 | 20 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
142 | |||
143 | /** |
||
144 | * @param Query|Mutation $query |
||
145 | * @param AbstractType $fieldType |
||
146 | * @param mixed $resolvedValue |
||
147 | * @return array|mixed |
||
148 | */ |
||
149 | 20 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
150 | { |
||
151 | 20 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
152 | 20 | if (is_null($resolvedValue)) return null; |
|
153 | |||
154 | 18 | $value = []; |
|
155 | |||
156 | 18 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
157 | 9 | if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
|
158 | 9 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|||
159 | 8 | $value[] = []; |
|
160 | 8 | $index = count($value) - 1; |
|
161 | |||
162 | |||
163 | 8 | $namedType = $fieldType->getNamedType(); |
|
164 | 8 | $namedType = $this->resolveValidator->resolveTypeIfAbstract($namedType, $resolvedValueItem); |
|
165 | 8 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
166 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
167 | 1 | $value[$index] = null; |
|
168 | 1 | continue; |
|
169 | } |
||
170 | |||
171 | 9 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
172 | } |
||
173 | } else { |
||
174 | 18 | if (!$query->hasFields()) { |
|
175 | 2 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
176 | } |
||
177 | |||
178 | 18 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
179 | } |
||
180 | |||
181 | 18 | return $value; |
|
182 | } |
||
183 | |||
184 | /** |
||
185 | * @param FieldAst $fieldAst |
||
186 | * @param AbstractField $field |
||
187 | * |
||
188 | * @param mixed $contextValue |
||
189 | * @return array|mixed|null |
||
190 | * @throws ResolveException |
||
191 | * @throws \Exception |
||
192 | */ |
||
193 | 17 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
194 | { |
||
195 | 17 | $value = null; |
|
196 | 17 | $fieldType = $field->getType(); |
|
197 | 17 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
198 | |||
199 | 17 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
200 | 1 | $listValue = []; |
|
201 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
202 | 1 | $type = $fieldType->getNamedType(); |
|
203 | |||
204 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
205 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $field->getName()))); |
||
206 | |||
207 | $listValue = null; |
||
208 | break; |
||
209 | } |
||
210 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
211 | } |
||
212 | |||
213 | 1 | $value = $listValue; |
|
214 | } else { |
||
215 | 17 | $value = $this->getFieldValidatedValue($field, $preResolvedValue); |
|
216 | } |
||
217 | |||
218 | 17 | return $value; |
|
219 | } |
||
220 | |||
221 | /** |
||
222 | * @param AbstractField $field |
||
223 | * @param mixed $contextValue |
||
224 | * @param Query $query |
||
225 | * |
||
226 | * @return mixed |
||
227 | */ |
||
228 | 20 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
240 | |||
241 | /** |
||
242 | * @param $contextValue |
||
243 | * @param FieldAst $fieldAst |
||
244 | * @param AbstractField $field |
||
245 | * |
||
246 | * @throws \Exception |
||
247 | * |
||
248 | * @return mixed |
||
249 | */ |
||
250 | 17 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
251 | { |
||
252 | 17 | $resolved = false; |
|
253 | 17 | $resolverValue = null; |
|
254 | |||
255 | 17 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
256 | 13 | $resolverValue = $contextValue[$fieldAst->getName()]; |
|
257 | 13 | $resolved = true; |
|
258 | 8 | } elseif (is_object($contextValue)) { |
|
259 | 6 | $resolverValue = TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
260 | 6 | $resolved = true; |
|
261 | } |
||
262 | |||
263 | 17 | if (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
264 | 2 | $resolved = true; |
|
265 | } |
||
266 | |||
267 | 17 | if ($resolveFunction = $field->getConfig()->getResolveFunction()) { |
|
268 | 2 | $resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext); |
|
269 | |||
270 | 2 | if (!$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
|
271 | throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
||
272 | |||
273 | } else { |
||
274 | 2 | $resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo); |
|
275 | } |
||
276 | |||
277 | } |
||
278 | |||
279 | 17 | if (!$resolverValue && !$resolved) { |
|
280 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
281 | } |
||
282 | |||
283 | 17 | return $resolverValue; |
|
284 | } |
||
285 | |||
286 | /** |
||
287 | * @param $field AbstractField |
||
288 | * @param $query Query |
||
289 | * |
||
290 | * @return array |
||
291 | */ |
||
292 | 20 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
293 | { |
||
294 | 20 | $args = []; |
|
295 | 20 | foreach ($query->getArguments() as $argument) { |
|
296 | 8 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
297 | 8 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
298 | } |
||
299 | } |
||
300 | |||
301 | 20 | return $args; |
|
302 | } |
||
303 | |||
304 | /** |
||
305 | * @param $query Query|FragmentInterface |
||
306 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
307 | * @param $resolvedValue mixed |
||
308 | * @param $value array |
||
309 | * |
||
310 | * @throws \Exception |
||
311 | * |
||
312 | * @return array |
||
313 | */ |
||
314 | 18 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
315 | { |
||
316 | 18 | if ($queryType instanceof AbstractScalarType && !$query->hasFields()) { |
|
317 | 1 | return $this->getOutputValue($queryType, $resolvedValue); |
|
318 | } |
||
319 | |||
320 | 18 | foreach ($query->getFields() as $fieldAst) { |
|
321 | 18 | $fieldResolvedValue = null; |
|
322 | |||
323 | 18 | if ($fieldAst instanceof FragmentInterface) { |
|
324 | /** @var TypedFragmentReference $fragment */ |
||
325 | 3 | $fragment = $fieldAst; |
|
326 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
327 | /** @var Fragment $fragment */ |
||
328 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAst->getName()); |
|
329 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
330 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
331 | 1 | continue; |
|
332 | } |
||
333 | |||
334 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
335 | 3 | $fieldResolvedValue = is_array($fragmentValue) ? $fragmentValue : []; |
|
336 | } else { |
||
337 | 18 | $alias = $fieldAst->getAlias() ?: $fieldAst->getName(); |
|
338 | 18 | $currentType = $queryType->getNullableType(); |
|
339 | |||
340 | 18 | if ($fieldAst->getName() == self::TYPE_NAME_QUERY) { |
|
341 | 1 | $fieldResolvedValue = [$alias => $queryType->getName()]; |
|
342 | } else { |
||
343 | 18 | if (!$this->resolveValidator->objectHasField($currentType, $fieldAst)) { |
|
344 | 2 | $fieldResolvedValue = null; |
|
345 | } else { |
||
346 | 18 | if ($fieldAst instanceof Query) { |
|
347 | 10 | $queryAst = $currentType->getField($fieldAst->getName()); |
|
348 | 10 | $fieldValue = $queryAst ? $this->processQueryAST($fieldAst, $queryAst, $resolvedValue) : null; |
|
349 | 10 | $fieldResolvedValue = [$alias => $fieldValue]; |
|
350 | } elseif ($fieldAst instanceof FieldAst) { |
||
351 | $fieldResolvedValue = [ |
||
352 | 17 | $alias => $this->processFieldAST($fieldAst, $currentType->getField($fieldAst->getName()), $resolvedValue) |
|
353 | ]; |
||
354 | } |
||
355 | } |
||
356 | |||
357 | |||
358 | } |
||
359 | } |
||
360 | |||
361 | 18 | $value = $this->collectValue($value, $fieldResolvedValue); |
|
362 | } |
||
363 | |||
364 | 18 | return $value; |
|
365 | } |
||
366 | |||
367 | 17 | protected function getFieldValidatedValue(AbstractField $field, $value) |
|
371 | |||
372 | 17 | protected function getOutputValue(AbstractType $type, $value) |
|
376 | |||
377 | 18 | protected function collectValue($value, $queryValue) |
|
378 | { |
||
379 | 18 | if ($queryValue && is_array($queryValue)) { |
|
380 | 18 | $value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
381 | } else { |
||
382 | 2 | $value = $queryValue; |
|
383 | } |
||
384 | |||
385 | 18 | return $value; |
|
386 | } |
||
387 | |||
388 | 22 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
396 | |||
397 | 22 | public function getResponseData() |
|
398 | { |
||
399 | 22 | $result = []; |
|
400 | |||
401 | 22 | if (!empty($this->data)) { |
|
402 | 20 | $result['data'] = $this->data; |
|
403 | } |
||
411 | } |
||
412 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.