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 | 28 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 70 | { |
||
| 71 | 28 | if ($this->executionContext->hasErrors()) { |
|
| 72 | 7 | $this->executionContext->clearErrors(); |
|
| 73 | } |
||
| 74 | |||
| 75 | 28 | $this->data = []; |
|
| 76 | |||
| 77 | try { |
||
| 78 | 28 | $this->parseAndCreateRequest($payload, $variables); |
|
| 79 | |||
| 80 | 28 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 81 | 28 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 82 | |||
| 83 | 28 | if ($this->maxComplexity) { |
|
| 84 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 85 | } |
||
| 86 | |||
| 87 | 28 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 88 | |||
| 89 | 28 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 90 | 28 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 91 | 28 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | }; |
||
| 93 | } |
||
| 94 | |||
| 95 | 4 | } catch (\Exception $e) { |
|
| 96 | 4 | $this->executionContext->addError($e); |
|
| 97 | } |
||
| 98 | |||
| 99 | 28 | return $this; |
|
| 100 | } |
||
| 101 | |||
| 102 | 28 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param Query|Field $query |
||
| 115 | * @param AbstractObjectType $currentLevelSchema |
||
| 116 | * @return array|bool|mixed |
||
| 117 | */ |
||
| 118 | 28 | 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 | 25 | 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 | 25 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @param FieldAst $fieldAst |
||
| 213 | * @param AbstractField $field |
||
| 214 | * |
||
| 215 | * @param mixed $contextValue |
||
| 216 | * @return array|mixed|null |
||
| 217 | * @throws ResolveException |
||
| 218 | * @throws \Exception |
||
| 219 | */ |
||
| 220 | 20 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 241 | |||
| 242 | 25 | protected function createResolveInfo($field, $fields) { |
|
| 245 | |||
| 246 | /** |
||
| 247 | * @param $contextValue |
||
| 248 | * @param FieldAst $fieldAst |
||
| 249 | * @param AbstractField $field |
||
| 250 | * |
||
| 251 | * @throws \Exception |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | 20 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param $field AbstractField |
||
| 267 | * @param $query Query |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 25 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * @param $query Query|FragmentInterface |
||
| 285 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 286 | * @param $resolvedValue mixed |
||
| 287 | * @param $value array |
||
| 288 | * |
||
| 289 | * @throws \Exception |
||
| 290 | * |
||
| 291 | * @return array |
||
| 292 | */ |
||
| 293 | 21 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 350 | |||
| 351 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 355 | |||
| 356 | 22 | protected function getOutputValue(AbstractType $type, $value) |
|
| 360 | |||
| 361 | 28 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 367 | |||
| 368 | 28 | public function getResponseData() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 385 | * |
||
| 386 | * @param int $max |
||
| 387 | */ |
||
| 388 | 1 | public function setMaxComplexity($max) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 395 | * performing look-ahead query planning, etc. |
||
| 396 | * |
||
| 397 | * @param AbstractType $queryType |
||
| 398 | * @param AbstractType $mutationType |
||
| 399 | * @param AbstractQueryVisitor[] $reducers |
||
| 400 | */ |
||
| 401 | 28 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 409 | |||
| 410 | /** |
||
| 411 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 412 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 413 | * |
||
| 414 | * @param Query $query |
||
| 415 | * @param AbstractType $currentLevelSchema |
||
| 416 | * @param AbstractQueryVisitor $reducer |
||
| 417 | */ |
||
| 418 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 448 | * tuple of (queryNode, schemaNode, childScore) |
||
| 449 | * |
||
| 450 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 451 | * |
||
| 452 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 453 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 454 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 455 | * |
||
| 456 | * @param Query|Field|FragmentInterface $queryNode |
||
| 457 | * @param AbstractField $currentLevelAST |
||
| 458 | * |
||
| 459 | * @return \Generator |
||
| 460 | */ |
||
| 461 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 514 | } |
||
| 515 |
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.