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 |
||
| 38 | class Processor |
||
| 39 | { |
||
| 40 | |||
| 41 | const TYPE_NAME_QUERY = '__typename'; |
||
| 42 | |||
| 43 | /** @var array */ |
||
| 44 | protected $data; |
||
| 45 | |||
| 46 | /** @var ResolveValidatorInterface */ |
||
| 47 | protected $resolveValidator; |
||
| 48 | |||
| 49 | /** @var ExecutionContext */ |
||
| 50 | protected $executionContext; |
||
| 51 | |||
| 52 | /** @var #maxComplexity */ |
||
| 53 | protected $maxComplexity; |
||
| 54 | |||
| 55 | 24 | public function __construct(AbstractSchema $schema) |
|
| 65 | |||
| 66 | |||
| 67 | 23 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 68 | { |
||
| 69 | 23 | if ($this->executionContext->hasErrors()) { |
|
| 70 | 4 | $this->executionContext->clearErrors(); |
|
| 71 | } |
||
| 72 | |||
| 73 | 23 | $this->data = []; |
|
| 74 | |||
| 75 | try { |
||
| 76 | 23 | $this->parseAndCreateRequest($payload, $variables); |
|
| 77 | |||
| 78 | 23 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 79 | 23 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 80 | |||
| 81 | 23 | if ($this->maxComplexity) { |
|
| 82 | 1 | $reducers[] = new \Youshido\GraphQL\Execution\Visitor\MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 83 | } |
||
| 84 | |||
| 85 | 23 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 86 | |||
| 87 | 23 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 88 | 23 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 89 | 23 | $this->data = array_merge($this->data, $operationResult); |
|
| 90 | }; |
||
| 91 | } |
||
| 92 | |||
| 93 | 4 | } catch (\Exception $e) { |
|
| 94 | 4 | $this->executionContext->addError($e); |
|
| 95 | } |
||
| 96 | |||
| 97 | 23 | return $this; |
|
| 98 | } |
||
| 99 | |||
| 100 | 23 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @param Query|Field $query |
||
| 113 | * @param AbstractObjectType $currentLevelSchema |
||
| 114 | * @return array|bool|mixed |
||
| 115 | */ |
||
| 116 | 23 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 132 | |||
| 133 | /** |
||
| 134 | * @param Query $query |
||
| 135 | * @param AbstractField $field |
||
| 136 | * @param $contextValue |
||
| 137 | * @return array|mixed|null |
||
| 138 | */ |
||
| 139 | 21 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @param Query|Mutation $query |
||
| 156 | * @param AbstractType $fieldType |
||
| 157 | * @param mixed $resolvedValue |
||
| 158 | * @return array|mixed |
||
| 159 | */ |
||
| 160 | 21 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 161 | { |
||
| 162 | 21 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
| 163 | 21 | if (is_null($resolvedValue)) return null; |
|
| 164 | |||
| 165 | 19 | $value = []; |
|
| 166 | 19 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 167 | 8 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|||
| 168 | 7 | $value[] = []; |
|
| 169 | 7 | $index = count($value) - 1; |
|
| 170 | |||
| 171 | |||
| 172 | 7 | $namedType = $fieldType->getNamedType(); |
|
| 173 | 7 | $namedType = $this->resolveValidator->resolveTypeIfAbstract($namedType, $resolvedValueItem); |
|
| 174 | 7 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 175 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 176 | 1 | $value[$index] = null; |
|
| 177 | 1 | continue; |
|
| 178 | } |
||
| 179 | |||
| 180 | 8 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 181 | } |
||
| 182 | } else { |
||
| 183 | 19 | if (!$query->hasFields()) { |
|
| 184 | 2 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 185 | } |
||
| 186 | |||
| 187 | 19 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 188 | } |
||
| 189 | |||
| 190 | 19 | return $value; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param FieldAst $fieldAst |
||
| 195 | * @param AbstractField $field |
||
| 196 | * |
||
| 197 | * @param mixed $contextValue |
||
| 198 | * @return array|mixed|null |
||
| 199 | * @throws ResolveException |
||
| 200 | * @throws \Exception |
||
| 201 | */ |
||
| 202 | 18 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 203 | { |
||
| 204 | 18 | $value = null; |
|
| 205 | 18 | $fieldType = $field->getType(); |
|
| 206 | 18 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 207 | |||
| 208 | 18 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 209 | 1 | $listValue = []; |
|
| 210 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 211 | 1 | $type = $fieldType->getNamedType(); |
|
| 212 | |||
| 213 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
| 214 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $field->getName()))); |
||
| 215 | |||
| 216 | $listValue = null; |
||
| 217 | break; |
||
| 218 | } |
||
| 219 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
| 220 | } |
||
| 221 | |||
| 222 | 1 | $value = $listValue; |
|
| 223 | } else { |
||
| 224 | 18 | $value = $this->getFieldValidatedValue($field, $preResolvedValue); |
|
| 225 | } |
||
| 226 | |||
| 227 | 18 | return $value; |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param AbstractField $field |
||
| 232 | * @param mixed $contextValue |
||
| 233 | * @param Query $query |
||
| 234 | * |
||
| 235 | * @return mixed |
||
| 236 | */ |
||
| 237 | 21 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @param $contextValue |
||
| 252 | * @param FieldAst $fieldAst |
||
| 253 | * @param AbstractField $field |
||
| 254 | * |
||
| 255 | * @throws \Exception |
||
| 256 | * |
||
| 257 | * @return mixed |
||
| 258 | */ |
||
| 259 | 18 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 260 | { |
||
| 261 | 18 | $resolved = false; |
|
| 262 | 18 | $resolverValue = null; |
|
| 263 | |||
| 264 | 18 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
| 265 | 14 | $resolverValue = $contextValue[$fieldAst->getName()]; |
|
| 266 | 14 | $resolved = true; |
|
| 267 | 9 | } elseif (is_object($contextValue)) { |
|
| 268 | 6 | $resolverValue = TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
| 269 | 6 | $resolved = true; |
|
| 270 | } |
||
| 271 | |||
| 272 | 18 | if (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 273 | 3 | $resolved = true; |
|
| 274 | } |
||
| 275 | |||
| 276 | 18 | if ($resolveFunction = $field->getConfig()->getResolveFunction()) { |
|
| 277 | 3 | $resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext); |
|
| 278 | |||
| 279 | 3 | if (!$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
|
| 280 | throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
||
| 281 | |||
| 282 | } else { |
||
| 283 | 3 | $resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo); |
|
| 284 | } |
||
| 285 | |||
| 286 | } |
||
| 287 | |||
| 288 | 18 | if (!$resolverValue && !$resolved) { |
|
| 289 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
| 290 | } |
||
| 291 | |||
| 292 | 18 | return $resolverValue; |
|
| 293 | } |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param $field AbstractField |
||
| 297 | * @param $query Query |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 21 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 302 | { |
||
| 303 | 21 | $args = []; |
|
| 304 | 21 | foreach ($query->getArguments() as $argument) { |
|
| 305 | 9 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 306 | 9 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 307 | } |
||
| 308 | } |
||
| 309 | |||
| 310 | 21 | return $args; |
|
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * @param $query Query|FragmentInterface |
||
| 315 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 316 | * @param $resolvedValue mixed |
||
| 317 | * @param $value array |
||
| 318 | * |
||
| 319 | * @throws \Exception |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | 19 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 324 | { |
||
| 325 | 19 | foreach ($query->getFields() as $fieldAst) { |
|
| 326 | 19 | $fieldResolvedValue = null; |
|
| 327 | |||
| 328 | 19 | if ($fieldAst instanceof FragmentInterface) { |
|
| 329 | /** @var TypedFragmentReference $fragment */ |
||
| 330 | 3 | $fragment = $fieldAst; |
|
| 331 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 332 | /** @var Fragment $fragment */ |
||
| 333 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAst->getName()); |
|
| 334 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
| 335 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 336 | 1 | continue; |
|
| 337 | } |
||
| 338 | |||
| 339 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 340 | 3 | $fieldResolvedValue = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 341 | } else { |
||
| 342 | 19 | $alias = $fieldAst->getAlias() ?: $fieldAst->getName(); |
|
| 343 | 19 | $currentType = $queryType->getNullableType(); |
|
| 344 | |||
| 345 | 19 | if ($fieldAst->getName() == self::TYPE_NAME_QUERY) { |
|
| 346 | 1 | $fieldResolvedValue = [$alias => $queryType->getName()]; |
|
| 347 | } else { |
||
| 348 | 19 | if (!$this->resolveValidator->objectHasField($currentType, $fieldAst)) { |
|
| 349 | 2 | $fieldResolvedValue = null; |
|
| 350 | } else { |
||
| 351 | 19 | if ($fieldAst instanceof Query) { |
|
| 352 | 10 | $queryAst = $currentType->getField($fieldAst->getName()); |
|
| 353 | 10 | $fieldValue = $queryAst ? $this->processQueryAST($fieldAst, $queryAst, $resolvedValue) : null; |
|
| 354 | 10 | $fieldResolvedValue = [$alias => $fieldValue]; |
|
| 355 | } elseif ($fieldAst instanceof FieldAst) { |
||
| 356 | $fieldResolvedValue = [ |
||
| 357 | 18 | $alias => $this->processFieldAST($fieldAst, $currentType->getField($fieldAst->getName()), $resolvedValue) |
|
| 358 | ]; |
||
| 359 | } |
||
| 360 | } |
||
| 361 | |||
| 362 | |||
| 363 | } |
||
| 364 | } |
||
| 365 | |||
| 366 | 19 | $value = $this->collectValue($value, $fieldResolvedValue); |
|
| 367 | } |
||
| 368 | |||
| 369 | 19 | return $value; |
|
| 370 | } |
||
| 371 | |||
| 372 | 18 | protected function getFieldValidatedValue(AbstractField $field, $value) |
|
| 376 | |||
| 377 | 18 | protected function getOutputValue(AbstractType $type, $value) |
|
| 381 | |||
| 382 | 19 | protected function collectValue($value, $queryValue) |
|
| 383 | { |
||
| 384 | 19 | if ($queryValue && is_array($queryValue)) { |
|
| 385 | 19 | $value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
| 386 | } else { |
||
| 387 | 2 | $value = $queryValue; |
|
| 388 | } |
||
| 389 | |||
| 390 | 19 | return $value; |
|
| 391 | } |
||
| 392 | |||
| 393 | 23 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 401 | |||
| 402 | 23 | public function getResponseData() |
|
| 403 | { |
||
| 404 | 23 | $result = []; |
|
| 405 | |||
| 406 | 23 | if (!empty($this->data)) { |
|
| 407 | 21 | $result['data'] = $this->data; |
|
| 408 | } |
||
| 409 | |||
| 410 | 23 | if ($this->executionContext->hasErrors()) { |
|
| 411 | 7 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 412 | } |
||
| 413 | |||
| 414 | 23 | return $result; |
|
| 415 | } |
||
| 416 | |||
| 417 | 1 | public function setMaxComplexity($max) { |
|
| 420 | |||
| 421 | /** |
||
| 422 | * @param AbstractType $queryType |
||
| 423 | * @param AbstractType $mutationType |
||
| 424 | * @param array $reducers |
||
| 425 | */ |
||
| 426 | 23 | protected function reduceQuery($queryType, $mutationType, array $reducers) { |
|
| 427 | 23 | foreach ($reducers as $reducer) { |
|
| 428 | 1 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @param Query|Field $query |
||
| 436 | * @param AbstractObjectType $currentLevelSchema |
||
| 437 | * @param AbstractQueryVisitor $reducer |
||
| 438 | */ |
||
| 439 | 1 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * @param Query $query |
||
| 462 | * @param AbstractField $currentLevelAST |
||
| 463 | * |
||
| 464 | * @return \Generator |
||
| 465 | */ |
||
| 466 | 1 | protected function walkQuery($query, AbstractField $currentLevelAST) { |
|
| 496 | } |
||
| 497 |
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.