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 |
||
| 37 | class Processor |
||
| 38 | { |
||
| 39 | |||
| 40 | const TYPE_NAME_QUERY = '__typename'; |
||
| 41 | |||
| 42 | /** @var array */ |
||
| 43 | protected $data; |
||
| 44 | |||
| 45 | /** @var ResolveValidatorInterface */ |
||
| 46 | protected $resolveValidator; |
||
| 47 | |||
| 48 | /** @var ExecutionContext */ |
||
| 49 | protected $executionContext; |
||
| 50 | |||
| 51 | /** @var int */ |
||
| 52 | protected $maxComplexity; |
||
| 53 | |||
| 54 | 30 | public function __construct(AbstractSchema $schema) |
|
| 55 | { |
||
| 56 | 30 | if (empty($this->executionContext)) { |
|
| 57 | 30 | $this->executionContext = new ExecutionContext($schema); |
|
| 58 | 30 | $this->executionContext->setContainer(new Container()); |
|
| 59 | } |
||
| 60 | 30 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 61 | 30 | } |
|
| 62 | |||
| 63 | 28 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 64 | { |
||
| 65 | 28 | $this->data = []; |
|
| 66 | |||
| 67 | try { |
||
| 68 | 28 | $this->parseAndCreateRequest($payload, $variables); |
|
| 69 | |||
| 70 | 28 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 71 | 28 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 72 | |||
| 73 | 28 | if ($this->maxComplexity) { |
|
| 74 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 75 | } |
||
| 76 | |||
| 77 | 28 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 78 | |||
| 79 | 28 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 80 | 28 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 81 | 28 | $this->data = array_merge($this->data, $operationResult); |
|
| 82 | }; |
||
| 83 | } |
||
| 84 | |||
| 85 | 4 | } catch (\Exception $e) { |
|
| 86 | 4 | $this->executionContext->addError($e); |
|
| 87 | } |
||
| 88 | |||
| 89 | 28 | return $this; |
|
| 90 | } |
||
| 91 | |||
| 92 | 28 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 102 | |||
| 103 | /** |
||
| 104 | * @param Query|Field $query |
||
| 105 | * @param AbstractObjectType $currentLevelSchema |
||
| 106 | * @return array|bool|mixed |
||
| 107 | */ |
||
| 108 | 28 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 109 | { |
||
| 110 | 28 | if (!$this->resolveValidator->objectHasField($currentLevelSchema, $query)) { |
|
| 111 | 1 | return null; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** @var AbstractField $field */ |
||
| 115 | 28 | $operationField = $currentLevelSchema->getField($query->getName()); |
|
| 116 | 28 | $alias = $query->getAlias() ?: $query->getName(); |
|
| 117 | |||
| 118 | 28 | if (!$this->resolveValidator->validateArguments($operationField, $query, $this->executionContext->getRequest())) { |
|
| 119 | 6 | return null; |
|
| 120 | } |
||
| 121 | |||
| 122 | 25 | return [$alias => $this->processQueryAST($query, $operationField)]; |
|
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param Query $query |
||
| 127 | * @param AbstractField $field |
||
| 128 | * @param $contextValue |
||
| 129 | * @return array|mixed|null |
||
| 130 | */ |
||
| 131 | 25 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param Query|Mutation $query |
||
| 148 | * @param AbstractType $fieldType |
||
| 149 | * @param mixed $resolvedValue |
||
| 150 | * @return array|mixed |
||
| 151 | * @throws ResolveException |
||
| 152 | */ |
||
| 153 | 25 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 154 | { |
||
| 155 | 25 | if (is_null($resolvedValue)) { |
|
| 156 | 7 | return null; |
|
| 157 | } |
||
| 158 | |||
| 159 | 23 | $value = []; |
|
| 160 | |||
| 161 | 23 | if (!$query->hasFields()) { |
|
| 162 | 6 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
| 163 | |||
| 164 | 6 | if (TypeService::isObjectType($fieldType->getNamedType())) { |
|
| 165 | 1 | throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName())); |
|
| 166 | } |
||
| 167 | 5 | if (TypeService::isScalarType($fieldType)) { |
|
| 168 | 5 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | 20 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 173 | 9 | if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
|
| 174 | |||
| 175 | 9 | $namedType = $fieldType->getNamedType(); |
|
| 176 | 9 | $isAbstract = TypeService::isAbstractType($namedType); |
|
| 177 | 9 | $validItemStructure = false; |
|
| 178 | |||
| 179 | 9 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|||
| 180 | 8 | $value[] = []; |
|
| 181 | 8 | $index = count($value) - 1; |
|
| 182 | |||
| 183 | 8 | if ($isAbstract) { |
|
| 184 | 2 | $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem); |
|
| 185 | } |
||
| 186 | |||
| 187 | 8 | if (!$validItemStructure) { |
|
| 188 | 8 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 189 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 190 | 1 | $value[$index] = null; |
|
| 191 | 1 | continue; |
|
| 192 | } |
||
| 193 | 7 | $validItemStructure = true; |
|
| 194 | } |
||
| 195 | |||
| 196 | 9 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 197 | } |
||
| 198 | } else { |
||
| 199 | 20 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 200 | } |
||
| 201 | |||
| 202 | 20 | return $value; |
|
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @param FieldAst $fieldAst |
||
| 207 | * @param AbstractField $field |
||
| 208 | * |
||
| 209 | * @param mixed $contextValue |
||
| 210 | * @return array|mixed|null |
||
| 211 | * @throws ResolveException |
||
| 212 | * @throws \Exception |
||
| 213 | */ |
||
| 214 | 19 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 215 | { |
||
| 216 | 19 | $value = null; |
|
| 217 | 19 | $fieldType = $field->getType(); |
|
| 218 | 19 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 219 | |||
| 220 | 19 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 221 | 1 | $listValue = []; |
|
| 222 | 1 | $type = $fieldType->getNamedType(); |
|
| 223 | |||
| 224 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 225 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
| 226 | } |
||
| 227 | |||
| 228 | 1 | $value = $listValue; |
|
| 229 | } else { |
||
| 230 | 19 | $value = $this->getOutputValue($fieldType, $preResolvedValue); |
|
| 231 | } |
||
| 232 | |||
| 233 | 19 | return $value; |
|
| 234 | } |
||
| 235 | |||
| 236 | 25 | protected function createResolveInfo($field, $fields) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @param $contextValue |
||
| 243 | * @param FieldAst $fieldAst |
||
| 244 | * @param AbstractField $field |
||
| 245 | * |
||
| 246 | * @throws \Exception |
||
| 247 | * |
||
| 248 | * @return mixed |
||
| 249 | */ |
||
| 250 | 19 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 259 | |||
| 260 | 25 | protected function resolveFieldValue(AbstractField $field, $contextValue, array $fields, array $args) |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @param $field AbstractField |
||
| 267 | * @param $query Query |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 25 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 272 | { |
||
| 273 | 25 | $args = []; |
|
| 274 | 25 | foreach ($query->getArguments() as $argument) { |
|
| 275 | 15 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 276 | 15 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 277 | } |
||
| 278 | } |
||
| 279 | |||
| 280 | 25 | return $args; |
|
| 281 | } |
||
| 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 | 20 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 294 | { |
||
| 295 | 20 | $originalType = $queryType; |
|
| 296 | 20 | $queryType = $this->resolveValidator->resolveTypeIfAbstract($queryType, $resolvedValue); |
|
| 297 | 20 | $currentType = $queryType->getNullableType(); |
|
| 298 | |||
| 299 | |||
| 300 | 20 | View Code Duplication | if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
| 301 | 1 | if (!$query->hasFields()) { |
|
| 302 | 1 | return $this->getOutputValue($currentType, $resolvedValue); |
|
| 303 | } else { |
||
| 304 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
| 305 | |||
| 306 | 1 | return null; |
|
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | 20 | foreach ($query->getFields() as $fieldAst) { |
|
| 311 | |||
| 312 | 20 | if ($fieldAst instanceof FragmentInterface) { |
|
| 313 | /** @var TypedFragmentReference $fragment */ |
||
| 314 | 3 | $fragment = $fieldAst; |
|
| 315 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 316 | /** @var Fragment $fragment */ |
||
| 317 | 2 | $fieldAstName = $fieldAst->getName(); |
|
| 318 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
|
| 319 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $originalType); |
|
| 320 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 321 | 1 | continue; |
|
| 322 | } |
||
| 323 | |||
| 324 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 325 | 3 | $value = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 326 | } else { |
||
| 327 | 20 | $fieldAstName = $fieldAst->getName(); |
|
| 328 | 20 | $alias = $fieldAst->getAlias() ?: $fieldAstName; |
|
| 329 | |||
| 330 | 20 | if ($fieldAstName == self::TYPE_NAME_QUERY) { |
|
| 331 | 1 | $value[$alias] = $queryType->getName(); |
|
| 332 | } else { |
||
| 333 | 20 | $field = $currentType->getField($fieldAstName); |
|
| 334 | 20 | if (!$field) { |
|
| 335 | 3 | $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
|
| 336 | |||
| 337 | 3 | return null; |
|
| 338 | } |
||
| 339 | 20 | if ($fieldAst instanceof Query) { |
|
| 340 | 10 | $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
|
| 341 | } elseif ($fieldAst instanceof FieldAst) { |
||
| 342 | 19 | $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
|
| 343 | } else { |
||
| 344 | 20 | return $value; |
|
| 345 | } |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | 20 | return $value; |
|
| 352 | } |
||
| 353 | |||
| 354 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 358 | |||
| 359 | 22 | protected function getOutputValue(AbstractType $type, $value) |
|
| 363 | |||
| 364 | 28 | public function getResponseData() |
|
| 365 | { |
||
| 366 | 28 | $result = []; |
|
| 367 | |||
| 368 | 28 | if (!empty($this->data)) { |
|
| 369 | 25 | $result['data'] = $this->data; |
|
| 370 | } |
||
| 371 | |||
| 372 | 28 | if ($this->executionContext->hasErrors()) { |
|
| 373 | 10 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 374 | } |
||
| 375 | |||
| 376 | 28 | return $result; |
|
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * You can access ExecutionContext to check errors and inject dependencies |
||
| 381 | * |
||
| 382 | * @return ExecutionContext |
||
| 383 | */ |
||
| 384 | 9 | public function getExecutionContext() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 391 | * |
||
| 392 | * @param int $max |
||
| 393 | */ |
||
| 394 | 1 | public function setMaxComplexity($max) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 401 | * performing look-ahead query planning, etc. |
||
| 402 | * |
||
| 403 | * @param AbstractType $queryType |
||
| 404 | * @param AbstractType $mutationType |
||
| 405 | * @param AbstractQueryVisitor[] $reducers |
||
| 406 | */ |
||
| 407 | 28 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 418 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 419 | * |
||
| 420 | * @param Query $query |
||
| 421 | * @param AbstractType $currentLevelSchema |
||
| 422 | * @param AbstractQueryVisitor $reducer |
||
| 423 | */ |
||
| 424 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 454 | * tuple of (queryNode, schemaNode, childScore) |
||
| 455 | * |
||
| 456 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 457 | * |
||
| 458 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 459 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 460 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 461 | * |
||
| 462 | * @param Query|Field|FragmentInterface $queryNode |
||
| 463 | * @param AbstractField $currentLevelAST |
||
| 464 | * |
||
| 465 | * @return \Generator |
||
| 466 | */ |
||
| 467 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 520 | } |
||
| 521 |
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.