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 = []) |
|
| 111 | |||
| 112 | 19 | protected function parseAndCreateRequest($query, $variables = []) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * @param Query|Field $query |
||
| 124 | * @param ObjectType|QueryType $currentLevelSchema |
||
| 125 | * @param null $contextValue |
||
| 126 | * @return array|bool|mixed |
||
| 127 | */ |
||
| 128 | 19 | protected function executeQuery($query, $currentLevelSchema, $contextValue = null) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @param Mutation $mutation |
||
| 154 | * @param ObjectType $currentLevelSchema |
||
| 155 | * @return array|null |
||
| 156 | * @throws ConfigurationException |
||
| 157 | */ |
||
| 158 | protected function executeMutation(Mutation $mutation, $currentLevelSchema) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * @param AstField $astField |
||
| 199 | * @param mixed $contextValue |
||
| 200 | * @param Field $field |
||
| 201 | * @return array|mixed|null |
||
| 202 | * @throws \Exception |
||
| 203 | */ |
||
| 204 | 17 | protected function processAstFieldQuery(AstField $astField, $contextValue, Field $field) |
|
| 205 | { |
||
| 206 | 17 | $value = null; |
|
| 207 | 17 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $astField, $field); |
|
| 208 | |||
| 209 | 17 | if ($field->getConfig()->getType()->getKind() == TypeMap::KIND_LIST) { |
|
| 210 | 1 | if (!is_array($preResolvedValue)) { |
|
| 211 | $this->resolveValidator->addError(new ResolveException('Not valid resolve value for list type')); |
||
| 212 | |||
| 213 | return null; |
||
| 214 | } |
||
| 215 | |||
| 216 | 1 | $listValue = []; |
|
| 217 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 218 | /** @var TypeInterface $type */ |
||
| 219 | 1 | $type = $field->getType()->getConfig()->getItem(); |
|
| 220 | |||
| 221 | 1 | if ($type->getKind() == TypeMap::KIND_ENUM) { |
|
| 222 | /** @var $type AbstractEnumType */ |
||
| 223 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
| 224 | $this->resolveValidator->addError(new ResolveException('Not valid value for enum type')); |
||
| 225 | |||
| 226 | $listValue = null; |
||
| 227 | break; |
||
| 228 | } |
||
| 229 | |||
| 230 | 1 | $listValue[] = $type->resolve($resolvedValueItem); |
|
| 231 | } else { |
||
| 232 | /** @var AbstractScalarType $type */ |
||
| 233 | 1 | $listValue[] = $type->serialize($preResolvedValue); |
|
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | 1 | $value = $listValue; |
|
| 238 | } else { |
||
| 239 | 17 | if ($field->getType()->getKind() == TypeMap::KIND_ENUM) { |
|
| 240 | if (!$field->getType()->isValidValue($preResolvedValue)) { |
||
| 241 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s type', ($field->getType()->getKind())))); |
||
| 242 | $value = null; |
||
| 243 | } else { |
||
| 244 | $value = $preResolvedValue; |
||
| 245 | /** $field->getType()->resolve($preResolvedValue); */ |
||
| 246 | } |
||
| 247 | 17 | } elseif ($field->getType()->getKind() == TypeMap::KIND_NON_NULL) { |
|
| 248 | if (!$field->getType()->isValidValue($preResolvedValue)) { |
||
| 249 | $this->resolveValidator->addError(new ResolveException(sprintf('Cannot return null for non-nullable field %s', $astField->getName() . '.' . $field->getName()))); |
||
| 250 | } elseif (!$field->getType()->getNullableType()->isValidValue($preResolvedValue)) { |
||
| 251 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid value for %s field %s', $field->getType()->getNullableType()->getKind(), $field->getName()))); |
||
| 252 | $value = null; |
||
| 253 | } else { |
||
| 254 | $value = $preResolvedValue; |
||
| 255 | } |
||
| 256 | } else { |
||
| 257 | 17 | $value = $field->getType()->serialize($preResolvedValue); |
|
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | 17 | return $value; |
|
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @param $query |
||
| 266 | * @param mixed $contextValue |
||
| 267 | * @param Field $field |
||
| 268 | * @return null |
||
| 269 | * @throws \Exception |
||
| 270 | */ |
||
| 271 | 17 | protected function processFieldTypeQuery($query, $contextValue, $field) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param AbstractType $fieldType |
||
| 288 | * @param mixed $resolvedValue |
||
| 289 | * @param Query|Mutation $query |
||
| 290 | * @return array|mixed |
||
| 291 | * @throws \Exception |
||
| 292 | */ |
||
| 293 | 17 | protected function collectListOrSingleValue(AbstractType $fieldType, $resolvedValue, $query) |
|
| 294 | { |
||
| 295 | 17 | $value = []; |
|
| 296 | 17 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 297 | 7 | foreach ($resolvedValue as $resolvedValueItem) { |
|
| 298 | 7 | $value[] = []; |
|
| 299 | 7 | $index = count($value) - 1; |
|
| 300 | 7 | $namedType = $fieldType->getNamedType(); |
|
| 301 | |||
| 302 | 7 | if ($namedType->isAbstractType()) { |
|
| 303 | 4 | $resolvedType = $namedType->getConfig()->resolveType($resolvedValueItem); |
|
| 304 | 4 | if ($namedType instanceof AbstractInterfaceType) { |
|
| 305 | 2 | $this->resolveValidator->assertTypeImplementsInterface($resolvedType, $namedType); |
|
| 306 | } |
||
| 307 | 4 | $namedType = $resolvedType; |
|
| 308 | } |
||
| 309 | |||
| 310 | 7 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 311 | } |
||
| 312 | } else { |
||
| 313 | 15 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 314 | } |
||
| 315 | |||
| 316 | 17 | return $value; |
|
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * @param $value |
||
| 321 | * @param AstField $astField |
||
| 322 | * @param Field $field |
||
| 323 | * |
||
| 324 | * @throws \Exception |
||
| 325 | * |
||
| 326 | * @return mixed |
||
| 327 | */ |
||
| 328 | 17 | protected function getPreResolvedValue($value, AstField $astField, Field $field) |
|
| 329 | { |
||
| 330 | 17 | $resolved = false; |
|
| 331 | 17 | $resolverValue = null; |
|
| 332 | |||
| 333 | 17 | if (is_array($value) && array_key_exists($astField->getName(), $value)) { |
|
| 334 | 12 | $resolverValue = $value[$astField->getName()]; |
|
| 335 | 12 | $resolved = true; |
|
| 336 | 5 | } elseif (is_object($value)) { |
|
| 337 | try { |
||
| 338 | 5 | $resolverValue = $this->getPropertyValue($value, $astField->getName()); |
|
| 339 | 5 | $resolved = true; |
|
| 340 | 5 | } catch (\Exception $e) { |
|
| 341 | } |
||
| 342 | } elseif ($field->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
||
| 343 | $resolved = true; |
||
| 344 | } |
||
| 345 | |||
| 346 | 17 | if ($resolved) { |
|
| 347 | 17 | if ($field->getConfig() && ($field->getConfig()->issetResolve())) { |
|
| 348 | 1 | $resolverValue = $field->resolve($resolverValue, $astField->getKeyValueArguments(), $field->getType()); |
|
| 349 | } |
||
| 350 | |||
| 351 | 17 | return $resolverValue; |
|
| 352 | } |
||
| 353 | |||
| 354 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $astField->getName())); |
||
| 355 | } |
||
| 356 | |||
| 357 | 5 | protected function getPropertyValue($data, $path) |
|
| 369 | |||
| 370 | 5 | protected function classify($text) |
|
| 371 | { |
||
| 372 | 5 | $text = explode(' ', str_replace(['_', '/', '-', '.'], ' ', $text)); |
|
| 373 | 5 | $textLength = count($text); |
|
| 374 | 5 | for ($i = 0; $i < $textLength; $i++) { |
|
| 375 | 5 | $text[$i] = ucfirst($text[$i]); |
|
| 376 | } |
||
| 377 | 5 | $text = ucfirst(implode('', $text)); |
|
| 378 | |||
| 379 | 5 | return $text; |
|
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * @param Field $field |
||
| 384 | * @param mixed $contextValue |
||
| 385 | * @param Query $query |
||
| 386 | * |
||
| 387 | * @return mixed |
||
| 388 | */ |
||
| 389 | 17 | protected function resolveValue($field, $contextValue, $query) |
|
| 390 | { |
||
| 391 | 17 | $resolvedValue = $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $field->getType()); |
|
| 392 | |||
| 393 | 17 | if ($field->getType()->isAbstractType()) { |
|
| 394 | 6 | $resolvedType = $field->getType()->resolveType($resolvedValue); |
|
| 395 | 6 | $field->setType($resolvedType); |
|
| 396 | } |
||
| 397 | |||
| 398 | 17 | return $resolvedValue; |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param $field Field |
||
| 403 | * @param $query Query |
||
| 404 | * |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | 17 | public function parseArgumentsValues($field, $query) |
|
| 408 | { |
||
| 409 | 17 | if ($query instanceof \Youshido\GraphQL\Parser\Ast\Field) { |
|
| 410 | return []; |
||
| 411 | } |
||
| 412 | |||
| 413 | 17 | $args = []; |
|
| 414 | 17 | foreach ($query->getArguments() as $argument) { |
|
| 415 | 5 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 416 | 5 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 417 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | 17 | return $args; |
|
| 421 | } |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param $query Query |
||
| 425 | * @param $queryType ObjectType|TypeInterface|Field |
||
| 426 | * @param $resolvedValue mixed |
||
| 427 | * @param $value array |
||
| 428 | * |
||
| 429 | * @throws \Exception |
||
| 430 | * |
||
| 431 | * @return array |
||
| 432 | */ |
||
| 433 | 17 | protected function processQueryFields($query, $queryType, $resolvedValue, $value) |
|
| 434 | { |
||
| 435 | 17 | foreach ($query->getFields() as $field) { |
|
| 436 | 17 | if ($field instanceof FragmentReference) { |
|
| 437 | 1 | if (!$fragment = $this->request->getFragment($field->getName())) { |
|
| 438 | throw new \Exception(sprintf('Fragment reference "%s" not found', $field->getName())); |
||
| 439 | } |
||
| 440 | |||
| 441 | 1 | if ($fragment->getModel() !== $queryType->getName()) { |
|
| 442 | throw new \Exception(sprintf('Fragment reference "%s" not found on model "%s"', $field->getName(), $queryType->getName())); |
||
| 443 | } |
||
| 444 | |||
| 445 | 1 | foreach ($fragment->getFields() as $fragmentField) { |
|
| 446 | 1 | $value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
| 447 | } |
||
| 448 | } elseif ($field instanceof TypedFragmentReference) { |
||
| 449 | 2 | if ($field->getTypeName() !== $queryType->getName()) { |
|
| 450 | 1 | continue; |
|
| 451 | } |
||
| 452 | |||
| 453 | 2 | foreach ($field->getFields() as $fragmentField) { |
|
| 454 | 2 | $value = $this->collectValue($value, $this->executeQuery($fragmentField, $queryType, $resolvedValue)); |
|
| 455 | } |
||
| 456 | 17 | } elseif ($field->getName() == self::TYPE_NAME_QUERY) { |
|
| 457 | 1 | $value = $this->collectValue($value, [$field->getAlias() ?: $field->getName() => $queryType->getName()]); |
|
| 458 | } else { |
||
| 459 | 17 | $value = $this->collectValue($value, $this->executeQuery($field, $queryType, $resolvedValue)); |
|
| 460 | } |
||
| 461 | } |
||
| 462 | |||
| 463 | 17 | return $value; |
|
| 464 | } |
||
| 465 | |||
| 466 | 17 | protected function collectValue($value, $queryValue) |
|
| 467 | { |
||
| 468 | 17 | if ($queryValue && is_array($queryValue)) { |
|
| 469 | 17 | $value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
| 470 | } else { |
||
| 471 | $value = $queryValue; |
||
| 472 | } |
||
| 473 | |||
| 474 | 17 | return $value; |
|
| 475 | } |
||
| 476 | |||
| 477 | 19 | public function getSchema() |
|
| 481 | |||
| 482 | 19 | public function getResponseData() |
|
| 483 | { |
||
| 484 | 19 | $result = []; |
|
| 485 | |||
| 486 | 19 | if (!empty($this->data)) { |
|
| 487 | 17 | $result['data'] = $this->data; |
|
| 488 | } |
||
| 489 | |||
| 500 | } |
||
| 501 |
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: