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 |
||
| 39 | class Processor |
||
| 40 | { |
||
| 41 | |||
| 42 | const TYPE_NAME_QUERY = '__typename'; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | protected $data; |
||
| 46 | |||
| 47 | /** @var ResolveValidatorInterface */ |
||
| 48 | protected $resolveValidator; |
||
| 49 | |||
| 50 | /** @var ExecutionContext */ |
||
| 51 | protected $executionContext; |
||
| 52 | |||
| 53 | /** @var int */ |
||
| 54 | protected $maxComplexity; |
||
| 55 | |||
| 56 | 29 | public function __construct(AbstractSchema $schema) |
|
| 57 | { |
||
| 58 | 29 | (new SchemaValidator())->validate($schema); |
|
| 59 | |||
| 60 | 28 | $this->introduceIntrospectionFields($schema); |
|
| 61 | 28 | $this->executionContext = new ExecutionContext(); |
|
| 62 | 28 | $this->executionContext->setSchema($schema); |
|
| 63 | |||
| 64 | 28 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 65 | 28 | } |
|
| 66 | |||
| 67 | |||
| 68 | 27 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 69 | { |
||
| 70 | 27 | if ($this->executionContext->hasErrors()) { |
|
| 71 | 7 | $this->executionContext->clearErrors(); |
|
| 72 | 7 | } |
|
| 73 | |||
| 74 | 27 | $this->data = []; |
|
| 75 | |||
| 76 | try { |
||
| 77 | 27 | $this->parseAndCreateRequest($payload, $variables); |
|
| 78 | |||
| 79 | 27 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 80 | 27 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 81 | |||
| 82 | 27 | if ($this->maxComplexity) { |
|
| 83 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 84 | 1 | } |
|
| 85 | |||
| 86 | 27 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 87 | |||
| 88 | 27 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 89 | 27 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 90 | 24 | $this->data = array_merge($this->data, $operationResult); |
|
| 91 | 24 | }; |
|
| 92 | 27 | } |
|
| 93 | |||
| 94 | 27 | } catch (\Exception $e) { |
|
| 95 | 4 | $this->executionContext->addError($e); |
|
| 96 | } |
||
| 97 | |||
| 98 | 27 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | 27 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param Query|Field $query |
||
| 114 | * @param AbstractObjectType $currentLevelSchema |
||
| 115 | * @return array|bool|mixed |
||
| 116 | */ |
||
| 117 | 27 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param Query $query |
||
| 136 | * @param AbstractField $field |
||
| 137 | * @param $contextValue |
||
| 138 | * @return array|mixed|null |
||
| 139 | */ |
||
| 140 | 24 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param Query|Mutation $query |
||
| 157 | * @param AbstractType $fieldType |
||
| 158 | * @param mixed $resolvedValue |
||
| 159 | * @return array|mixed |
||
| 160 | */ |
||
| 161 | 24 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 162 | { |
||
| 163 | 24 | if (is_null($resolvedValue)) { |
|
| 164 | 7 | return null; |
|
| 165 | } |
||
| 166 | |||
| 167 | 22 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
| 168 | 22 | $value = []; |
|
| 169 | |||
| 170 | 22 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 171 | 9 | if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
|
| 172 | |||
| 173 | 9 | $namedType = $fieldType->getNamedType(); |
|
| 174 | 9 | $isAbstract = TypeService::isAbstractType($namedType); |
|
| 175 | 9 | $validItemStructure = false; |
|
| 176 | |||
| 177 | 9 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|||
| 178 | 8 | $value[] = []; |
|
| 179 | 8 | $index = count($value) - 1; |
|
| 180 | |||
| 181 | 8 | if ($isAbstract) { |
|
| 182 | 2 | $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem); |
|
| 183 | 2 | } |
|
| 184 | |||
| 185 | 8 | if (!$validItemStructure) { |
|
| 186 | 8 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 187 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 188 | 1 | $value[$index] = null; |
|
| 189 | 1 | continue; |
|
| 190 | } |
||
| 191 | 7 | $validItemStructure = true; |
|
| 192 | 7 | } |
|
| 193 | |||
| 194 | 7 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 195 | 9 | } |
|
| 196 | 9 | } else { |
|
| 197 | 22 | if (!$query->hasFields()) { |
|
| 198 | 5 | if (TypeService::isObjectType($fieldType)) { |
|
| 199 | 1 | throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName())); |
|
| 200 | } |
||
| 201 | |||
| 202 | 4 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 203 | } |
||
| 204 | |||
| 205 | 20 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 206 | } |
||
| 207 | |||
| 208 | 20 | return $value; |
|
| 209 | } |
||
| 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 | 19 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 241 | |||
| 242 | 24 | protected function createResolveInfo($field, $fields) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param $contextValue |
||
| 249 | * @param FieldAst $fieldAst |
||
| 250 | * @param AbstractField $field |
||
| 251 | * |
||
| 252 | * @throws \Exception |
||
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | 19 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param $field AbstractField |
||
| 269 | * @param $query Query |
||
| 270 | * |
||
| 271 | * @return array |
||
| 272 | */ |
||
| 273 | 24 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 284 | |||
| 285 | /** |
||
| 286 | * @param $query Query|FragmentInterface |
||
| 287 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 288 | * @param $resolvedValue mixed |
||
| 289 | * @param $value array |
||
| 290 | * |
||
| 291 | * @throws \Exception |
||
| 292 | * |
||
| 293 | * @return array |
||
| 294 | */ |
||
| 295 | 20 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 352 | |||
| 353 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 357 | |||
| 358 | 21 | protected function getOutputValue(AbstractType $type, $value) |
|
| 362 | |||
| 363 | 28 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 369 | |||
| 370 | 27 | public function getResponseData() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 387 | * |
||
| 388 | * @param int $max |
||
| 389 | */ |
||
| 390 | 1 | public function setMaxComplexity($max) |
|
| 394 | |||
| 395 | /** |
||
| 396 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 397 | * performing look-ahead query planning, etc. |
||
| 398 | * |
||
| 399 | * @param AbstractType $queryType |
||
| 400 | * @param AbstractType $mutationType |
||
| 401 | * @param AbstractQueryVisitor[] $reducers |
||
| 402 | */ |
||
| 403 | 27 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 414 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 415 | * |
||
| 416 | * @param Query $query |
||
| 417 | * @param AbstractType $currentLevelSchema |
||
| 418 | * @param AbstractQueryVisitor $reducer |
||
| 419 | */ |
||
| 420 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 450 | * tuple of (queryNode, schemaNode, childScore) |
||
| 451 | * |
||
| 452 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 453 | * |
||
| 454 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 455 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 456 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 457 | * |
||
| 458 | * @param Query|Field|FragmentInterface $queryNode |
||
| 459 | * @param AbstractField $currentLevelAST |
||
| 460 | * |
||
| 461 | * @return \Generator |
||
| 462 | */ |
||
| 463 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 516 | } |
||
| 517 |
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.