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 |
||
| 43 | class Processor |
||
| 44 | { |
||
| 45 | |||
| 46 | const TYPE_NAME_QUERY = '__typename'; |
||
| 47 | |||
| 48 | /** @var ExecutionContext */ |
||
| 49 | protected $executionContext; |
||
| 50 | |||
| 51 | /** @var ResolveValidatorInterface */ |
||
| 52 | protected $resolveValidator; |
||
| 53 | |||
| 54 | /** @var array */ |
||
| 55 | protected $data; |
||
| 56 | |||
| 57 | /** @var int */ |
||
| 58 | protected $maxComplexity; |
||
| 59 | |||
| 60 | /** @var array DeferredResult[] */ |
||
| 61 | protected $deferredResultsLeaf = []; |
||
| 62 | |||
| 63 | /** @var array DeferredResult[] */ |
||
| 64 | protected $deferredResultsComplex = []; |
||
| 65 | |||
| 66 | 71 | public function __construct(AbstractSchema $schema) |
|
| 67 | { |
||
| 68 | 71 | if (empty($this->executionContext)) { |
|
| 69 | 71 | $this->executionContext = new ExecutionContext($schema); |
|
| 70 | 71 | $this->executionContext->setContainer(new Container()); |
|
| 71 | 71 | } |
|
| 72 | |||
| 73 | 71 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 74 | 71 | } |
|
| 75 | |||
| 76 | 69 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 77 | 1 | { |
|
| 78 | 69 | $this->data = []; |
|
| 79 | |||
| 80 | try { |
||
| 81 | 69 | $this->parseAndCreateRequest($payload, $variables); |
|
| 82 | |||
| 83 | 68 | if ($this->maxComplexity) { |
|
| 84 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 85 | 1 | } |
|
| 86 | |||
| 87 | 68 | if ($reducers) { |
|
|
|
|||
| 88 | 2 | $reducer = new Reducer(); |
|
| 89 | 2 | $reducer->reduceQuery($this->executionContext, $reducers); |
|
| 90 | 2 | } |
|
| 91 | |||
| 92 | 68 | foreach ($this->executionContext->getRequest()->getAllOperations() as $query) { |
|
| 93 | 68 | if ($operationResult = $this->resolveQuery($query)) { |
|
| 94 | 68 | $this->data = array_replace_recursive($this->data, $operationResult); |
|
| 95 | 68 | }; |
|
| 96 | 68 | } |
|
| 97 | |||
| 98 | // If the processor found any deferred results, resolve them now. |
||
| 99 | 68 | if (!empty($this->data) && (!empty($this->deferredResultsLeaf) || !empty($this->deferredResultsComplex))) { |
|
| 100 | try { |
||
| 101 | 4 | while ($deferredResolver = array_shift($this->deferredResultsComplex)) { |
|
| 102 | 4 | $deferredResolver->resolve(); |
|
| 103 | 4 | } |
|
| 104 | |||
| 105 | // Deferred scalar and enum fields should be resolved last to |
||
| 106 | // pick up as many as possible for a single batch. |
||
| 107 | 4 | while ($deferredResolver = array_shift($this->deferredResultsLeaf)) { |
|
| 108 | $deferredResolver->resolve(); |
||
| 109 | } |
||
| 110 | 4 | } catch (\Exception $e) { |
|
| 111 | $this->executionContext->addError($e); |
||
| 112 | 4 | } finally { |
|
| 113 | 4 | $this->data = static::unpackDeferredResults($this->data); |
|
| 114 | 4 | } |
|
| 115 | 4 | } |
|
| 116 | |||
| 117 | 69 | } catch (\Exception $e) { |
|
| 118 | 5 | $this->executionContext->addError($e); |
|
| 119 | } |
||
| 120 | |||
| 121 | 69 | return $this; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Unpack results stored inside deferred resolvers. |
||
| 126 | * |
||
| 127 | * @param mixed $result |
||
| 128 | * The result ree. |
||
| 129 | * |
||
| 130 | * @return mixed |
||
| 131 | * The unpacked result. |
||
| 132 | */ |
||
| 133 | 4 | public static function unpackDeferredResults($result) |
|
| 134 | { |
||
| 135 | 4 | while ($result instanceof DeferredResult) { |
|
| 136 | 4 | $result = $result->result; |
|
| 137 | 4 | } |
|
| 138 | |||
| 139 | 4 | if (is_array($result)) { |
|
| 140 | 4 | foreach ($result as $key => $value) { |
|
| 141 | 4 | $result[$key] = static::unpackDeferredResults($value); |
|
| 142 | 4 | } |
|
| 143 | 4 | } |
|
| 144 | |||
| 145 | 4 | return $result; |
|
| 146 | } |
||
| 147 | |||
| 148 | 68 | protected function resolveQuery(AstQuery $query) |
|
| 166 | |||
| 167 | 68 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false) |
|
| 168 | { |
||
| 226 | |||
| 227 | 68 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
|
| 237 | |||
| 238 | 38 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request) |
|
| 302 | |||
| 303 | 9 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request) |
|
| 327 | |||
| 328 | |||
| 329 | /** |
||
| 330 | * @param FieldInterface $field |
||
| 331 | * @param AbstractObjectType $type |
||
| 332 | * @param AstFieldInterface $ast |
||
| 333 | * @param $resolvedValue |
||
| 334 | * @return array |
||
| 335 | */ |
||
| 336 | 40 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue) |
|
| 337 | { |
||
| 338 | 40 | $result = []; |
|
| 339 | |||
| 340 | 40 | foreach ($ast->getFields() as $astField) { |
|
| 341 | 40 | switch (true) { |
|
| 342 | 40 | case $astField instanceof TypedFragmentReference: |
|
| 343 | 2 | $astName = $astField->getTypeName(); |
|
| 344 | 2 | $typeName = $type->getName(); |
|
| 345 | |||
| 346 | 2 | View Code Duplication | if ($typeName !== $astName) { |
| 347 | 2 | foreach ($type->getInterfaces() as $interface) { |
|
| 348 | 1 | if ($interface->getName() === $astName) { |
|
| 349 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
||
| 350 | |||
| 351 | break; |
||
| 352 | } |
||
| 353 | 2 | } |
|
| 354 | |||
| 355 | 2 | continue 2; |
|
| 356 | } |
||
| 357 | |||
| 358 | 2 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
|
| 359 | |||
| 360 | 2 | break; |
|
| 361 | |||
| 362 | 40 | case $astField instanceof FragmentReference: |
|
| 363 | 8 | $astFragment = $this->executionContext->getRequest()->getFragment($astField->getName()); |
|
| 364 | 8 | $astFragmentModel = $astFragment->getModel(); |
|
| 365 | 8 | $typeName = $type->getName(); |
|
| 366 | |||
| 367 | 8 | View Code Duplication | if ($typeName !== $astFragmentModel) { |
| 368 | 2 | foreach ($type->getInterfaces() as $interface) { |
|
| 369 | 1 | if ($interface->getName() === $astFragmentModel) { |
|
| 370 | 1 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 371 | |||
| 372 | 1 | break; |
|
| 373 | } |
||
| 374 | 2 | } |
|
| 375 | |||
| 376 | 2 | continue 2; |
|
| 377 | } |
||
| 378 | |||
| 379 | 7 | $result = array_replace_recursive($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 380 | |||
| 381 | 7 | break; |
|
| 382 | |||
| 383 | 39 | default: |
|
| 384 | 39 | $result = array_replace_recursive($result, [$this->getAlias($astField) => $this->resolveField($field, $astField, $resolvedValue, true)]); |
|
| 385 | 39 | } |
|
| 386 | 40 | } |
|
| 387 | |||
| 388 | 40 | return $result; |
|
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Apply post-process callbacks to all deferred resolvers. |
||
| 393 | */ |
||
| 394 | 62 | protected function deferredResolve($resolvedValue, FieldInterface $field, callable $callback) { |
|
| 395 | 62 | if ($resolvedValue instanceof DeferredResolverInterface) { |
|
| 396 | $deferredResult = new DeferredResult($resolvedValue, function ($resolvedValue) use ($field, $callback) { |
||
| 397 | // Allow nested deferred resolvers. |
||
| 398 | 4 | return $this->deferredResolve($resolvedValue, $field, $callback); |
|
| 399 | 4 | }); |
|
| 400 | |||
| 401 | // Whenever we stumble upon a deferred resolver, add it to the queue |
||
| 402 | // to be resolved later. |
||
| 403 | 4 | $type = $field->getType()->getNamedType(); |
|
| 404 | 4 | if ($type instanceof AbstractScalarType || $type instanceof AbstractEnumType) { |
|
| 405 | array_push($this->deferredResultsLeaf, $deferredResult); |
||
| 406 | } else { |
||
| 407 | 4 | array_push($this->deferredResultsComplex, $deferredResult); |
|
| 408 | } |
||
| 409 | |||
| 410 | 4 | return $deferredResult; |
|
| 411 | } |
||
| 412 | // For simple values, invoke the callback immediately. |
||
| 413 | 62 | return $callback($resolvedValue); |
|
| 414 | } |
||
| 415 | |||
| 416 | 55 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 428 | |||
| 429 | 28 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 430 | { |
||
| 431 | /** @var AstQuery $ast */ |
||
| 432 | 28 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 433 | |||
| 434 | return $this->deferredResolve($resolvedValue, $field, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
| 435 | 28 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 436 | |||
| 437 | 26 | if (null === $resolvedValue) { |
|
| 438 | 8 | return null; |
|
| 439 | } |
||
| 440 | |||
| 441 | /** @var AbstractListType $type */ |
||
| 442 | 25 | $type = $field->getType()->getNullableType(); |
|
| 443 | 25 | $itemType = $type->getNamedType(); |
|
| 444 | |||
| 445 | 25 | $fakeAst = clone $ast; |
|
| 446 | 25 | if ($fakeAst instanceof AstQuery) { |
|
| 447 | 24 | $fakeAst->setArguments([]); |
|
| 448 | 24 | } |
|
| 449 | |||
| 450 | 25 | $fakeField = new Field([ |
|
| 451 | 25 | 'name' => $field->getName(), |
|
| 452 | 25 | 'type' => $itemType, |
|
| 453 | 25 | 'args' => $field->getArguments(), |
|
| 454 | 25 | ]); |
|
| 455 | |||
| 456 | 25 | $result = []; |
|
| 457 | 25 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 458 | try { |
||
| 459 | $fakeField->getConfig()->set('resolve', function () use ($resolvedValueItem) { |
||
| 460 | 24 | return $resolvedValueItem; |
|
| 461 | 24 | }); |
|
| 462 | |||
| 463 | 24 | switch ($itemType->getNullableType()->getKind()) { |
|
| 464 | 24 | case TypeMap::KIND_ENUM: |
|
| 465 | 24 | case TypeMap::KIND_SCALAR: |
|
| 466 | 6 | $value = $this->resolveScalar($fakeField, $fakeAst, $resolvedValueItem); |
|
| 467 | |||
| 468 | 5 | break; |
|
| 469 | |||
| 470 | |||
| 471 | 20 | case TypeMap::KIND_OBJECT: |
|
| 472 | 17 | $value = $this->resolveObject($fakeField, $fakeAst, $resolvedValueItem); |
|
| 473 | |||
| 474 | 17 | break; |
|
| 475 | |||
| 476 | 4 | case TypeMap::KIND_UNION: |
|
| 477 | 4 | case TypeMap::KIND_INTERFACE: |
|
| 478 | 4 | $value = $this->resolveComposite($fakeField, $fakeAst, $resolvedValueItem); |
|
| 479 | |||
| 480 | 4 | break; |
|
| 481 | |||
| 482 | default: |
||
| 483 | $value = null; |
||
| 484 | 23 | } |
|
| 485 | 24 | } catch (\Exception $e) { |
|
| 486 | 1 | $this->executionContext->addError($e); |
|
| 487 | |||
| 488 | 1 | $value = null; |
|
| 489 | } |
||
| 490 | |||
| 491 | 24 | $result[] = $value; |
|
| 492 | 25 | } |
|
| 493 | |||
| 494 | 25 | return $result; |
|
| 495 | 28 | }); |
|
| 496 | } |
||
| 497 | |||
| 498 | 41 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false) |
|
| 499 | { |
||
| 500 | 41 | $resolvedValue = $parentValue; |
|
| 501 | 41 | if (!$fromUnion) { |
|
| 502 | 35 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 503 | 35 | } |
|
| 504 | |||
| 505 | return $this->deferredResolve($resolvedValue, $field, function ($resolvedValue) use ($field, $ast, $parentValue) { |
||
| 506 | 41 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 507 | |||
| 508 | 41 | if (null === $resolvedValue) { |
|
| 509 | 8 | return null; |
|
| 510 | } |
||
| 511 | /** @var AbstractObjectType $type */ |
||
| 512 | 40 | $type = $field->getType()->getNullableType(); |
|
| 513 | |||
| 514 | try { |
||
| 515 | 40 | return $this->collectResult($field, $type, $ast, $resolvedValue); |
|
| 516 | 4 | } catch (\Exception $e) { |
|
| 517 | 4 | return null; |
|
| 518 | } |
||
| 519 | 41 | }); |
|
| 520 | } |
||
| 521 | |||
| 522 | 8 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 523 | { |
||
| 524 | /** @var AstQuery $ast */ |
||
| 525 | 8 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 526 | 8 | return $this->deferredResolve($resolvedValue, $field, function ($resolvedValue) use ($field, $ast, $parentValue) { |
|
| 527 | 8 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 528 | |||
| 529 | 8 | if (null === $resolvedValue) { |
|
| 530 | return null; |
||
| 531 | } |
||
| 532 | |||
| 533 | /** @var AbstractUnionType $type */ |
||
| 534 | 8 | $type = $field->getType()->getNullableType(); |
|
| 535 | 8 | $resolveInfo = new ResolveInfo( |
|
| 536 | 8 | $field, |
|
| 537 | 8 | $ast instanceof AstQuery ? $ast->getFields() : [], |
|
| 538 | 8 | $this->executionContext |
|
| 539 | 8 | ); |
|
| 540 | 8 | $resolvedType = $type->resolveType($resolvedValue, $resolveInfo); |
|
| 541 | |||
| 542 | 8 | if (!$resolvedType) { |
|
| 543 | throw new ResolveException('Resolving function must return type'); |
||
| 544 | } |
||
| 545 | |||
| 546 | 8 | if ($type instanceof AbstractInterfaceType) { |
|
| 547 | 6 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $type); |
|
| 548 | 6 | } else { |
|
| 549 | 2 | $this->resolveValidator->assertTypeInUnionTypes($resolvedType, $type); |
|
| 550 | } |
||
| 551 | |||
| 552 | 8 | $fakeField = new Field([ |
|
| 553 | 8 | 'name' => $field->getName(), |
|
| 554 | 8 | 'type' => $resolvedType, |
|
| 555 | 8 | 'args' => $field->getArguments(), |
|
| 556 | 8 | ]); |
|
| 557 | |||
| 558 | 8 | return $this->resolveObject($fakeField, $ast, $resolvedValue, true); |
|
| 559 | 8 | }); |
|
| 560 | } |
||
| 561 | |||
| 562 | 69 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 575 | |||
| 576 | 62 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null) |
|
| 584 | |||
| 585 | 62 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) |
|
| 586 | { |
||
| 587 | 62 | $values = []; |
|
| 588 | 62 | $defaults = []; |
|
| 589 | |||
| 590 | 62 | foreach ($field->getArguments() as $argument) { |
|
| 591 | /** @var $argument InputField */ |
||
| 592 | 45 | if ($argument->getConfig()->has('defaultValue')) { |
|
| 593 | 9 | $defaults[$argument->getName()] = $argument->getConfig()->getDefaultValue(); |
|
| 594 | 9 | } |
|
| 595 | 62 | } |
|
| 596 | |||
| 597 | 62 | foreach ($ast->getArguments() as $astArgument) { |
|
| 598 | 34 | $argument = $field->getArgument($astArgument->getName()); |
|
| 599 | 34 | $argumentType = $argument->getType()->getNullableType(); |
|
| 600 | |||
| 601 | 34 | $values[$argument->getName()] = $argumentType->parseValue($astArgument->getValue()); |
|
| 602 | |||
| 603 | 34 | if (isset($defaults[$argument->getName()])) { |
|
| 604 | 3 | unset($defaults[$argument->getName()]); |
|
| 605 | 3 | } |
|
| 606 | 62 | } |
|
| 607 | |||
| 608 | 62 | return array_merge($values, $defaults); |
|
| 609 | } |
||
| 610 | |||
| 611 | 68 | private function getAlias(AstFieldInterface $ast) |
|
| 615 | |||
| 616 | 62 | protected function createResolveInfo(FieldInterface $field, array $astFields) |
|
| 620 | |||
| 621 | |||
| 622 | /** |
||
| 623 | * You can access ExecutionContext to check errors and inject dependencies |
||
| 624 | * |
||
| 625 | * @return ExecutionContext |
||
| 626 | */ |
||
| 627 | 11 | public function getExecutionContext() |
|
| 631 | |||
| 632 | 68 | public function getResponseData() |
|
| 633 | { |
||
| 634 | 68 | $result = []; |
|
| 635 | |||
| 636 | 68 | if (!empty($this->data)) { |
|
| 637 | 67 | $result['data'] = $this->data; |
|
| 638 | 67 | } |
|
| 639 | |||
| 640 | 68 | if ($this->executionContext->hasErrors()) { |
|
| 641 | 19 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 642 | 19 | } |
|
| 643 | |||
| 644 | 68 | return $result; |
|
| 645 | } |
||
| 646 | |||
| 647 | /** |
||
| 648 | * @return int |
||
| 649 | */ |
||
| 650 | public function getMaxComplexity() |
||
| 654 | |||
| 655 | /** |
||
| 656 | * @param int $maxComplexity |
||
| 657 | */ |
||
| 658 | 1 | public function setMaxComplexity($maxComplexity) |
|
| 662 | |||
| 663 | } |
||
| 664 |
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.