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 | 7 | } |
|
| 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 | 1 | } |
|
| 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 | 24 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | 24 | }; |
|
| 93 | 27 | } |
|
| 94 | |||
| 95 | 27 | } 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) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param FieldAst $fieldAst |
||
| 214 | * @param AbstractField $field |
||
| 215 | * |
||
| 216 | * @param mixed $contextValue |
||
| 217 | * @return array|mixed|null |
||
| 218 | * @throws ResolveException |
||
| 219 | * @throws \Exception |
||
| 220 | 19 | */ |
|
| 221 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param AbstractField $field |
||
| 245 | * @param mixed $contextValue |
||
| 246 | * @param Query $query |
||
| 247 | * |
||
| 248 | * @return mixed |
||
| 249 | 24 | */ |
|
| 250 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
||
| 260 | 24 | ||
| 261 | 24 | protected function createResolveInfo($field, $fields) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param $contextValue |
||
| 268 | * @param FieldAst $fieldAst |
||
| 269 | * @param AbstractField $field |
||
| 270 | * |
||
| 271 | * @throws \Exception |
||
| 272 | * |
||
| 273 | 19 | * @return mixed |
|
| 274 | */ |
||
| 275 | 19 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param $field AbstractField |
||
| 287 | * @param $query Query |
||
| 288 | 19 | * |
|
| 289 | 14 | * @return array |
|
| 290 | 8 | */ |
|
| 291 | 6 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @param $query Query|FragmentInterface |
||
| 305 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 306 | * @param $resolvedValue mixed |
||
| 307 | * @param $value array |
||
| 308 | * |
||
| 309 | 24 | * @throws \Exception |
|
| 310 | * |
||
| 311 | 24 | * @return array |
|
| 312 | 24 | */ |
|
| 313 | 15 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 370 | 3 | ||
| 371 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 375 | 10 | ||
| 376 | 20 | protected function getOutputValue(AbstractType $type, $value) |
|
| 380 | |||
| 381 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
||
| 387 | |||
| 388 | public function getResponseData() |
||
| 402 | 28 | ||
| 403 | 28 | /** |
|
| 404 | 28 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
|
| 405 | * |
||
| 406 | 27 | * @param int $max |
|
| 407 | */ |
||
| 408 | 27 | public function setMaxComplexity($max) |
|
| 412 | 24 | ||
| 413 | /** |
||
| 414 | 27 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
|
| 415 | 10 | * performing look-ahead query planning, etc. |
|
| 416 | 10 | * |
|
| 417 | * @param AbstractType $queryType |
||
| 418 | 27 | * @param AbstractType $mutationType |
|
| 419 | * @param AbstractQueryVisitor[] $reducers |
||
| 420 | */ |
||
| 421 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
||
| 429 | 1 | ||
| 430 | /** |
||
| 431 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 432 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 433 | * |
||
| 434 | * @param Query $query |
||
| 435 | * @param AbstractType $currentLevelSchema |
||
| 436 | * @param AbstractQueryVisitor $reducer |
||
| 437 | */ |
||
| 438 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
||
| 465 | |||
| 466 | 2 | /** |
|
| 467 | 2 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
|
| 468 | 2 | * tuple of (queryNode, schemaNode, childScore) |
|
| 469 | * |
||
| 470 | 2 | * childScore costs are accumulated via values sent into the coroutine. |
|
| 471 | * |
||
| 472 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 473 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 474 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 475 | * |
||
| 476 | 2 | * @param Query|Field|FragmentInterface $queryNode |
|
| 477 | 2 | * @param AbstractField $currentLevelAST |
|
| 478 | 2 | * |
|
| 479 | 2 | * @return \Generator |
|
| 480 | 2 | */ |
|
| 481 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 534 | } |
||
| 535 |
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.