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 | 26 | public function __construct(AbstractSchema $schema) |
|
| 58 | { |
||
| 59 | 26 | (new SchemaValidator())->validate($schema); |
|
| 60 | |||
| 61 | 25 | $this->introduceIntrospectionFields($schema); |
|
| 62 | 25 | $this->executionContext = new ExecutionContext(); |
|
| 63 | 25 | $this->executionContext->setSchema($schema); |
|
| 64 | |||
| 65 | 25 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 66 | 25 | } |
|
| 67 | |||
| 68 | |||
| 69 | 25 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 70 | { |
||
| 71 | 25 | if ($this->executionContext->hasErrors()) { |
|
| 72 | 5 | $this->executionContext->clearErrors(); |
|
| 73 | } |
||
| 74 | |||
| 75 | 25 | $this->data = []; |
|
| 76 | |||
| 77 | try { |
||
| 78 | 25 | $this->parseAndCreateRequest($payload, $variables); |
|
| 79 | |||
| 80 | 25 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 81 | 25 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 82 | |||
| 83 | 25 | if ($this->maxComplexity) { |
|
| 84 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 85 | } |
||
| 86 | |||
| 87 | 25 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 88 | |||
| 89 | 25 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 90 | 25 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 91 | 25 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | }; |
||
| 93 | } |
||
| 94 | |||
| 95 | 4 | } catch (\Exception $e) { |
|
| 96 | 4 | $this->executionContext->addError($e); |
|
| 97 | } |
||
| 98 | |||
| 99 | 25 | return $this; |
|
| 100 | } |
||
| 101 | |||
| 102 | 25 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 103 | { |
||
| 104 | 25 | if (empty($payload)) { |
|
| 105 | 1 | throw new \Exception('Must provide an operation.'); |
|
| 106 | } |
||
| 107 | 25 | $parser = new Parser(); |
|
| 108 | |||
| 109 | 25 | $data = $parser->parse($payload); |
|
| 110 | 25 | $this->executionContext->setRequest(new Request($data, $variables)); |
|
| 111 | 25 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @param Query|Field $query |
||
| 115 | * @param AbstractObjectType $currentLevelSchema |
||
| 116 | * @return array|bool|mixed |
||
| 117 | */ |
||
| 118 | 25 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 119 | { |
||
| 120 | 25 | if (!$this->resolveValidator->objectHasField($currentLevelSchema, $query)) { |
|
| 121 | 1 | return null; |
|
| 122 | } |
||
| 123 | |||
| 124 | /** @var AbstractField $field */ |
||
| 125 | 25 | $operationField = $currentLevelSchema->getField($query->getName()); |
|
| 126 | 25 | $alias = $query->getAlias() ?: $query->getName(); |
|
| 127 | |||
| 128 | 25 | if (!$this->resolveValidator->validateArguments($operationField, $query, $this->executionContext->getRequest())) { |
|
| 129 | 3 | return null; |
|
| 130 | } |
||
| 131 | |||
| 132 | 23 | return [$alias => $this->processQueryAST($query, $operationField)]; |
|
| 133 | } |
||
| 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) |
|
| 142 | { |
||
| 143 | 23 | if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) { |
|
| 144 | return null; |
||
| 145 | } |
||
| 146 | |||
| 147 | 23 | $resolvedValue = $this->resolveFieldValue($field, $contextValue, $query); |
|
| 148 | |||
| 149 | 23 | if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) { |
|
| 150 | 2 | return null; |
|
| 151 | } |
||
| 152 | |||
| 153 | 23 | return $this->collectValueForQueryWithType($query, $field->getType(), $resolvedValue); |
|
| 154 | } |
||
| 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) |
|
| 163 | { |
||
| 164 | 23 | if (is_null($resolvedValue)) { |
|
| 165 | 7 | return null; |
|
| 166 | } |
||
| 167 | |||
| 168 | 21 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
| 169 | 21 | $value = []; |
|
| 170 | |||
| 171 | 21 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 172 | 10 | if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
|
| 173 | |||
| 174 | 10 | $namedType = $fieldType->getNamedType(); |
|
| 175 | 10 | $isAbstract = TypeService::isAbstractType($namedType); |
|
| 176 | 10 | $validItemStructure = false; |
|
| 177 | |||
| 178 | 10 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|||
| 179 | 9 | $value[] = []; |
|
| 180 | 9 | $index = count($value) - 1; |
|
| 181 | |||
| 182 | 9 | if ($isAbstract) { |
|
| 183 | 2 | $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem); |
|
| 184 | } |
||
| 185 | |||
| 186 | 9 | if (!$validItemStructure) { |
|
| 187 | 9 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 188 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 189 | 1 | $value[$index] = null; |
|
| 190 | 1 | continue; |
|
| 191 | } |
||
| 192 | 8 | $validItemStructure = true; |
|
| 193 | } |
||
| 194 | |||
| 195 | 10 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 196 | } |
||
| 197 | } else { |
||
| 198 | 20 | if (!$query->hasFields()) { |
|
| 199 | 2 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 200 | } |
||
| 201 | |||
| 202 | 20 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 203 | } |
||
| 204 | |||
| 205 | 21 | return $value; |
|
| 206 | } |
||
| 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 | 20 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 218 | { |
||
| 219 | 20 | $value = null; |
|
| 220 | 20 | $fieldType = $field->getType(); |
|
| 221 | 20 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 222 | |||
| 223 | 20 | 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 | } |
||
| 236 | |||
| 237 | 1 | $value = $listValue; |
|
| 238 | } else { |
||
| 239 | 20 | $value = $this->getOutputValue($field->getType(), $preResolvedValue);//$this->getFieldValidatedValue($field, $preResolvedValue); |
|
| 240 | } |
||
| 241 | |||
| 242 | 20 | 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 | $resolveInfo = new ResolveInfo($field, $query->getFields(), $field->getType(), $this->executionContext); |
|
| 255 | |||
| 256 | 23 | if ($resolveFunc = $field->getConfig()->getResolveFunction()) { |
|
| 257 | 21 | return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo); |
|
| 258 | 10 | } elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) { |
|
| 259 | 2 | return $propertyValue; |
|
| 260 | } else { |
||
| 261 | 8 | return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo); |
|
| 262 | } |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param $contextValue |
||
| 267 | * @param FieldAst $fieldAst |
||
| 268 | * @param AbstractField $field |
||
| 269 | * |
||
| 270 | * @throws \Exception |
||
| 271 | * |
||
| 272 | * @return mixed |
||
| 273 | */ |
||
| 274 | 20 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 275 | { |
||
| 276 | 20 | $resolved = false; |
|
| 277 | 20 | $resolverValue = null; |
|
| 278 | |||
| 279 | 20 | if ($resolveFunction = $field->getConfig()->getResolveFunction()) { |
|
| 280 | 3 | $resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext); |
|
| 281 | |||
| 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 | $resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo); |
|
| 287 | 3 | $resolved = true; |
|
| 288 | } |
||
| 289 | |||
| 290 | } |
||
| 291 | |||
| 292 | 20 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
| 293 | 16 | $resolverValue = $contextValue[$fieldAst->getName()]; |
|
| 294 | 16 | $resolved = true; |
|
| 295 | 9 | } elseif (is_object($contextValue)) { |
|
| 296 | 6 | $resolverValue = TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
| 297 | 6 | $resolved = true; |
|
| 298 | 3 | } elseif (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 299 | 2 | $resolved = true; |
|
| 300 | } |
||
| 301 | |||
| 302 | 20 | if (!$resolverValue && !$resolved) { |
|
| 303 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
| 304 | } |
||
| 305 | |||
| 306 | 20 | return $resolverValue; |
|
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param $field AbstractField |
||
| 311 | * @param $query Query |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | 23 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 316 | { |
||
| 317 | 23 | $args = []; |
|
| 318 | 23 | foreach ($query->getArguments() as $argument) { |
|
| 319 | 9 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 320 | 9 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 321 | } |
||
| 322 | } |
||
| 323 | |||
| 324 | 23 | return $args; |
|
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * @param $query Query|FragmentInterface |
||
| 329 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 330 | * @param $resolvedValue mixed |
||
| 331 | * @param $value array |
||
| 332 | * |
||
| 333 | * @throws \Exception |
||
| 334 | * |
||
| 335 | * @return array |
||
| 336 | */ |
||
| 337 | 21 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 338 | { |
||
| 339 | 21 | $currentType = $queryType->getNullableType(); |
|
| 340 | |||
| 341 | 21 | View Code Duplication | if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
| 342 | 1 | if (!$query->hasFields()) { |
|
| 343 | 1 | return $this->getOutputValue($currentType, $resolvedValue); |
|
| 344 | } else { |
||
| 345 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
| 346 | 1 | return null; |
|
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | 21 | foreach ($query->getFields() as $fieldAst) { |
|
| 351 | |||
| 352 | 21 | if ($fieldAst instanceof FragmentInterface) { |
|
| 353 | /** @var TypedFragmentReference $fragment */ |
||
| 354 | 3 | $fragment = $fieldAst; |
|
| 355 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 356 | /** @var Fragment $fragment */ |
||
| 357 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAst->getName()); |
|
| 358 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
| 359 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 360 | 1 | continue; |
|
| 361 | } |
||
| 362 | |||
| 363 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 364 | 3 | $value = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 365 | } else { |
||
| 366 | 21 | $alias = $fieldAst->getAlias() ?: $fieldAst->getName(); |
|
| 367 | |||
| 368 | 21 | if ($fieldAst->getName() == self::TYPE_NAME_QUERY) { |
|
| 369 | 1 | $value[$alias] = $queryType->getName(); |
|
| 370 | } else { |
||
| 371 | 21 | $queryAst = $currentType->getField($fieldAst->getName()); |
|
| 372 | 21 | if (!$queryAst) { |
|
| 373 | 3 | $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAst->getName(), $currentType->getName()))); |
|
| 374 | |||
| 375 | 3 | return null; |
|
| 376 | } |
||
| 377 | |||
| 378 | 21 | if ($fieldAst instanceof Query) { |
|
| 379 | 11 | $value[$alias] = $this->processQueryAST($fieldAst, $queryAst, $resolvedValue); |
|
| 380 | } elseif ($fieldAst instanceof FieldAst) { |
||
| 381 | 20 | $value[$alias] = $this->processFieldAST($fieldAst, $queryAst, $resolvedValue); |
|
| 382 | } else { |
||
| 383 | 21 | return $value; |
|
| 384 | } |
||
| 385 | } |
||
| 386 | } |
||
| 387 | |||
| 388 | } |
||
| 389 | |||
| 390 | 21 | return $value; |
|
| 391 | } |
||
| 392 | |||
| 393 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 394 | { |
||
| 395 | return ($this->resolveValidator->isValidValueForField($field, $value)) ? $this->getOutputValue($field->getType(), $value) : null; |
||
| 396 | } |
||
| 397 | |||
| 398 | 20 | protected function getOutputValue(AbstractType $type, $value) |
|
| 399 | { |
||
| 400 | 20 | return in_array($type->getKind(), [TypeMap::KIND_OBJECT, TypeMap::KIND_NON_NULL]) ? $value : $type->serialize($value); |
|
| 401 | } |
||
| 402 | |||
| 403 | 25 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 411 | |||
| 412 | 25 | public function getResponseData() |
|
| 413 | { |
||
| 414 | 25 | $result = []; |
|
| 415 | |||
| 416 | 25 | if (!empty($this->data)) { |
|
| 417 | 23 | $result['data'] = $this->data; |
|
| 418 | } |
||
| 419 | |||
| 420 | 25 | if ($this->executionContext->hasErrors()) { |
|
| 421 | 7 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 422 | } |
||
| 423 | |||
| 424 | 25 | return $result; |
|
| 425 | } |
||
| 426 | |||
| 427 | 1 | public function setMaxComplexity($max) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * @param AbstractType $queryType |
||
| 434 | * @param AbstractType $mutationType |
||
| 435 | * @param array $reducers |
||
| 436 | */ |
||
| 437 | 25 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * @param Query $query |
||
| 448 | * @param AbstractType $currentLevelSchema |
||
| 449 | * @param AbstractQueryVisitor $reducer |
||
| 450 | */ |
||
| 451 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Coroutine to walk the query and schema in DFS manner and yield a tuple of (queryNode, schemaNode, childScore) |
||
| 481 | * |
||
| 482 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 483 | * |
||
| 484 | * @param Query|Field|FragmentInterface $queryNode |
||
| 485 | * @param AbstractField $currentLevelAST |
||
| 486 | * |
||
| 487 | * @return \Generator |
||
| 488 | */ |
||
| 489 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 540 | } |
||
| 541 |
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.