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 | 5 | } |
|
| 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 | 1 | } |
|
| 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 | 23 | $this->data = array_merge($this->data, $operationResult); |
|
| 92 | 23 | }; |
|
| 93 | 25 | } |
|
| 94 | |||
| 95 | 25 | } catch (\Exception $e) { |
|
| 96 | 4 | $this->executionContext->addError($e); |
|
| 97 | } |
||
| 98 | |||
| 99 | 25 | return $this; |
|
| 100 | } |
||
| 101 | |||
| 102 | 25 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 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) |
|
| 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 | 2 | } |
|
| 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 | 8 | } |
|
| 194 | |||
| 195 | 8 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 196 | 10 | } |
|
| 197 | 10 | } 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 | 1 | } |
|
| 236 | |||
| 237 | 1 | $value = $listValue; |
|
| 238 | 1 | } else { |
|
| 239 | /** |
||
| 240 | * $this->getOutputValue($field->getType(), $preResolvedValue); decrease performance |
||
| 241 | */ |
||
| 242 | 20 | $value = $preResolvedValue; |
|
| 243 | } |
||
| 244 | |||
| 245 | 20 | return $value; |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * @param AbstractField $field |
||
| 250 | * @param mixed $contextValue |
||
| 251 | * @param Query $query |
||
| 252 | * |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | 23 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
| 256 | { |
||
| 257 | 23 | $resolveInfo = new ResolveInfo($field, $query->getFields(), $field->getType(), $this->executionContext); |
|
| 258 | |||
| 259 | 23 | if ($resolveFunc = $field->getConfig()->getResolveFunction()) { |
|
| 260 | 21 | return $resolveFunc($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo); |
|
| 261 | 10 | } elseif ($propertyValue = TypeService::getPropertyValue($contextValue, $field->getName())) { |
|
| 262 | 2 | return $propertyValue; |
|
| 263 | } else { |
||
| 264 | 8 | return $field->resolve($contextValue, $this->parseArgumentsValues($field, $query), $resolveInfo); |
|
| 265 | } |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param $contextValue |
||
| 270 | * @param FieldAst $fieldAst |
||
| 271 | * @param AbstractField $field |
||
| 272 | * |
||
| 273 | * @throws \Exception |
||
| 274 | * |
||
| 275 | * @return mixed |
||
| 276 | */ |
||
| 277 | 20 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 278 | { |
||
| 279 | 20 | $resolved = false; |
|
| 280 | 20 | $resolverValue = null; |
|
| 281 | |||
| 282 | 20 | if ($resolveFunction = $field->getConfig()->getResolveFunction()) { |
|
| 283 | 3 | $resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext); |
|
| 284 | |||
| 285 | 3 | if (!$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
|
| 286 | throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
||
| 287 | |||
| 288 | } else { |
||
| 289 | 3 | $resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo); |
|
| 290 | 3 | $resolved = true; |
|
| 291 | } |
||
| 292 | |||
| 293 | 3 | } |
|
| 294 | |||
| 295 | 20 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
| 296 | 16 | $resolverValue = $contextValue[$fieldAst->getName()]; |
|
| 297 | 16 | $resolved = true; |
|
| 298 | 20 | } elseif (is_object($contextValue)) { |
|
| 299 | 6 | $resolverValue = TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
| 300 | 6 | $resolved = true; |
|
| 301 | 9 | } elseif (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
| 302 | 2 | $resolved = true; |
|
| 303 | 2 | } |
|
| 304 | |||
| 305 | 20 | if (!$resolverValue && !$resolved) { |
|
| 306 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
| 307 | } |
||
| 308 | |||
| 309 | 20 | return $resolverValue; |
|
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @param $field AbstractField |
||
| 314 | * @param $query Query |
||
| 315 | * |
||
| 316 | * @return array |
||
| 317 | */ |
||
| 318 | 23 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 319 | { |
||
| 320 | 23 | $args = []; |
|
| 321 | 23 | foreach ($query->getArguments() as $argument) { |
|
| 322 | 9 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 323 | 9 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 324 | 9 | } |
|
| 325 | 23 | } |
|
| 326 | |||
| 327 | 23 | return $args; |
|
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * @param $query Query|FragmentInterface |
||
| 332 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 333 | * @param $resolvedValue mixed |
||
| 334 | * @param $value array |
||
| 335 | * |
||
| 336 | * @throws \Exception |
||
| 337 | * |
||
| 338 | * @return array |
||
| 339 | */ |
||
| 340 | 21 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 341 | { |
||
| 342 | 21 | $currentType = $queryType->getNullableType(); |
|
| 343 | |||
| 344 | 21 | View Code Duplication | if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
| 345 | 1 | if (!$query->hasFields()) { |
|
| 346 | 1 | return $this->getOutputValue($currentType, $resolvedValue); |
|
| 347 | } else { |
||
| 348 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
| 349 | |||
| 350 | 1 | return null; |
|
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | 21 | foreach ($query->getFields() as $fieldAst) { |
|
| 355 | |||
| 356 | 21 | if ($fieldAst instanceof FragmentInterface) { |
|
| 357 | /** @var TypedFragmentReference $fragment */ |
||
| 358 | 3 | $fragment = $fieldAst; |
|
| 359 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 360 | /** @var Fragment $fragment */ |
||
| 361 | 2 | $fieldAstName = $fieldAst->getName(); |
|
| 362 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
|
| 363 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
| 364 | 3 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 365 | 1 | continue; |
|
| 366 | } |
||
| 367 | |||
| 368 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 369 | 3 | $value = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 370 | 3 | } else { |
|
| 371 | 21 | $fieldAstName = $fieldAst->getName(); |
|
| 372 | 21 | $alias = $fieldAst->getAlias() ?: $fieldAstName; |
|
| 373 | |||
| 374 | 21 | if ($fieldAstName == self::TYPE_NAME_QUERY) { |
|
| 375 | 1 | $value[$alias] = $queryType->getName(); |
|
| 376 | 1 | } else { |
|
| 377 | 21 | $field = $currentType->getField($fieldAstName); |
|
| 378 | 21 | if (!$field) { |
|
| 379 | 3 | $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
|
| 380 | |||
| 381 | 3 | return null; |
|
| 382 | } |
||
| 383 | |||
| 384 | 21 | if ($fieldAst instanceof Query) { |
|
| 385 | 11 | $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
|
| 386 | 21 | } elseif ($fieldAst instanceof FieldAst) { |
|
| 387 | 20 | $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
|
| 388 | 20 | } else { |
|
| 389 | return $value; |
||
| 390 | } |
||
| 391 | } |
||
| 392 | } |
||
| 393 | |||
| 394 | 21 | } |
|
| 395 | |||
| 396 | 21 | return $value; |
|
| 397 | } |
||
| 398 | |||
| 399 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 403 | |||
| 404 | 3 | protected function getOutputValue(AbstractType $type, $value) |
|
| 408 | |||
| 409 | 25 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 417 | |||
| 418 | 25 | public function getResponseData() |
|
| 419 | { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 435 | * |
||
| 436 | * @param int $max |
||
| 437 | */ |
||
| 438 | 1 | public function setMaxComplexity($max) |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 445 | * performing look-ahead query planning, etc. |
||
| 446 | * |
||
| 447 | * @param AbstractType $queryType |
||
| 448 | * @param AbstractType $mutationType |
||
| 449 | * @param AbstractQueryVisitor[] $reducers |
||
| 450 | */ |
||
| 451 | 25 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 459 | |||
| 460 | /** |
||
| 461 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 462 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 463 | * |
||
| 464 | * @param Query $query |
||
| 465 | * @param AbstractType $currentLevelSchema |
||
| 466 | * @param AbstractQueryVisitor $reducer |
||
| 467 | */ |
||
| 468 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 498 | * tuple of (queryNode, schemaNode, childScore) |
||
| 499 | * |
||
| 500 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 501 | * |
||
| 502 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 503 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 504 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 505 | * |
||
| 506 | * @param Query|Field|FragmentInterface $queryNode |
||
| 507 | * @param AbstractField $currentLevelAST |
||
| 508 | * |
||
| 509 | * @return \Generator |
||
| 510 | */ |
||
| 511 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 564 | } |
||
| 565 |
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.