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 | 57 | public function __construct(AbstractSchema $schema) |
|
| 69 | |||
| 70 | 55 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 97 | |||
| 98 | 54 | protected function resolveQuery(AstQuery $query) |
|
| 116 | |||
| 117 | 54 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false) |
|
| 176 | |||
| 177 | 54 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
|
| 187 | |||
| 188 | 30 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request) |
|
| 244 | |||
| 245 | 6 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request) |
|
| 269 | |||
| 270 | 30 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false) |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @param FieldInterface $field |
||
| 294 | * @param AbstractObjectType $type |
||
| 295 | * @param AstFieldInterface $ast |
||
| 296 | * @param $resolvedValue |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 29 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue) |
|
| 300 | { |
||
| 301 | 29 | $result = []; |
|
| 302 | |||
| 303 | 29 | foreach ($ast->getFields() as $astField) { |
|
| 304 | switch (true) { |
||
| 305 | 29 | case $astField instanceof TypedFragmentReference: |
|
| 306 | 2 | $astName = $astField->getTypeName(); |
|
| 307 | 2 | $typeName = $type->getName(); |
|
| 308 | |||
| 309 | 2 | View Code Duplication | if ($typeName !== $astName) { |
| 310 | 2 | foreach ($type->getInterfaces() as $interface) { |
|
| 311 | 1 | if ($interface->getName() === $astName) { |
|
| 312 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
||
| 313 | |||
| 314 | 1 | break; |
|
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | 2 | continue; |
|
| 319 | } |
||
| 320 | |||
| 321 | 2 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
|
| 322 | |||
| 323 | 2 | break; |
|
| 324 | |||
| 325 | 29 | case $astField instanceof FragmentReference: |
|
| 326 | 4 | $astFragment = $this->executionContext->getRequest()->getFragment($astField->getName()); |
|
| 327 | 4 | $astFragmentModel = $astFragment->getModel(); |
|
| 328 | 4 | $typeName = $type->getName(); |
|
| 329 | |||
| 330 | 4 | View Code Duplication | if ($typeName !== $astFragmentModel) { |
| 331 | 1 | foreach ($type->getInterfaces() as $interface) { |
|
| 332 | 1 | if ($interface->getName() === $astFragmentModel) { |
|
| 333 | 1 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 334 | |||
| 335 | 1 | break; |
|
| 336 | } |
||
| 337 | |||
| 338 | } |
||
| 339 | |||
| 340 | 1 | continue; |
|
| 341 | } |
||
| 342 | |||
| 343 | 4 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 344 | |||
| 345 | 4 | break; |
|
| 346 | |||
| 347 | default: |
||
| 348 | 29 | $result[$this->getAlias($astField)] = $this->resolveField($field, $astField, $resolvedValue, true); |
|
| 349 | } |
||
| 350 | } |
||
| 351 | |||
| 352 | 29 | return $result; |
|
| 353 | } |
||
| 354 | |||
| 355 | 43 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 366 | |||
| 367 | 17 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 368 | { |
||
| 369 | /** @var AstQuery $ast */ |
||
| 370 | 17 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 371 | |||
| 372 | 17 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 373 | |||
| 374 | 15 | if (null === $resolvedValue) { |
|
| 375 | 5 | return null; |
|
| 376 | } |
||
| 377 | |||
| 378 | /** @var AbstractListType $type */ |
||
| 379 | 14 | $type = $field->getType()->getNullableType(); |
|
| 380 | 14 | $itemType = $type->getNamedType(); |
|
| 381 | |||
| 382 | 14 | $fakeAst = clone $ast; |
|
| 383 | 14 | if ($fakeAst instanceof AstQuery) { |
|
| 384 | 14 | $fakeAst->setArguments([]); |
|
| 385 | } |
||
| 386 | |||
| 387 | 14 | $fakeField = new Field([ |
|
| 388 | 14 | 'name' => $field->getName(), |
|
| 389 | 14 | 'type' => $itemType, |
|
| 390 | ]); |
||
| 391 | |||
| 392 | 14 | $result = []; |
|
| 393 | 14 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 394 | try { |
||
| 395 | 13 | $fakeField->getConfig()->set('resolve', function () use ($resolvedValueItem) { |
|
| 396 | 13 | return $resolvedValueItem; |
|
| 397 | 13 | }); |
|
| 398 | |||
| 399 | 13 | switch ($itemType->getNullableType()->getKind()) { |
|
| 400 | 13 | case TypeMap::KIND_ENUM: |
|
| 401 | 12 | case TypeMap::KIND_SCALAR: |
|
| 402 | 3 | $value = $this->resolveScalar($fakeField, $fakeAst, $resolvedValueItem); |
|
| 403 | |||
| 404 | 2 | break; |
|
| 405 | |||
| 406 | |||
| 407 | 11 | case TypeMap::KIND_OBJECT: |
|
| 408 | 9 | $value = $this->resolveObject($fakeField, $fakeAst, $resolvedValueItem); |
|
| 409 | |||
| 410 | 9 | break; |
|
| 411 | |||
| 412 | 3 | case TypeMap::KIND_UNION: |
|
| 413 | 3 | case TypeMap::KIND_INTERFACE: |
|
| 414 | 3 | $value = $this->resolveComposite($fakeField, $fakeAst, $resolvedValueItem); |
|
| 415 | |||
| 416 | 3 | break; |
|
| 417 | |||
| 418 | default: |
||
| 419 | 12 | $value = null; |
|
| 420 | } |
||
| 421 | 1 | } catch (\Exception $e) { |
|
| 422 | 1 | $this->executionContext->addError($e); |
|
| 423 | |||
| 424 | 1 | $value = null; |
|
| 425 | } |
||
| 426 | |||
| 427 | 13 | $result[] = $value; |
|
| 428 | } |
||
| 429 | |||
| 430 | 14 | return $result; |
|
| 431 | } |
||
| 432 | |||
| 433 | 7 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 434 | { |
||
| 435 | /** @var AstQuery $ast */ |
||
| 436 | 7 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 437 | |||
| 438 | 7 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 439 | |||
| 440 | /** @var AbstractUnionType $type */ |
||
| 441 | 7 | $type = $field->getType()->getNullableType(); |
|
| 442 | 7 | $resolvedType = $type->resolveType($resolvedValue); |
|
| 443 | |||
| 444 | 7 | if (!$resolvedType) { |
|
| 445 | throw new ResolveException('Resolving function must return type'); |
||
| 446 | } |
||
| 447 | |||
| 448 | 7 | if ($type instanceof AbstractInterfaceType) { |
|
| 449 | 6 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $type); |
|
| 450 | } else { |
||
| 451 | 1 | $this->resolveValidator->assertTypeInUnionTypes($resolvedType, $type); |
|
| 452 | } |
||
| 453 | |||
| 454 | 7 | $fakeField = new Field([ |
|
| 455 | 7 | 'name' => $field->getName(), |
|
| 456 | 7 | 'type' => $resolvedType, |
|
| 457 | ]); |
||
| 458 | |||
| 459 | 7 | return $this->resolveObject($fakeField, $ast, $resolvedValue, true); |
|
| 460 | } |
||
| 461 | |||
| 462 | 55 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 475 | |||
| 476 | 49 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null) |
|
| 484 | |||
| 485 | 49 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) |
|
| 486 | { |
||
| 487 | 49 | $values = []; |
|
| 488 | 49 | $defaults = []; |
|
| 489 | |||
| 490 | 49 | foreach ($field->getArguments() as $argument) { |
|
| 491 | /** @var $argument InputField */ |
||
| 492 | 34 | if ($argument->getConfig()->has('defaultValue')) { |
|
| 493 | 34 | $defaults[$argument->getName()] = $argument->getConfig()->getDefaultValue(); |
|
| 494 | } |
||
| 495 | } |
||
| 496 | |||
| 497 | 49 | foreach ($ast->getArguments() as $astArgument) { |
|
| 498 | 27 | $argument = $field->getArgument($astArgument->getName()); |
|
| 499 | 27 | $argumentType = $argument->getType()->getNullableType(); |
|
| 500 | |||
| 501 | 27 | $values[$argument->getName()] = $argumentType->parseValue($astArgument->getValue()); |
|
| 502 | |||
| 503 | 27 | if (isset($defaults[$argument->getName()])) { |
|
| 504 | 27 | unset($defaults[$argument->getName()]); |
|
| 505 | } |
||
| 506 | } |
||
| 507 | |||
| 508 | 49 | return array_merge($values, $defaults); |
|
| 509 | } |
||
| 510 | |||
| 511 | 54 | private function getAlias(AstFieldInterface $ast) |
|
| 515 | |||
| 516 | 49 | protected function createResolveInfo(FieldInterface $field, array $astFields) |
|
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * You can access ExecutionContext to check errors and inject dependencies |
||
| 524 | * |
||
| 525 | * @return ExecutionContext |
||
| 526 | */ |
||
| 527 | 11 | public function getExecutionContext() |
|
| 531 | |||
| 532 | 55 | public function getResponseData() |
|
| 533 | { |
||
| 534 | 55 | $result = []; |
|
| 535 | |||
| 536 | 55 | if (!empty($this->data)) { |
|
| 537 | 54 | $result['data'] = $this->data; |
|
| 538 | } |
||
| 539 | |||
| 540 | 55 | if ($this->executionContext->hasErrors()) { |
|
| 541 | 18 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 542 | } |
||
| 543 | |||
| 544 | 55 | return $result; |
|
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * @return int |
||
| 549 | */ |
||
| 550 | public function getMaxComplexity() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * @param int $maxComplexity |
||
| 557 | */ |
||
| 558 | 1 | public function setMaxComplexity($maxComplexity) |
|
| 562 | |||
| 563 | } |
||
| 564 |
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
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.