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 | 28 | public function __construct(AbstractSchema $schema) |
|
| 58 | { |
||
| 59 | 28 | (new SchemaValidator())->validate($schema); |
|
| 60 | |||
| 61 | 27 | $this->introduceIntrospectionFields($schema); |
|
| 62 | 27 | $this->executionContext = new ExecutionContext(); |
|
| 63 | 27 | $this->executionContext->setSchema($schema); |
|
| 64 | |||
| 65 | 27 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 66 | 27 | } |
|
| 67 | |||
| 68 | |||
| 69 | 26 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 70 | { |
||
| 71 | 26 | if ($this->executionContext->hasErrors()) { |
|
| 72 | 6 | $this->executionContext->clearErrors(); |
|
| 73 | 6 | } |
|
| 74 | |||
| 75 | 26 | $this->data = []; |
|
| 76 | |||
| 77 | try { |
||
| 78 | 26 | $this->parseAndCreateRequest($payload, $variables); |
|
| 79 | |||
| 80 | 26 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 81 | 26 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 82 | |||
| 83 | 26 | if ($this->maxComplexity) { |
|
| 84 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 85 | 1 | } |
|
| 86 | |||
| 87 | 26 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 88 | |||
| 89 | 26 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 90 | 26 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 91 | 23 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | 23 | }; |
|
| 93 | 26 | } |
|
| 94 | |||
| 95 | 26 | } catch (\Exception $e) { |
|
| 96 | 4 | $this->executionContext->addError($e); |
|
| 97 | } |
||
| 98 | |||
| 99 | 26 | return $this; |
|
| 100 | } |
||
| 101 | |||
| 102 | 26 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param Query|Field $query |
||
| 115 | * @param AbstractObjectType $currentLevelSchema |
||
| 116 | * @return array|bool|mixed |
||
| 117 | */ |
||
| 118 | 26 | 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 | 23 | 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 | 23 | 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) |
|
| 218 | { |
||
| 219 | 19 | $value = null; |
|
| 220 | 19 | $fieldType = $field->getType(); |
|
| 221 | 19 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 222 | |||
| 223 | 19 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 224 | 1 | $listValue = []; |
|
| 225 | 1 | $type = $fieldType->getNamedType(); |
|
| 226 | |||
| 227 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 228 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
| 229 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $field->getName()))); |
||
| 230 | |||
| 231 | $listValue = null; |
||
| 232 | break; |
||
| 233 | } |
||
| 234 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
| 235 | 1 | } |
|
| 236 | |||
| 237 | 1 | $value = $listValue; |
|
| 238 | 1 | } else { |
|
| 239 | 19 | $value = $preResolvedValue; |
|
| 240 | } |
||
| 241 | |||
| 242 | 19 | return $value; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @param AbstractField $field |
||
| 247 | * @param mixed $contextValue |
||
| 248 | * @param Query $query |
||
| 249 | * |
||
| 250 | * @return mixed |
||
| 251 | */ |
||
| 252 | 23 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
| 253 | { |
||
| 254 | 23 | if ($resolveFunc = $field->getConfig()->getResolveFunction()) { |
|
| 255 | 21 | 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 | 23 | protected function createResolveInfo($field, $fields) { |
|
| 264 | 23 | 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 | 2 | } |
|
| 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 | 23 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 313 | { |
||
| 314 | 23 | $args = []; |
|
| 315 | 23 | foreach ($query->getArguments() as $argument) { |
|
| 316 | 14 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 317 | 14 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 318 | 14 | } |
|
| 319 | 23 | } |
|
| 320 | |||
| 321 | 23 | return $args; |
|
| 322 | } |
||
| 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) |
|
| 335 | { |
||
| 336 | 20 | $currentType = $queryType->getNullableType(); |
|
| 337 | |||
| 338 | 20 | View Code Duplication | if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
| 339 | 1 | if (!$query->hasFields()) { |
|
| 340 | 1 | return $this->getOutputValue($currentType, $resolvedValue); |
|
| 341 | } else { |
||
| 342 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
| 343 | |||
| 344 | 1 | return null; |
|
| 345 | } |
||
| 346 | } |
||
| 347 | |||
| 348 | 20 | foreach ($query->getFields() as $fieldAst) { |
|
| 349 | |||
| 350 | 20 | if ($fieldAst instanceof FragmentInterface) { |
|
| 351 | /** @var TypedFragmentReference $fragment */ |
||
| 352 | 3 | $fragment = $fieldAst; |
|
| 353 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 354 | /** @var Fragment $fragment */ |
||
| 355 | 2 | $fieldAstName = $fieldAst->getName(); |
|
| 356 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
|
| 357 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
| 358 | 3 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 359 | 1 | continue; |
|
| 360 | } |
||
| 361 | |||
| 362 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 363 | 3 | $value = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 364 | 3 | } else { |
|
| 365 | 20 | $fieldAstName = $fieldAst->getName(); |
|
| 366 | 20 | $alias = $fieldAst->getAlias() ?: $fieldAstName; |
|
| 367 | |||
| 368 | 20 | if ($fieldAstName == self::TYPE_NAME_QUERY) { |
|
| 369 | 1 | $value[$alias] = $queryType->getName(); |
|
| 370 | 1 | } else { |
|
| 371 | 20 | $field = $currentType->getField($fieldAstName); |
|
| 372 | 20 | if (!$field) { |
|
| 373 | 3 | $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
|
| 374 | |||
| 375 | 3 | return null; |
|
| 376 | } |
||
| 377 | 20 | if ($fieldAst instanceof Query) { |
|
| 378 | 10 | $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
|
| 379 | 20 | } elseif ($fieldAst instanceof FieldAst) { |
|
| 380 | 19 | $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
|
| 381 | 19 | } else { |
|
| 382 | return $value; |
||
| 383 | } |
||
| 384 | } |
||
| 385 | } |
||
| 386 | |||
| 387 | 20 | } |
|
| 388 | |||
| 389 | 20 | return $value; |
|
| 390 | } |
||
| 391 | |||
| 392 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 396 | |||
| 397 | 4 | protected function getOutputValue(AbstractType $type, $value) |
|
| 401 | |||
| 402 | 27 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 408 | |||
| 409 | 26 | public function getResponseData() |
|
| 410 | { |
||
| 411 | 26 | $result = []; |
|
| 412 | |||
| 413 | 26 | if (!empty($this->data)) { |
|
| 414 | 23 | $result['data'] = $this->data; |
|
| 415 | 23 | } |
|
| 416 | |||
| 417 | 26 | if ($this->executionContext->hasErrors()) { |
|
| 418 | 9 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 419 | 9 | } |
|
| 420 | |||
| 421 | 26 | return $result; |
|
| 422 | } |
||
| 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 | 26 | 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.