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 |
||
| 40 | class Processor |
||
| 41 | { |
||
| 42 | |||
| 43 | const TYPE_NAME_QUERY = '__typename'; |
||
| 44 | |||
| 45 | /** @var array */ |
||
| 46 | protected $data; |
||
| 47 | |||
| 48 | /** @var ResolveValidatorInterface */ |
||
| 49 | protected $resolveValidator; |
||
| 50 | |||
| 51 | /** @var ExecutionContext */ |
||
| 52 | protected $executionContext; |
||
| 53 | |||
| 54 | /** @var int */ |
||
| 55 | protected $maxComplexity; |
||
| 56 | |||
| 57 | 29 | public function __construct(AbstractSchema $schema) |
|
| 58 | { |
||
| 59 | 29 | (new SchemaValidator())->validate($schema); |
|
| 60 | |||
| 61 | 28 | $this->introduceIntrospectionFields($schema); |
|
| 62 | 28 | $this->executionContext = new ExecutionContext(); |
|
| 63 | 28 | $this->executionContext->setSchema($schema); |
|
| 64 | |||
| 65 | 28 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 66 | 28 | } |
|
| 67 | |||
| 68 | |||
| 69 | 27 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 70 | { |
||
| 71 | 27 | if ($this->executionContext->hasErrors()) { |
|
| 72 | 7 | $this->executionContext->clearErrors(); |
|
| 73 | } |
||
| 74 | |||
| 75 | 27 | $this->data = []; |
|
| 76 | |||
| 77 | try { |
||
| 78 | 27 | $this->parseAndCreateRequest($payload, $variables); |
|
| 79 | |||
| 80 | 27 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 81 | 27 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 82 | |||
| 83 | 27 | if ($this->maxComplexity) { |
|
| 84 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 85 | } |
||
| 86 | |||
| 87 | 27 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 88 | |||
| 89 | 27 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 90 | 27 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 91 | 27 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | }; |
||
| 93 | } |
||
| 94 | |||
| 95 | 4 | } catch (\Exception $e) { |
|
| 96 | 4 | $this->executionContext->addError($e); |
|
| 97 | } |
||
| 98 | |||
| 99 | 27 | return $this; |
|
| 100 | } |
||
| 101 | |||
| 102 | 27 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param Query|Field $query |
||
| 115 | * @param AbstractObjectType $currentLevelSchema |
||
| 116 | * @return array|bool|mixed |
||
| 117 | */ |
||
| 118 | 27 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @param Query $query |
||
| 137 | * @param AbstractField $field |
||
| 138 | * @param $contextValue |
||
| 139 | * @return array|mixed|null |
||
| 140 | */ |
||
| 141 | 24 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * @param Query|Mutation $query |
||
| 158 | * @param AbstractType $fieldType |
||
| 159 | * @param mixed $resolvedValue |
||
| 160 | * @return array|mixed |
||
| 161 | */ |
||
| 162 | 24 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param FieldAst $fieldAst |
||
| 210 | * @param AbstractField $field |
||
| 211 | * |
||
| 212 | * @param mixed $contextValue |
||
| 213 | * @return array|mixed|null |
||
| 214 | * @throws ResolveException |
||
| 215 | * @throws \Exception |
||
| 216 | */ |
||
| 217 | 19 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * @param AbstractField $field |
||
| 247 | * @param mixed $contextValue |
||
| 248 | * @param Query $query |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | 24 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
| 253 | { |
||
| 254 | 24 | if ($resolveFunc = $field->getConfig()->getResolveFunction()) { |
|
| 255 | 22 | return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $this->createResolveInfo($field, $query->getFields())); |
|
| 256 | 9 | } elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) { |
|
| 257 | 1 | return $propertyValue; |
|
| 258 | } else { |
||
| 259 | 8 | return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $this->createResolveInfo($field, $query->getFields())); |
|
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | 24 | protected function createResolveInfo($field, $fields) { |
|
| 264 | 24 | return new ResolveInfo($field, $fields, $this->executionContext); |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param $contextValue |
||
| 269 | * @param FieldAst $fieldAst |
||
| 270 | * @param AbstractField $field |
||
| 271 | * |
||
| 272 | * @throws \Exception |
||
| 273 | * |
||
| 274 | * @return mixed |
||
| 275 | */ |
||
| 276 | 19 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 277 | { |
||
| 278 | 19 | $resolved = false; |
|
| 279 | 19 | $resolverValue = null; |
|
| 280 | |||
| 281 | 19 | if ($resolveFunction = $field->getResolveFunction()) { |
|
| 282 | 3 | if (!$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
|
| 283 | throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
||
| 284 | |||
| 285 | } else { |
||
| 286 | 3 | return $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $this->createResolveInfo($field, [$fieldAst])); |
|
| 287 | } |
||
| 288 | |||
| 289 | } |
||
| 290 | |||
| 291 | 19 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
| 292 | 14 | return $contextValue[$fieldAst->getName()]; |
|
| 293 | 8 | } elseif (is_object($contextValue)) { |
|
| 294 | 6 | return TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
| 295 | 2 | } elseif (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 296 | 2 | $resolved = true; |
|
| 297 | } |
||
| 298 | |||
| 299 | 2 | if (!$resolverValue && !$resolved) { |
|
| 300 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
| 301 | } |
||
| 302 | |||
| 303 | 2 | return $resolverValue; |
|
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * @param $field AbstractField |
||
| 308 | * @param $query Query |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | 24 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @param $query Query|FragmentInterface |
||
| 326 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 327 | * @param $resolvedValue mixed |
||
| 328 | * @param $value array |
||
| 329 | * |
||
| 330 | * @throws \Exception |
||
| 331 | * |
||
| 332 | * @return array |
||
| 333 | */ |
||
| 334 | 20 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 391 | |||
| 392 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 396 | |||
| 397 | 21 | protected function getOutputValue(AbstractType $type, $value) |
|
| 401 | |||
| 402 | 28 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 408 | |||
| 409 | 27 | public function getResponseData() |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 426 | * |
||
| 427 | * @param int $max |
||
| 428 | */ |
||
| 429 | 1 | public function setMaxComplexity($max) |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 436 | * performing look-ahead query planning, etc. |
||
| 437 | * |
||
| 438 | * @param AbstractType $queryType |
||
| 439 | * @param AbstractType $mutationType |
||
| 440 | * @param AbstractQueryVisitor[] $reducers |
||
| 441 | */ |
||
| 442 | 27 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 453 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 454 | * |
||
| 455 | * @param Query $query |
||
| 456 | * @param AbstractType $currentLevelSchema |
||
| 457 | * @param AbstractQueryVisitor $reducer |
||
| 458 | */ |
||
| 459 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 489 | * tuple of (queryNode, schemaNode, childScore) |
||
| 490 | * |
||
| 491 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 492 | * |
||
| 493 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 494 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 495 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 496 | * |
||
| 497 | * @param Query|Field|FragmentInterface $queryNode |
||
| 498 | * @param AbstractField $currentLevelAST |
||
| 499 | * |
||
| 500 | * @return \Generator |
||
| 501 | */ |
||
| 502 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 555 | } |
||
| 556 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.