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 | 70 | public function __construct(AbstractSchema $schema) |
|
| 63 | { |
||
| 64 | 70 | if (empty($this->executionContext)) { |
|
| 65 | 70 | $this->executionContext = new ExecutionContext($schema); |
|
| 66 | 70 | $this->executionContext->setContainer(new Container()); |
|
| 67 | 70 | } |
|
| 68 | |||
| 69 | 70 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 70 | 70 | } |
|
| 71 | |||
| 72 | 68 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 73 | { |
||
| 74 | 68 | $this->data = []; |
|
| 75 | |||
| 76 | try { |
||
| 77 | 68 | $this->parseAndCreateRequest($payload, $variables); |
|
| 78 | |||
| 79 | 67 | if ($this->maxComplexity) { |
|
| 80 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 81 | 1 | } |
|
| 82 | |||
| 83 | 67 | if ($reducers) { |
|
|
|
|||
| 84 | 2 | $reducer = new Reducer(); |
|
| 85 | 2 | $reducer->reduceQuery($this->executionContext, $reducers); |
|
| 86 | 2 | } |
|
| 87 | |||
| 88 | 67 | foreach ($this->executionContext->getRequest()->getAllOperations() as $query) { |
|
| 89 | 67 | if ($operationResult = $this->resolveQuery($query)) { |
|
| 90 | 67 | $this->data = array_replace_recursive($this->data, $operationResult); |
|
| 91 | 67 | }; |
|
| 92 | 67 | } |
|
| 93 | |||
| 94 | // If the processor found any deferred results, resolve them now. |
||
| 95 | 67 | 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 | 4 | } |
|
| 105 | 4 | } |
|
| 106 | |||
| 107 | 68 | } catch (\Exception $e) { |
|
| 108 | 5 | $this->executionContext->addError($e); |
|
| 109 | } |
||
| 110 | |||
| 111 | 68 | 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 | 67 | protected function resolveQuery(AstQuery $query) |
|
| 156 | |||
| 157 | 67 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false) |
|
| 216 | |||
| 217 | 67 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
|
| 227 | |||
| 228 | 38 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request) |
|
| 229 | { |
||
| 292 | |||
| 293 | 9 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request) |
|
| 317 | |||
| 318 | |||
| 319 | /** |
||
| 320 | * @param FieldInterface $field |
||
| 321 | * @param AbstractObjectType $type |
||
| 322 | * @param AstFieldInterface $ast |
||
| 323 | * @param $resolvedValue |
||
| 324 | * @return array |
||
| 325 | */ |
||
| 326 | 39 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue) |
|
| 327 | { |
||
| 328 | 39 | $result = []; |
|
| 329 | |||
| 330 | 39 | foreach ($ast->getFields() as $astField) { |
|
| 331 | 39 | switch (true) { |
|
| 332 | 39 | case $astField instanceof TypedFragmentReference: |
|
| 333 | 3 | $astName = $astField->getTypeName(); |
|
| 334 | 3 | $typeName = $type->getName(); |
|
| 335 | |||
| 336 | 3 | View Code Duplication | if ($typeName !== $astName) { |
| 337 | 3 | foreach ($type->getInterfaces() as $interface) { |
|
| 338 | 1 | if ($interface->getName() === $astName) { |
|
| 339 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
||
| 340 | |||
| 341 | break; |
||
| 342 | } |
||
| 343 | 3 | } |
|
| 344 | |||
| 345 | 3 | continue 2; |
|
| 346 | } |
||
| 347 | |||
| 348 | 3 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
|
| 349 | |||
| 350 | 3 | break; |
|
| 351 | |||
| 352 | 39 | case $astField instanceof FragmentReference: |
|
| 353 | 7 | $astFragment = $this->executionContext->getRequest()->getFragment($astField->getName()); |
|
| 354 | 7 | $astFragmentModel = $astFragment->getModel(); |
|
| 355 | 7 | $typeName = $type->getName(); |
|
| 356 | |||
| 357 | 7 | View Code Duplication | if ($typeName !== $astFragmentModel && $type->getInterfaces()) { |
| 358 | 1 | foreach ($type->getInterfaces() as $interface) { |
|
| 359 | 1 | if ($interface->getName() === $astFragmentModel) { |
|
| 360 | 1 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 361 | |||
| 362 | 1 | break; |
|
| 363 | } |
||
| 364 | 1 | } |
|
| 365 | |||
| 366 | 1 | continue 2; |
|
| 367 | } |
||
| 368 | |||
| 369 | 7 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 370 | |||
| 371 | 7 | break; |
|
| 372 | |||
| 373 | 39 | default: |
|
| 374 | 39 | $result = array_replace_recursive($result, [$this->getAlias($astField) => $this->resolveField($field, $astField, $resolvedValue, true)]); |
|
| 375 | 39 | } |
|
| 376 | 39 | } |
|
| 377 | |||
| 378 | 39 | return $result; |
|
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Apply post-process callbacks to all deferred resolvers. |
||
| 383 | */ |
||
| 384 | 61 | protected function deferredResolve($resolvedValue, callable $callback) { |
|
| 395 | |||
| 396 | 55 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 408 | |||
| 409 | 27 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 410 | { |
||
| 411 | /** @var AstQuery $ast */ |
||
| 412 | 27 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 413 | |||
| 414 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
| 415 | 27 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 416 | |||
| 417 | 25 | if (null === $resolvedValue) { |
|
| 418 | 7 | return null; |
|
| 419 | } |
||
| 420 | |||
| 421 | /** @var AbstractListType $type */ |
||
| 422 | 24 | $type = $field->getType()->getNullableType(); |
|
| 423 | 24 | $itemType = $type->getNamedType(); |
|
| 424 | |||
| 425 | 24 | $fakeAst = clone $ast; |
|
| 426 | 24 | if ($fakeAst instanceof AstQuery) { |
|
| 427 | 23 | $fakeAst->setArguments([]); |
|
| 428 | 23 | } |
|
| 429 | |||
| 430 | 24 | $fakeField = new Field([ |
|
| 431 | 24 | 'name' => $field->getName(), |
|
| 432 | 24 | 'type' => $itemType, |
|
| 433 | 24 | 'args' => $field->getArguments(), |
|
| 434 | 24 | ]); |
|
| 435 | |||
| 436 | 24 | $result = []; |
|
| 437 | 24 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 438 | try { |
||
| 439 | $fakeField->getConfig()->set('resolve', function () use ($resolvedValueItem) { |
||
| 440 | 23 | return $resolvedValueItem; |
|
| 441 | 23 | }); |
|
| 442 | |||
| 443 | 23 | switch ($itemType->getNullableType()->getKind()) { |
|
| 444 | 23 | case TypeMap::KIND_ENUM: |
|
| 445 | 23 | case TypeMap::KIND_SCALAR: |
|
| 446 | 5 | $value = $this->resolveScalar($fakeField, $fakeAst, $resolvedValueItem); |
|
| 447 | |||
| 448 | 4 | break; |
|
| 449 | |||
| 450 | |||
| 451 | 19 | case TypeMap::KIND_OBJECT: |
|
| 452 | 16 | $value = $this->resolveObject($fakeField, $fakeAst, $resolvedValueItem); |
|
| 453 | |||
| 454 | 16 | break; |
|
| 455 | |||
| 456 | 4 | case TypeMap::KIND_UNION: |
|
| 457 | 4 | case TypeMap::KIND_INTERFACE: |
|
| 458 | 4 | $value = $this->resolveComposite($fakeField, $fakeAst, $resolvedValueItem); |
|
| 459 | |||
| 460 | 4 | break; |
|
| 461 | |||
| 462 | default: |
||
| 463 | $value = null; |
||
| 464 | 22 | } |
|
| 465 | 23 | } catch (\Exception $e) { |
|
| 466 | 1 | $this->executionContext->addError($e); |
|
| 467 | |||
| 468 | 1 | $value = null; |
|
| 469 | } |
||
| 470 | |||
| 471 | 23 | $result[] = $value; |
|
| 472 | 24 | } |
|
| 473 | |||
| 474 | 24 | return $result; |
|
| 475 | 27 | }); |
|
| 476 | } |
||
| 477 | |||
| 478 | 40 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false) |
|
| 479 | { |
||
| 480 | 40 | $resolvedValue = $parentValue; |
|
| 481 | 40 | if (!$fromUnion) { |
|
| 482 | 34 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 483 | 34 | } |
|
| 484 | |||
| 485 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
| 486 | 40 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 487 | |||
| 488 | 40 | if (null === $resolvedValue) { |
|
| 489 | 7 | return null; |
|
| 490 | } |
||
| 491 | /** @var AbstractObjectType $type */ |
||
| 492 | 39 | $type = $field->getType()->getNullableType(); |
|
| 493 | |||
| 494 | try { |
||
| 495 | 39 | return $this->collectResult($field, $type, $ast, $resolvedValue); |
|
| 496 | 4 | } catch (\Exception $e) { |
|
| 497 | 4 | return null; |
|
| 498 | } |
||
| 499 | 40 | }); |
|
| 500 | } |
||
| 501 | |||
| 502 | 8 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 503 | { |
||
| 504 | /** @var AstQuery $ast */ |
||
| 505 | 8 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 506 | 8 | return $this->deferredResolve($resolvedValue, function ($resolvedValue) use ($field, $ast, $parentValue) { |
|
| 507 | 8 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 508 | |||
| 509 | 8 | if (null === $resolvedValue) { |
|
| 510 | return null; |
||
| 511 | } |
||
| 512 | |||
| 513 | /** @var AbstractUnionType $type */ |
||
| 514 | 8 | $type = $field->getType()->getNullableType(); |
|
| 515 | 8 | $resolveInfo = new ResolveInfo( |
|
| 516 | 8 | $field, |
|
| 517 | 8 | $ast instanceof AstQuery ? $ast->getFields() : [], |
|
| 518 | 8 | $this->executionContext |
|
| 519 | 8 | ); |
|
| 520 | 8 | $resolvedType = $type->resolveType($resolvedValue, $resolveInfo); |
|
| 521 | |||
| 522 | 8 | if (!$resolvedType) { |
|
| 523 | throw new ResolveException('Resolving function must return type'); |
||
| 524 | } |
||
| 525 | |||
| 526 | 8 | if ($type instanceof AbstractInterfaceType) { |
|
| 527 | 6 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $type); |
|
| 528 | 6 | } else { |
|
| 529 | 2 | $this->resolveValidator->assertTypeInUnionTypes($resolvedType, $type); |
|
| 530 | } |
||
| 531 | |||
| 532 | 8 | $fakeField = new Field([ |
|
| 533 | 8 | 'name' => $field->getName(), |
|
| 534 | 8 | 'type' => $resolvedType, |
|
| 535 | 8 | 'args' => $field->getArguments(), |
|
| 536 | 8 | ]); |
|
| 537 | |||
| 538 | 8 | return $this->resolveObject($fakeField, $ast, $resolvedValue, true); |
|
| 539 | 8 | }); |
|
| 540 | } |
||
| 541 | |||
| 542 | 68 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 555 | |||
| 556 | 61 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null) |
|
| 564 | |||
| 565 | 61 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) |
|
| 566 | { |
||
| 567 | 61 | $values = []; |
|
| 568 | 61 | $defaults = []; |
|
| 569 | |||
| 570 | 61 | foreach ($field->getArguments() as $argument) { |
|
| 571 | /** @var $argument InputField */ |
||
| 572 | 44 | if ($argument->getConfig()->has('defaultValue')) { |
|
| 573 | 8 | $defaults[$argument->getName()] = $argument->getConfig()->getDefaultValue(); |
|
| 574 | 8 | } |
|
| 575 | 61 | } |
|
| 576 | |||
| 577 | 61 | foreach ($ast->getArguments() as $astArgument) { |
|
| 578 | 34 | $argument = $field->getArgument($astArgument->getName()); |
|
| 579 | 34 | $argumentType = $argument->getType()->getNullableType(); |
|
| 580 | |||
| 581 | 34 | $values[$argument->getName()] = $argumentType->parseValue($astArgument->getValue()); |
|
| 582 | |||
| 583 | 34 | if (isset($defaults[$argument->getName()])) { |
|
| 584 | 3 | unset($defaults[$argument->getName()]); |
|
| 585 | 3 | } |
|
| 586 | 61 | } |
|
| 587 | |||
| 588 | 61 | return array_merge($values, $defaults); |
|
| 589 | } |
||
| 590 | |||
| 591 | 67 | private function getAlias(AstFieldInterface $ast) |
|
| 595 | |||
| 596 | 61 | protected function createResolveInfo(FieldInterface $field, array $astFields) |
|
| 600 | |||
| 601 | |||
| 602 | /** |
||
| 603 | * You can access ExecutionContext to check errors and inject dependencies |
||
| 604 | * |
||
| 605 | * @return ExecutionContext |
||
| 606 | */ |
||
| 607 | 11 | public function getExecutionContext() |
|
| 611 | |||
| 612 | 67 | public function getResponseData() |
|
| 613 | { |
||
| 614 | 67 | $result = []; |
|
| 615 | |||
| 616 | 67 | if (!empty($this->data)) { |
|
| 617 | 66 | $result['data'] = $this->data; |
|
| 618 | 66 | } |
|
| 619 | |||
| 620 | 67 | if ($this->executionContext->hasErrors()) { |
|
| 621 | 19 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 622 | 19 | } |
|
| 623 | |||
| 624 | 67 | return $result; |
|
| 625 | } |
||
| 626 | |||
| 627 | /** |
||
| 628 | * @return int |
||
| 629 | */ |
||
| 630 | public function getMaxComplexity() |
||
| 634 | |||
| 635 | /** |
||
| 636 | * @param int $maxComplexity |
||
| 637 | */ |
||
| 638 | 1 | public function setMaxComplexity($maxComplexity) |
|
| 642 | |||
| 643 | } |
||
| 644 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.