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 |
||
| 36 | class Processor |
||
| 37 | { |
||
| 38 | use ErrorContainerTrait; |
||
| 39 | |||
| 40 | const TYPE_NAME_QUERY = '__typename'; |
||
| 41 | |||
| 42 | /** @var array */ |
||
| 43 | protected $data; |
||
| 44 | |||
| 45 | /** @var ResolveValidatorInterface */ |
||
| 46 | protected $resolveValidator; |
||
| 47 | |||
| 48 | /** @var SchemaValidator */ |
||
| 49 | protected $schemaValidator; |
||
| 50 | |||
| 51 | /** @var AbstractSchema */ |
||
| 52 | protected $schema; |
||
| 53 | |||
| 54 | /** @var Request */ |
||
| 55 | protected $request; |
||
| 56 | |||
| 57 | 19 | public function __construct() |
|
| 62 | |||
| 63 | 19 | public function setSchema(AbstractSchema $schema) |
|
| 81 | |||
| 82 | 19 | public function processRequest($payload, $variables = []) |
|
| 83 | { |
||
| 84 | 19 | if (!$this->getSchema()) { |
|
| 85 | $this->addError(new ConfigurationException('You have to set GraphQL Schema to process')); |
||
| 86 | } |
||
| 87 | 19 | if (empty($payload) || $this->hasErrors()) return $this; |
|
| 88 | |||
| 89 | 19 | $this->data = []; |
|
| 90 | |||
| 91 | try { |
||
| 92 | 19 | $this->parseAndCreateRequest($payload, $variables); |
|
| 93 | |||
| 94 | 19 | foreach ($this->request->getQueries() as $query) { |
|
| 95 | 19 | if ($queryResult = $this->executeQuery($query, $this->getSchema()->getQueryType())) { |
|
| 96 | 17 | $this->data = array_merge($this->data, $queryResult); |
|
| 97 | 17 | }; |
|
| 98 | 19 | } |
|
| 99 | |||
| 100 | 19 | foreach ($this->request->getMutations() as $mutation) { |
|
| 101 | if ($mutationResult = $this->executeMutation($mutation, $this->getSchema()->getMutationType())) { |
||
| 102 | $this->data = array_merge($this->data, $mutationResult); |
||
| 103 | } |
||
| 104 | 19 | } |
|
| 105 | |||
| 106 | 19 | } catch (\Exception $e) { |
|
| 107 | $this->resolveValidator->clearErrors(); |
||
| 108 | |||
| 109 | $this->resolveValidator->addError($e); |
||
| 110 | } |
||
| 111 | |||
| 112 | 19 | return $this; |
|
| 113 | } |
||
| 114 | |||
| 115 | 19 | protected function parseAndCreateRequest($query, $variables = []) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * @param Query|Field $query |
||
| 127 | * @param ObjectType|QueryType $currentLevelSchema |
||
| 128 | * @param null $contextValue |
||
| 129 | * @return array|bool|mixed |
||
| 130 | */ |
||
| 131 | 19 | protected function executeQuery($query, $currentLevelSchema, $contextValue = null) |
|
| 132 | { |
||
| 133 | 19 | if (!$this->resolveValidator->checkFieldExist($currentLevelSchema, $query)) { |
|
| 134 | return null; |
||
| 135 | } |
||
| 136 | |||
| 137 | /** @var Field $field */ |
||
| 138 | 19 | $field = $currentLevelSchema->getField($query->getName()); |
|
|
|
|||
| 139 | 19 | $alias = $query->getAlias() ?: $query->getName(); |
|
| 140 | |||
| 141 | 19 | if ($query instanceof AstField) { |
|
| 142 | 17 | $value = $this->processAstFieldQuery($query, $contextValue, $field); |
|
| 143 | 17 | } else { |
|
| 144 | 19 | if (!$this->resolveValidator->validateArguments($field, $query, $this->request)) { |
|
| 145 | 2 | return null; |
|
| 146 | } |
||
| 147 | |||
| 148 | 17 | $value = $this->processFieldTypeQuery($query, $contextValue, $field); |
|
| 149 | |||
| 150 | } |
||
| 151 | |||
| 152 | 17 | return [$alias => $value]; |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Mutation $mutation |
||
| 157 | * @param ObjectType $currentLevelSchema |
||
| 158 | * @return array|null |
||
| 159 | * @throws ConfigurationException |
||
| 160 | */ |
||
| 161 | protected function executeMutation(Mutation $mutation, $currentLevelSchema) |
||
| 162 | { |
||
| 163 | if (!$currentLevelSchema) throw new ConfigurationException('There is no mutation ' . $mutation->getName()); |
||
| 164 | |||
| 165 | if (!$this->resolveValidator->checkFieldExist($currentLevelSchema, $mutation)) { |
||
| 166 | return null; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** @var Field $field */ |
||
| 170 | $field = $currentLevelSchema->getConfig()->getField($mutation->getName()); |
||
| 171 | $alias = $mutation->getAlias() ?: $mutation->getName(); |
||
| 172 | |||
| 173 | if (!$this->resolveValidator->validateArguments($field, $mutation, $this->request)) { |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | |||
| 177 | $resolvedValue = $this->resolveValue($field, null, $mutation); |
||
| 178 | |||
| 179 | View Code Duplication | if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType())) { |
|
| 180 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for mutation "%s"', $field->getType()->getName()))); |
||
| 181 | |||
| 182 | return [$alias => null]; |
||
| 183 | } |
||
| 184 | |||
| 185 | $value = $resolvedValue; |
||
| 186 | if ($mutation->hasFields()) { |
||
| 187 | if ($field->getType()->isAbstractType()) { |
||
| 188 | $outputType = $field->getType()->getConfig()->resolveType($resolvedValue); |
||
| 189 | } else { |
||
| 190 | /** @var AbstractType $outputType */ |
||
| 191 | $outputType = $field->getType(); |
||
| 192 | } |
||
| 193 | |||
| 194 | $value = $this->collectListOrSingleValue($outputType, $resolvedValue, $mutation); |
||
| 195 | } |
||
| 196 | |||
| 197 | return [$alias => $value]; |
||
| 198 | } |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param AstField $astField |
||
| 202 | * @param mixed $contextValue |
||
| 203 | * @param Field $field |
||
| 204 | * @return array|mixed|null |
||
| 205 | * @throws \Exception |
||
| 206 | */ |
||
| 207 | 17 | protected function processAstFieldQuery(AstField $astField, $contextValue, Field $field) |
|
| 208 | { |
||
| 209 | 17 | $value = null; |
|
| 210 | 17 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $astField, $field); |
|
| 211 | |||
| 212 | 17 | if ($field->getConfig()->getType()->getKind() == TypeMap::KIND_LIST) { |
|
| 213 | 1 | if (!is_array($preResolvedValue)) { |
|
| 214 | $this->resolveValidator->addError(new ResolveException('Not valid resolve value for list type')); |
||
| 215 | |||
| 216 | return null; |
||
| 217 | } |
||
| 218 | |||
| 219 | 1 | $listValue = []; |
|
| 220 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 221 | /** @var TypeInterface $type */ |
||
| 222 | 1 | $type = $field->getType()->getConfig()->getItem(); |
|
| 223 | |||
| 224 | 1 | if ($type->getKind() == TypeMap::KIND_ENUM) { |
|
| 225 | /** @var $type AbstractEnumType */ |
||
| 226 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
| 227 | $this->resolveValidator->addError(new ResolveException('Not valid value for enum type')); |
||
| 228 | |||
| 229 | $listValue = null; |
||
| 230 | break; |
||
| 231 | } |
||
| 232 | |||
| 233 | 1 | $listValue[] = $type->resolve($resolvedValueItem); |
|
| 234 | 1 | } else { |
|
| 235 | /** @var AbstractScalarType $type */ |
||
| 236 | $listValue[] = $type->serialize($preResolvedValue); |
||
| 237 | } |
||
| 238 | 1 | } |
|
| 239 | |||
| 240 | 1 | $value = $listValue; |
|
| 241 | 1 | } else { |
|
| 242 | 17 | if ($field->getType()->getKind() == TypeMap::KIND_ENUM) { |
|
| 243 | if (!$field->getType()->isValidValue($preResolvedValue)) { |
||
| 244 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s type', ($field->getType()->getKind())))); |
||
| 245 | $value = null; |
||
| 246 | } else { |
||
| 247 | $value = $preResolvedValue; |
||
| 248 | /** $field->getType()->resolve($preResolvedValue); */ |
||
| 249 | } |
||
| 250 | 17 | } elseif ($field->getType()->getKind() == TypeMap::KIND_NON_NULL) { |
|
| 251 | if (!$field->getType()->isValidValue($preResolvedValue)) { |
||
| 252 | $this->resolveValidator->addError(new ResolveException(sprintf('Cannot return null for non-nullable field %s', $astField->getName() . '.' . $field->getName()))); |
||
| 253 | } elseif (!$field->getType()->getNullableType()->isValidValue($preResolvedValue)) { |
||
| 254 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s field %s', $field->getType()->getNullableType()->getKind(), $field->getName()))); |
||
| 255 | $value = null; |
||
| 256 | } else { |
||
| 257 | $value = $preResolvedValue; |
||
| 258 | } |
||
| 259 | } else { |
||
| 260 | 17 | $value = $field->getType()->serialize($preResolvedValue); |
|
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | 17 | return $value; |
|
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * @param $query |
||
| 269 | * @param mixed $contextValue |
||
| 270 | * @param Field $field |
||
| 271 | * @return null |
||
| 272 | * @throws \Exception |
||
| 273 | */ |
||
| 274 | 17 | protected function processFieldTypeQuery($query, $contextValue, $field) |
|
| 288 | |||
| 289 | /** |
||
| 290 | * @param AbstractType $fieldType |
||
| 291 | * @param mixed $resolvedValue |
||
| 292 | * @param Query|Mutation $query |
||
| 293 | * @return array|mixed |
||
| 294 | * @throws \Exception |
||
| 295 | */ |
||
| 296 | 17 | protected function collectListOrSingleValue(AbstractType $fieldType, $resolvedValue, $query) |
|
| 297 | { |
||
| 298 | 17 | $value = []; |
|
| 299 | 17 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 300 | 7 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 301 | 7 | $value[] = []; |
|
| 302 | 7 | $index = count($value) - 1; |
|
| 303 | 7 | $namedType = $fieldType->getNamedType(); |
|
| 304 | |||
| 305 | 7 | if ($namedType->isAbstractType()) { |
|
| 306 | 4 | $resolvedType = $namedType->getConfig()->resolveType($resolvedValueItem); |
|
| 307 | 4 | if ($namedType instanceof AbstractInterfaceType) { |
|
| 308 | 2 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $namedType); |
|
| 309 | 2 | } |
|
| 310 | 4 | $namedType = $resolvedType; |
|
| 311 | 4 | } |
|
| 312 | |||
| 313 | 7 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 314 | 7 | } |
|
| 315 | 7 | } else { |
|
| 316 | 15 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 317 | } |
||
| 318 | |||
| 319 | 17 | return $value; |
|
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * @param $value |
||
| 324 | * @param AstField $astField |
||
| 325 | * @param Field $field |
||
| 326 | * |
||
| 327 | * @throws \Exception |
||
| 328 | * |
||
| 329 | * @return mixed |
||
| 330 | */ |
||
| 331 | 17 | protected function getPreResolvedValue($value, AstField $astField, Field $field) |
|
| 332 | { |
||
| 333 | 17 | $resolved = false; |
|
| 334 | 17 | $resolverValue = null; |
|
| 335 | |||
| 336 | 17 | if (is_array($value) && array_key_exists($astField->getName(), $value)) { |
|
| 337 | 12 | $resolverValue = $value[$astField->getName()]; |
|
| 338 | 12 | $resolved = true; |
|
| 339 | 17 | } elseif (is_object($value)) { |
|
| 340 | try { |
||
| 341 | 5 | $resolverValue = $this->getPropertyValue($value, $astField->getName()); |
|
| 342 | 5 | $resolved = true; |
|
| 343 | 5 | } catch (\Exception $e) { |
|
| 344 | } |
||
| 345 | 5 | } elseif ($field->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 346 | $resolved = true; |
||
| 347 | } |
||
| 348 | |||
| 349 | 17 | if ($resolved) { |
|
| 350 | 17 | if ($field->getConfig() && ($field->getConfig()->issetResolve())) { |
|
| 351 | 1 | $resolverValue = $field->resolve($resolverValue, $astField->getKeyValueArguments(), $field->getType()); |
|
| 352 | 1 | } |
|
| 353 | |||
| 354 | 17 | return $resolverValue; |
|
| 355 | } |
||
| 356 | |||
| 357 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $astField->getName())); |
||
| 358 | } |
||
| 359 | |||
| 360 | 5 | protected function getPropertyValue($data, $path) |
|
| 372 | |||
| 373 | 5 | protected function classify($text) |
|
| 374 | { |
||
| 375 | 5 | $text = explode(' ', str_replace(['_', '/', '-', '.'], ' ', $text)); |
|
| 376 | 5 | $textLength = count($text); |
|
| 377 | 5 | for ($i = 0; $i < $textLength; $i++) { |
|
| 378 | 5 | $text[$i] = ucfirst($text[$i]); |
|
| 379 | 5 | } |
|
| 380 | 5 | $text = ucfirst(implode('', $text)); |
|
| 381 | |||
| 382 | 5 | return $text; |
|
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param Field $field |
||
| 387 | * @param mixed $contextValue |
||
| 388 | * @param Query $query |
||
| 389 | * |
||
| 390 | * @return mixed |
||
| 391 | */ |
||
| 392 | 17 | protected function resolveValue($field, $contextValue, $query) |
|
| 393 | { |
||
| 394 | 17 | $resolvedValue = $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $field->getType()); |
|
| 395 | |||
| 396 | 17 | if ($field->getType()->isAbstractType()) { |
|
| 397 | 6 | $resolvedType = $field->getType()->resolveType($resolvedValue); |
|
| 398 | 6 | $field->setType($resolvedType); |
|
| 399 | 6 | } |
|
| 400 | |||
| 401 | 17 | return $resolvedValue; |
|
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @param $field Field |
||
| 406 | * @param $query Query |
||
| 407 | * |
||
| 408 | * @return array |
||
| 409 | */ |
||
| 410 | 17 | public function parseArgumentsValues($field, $query) |
|
| 411 | { |
||
| 412 | 17 | if ($query instanceof \Youshido\GraphQL\Parser\Ast\Field) { |
|
| 413 | return []; |
||
| 414 | } |
||
| 415 | |||
| 416 | 17 | $args = []; |
|
| 417 | 17 | foreach ($query->getArguments() as $argument) { |
|
| 418 | 5 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 419 | 5 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 420 | 5 | } |
|
| 421 | 17 | } |
|
| 422 | |||
| 423 | 17 | return $args; |
|
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * @param $query Query |
||
| 428 | * @param $queryType ObjectType|TypeInterface|Field |
||
| 429 | * @param $resolvedValue mixed |
||
| 430 | * @param $value array |
||
| 431 | * |
||
| 432 | * @throws \Exception |
||
| 433 | * |
||
| 434 | * @return array |
||
| 435 | */ |
||
| 436 | 17 | protected function processQueryFields($query, $queryType, $resolvedValue, $value) |
|
| 437 | { |
||
| 438 | 17 | foreach ($query->getFields() as $field) { |
|
| 439 | 17 | if ($field instanceof FragmentReference) { |
|
| 440 | 1 | if (!$fragment = $this->request->getFragment($field->getName())) { |
|
| 441 | throw new \Exception(sprintf('Fragment reference "%s" not found', $field->getName())); |
||
| 442 | } |
||
| 443 | |||
| 444 | 1 | if ($fragment->getModel() !== $queryType->getName()) { |
|
| 445 | throw new \Exception(sprintf('Fragment reference "%s" not found on model "%s"', $field->getName(), $queryType->getName())); |
||
| 446 | } |
||
| 447 | |||
| 448 | 1 | foreach ($fragment->getFields() as $fragmentField) { |
|
| 449 | 1 | $value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
| 450 | 1 | } |
|
| 451 | 17 | } elseif ($field instanceof TypedFragmentReference) { |
|
| 452 | 2 | if ($field->getTypeName() !== $queryType->getName()) { |
|
| 453 | 1 | continue; |
|
| 454 | } |
||
| 455 | |||
| 456 | 2 | foreach ($field->getFields() as $fragmentField) { |
|
| 457 | 2 | $value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
| 458 | 2 | } |
|
| 459 | 17 | } elseif ($field->getName() == self::TYPE_NAME_QUERY) { |
|
| 460 | 1 | $value = $this->collectValue($value, [$field->getAlias() ?: $field->getName() => $queryType->getName()]); |
|
| 461 | 1 | } else { |
|
| 462 | 17 | $value = $this->collectValue($value, $this->executeQuery($field, $queryType, $resolvedValue)); |
|
| 463 | } |
||
| 464 | 17 | } |
|
| 465 | |||
| 466 | 17 | return $value; |
|
| 467 | } |
||
| 468 | |||
| 469 | 17 | protected function collectValue($value, $queryValue) |
|
| 470 | { |
||
| 471 | 17 | if ($queryValue && is_array($queryValue)) { |
|
| 472 | 17 | $value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
| 473 | 17 | } else { |
|
| 474 | $value = $queryValue; |
||
| 475 | } |
||
| 476 | |||
| 477 | 17 | return $value; |
|
| 478 | } |
||
| 479 | |||
| 480 | 19 | public function getSchema() |
|
| 484 | |||
| 485 | 19 | public function getResponseData() |
|
| 486 | { |
||
| 487 | 19 | $result = []; |
|
| 488 | |||
| 489 | 19 | if (!empty($this->data)) { |
|
| 490 | 17 | $result['data'] = $this->data; |
|
| 491 | 17 | } |
|
| 492 | |||
| 493 | 19 | $this->mergeErrors($this->resolveValidator); |
|
| 494 | 19 | if ($this->hasErrors()) { |
|
| 495 | 2 | $result['errors'] = $this->getErrorsArray(); |
|
| 503 | } |
||
| 504 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: