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 | 55 | public function __construct(AbstractSchema $schema) |
|
| 68 | |||
| 69 | 53 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 70 | { |
||
| 71 | 53 | $this->data = []; |
|
| 72 | |||
| 73 | try { |
||
| 74 | 53 | $this->parseAndCreateRequest($payload, $variables); |
|
| 75 | |||
| 76 | 52 | if ($this->maxComplexity) { |
|
| 77 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 78 | 1 | } |
|
| 79 | |||
| 80 | 52 | if ($reducers) { |
|
| 81 | 2 | $reducer = new Reducer(); |
|
| 82 | 2 | $reducer->reduceQuery($this->executionContext, $reducers); |
|
| 83 | 2 | } |
|
| 84 | |||
| 85 | 52 | foreach ($this->executionContext->getRequest()->getAllOperations() as $query) { |
|
| 86 | 52 | if ($operationResult = $this->resolveQuery($query)) { |
|
| 87 | 52 | $this->data = array_merge($this->data, $operationResult); |
|
| 88 | 52 | }; |
|
| 89 | 52 | } |
|
| 90 | 53 | } catch (\Exception $e) { |
|
| 91 | 5 | $this->executionContext->addError($e); |
|
| 92 | } |
||
| 93 | |||
| 94 | 53 | return $this; |
|
| 95 | } |
||
| 96 | |||
| 97 | 53 | public function getResponseData() |
|
| 111 | |||
| 112 | /** |
||
| 113 | * You can access ExecutionContext to check errors and inject dependencies |
||
| 114 | * |
||
| 115 | * @return ExecutionContext |
||
| 116 | */ |
||
| 117 | 11 | public function getExecutionContext() |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function getMaxComplexity() |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param int $maxComplexity |
||
| 132 | */ |
||
| 133 | 1 | public function setMaxComplexity($maxComplexity) |
|
| 137 | |||
| 138 | 52 | protected function resolveQuery(AstQuery $query) |
|
| 156 | |||
| 157 | 52 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false) |
|
| 216 | |||
| 217 | 52 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request) |
|
| 227 | |||
| 228 | 29 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request) |
|
| 229 | { |
||
| 230 | 29 | switch ($argumentType->getKind()) { |
|
| 231 | 29 | case TypeMap::KIND_LIST: |
|
| 232 | /** @var $argumentType AbstractListType */ |
||
| 233 | 6 | $result = []; |
|
| 234 | 6 | if ($argumentValue instanceof AstInputList || is_array($argumentValue)) { |
|
| 235 | 5 | $list = is_array($argumentValue) ? $argumentValue : $argumentValue->getValue(); |
|
| 236 | 5 | foreach ($list as $item) { |
|
| 237 | 5 | $result[] = $this->prepareArgumentValue($item, $argumentType->getItemType()->getNullableType(), $request); |
|
| 238 | 5 | } |
|
| 239 | 6 | } else if ($argumentValue instanceof VariableReference) { |
|
| 240 | 1 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
|
| 241 | } |
||
| 242 | |||
| 243 | 5 | return $result; |
|
| 244 | |||
| 245 | 28 | case TypeMap::KIND_INPUT_OBJECT: |
|
| 246 | /** @var $argumentType AbstractInputObjectType */ |
||
| 247 | 5 | $result = []; |
|
| 248 | 5 | if ($argumentValue instanceof AstInputObject) { |
|
| 249 | 4 | foreach ($argumentType->getFields() as $field) { |
|
| 250 | /** @var $field Field */ |
||
| 251 | 4 | if ($field->getConfig()->has('default')) { |
|
| 252 | 1 | $result[$field->getName()] = $field->getType()->getNullableType()->parseInputValue($field->getConfig()->get('default')); |
|
| 253 | 1 | } |
|
| 254 | 4 | } |
|
| 255 | 4 | foreach ($argumentValue->getValue() as $key => $item) { |
|
| 256 | 4 | if ($argumentType->hasField($key)) { |
|
| 257 | 4 | $result[$key] = $this->prepareArgumentValue($item, $argumentType->getField($key)->getType()->getNullableType(), $request); |
|
| 258 | 4 | } else { |
|
| 259 | $result[$key] = $item; |
||
| 260 | } |
||
| 261 | 4 | } |
|
| 262 | 5 | } else if ($argumentValue instanceof VariableReference) { |
|
| 263 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
||
| 264 | 2 | } else if (is_array($argumentValue)) { |
|
| 265 | 1 | return $argumentValue; |
|
| 266 | } |
||
| 267 | |||
| 268 | 5 | return $result; |
|
| 269 | |||
| 270 | 27 | case TypeMap::KIND_SCALAR: |
|
| 271 | 27 | case TypeMap::KIND_ENUM: |
|
| 272 | /** @var $argumentValue AstLiteral|VariableReference */ |
||
| 273 | 27 | if ($argumentValue instanceof VariableReference) { |
|
| 274 | 4 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request); |
|
| 275 | 24 | } else if ($argumentValue instanceof AstLiteral) { |
|
| 276 | 17 | return $argumentValue->getValue(); |
|
| 277 | } else { |
||
| 278 | 8 | return $argumentValue; |
|
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | throw new ResolveException('Argument type not supported'); |
||
| 283 | } |
||
| 284 | |||
| 285 | 5 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request) |
|
| 286 | { |
||
| 287 | 5 | $variable = $variableReference->getVariable(); |
|
| 288 | 5 | if ($argumentType->getKind() === TypeMap::KIND_LIST) { |
|
| 289 | if ( |
||
| 290 | 1 | !$variable->isArray() || |
|
| 291 | 1 | ($variable->getTypeName() !== $argumentType->getNamedType()->getNullableType()->getName()) || |
|
| 292 | 1 | ($argumentType->getNamedType()->getKind() === TypeMap::KIND_NON_NULL && $variable->isArrayElementNullable()) |
|
| 293 | 1 | ) { |
|
| 294 | 1 | throw new ResolveException(sprintf('Invalid variable "%s" type, allowed type is "%s"', $variable->getName(), $argumentType->getNamedType()->getNullableType()->getName()), $variable->getLocation()); |
|
| 295 | } |
||
| 296 | 1 | } else { |
|
| 297 | 4 | if ($variable->getTypeName() !== $argumentType->getName()) { |
|
| 298 | 1 | throw new ResolveException(sprintf('Invalid variable "%s" type, allowed type is "%s"', $variable->getName(), $argumentType->getName()), $variable->getLocation()); |
|
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | 4 | $requestValue = $request->getVariable($variable->getName()); |
|
| 303 | 4 | if ((null === $requestValue && $variable->isNullable()) && !$request->hasVariable($variable->getName())) { |
|
| 304 | throw new ResolveException(sprintf('Variable "%s" does not exist in request', $variable->getName()), $variable->getLocation()); |
||
| 305 | } |
||
| 306 | |||
| 307 | 4 | return $requestValue; |
|
| 308 | } |
||
| 309 | |||
| 310 | 28 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false) |
|
| 311 | { |
||
| 312 | 28 | $resolvedValue = $parentValue; |
|
| 313 | 28 | if (!$fromUnion) { |
|
| 314 | 23 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 315 | 23 | } |
|
| 316 | |||
| 317 | 28 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 318 | |||
| 319 | 28 | if (null === $resolvedValue) { |
|
| 320 | 5 | return null; |
|
| 321 | } |
||
| 322 | /** @var AbstractObjectType $type */ |
||
| 323 | 27 | $type = $field->getType()->getNullableType(); |
|
| 324 | |||
| 325 | try { |
||
| 326 | 27 | return $this->collectResult($field, $type, $ast, $resolvedValue); |
|
| 327 | 4 | } catch (\Exception $e) { |
|
| 328 | 4 | return null; |
|
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | 27 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue) |
|
| 333 | { |
||
| 334 | /** @var AstQuery $ast */ |
||
| 335 | 27 | $result = []; |
|
| 336 | |||
| 337 | 27 | foreach ($ast->getFields() as $astField) { |
|
| 338 | 27 | switch (true) { |
|
| 339 | 27 | case $astField instanceof TypedFragmentReference: |
|
| 340 | 2 | $astName = $astField->getTypeName(); |
|
| 341 | 2 | $typeName = $type->getName(); |
|
| 342 | |||
| 343 | 2 | View Code Duplication | if ($typeName !== $astName) { |
| 344 | 2 | foreach ($type->getInterfaces() as $interface) { |
|
| 345 | 1 | if ($interface->getName() === $astName) { |
|
| 346 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
||
| 347 | |||
| 348 | break; |
||
| 349 | } |
||
| 350 | 2 | } |
|
| 351 | |||
| 352 | 2 | continue; |
|
| 353 | } |
||
| 354 | |||
| 355 | 2 | $result = array_merge($result, $this->collectResult($field, $type, $astField, $resolvedValue)); |
|
| 356 | |||
| 357 | 2 | break; |
|
| 358 | |||
| 359 | 27 | case $astField instanceof FragmentReference: |
|
| 360 | 4 | $astFragment = $this->executionContext->getRequest()->getFragment($astField->getName()); |
|
| 361 | 4 | $astFragmentModel = $astFragment->getModel(); |
|
| 362 | 4 | $typeName = $type->getName(); |
|
| 363 | |||
| 364 | 4 | View Code Duplication | if ($typeName !== $astFragmentModel) { |
| 365 | 1 | foreach ($type->getInterfaces() as $interface) { |
|
| 366 | 1 | if ($interface->getName() === $astFragmentModel) { |
|
| 367 | 1 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 368 | |||
| 369 | 1 | break; |
|
| 370 | } |
||
| 371 | |||
| 372 | 1 | } |
|
| 373 | |||
| 374 | 1 | continue; |
|
| 375 | } |
||
| 376 | |||
| 377 | 4 | $result = array_merge($result, $this->collectResult($field, $type, $astFragment, $resolvedValue)); |
|
| 378 | |||
| 379 | 4 | break; |
|
| 380 | |||
| 381 | 27 | default: |
|
| 382 | 27 | $result[$this->getAlias($astField)] = $this->resolveField($field, $astField, $resolvedValue, true); |
|
| 383 | 27 | } |
|
| 384 | 27 | } |
|
| 385 | |||
| 386 | 27 | return $result; |
|
| 387 | } |
||
| 388 | |||
| 389 | 41 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 400 | |||
| 401 | 15 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 402 | { |
||
| 403 | /** @var AstQuery $ast */ |
||
| 404 | 15 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 405 | |||
| 406 | 15 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 407 | |||
| 408 | 13 | if (null === $resolvedValue) { |
|
| 409 | 5 | return null; |
|
| 410 | } |
||
| 411 | |||
| 412 | /** @var AbstractListType $type */ |
||
| 413 | 12 | $type = $field->getType()->getNullableType(); |
|
| 414 | 12 | $itemType = $type->getNamedType(); |
|
| 415 | |||
| 416 | 12 | $fakeAst = clone $ast; |
|
| 417 | 12 | if ($fakeAst instanceof AstQuery) { |
|
| 418 | 12 | $fakeAst->setArguments([]); |
|
| 419 | 12 | } |
|
| 420 | |||
| 421 | 12 | $fakeField = new Field([ |
|
| 422 | 12 | 'name' => $field->getName(), |
|
| 423 | 12 | 'type' => $itemType, |
|
| 424 | 12 | ]); |
|
| 425 | |||
| 426 | 12 | $result = []; |
|
| 427 | 12 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 428 | try { |
||
| 429 | 11 | $fakeField->getConfig()->set('resolve', function () use ($resolvedValueItem) { |
|
| 430 | 11 | return $resolvedValueItem; |
|
| 431 | 11 | }); |
|
| 432 | |||
| 433 | 11 | switch ($itemType->getNullableType()->getKind()) { |
|
| 434 | 11 | case TypeMap::KIND_ENUM: |
|
| 435 | 11 | case TypeMap::KIND_SCALAR: |
|
| 436 | 3 | $value = $this->resolveScalar($fakeField, $fakeAst, $resolvedValueItem); |
|
| 437 | |||
| 438 | 2 | break; |
|
| 439 | |||
| 440 | |||
| 441 | 9 | case TypeMap::KIND_OBJECT: |
|
| 442 | 7 | $value = $this->resolveObject($fakeField, $fakeAst, $resolvedValueItem); |
|
| 443 | |||
| 444 | 7 | break; |
|
| 445 | |||
| 446 | 3 | case TypeMap::KIND_UNION: |
|
| 447 | 3 | case TypeMap::KIND_INTERFACE: |
|
| 448 | 3 | $value = $this->resolveComposite($fakeField, $fakeAst, $resolvedValueItem); |
|
| 449 | |||
| 450 | 3 | break; |
|
| 451 | |||
| 452 | default: |
||
| 453 | $value = null; |
||
| 454 | 10 | } |
|
| 455 | 11 | } catch (\Exception $e) { |
|
| 456 | 1 | $this->executionContext->addError($e); |
|
| 457 | |||
| 458 | 1 | $value = null; |
|
| 459 | } |
||
| 460 | |||
| 461 | 11 | $result[] = $value; |
|
| 462 | 12 | } |
|
| 463 | |||
| 464 | 12 | return $result; |
|
| 465 | } |
||
| 466 | |||
| 467 | 7 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue) |
|
| 468 | { |
||
| 469 | /** @var AstQuery $ast */ |
||
| 470 | 7 | $resolvedValue = $this->doResolve($field, $ast, $parentValue); |
|
| 471 | |||
| 472 | 7 | $this->resolveValidator->assertValidResolvedValueForField($field, $resolvedValue); |
|
| 473 | |||
| 474 | /** @var AbstractUnionType $type */ |
||
| 475 | 7 | $type = $field->getType()->getNullableType(); |
|
| 476 | 7 | $resolvedType = $type->resolveType($resolvedValue); |
|
| 477 | |||
| 478 | 7 | if (!$resolvedType) { |
|
| 479 | throw new ResolveException('Resolving function must return type'); |
||
| 480 | } |
||
| 481 | |||
| 482 | 7 | if ($type instanceof AbstractInterfaceType) { |
|
| 483 | 6 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $type); |
|
| 484 | 6 | } else { |
|
| 485 | 1 | $this->resolveValidator->assertTypeInUnionTypes($resolvedType, $type); |
|
| 486 | } |
||
| 487 | |||
| 488 | 7 | $fakeField = new Field([ |
|
| 489 | 7 | 'name' => $field->getName(), |
|
| 490 | 7 | 'type' => $resolvedType, |
|
| 491 | 7 | ]); |
|
| 492 | |||
| 493 | 7 | return $this->resolveObject($fakeField, $ast, $resolvedValue, true); |
|
| 494 | } |
||
| 495 | |||
| 496 | 53 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 509 | |||
| 510 | 47 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null) |
|
| 518 | |||
| 519 | 47 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast) |
|
| 520 | { |
||
| 521 | 47 | $values = []; |
|
| 522 | 47 | $defaults = []; |
|
| 523 | |||
| 524 | 47 | foreach ($field->getArguments() as $argument) { |
|
| 525 | /** @var $argument InputField */ |
||
| 526 | 33 | if ($argument->getConfig()->has('default')) { |
|
| 527 | 6 | $defaults[$argument->getName()] = $argument->getConfig()->getDefaultValue(); |
|
| 528 | 6 | } |
|
| 529 | 47 | } |
|
| 530 | |||
| 531 | 47 | foreach ($ast->getArguments() as $astArgument) { |
|
| 532 | 26 | $argument = $field->getArgument($astArgument->getName()); |
|
| 533 | 26 | $argumentType = $argument->getType()->getNullableType(); |
|
| 534 | |||
| 535 | 26 | $values[$argument->getName()] = $argumentType->parseValue($astArgument->getValue()); |
|
| 536 | |||
| 537 | 26 | if (isset($defaults[$argument->getName()])) { |
|
| 538 | 3 | unset($defaults[$argument->getName()]); |
|
| 539 | 3 | } |
|
| 540 | 47 | } |
|
| 541 | |||
| 542 | 47 | return array_merge($values, $defaults); |
|
| 543 | } |
||
| 544 | |||
| 545 | 52 | private function getAlias(AstFieldInterface $ast) |
|
| 549 | |||
| 550 | 47 | protected function createResolveInfo(FieldInterface $field, array $astFields) |
|
| 554 | |||
| 555 | } |
||
| 556 |
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.