Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
42 | class Processor |
||
43 | { |
||
44 | |||
45 | const TYPE_NAME_QUERY = '__typename'; |
||
46 | |||
47 | /** @var ExecutionContext */ |
||
48 | protected $executionContext; |
||
49 | |||
50 | /** @var ResolveValidatorInterface */ |
||
51 | protected $resolveValidator; |
||
52 | |||
53 | /** @var array */ |
||
54 | protected $data; |
||
55 | |||
56 | /** @var int */ |
||
57 | protected $maxComplexity; |
||
58 | |||
59 | /** @var array DeferredResult[] */ |
||
60 | protected $deferredResults = []; |
||
61 | |||
62 | 66 | public function __construct(AbstractSchema $schema) |
|
63 | { |
||
64 | 66 | if (empty($this->executionContext)) { |
|
65 | 66 | $this->executionContext = new ExecutionContext($schema); |
|
66 | 66 | $this->executionContext->setContainer(new Container()); |
|
67 | 66 | } |
|
68 | |||
69 | 66 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
|
|||
70 | 66 | } |
|
71 | |||
72 | 64 | public function processPayload($payload, $variables = [], $reducers = []) |
|
73 | { |
||
74 | 64 | $this->data = []; |
|
75 | |||
76 | try { |
||
77 | 64 | $this->parseAndCreateRequest($payload, $variables); |
|
78 | |||
79 | 63 | if ($this->maxComplexity) { |
|
80 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
81 | 1 | } |
|
82 | |||
83 | 63 | if ($reducers) { |
|
84 | 2 | $reducer = new Reducer(); |
|
85 | 2 | $reducer->reduceQuery($this->executionContext, $reducers); |
|
86 | 2 | } |
|
87 | |||
88 | 63 | foreach ($this->executionContext->getRequest()->getAllOperations() as $query) { |
|
89 | 63 | if ($operationResult = $this->resolveQuery($query)) { |
|
90 | 63 | $this->data = array_merge($this->data, $operationResult); |
|
91 | 63 | }; |
|
92 | 63 | } |
|
93 | |||
94 | // If the processor found any deferred results, resolve them now. |
||
95 | 63 | if (!empty($this->data) && $this->deferredResults) { |
|
96 | try { |
||
97 | 4 | while ($deferredResolver = array_shift($this->deferredResults)) { |
|
98 | 4 | $deferredResolver->resolve(); |
|
99 | 4 | } |
|
100 | 4 | } catch (\Exception $e) { |
|
101 | $this->executionContext->addError($e); |
||
102 | 4 | } finally { |
|
103 | 4 | $this->data = static::unpackDeferredResults($this->data); |
|
104 | } |
||
105 | 4 | } |
|
106 | |||
107 | 64 | } catch (\Exception $e) { |
|
108 | 5 | $this->executionContext->addError($e); |
|
109 | } |
||
110 | |||
111 | 64 | return $this; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * Unpack results stored inside deferred resolvers. |
||
116 | * |
||
117 | * @param mixed $result |
||
118 | * The result ree. |
||
119 | * |
||
120 | * @return mixed |
||
121 | * The unpacked result. |
||
122 | */ |
||
123 | 4 | public static function unpackDeferredResults($result) |
|
124 | { |
||
125 | 4 | while ($result instanceof DeferredResult) { |
|
126 | 4 | $result = $result->result; |
|
127 | 4 | } |
|
128 | |||
129 | 4 | if (is_array($result)) { |
|
130 | 4 | foreach ($result as $key => $value) { |
|
131 | 4 | $result[$key] = static::unpackDeferredResults($value); |
|
132 | 4 | } |
|
133 | 4 | } |
|
134 | |||
135 | 4 | return $result; |
|
136 | } |
||
137 | |||
138 | 63 | protected function resolveQuery(AstQuery $query) |
|
156 | |||
157 | 63 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false) |
|
216 | |||
217 | 63 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
|
227 | |||
228 | 38 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request) |
|
229 | { |
||
230 | 38 | switch ($argumentType->getKind()) { |
|
231 | 38 | case TypeMap::KIND_LIST: |
|
232 | /** @var $argumentType AbstractListType */ |
||
233 | 12 | $result = []; |
|
234 | 12 | if ($argumentValue instanceof AstInputList || is_array($argumentValue)) { |
|
235 | 9 | $list = is_array($argumentValue) ? $argumentValue : $argumentValue->getValue(); |
|
236 | 9 | foreach ($list as $item) { |
|
237 | 9 | $result[] = $this->prepareArgumentValue($item, $argumentType->getItemType()->getNullableType(), $request); |
|
238 | 9 | } |
|
239 | 9 | } else { |
|
240 | 3 | if ($argumentValue instanceof VariableReference) { |
|
241 | 3 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
|
242 | } |
||
243 | } |
||
244 | |||
245 | 9 | return $result; |
|
246 | |||
247 | 37 | case TypeMap::KIND_INPUT_OBJECT: |
|
248 | /** @var $argumentType AbstractInputObjectType */ |
||
249 | 6 | $result = []; |
|
250 | 6 | if ($argumentValue instanceof AstInputObject) { |
|
251 | 5 | foreach ($argumentType->getFields() as $field) { |
|
252 | /** @var $field Field */ |
||
253 | 5 | if ($field->getConfig()->has('defaultValue')) { |
|
254 | 1 | $result[$field->getName()] = $field->getType()->getNullableType()->parseInputValue($field->getConfig()->get('defaultValue')); |
|
255 | 1 | } |
|
256 | 5 | } |
|
257 | 5 | foreach ($argumentValue->getValue() as $key => $item) { |
|
258 | 5 | if ($argumentType->hasField($key)) { |
|
259 | 5 | $result[$key] = $this->prepareArgumentValue($item, $argumentType->getField($key)->getType()->getNullableType(), $request); |
|
260 | 5 | } else { |
|
261 | $result[$key] = $item; |
||
262 | } |
||
263 | 5 | } |
|
264 | 5 | } else { |
|
265 | 2 | if ($argumentValue instanceof VariableReference) { |
|
266 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
||
267 | } else { |
||
268 | 2 | if (is_array($argumentValue)) { |
|
269 | 1 | return $argumentValue; |
|
270 | } |
||
271 | } |
||
272 | } |
||
273 | |||
274 | 6 | return $result; |
|
275 | |||
276 | 36 | case TypeMap::KIND_SCALAR: |
|
277 | 36 | case TypeMap::KIND_ENUM: |
|
278 | /** @var $argumentValue AstLiteral|VariableReference */ |
||
279 | 36 | if ($argumentValue instanceof VariableReference) { |
|
280 | 7 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
|
281 | } else { |
||
282 | 31 | if ($argumentValue instanceof AstLiteral) { |
|
283 | 19 | return $argumentValue->getValue(); |
|
284 | } else { |
||
285 | 13 | return $argumentValue; |
|
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | throw new ResolveException('Argument type not supported'); |
||
291 | } |
||
292 | |||
293 | 9 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request) |
|
294 | { |
||
295 | 9 | $variable = $variableReference->getVariable(); |
|
296 | 9 | if ($argumentType->getKind() === TypeMap::KIND_LIST) { |
|
297 | if ( |
||
298 | 3 | (!$variable->isArray() && !is_array($variable->getValue())) || |
|
299 | 3 | ($variable->getTypeName() !== $argumentType->getNamedType()->getNullableType()->getName()) || |
|
300 | 3 | ($argumentType->getNamedType()->getKind() === TypeMap::KIND_NON_NULL && $variable->isArrayElementNullable()) |
|
301 | 3 | ) { |
|
302 | 1 | throw new ResolveException(sprintf('Invalid variable "%s" type, allowed type is "%s"', $variable->getName(), $argumentType->getNamedType()->getNullableType()->getName()), $variable->getLocation()); |
|
303 | } |
||
304 | 3 | } else { |
|
305 | 7 | if ($variable->getTypeName() !== $argumentType->getName()) { |
|
306 | 1 | throw new ResolveException(sprintf('Invalid variable "%s" type, allowed type is "%s"', $variable->getName(), $argumentType->getName()), $variable->getLocation()); |
|
307 | } |
||
308 | } |
||
309 | |||
310 | 8 | $requestValue = $request->getVariable($variable->getName()); |
|
311 | 8 | if ((null === $requestValue && $variable->isNullable()) && !$request->hasVariable($variable->getName())) { |
|
312 | throw new ResolveException(sprintf('Variable "%s" does not exist in request', $variable->getName()), $variable->getLocation()); |
||
313 | } |
||
314 | |||
315 | 8 | return $requestValue; |
|
316 | } |
||
317 | |||
318 | |||
319 | /** |
||
320 | * @param FieldInterface $field |
||
321 | * @param AbstractObjectType $type |
||
322 | * @param AstFieldInterface $ast |
||
323 | * @param $resolvedValue |
||
324 | * @return array |
||
325 | */ |
||
326 | 35 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue) |
|
327 | { |
||
328 | 35 | $result = []; |
|
329 | |||
330 | 35 | foreach ($ast->getFields() as $astField) { |
|
331 | 35 | switch (true) { |
|
332 | 35 | case $astField instanceof TypedFragmentReference: |
|
333 | 2 | $astName = $astField->getTypeName(); |
|
334 | 2 | $typeName = $type->getName(); |
|
335 | |||
336 | 2 | View Code Duplication | if ($typeName !== $astName) { |
337 | 2 | foreach ($type->getInterfaces() as $interface) { |
|
338 | 1 | if ($interface->getName() === $astName) { |
|
339 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
||
340 | |||
341 | break; |
||
342 | } |
||
343 | 2 | } |
|
344 | |||
345 | 2 | continue; |
|
346 | } |
||
347 | |||
348 | 2 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
|
349 | |||
350 | 2 | break; |
|
351 | |||
352 | 35 | case $astField instanceof FragmentReference: |
|
353 | 4 | $astFragment = $this->executionContext->getRequest()->getFragment($astField->getName()); |
|
354 | 4 | $astFragmentModel = $astFragment->getModel(); |
|
355 | 4 | $typeName = $type->getName(); |
|
356 | |||
357 | 4 | View Code Duplication | if ($typeName !== $astFragmentModel) { |
358 | 1 | foreach ($type->getInterfaces() as $interface) { |
|
359 | 1 | if ($interface->getName() === $astFragmentModel) { |
|
360 | 1 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
361 | |||
362 | 1 | break; |
|
363 | } |
||
364 | |||
365 | 1 | } |
|
366 | |||
367 | 1 | continue; |
|
368 | } |
||
369 | |||
370 | 4 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
371 | |||
372 | 4 | break; |
|
373 | |||
374 | 35 | default: |
|
375 | 35 | $result[$this->getAlias($astField)] = $this->resolveField($field, $astField, $resolvedValue, true); |
|
376 | 35 | } |
|
377 | 35 | } |
|
378 | |||
379 | 35 | return $result; |
|
380 | } |
||
381 | |||
382 | /** |
||
383 | * Apply post-process callbacks to all deferred resolvers. |
||
384 | */ |
||
385 | 57 | protected function deferredResolve($resolvedValue, callable $callback) { |
|
386 | 57 | if ($resolvedValue instanceof DeferredResolverInterface) { |
|
387 | 4 | $deferredResult = new DeferredResult($resolvedValue, $callback); |
|
388 | // Whenever we stumble upon a deferred resolver, append it to the |
||
389 | // queue to be resolved later. |
||
390 | 4 | $this->deferredResults[] = $deferredResult; |
|
391 | 4 | return $deferredResult; |
|
392 | } |
||
393 | // For simple values, invoke the callback immediately. |
||
394 | 57 | return $callback($resolvedValue); |
|
395 | } |
||
396 | |||
397 | 51 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
398 | { |
||
399 | 51 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
400 | return $this->deferredResolve($resolvedValue, function($resolvedValue) use ($field, $ast, $parentValue) { |
||
401 | 51 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
402 | |||
403 | /** @var AbstractScalarType $type */ |
||
404 | 50 | $type = $field->getType()->getNullableType(); |
|
405 | |||
406 | 50 | return $type->serialize($resolvedValue); |
|
407 | 51 | }); |
|
408 | } |
||
409 | |||
410 | 23 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
411 | { |
||
412 | /** @var AstQuery $ast */ |
||
413 | 23 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
414 | |||
415 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
416 | 23 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
417 | |||
418 | 21 | if (null === $resolvedValue) { |
|
419 | 5 | return null; |
|
420 | } |
||
421 | |||
422 | /** @var AbstractListType $type */ |
||
423 | 20 | $type = $field->getType()->getNullableType(); |
|
424 | 20 | $itemType = $type->getNamedType(); |
|
425 | |||
426 | 20 | $fakeAst = clone $ast; |
|
427 | 20 | if ($fakeAst instanceof AstQuery) { |
|
428 | 19 | $fakeAst->setArguments([]); |
|
429 | 19 | } |
|
430 | |||
431 | 20 | $fakeField = new Field([ |
|
432 | 20 | 'name' => $field->getName(), |
|
433 | 20 | 'type' => $itemType, |
|
434 | 20 | 'args' => $field->getArguments(), |
|
435 | 20 | ]); |
|
436 | |||
437 | 20 | $result = []; |
|
438 | 20 | foreach ($resolvedValue as $resolvedValueItem) { |
|
439 | try { |
||
440 | $fakeField->getConfig()->set('resolve', function () use ($resolvedValueItem) { |
||
441 | 19 | return $resolvedValueItem; |
|
442 | 19 | }); |
|
443 | |||
444 | 19 | switch ($itemType->getNullableType()->getKind()) { |
|
445 | 19 | case TypeMap::KIND_ENUM: |
|
446 | 19 | case TypeMap::KIND_SCALAR: |
|
447 | 5 | $value = $this->resolveScalar($fakeField, $fakeAst, $resolvedValueItem); |
|
448 | |||
449 | 4 | break; |
|
450 | |||
451 | |||
452 | 15 | case TypeMap::KIND_OBJECT: |
|
453 | 13 | $value = $this->resolveObject($fakeField, $fakeAst, $resolvedValueItem); |
|
454 | |||
455 | 13 | break; |
|
456 | |||
457 | 3 | case TypeMap::KIND_UNION: |
|
458 | 3 | case TypeMap::KIND_INTERFACE: |
|
459 | 3 | $value = $this->resolveComposite($fakeField, $fakeAst, $resolvedValueItem); |
|
460 | |||
461 | 3 | break; |
|
462 | |||
463 | default: |
||
464 | $value = null; |
||
465 | 18 | } |
|
466 | 19 | } catch (\Exception $e) { |
|
467 | 1 | $this->executionContext->addError($e); |
|
468 | |||
469 | 1 | $value = null; |
|
470 | } |
||
471 | |||
472 | 19 | $result[] = $value; |
|
473 | 20 | } |
|
474 | |||
475 | 20 | return $result; |
|
476 | 23 | }); |
|
477 | } |
||
478 | |||
479 | 36 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false) |
|
480 | { |
||
481 | 36 | $resolvedValue = $parentValue; |
|
482 | 36 | if (!$fromUnion) { |
|
483 | 31 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
484 | 31 | } |
|
485 | |||
486 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
487 | 36 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
488 | |||
489 | 36 | if (null === $resolvedValue) { |
|
490 | 5 | return null; |
|
491 | } |
||
492 | /** @var AbstractObjectType $type */ |
||
493 | 35 | $type = $field->getType()->getNullableType(); |
|
494 | |||
495 | try { |
||
496 | 35 | return $this->collectResult($field, $type, $ast, $resolvedValue); |
|
497 | 4 | } catch (\Exception $e) { |
|
498 | 4 | return null; |
|
499 | } |
||
500 | 36 | }); |
|
501 | } |
||
502 | |||
503 | 7 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
504 | { |
||
505 | /** @var AstQuery $ast */ |
||
506 | 7 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
507 | 7 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
|
508 | 7 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
509 | |||
510 | 7 | if (null === $resolvedValue) { |
|
511 | return null; |
||
512 | } |
||
513 | |||
514 | /** @var AbstractUnionType $type */ |
||
515 | 7 | $type = $field->getType()->getNullableType(); |
|
516 | 7 | $resolvedType = $type->resolveType($resolvedValue); |
|
517 | |||
518 | 7 | if (!$resolvedType) { |
|
519 | throw new ResolveException('Resolving function must return type'); |
||
520 | } |
||
521 | |||
522 | 7 | if ($type instanceof AbstractInterfaceType) { |
|
523 | 6 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $type); |
|
524 | 6 | } else { |
|
525 | 1 | $this->resolveValidator->assertTypeInUnionTypes($resolvedType, $type); |
|
526 | } |
||
527 | |||
528 | 7 | $fakeField = new Field([ |
|
529 | 7 | 'name' => $field->getName(), |
|
530 | 7 | 'type' => $resolvedType, |
|
531 | 7 | 'args' => $field->getArguments(), |
|
532 | 7 | ]); |
|
533 | |||
534 | 7 | return $this->resolveObject($fakeField, $ast, $resolvedValue, true); |
|
535 | 7 | }); |
|
536 | } |
||
537 | |||
538 | 64 | protected function parseAndCreateRequest($payload, $variables = []) |
|
551 | |||
552 | 57 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null) |
|
560 | |||
561 | 57 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) |
|
586 | |||
587 | 63 | private function getAlias(AstFieldInterface $ast) |
|
591 | |||
592 | 57 | protected function createResolveInfo(FieldInterface $field, array $astFields) |
|
596 | |||
597 | |||
598 | /** |
||
599 | * You can access ExecutionContext to check errors and inject dependencies |
||
600 | * |
||
601 | * @return ExecutionContext |
||
602 | */ |
||
603 | 11 | public function getExecutionContext() |
|
607 | |||
608 | 64 | public function getResponseData() |
|
622 | |||
623 | /** |
||
624 | * @return int |
||
625 | */ |
||
626 | public function getMaxComplexity() |
||
630 | |||
631 | /** |
||
632 | * @param int $maxComplexity |
||
633 | */ |
||
634 | 1 | public function setMaxComplexity($maxComplexity) |
|
638 | |||
639 | } |
||
640 |
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.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.