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 |
||
| 39 | class Processor |
||
| 40 | { |
||
| 41 | |||
| 42 | const TYPE_NAME_QUERY = '__typename'; |
||
| 43 | |||
| 44 | /** @var array */ |
||
| 45 | protected $data; |
||
| 46 | |||
| 47 | /** @var ResolveValidatorInterface */ |
||
| 48 | protected $resolveValidator; |
||
| 49 | |||
| 50 | /** @var ExecutionContext */ |
||
| 51 | protected $executionContext; |
||
| 52 | |||
| 53 | /** @var int */ |
||
| 54 | protected $maxComplexity; |
||
| 55 | |||
| 56 | 29 | public function __construct(AbstractSchema $schema) |
|
| 57 | { |
||
| 58 | 29 | $this->executionContext = new ExecutionContext(); |
|
| 59 | |||
| 60 | 29 | (new SchemaValidator())->validate($schema); |
|
| 61 | |||
| 62 | 28 | $this->introduceIntrospectionFields($schema); |
|
| 63 | 28 | $this->executionContext->setSchema($schema); |
|
| 64 | |||
| 65 | 28 | $this->resolveValidator = new ResolveValidator($this->executionContext); |
|
| 66 | 28 | } |
|
| 67 | |||
| 68 | 27 | public function processPayload($payload, $variables = [], $reducers = []) |
|
| 69 | { |
||
| 70 | 27 | if ($this->executionContext->hasErrors()) { |
|
| 71 | 7 | $this->executionContext->clearErrors(); |
|
| 72 | } |
||
| 73 | |||
| 74 | 27 | $this->data = []; |
|
| 75 | |||
| 76 | try { |
||
| 77 | 27 | $this->parseAndCreateRequest($payload, $variables); |
|
| 78 | |||
| 79 | 27 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 80 | 27 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 81 | |||
| 82 | 27 | if ($this->maxComplexity) { |
|
| 83 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity); |
|
| 84 | } |
||
| 85 | |||
| 86 | 27 | $this->reduceQuery($queryType, $mutationType, $reducers); |
|
| 87 | |||
| 88 | 27 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 89 | 27 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 90 | 27 | $this->data = array_merge($this->data, $operationResult); |
|
| 91 | }; |
||
| 92 | } |
||
| 93 | |||
| 94 | 4 | } catch (\Exception $e) { |
|
| 95 | 4 | $this->executionContext->addError($e); |
|
| 96 | } |
||
| 97 | |||
| 98 | 27 | return $this; |
|
| 99 | } |
||
| 100 | |||
| 101 | 27 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * @param Query|Field $query |
||
| 114 | * @param AbstractObjectType $currentLevelSchema |
||
| 115 | * @return array|bool|mixed |
||
| 116 | */ |
||
| 117 | 27 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param Query $query |
||
| 136 | * @param AbstractField $field |
||
| 137 | * @param $contextValue |
||
| 138 | * @return array|mixed|null |
||
| 139 | */ |
||
| 140 | 24 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 141 | { |
||
| 142 | 24 | if (!$this->resolveValidator->validateArguments($field, $query, $this->executionContext->getRequest())) { |
|
| 143 | return null; |
||
| 144 | } |
||
| 145 | |||
| 146 | 24 | $resolvedValue = $this->resolveFieldValue($field, $contextValue, $query->getFields(), $this->parseArgumentsValues($field, $query)); |
|
| 147 | |||
| 148 | 24 | if (!$this->resolveValidator->isValidValueForField($field, $resolvedValue)) { |
|
| 149 | 2 | return null; |
|
| 150 | } |
||
| 151 | |||
| 152 | 24 | return $this->collectValueForQueryWithType($query, $field->getType(), $resolvedValue); |
|
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @param Query|Mutation $query |
||
| 157 | * @param AbstractType $fieldType |
||
| 158 | * @param mixed $resolvedValue |
||
| 159 | * @return array|mixed |
||
| 160 | */ |
||
| 161 | 24 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 162 | { |
||
| 163 | 24 | if (is_null($resolvedValue)) { |
|
| 164 | 7 | return null; |
|
| 165 | } |
||
| 166 | |||
| 167 | 22 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
| 168 | 22 | $value = []; |
|
| 169 | |||
| 170 | 22 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 171 | 9 | if (!$this->resolveValidator->hasArrayAccess($resolvedValue)) return null; |
|
| 172 | |||
| 173 | 9 | $namedType = $fieldType->getNamedType(); |
|
| 174 | 9 | $isAbstract = TypeService::isAbstractType($namedType); |
|
| 175 | 9 | $validItemStructure = false; |
|
| 176 | |||
| 177 | 9 | foreach ($resolvedValue as $resolvedValueItem) { |
|
|
|
|||
| 178 | 8 | $value[] = []; |
|
| 179 | 8 | $index = count($value) - 1; |
|
| 180 | |||
| 181 | 8 | if ($isAbstract) { |
|
| 182 | 2 | $namedType = $this->resolveValidator->resolveAbstractType($fieldType->getNamedType(), $resolvedValueItem); |
|
| 183 | } |
||
| 184 | |||
| 185 | 8 | if (!$validItemStructure) { |
|
| 186 | 8 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 187 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 188 | 1 | $value[$index] = null; |
|
| 189 | 1 | continue; |
|
| 190 | } |
||
| 191 | 7 | $validItemStructure = true; |
|
| 192 | } |
||
| 193 | |||
| 194 | 9 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 195 | } |
||
| 196 | } else { |
||
| 197 | 22 | if (!$query->hasFields()) { |
|
| 198 | 5 | if (TypeService::isObjectType($fieldType)) { |
|
| 199 | 1 | throw new ResolveException(sprintf('You have to specify fields for "%s"', $query->getName())); |
|
| 200 | } |
||
| 201 | |||
| 202 | 4 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 203 | } |
||
| 204 | |||
| 205 | 20 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 206 | } |
||
| 207 | |||
| 208 | 20 | return $value; |
|
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param FieldAst $fieldAst |
||
| 213 | * @param AbstractField $field |
||
| 214 | * |
||
| 215 | * @param mixed $contextValue |
||
| 216 | * @return array|mixed|null |
||
| 217 | * @throws ResolveException |
||
| 218 | * @throws \Exception |
||
| 219 | */ |
||
| 220 | 19 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 221 | { |
||
| 222 | 19 | $value = null; |
|
| 223 | 19 | $fieldType = $field->getType(); |
|
| 224 | 19 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 225 | |||
| 226 | 19 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 227 | 1 | $listValue = []; |
|
| 228 | 1 | $type = $fieldType->getNamedType(); |
|
| 229 | |||
| 230 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 231 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
| 232 | } |
||
| 233 | |||
| 234 | 1 | $value = $listValue; |
|
| 235 | } else { |
||
| 236 | 19 | $value = $this->getOutputValue($fieldType, $preResolvedValue); |
|
| 237 | } |
||
| 238 | |||
| 239 | 19 | return $value; |
|
| 240 | } |
||
| 241 | |||
| 242 | 24 | protected function createResolveInfo($field, $fields) |
|
| 246 | |||
| 247 | /** |
||
| 248 | * @param $contextValue |
||
| 249 | * @param FieldAst $fieldAst |
||
| 250 | * @param AbstractField $field |
||
| 251 | * |
||
| 252 | * @throws \Exception |
||
| 253 | * |
||
| 254 | * @return mixed |
||
| 255 | */ |
||
| 256 | 19 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 257 | { |
||
| 258 | 19 | if ($field->hasArguments() && !$this->resolveValidator->validateArguments($field, $fieldAst, $this->executionContext->getRequest())) { |
|
| 259 | throw new \Exception(sprintf('Not valid arguments for the field "%s"', $fieldAst->getName())); |
||
| 260 | |||
| 261 | } |
||
| 262 | |||
| 263 | 19 | return $this->resolveFieldValue($field, $contextValue, [$fieldAst], $fieldAst->getKeyValueArguments()); |
|
| 264 | |||
| 265 | } |
||
| 266 | |||
| 267 | 24 | protected function resolveFieldValue(AbstractField $field, $contextValue, array $fields, array $args) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @param $field AbstractField |
||
| 274 | * @param $query Query |
||
| 275 | * |
||
| 276 | * @return array |
||
| 277 | */ |
||
| 278 | 24 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 279 | { |
||
| 280 | 24 | $args = []; |
|
| 281 | 24 | foreach ($query->getArguments() as $argument) { |
|
| 282 | 15 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
| 283 | 15 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | 24 | return $args; |
|
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $query Query|FragmentInterface |
||
| 292 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 293 | * @param $resolvedValue mixed |
||
| 294 | * @param $value array |
||
| 295 | * |
||
| 296 | * @throws \Exception |
||
| 297 | * |
||
| 298 | * @return array |
||
| 299 | */ |
||
| 300 | 20 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 301 | { |
||
| 302 | 20 | $currentType = $queryType->getNullableType(); |
|
| 303 | |||
| 304 | 20 | View Code Duplication | if ($currentType->getKind() == TypeMap::KIND_SCALAR) { |
| 305 | 1 | if (!$query->hasFields()) { |
|
| 306 | 1 | return $this->getOutputValue($currentType, $resolvedValue); |
|
| 307 | } else { |
||
| 308 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Fields are not found in query "%s"', $query->getName()))); |
|
| 309 | |||
| 310 | 1 | return null; |
|
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | 20 | foreach ($query->getFields() as $fieldAst) { |
|
| 315 | |||
| 316 | 20 | if ($fieldAst instanceof FragmentInterface) { |
|
| 317 | /** @var TypedFragmentReference $fragment */ |
||
| 318 | 3 | $fragment = $fieldAst; |
|
| 319 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
| 320 | /** @var Fragment $fragment */ |
||
| 321 | 2 | $fieldAstName = $fieldAst->getName(); |
|
| 322 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAstName); |
|
| 323 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
| 324 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
| 325 | 1 | continue; |
|
| 326 | } |
||
| 327 | |||
| 328 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
| 329 | 3 | $value = is_array($fragmentValue) ? $fragmentValue : []; |
|
| 330 | } else { |
||
| 331 | 20 | $fieldAstName = $fieldAst->getName(); |
|
| 332 | 20 | $alias = $fieldAst->getAlias() ?: $fieldAstName; |
|
| 333 | |||
| 334 | 20 | if ($fieldAstName == self::TYPE_NAME_QUERY) { |
|
| 335 | 1 | $value[$alias] = $queryType->getName(); |
|
| 336 | } else { |
||
| 337 | 20 | $field = $currentType->getField($fieldAstName); |
|
| 338 | 20 | if (!$field) { |
|
| 339 | 3 | $this->executionContext->addError(new ResolveException(sprintf('Field "%s" is not found in type "%s"', $fieldAstName, $currentType->getName()))); |
|
| 340 | |||
| 341 | 3 | return null; |
|
| 342 | } |
||
| 343 | 20 | if ($fieldAst instanceof Query) { |
|
| 344 | 10 | $value[$alias] = $this->processQueryAST($fieldAst, $field, $resolvedValue); |
|
| 345 | } elseif ($fieldAst instanceof FieldAst) { |
||
| 346 | 19 | $value[$alias] = $this->processFieldAST($fieldAst, $field, $resolvedValue); |
|
| 347 | } else { |
||
| 348 | 20 | return $value; |
|
| 349 | } |
||
| 350 | } |
||
| 351 | } |
||
| 352 | |||
| 353 | } |
||
| 354 | |||
| 355 | 20 | return $value; |
|
| 356 | } |
||
| 357 | |||
| 358 | protected function getFieldValidatedValue(AbstractField $field, $value) |
||
| 362 | |||
| 363 | 21 | protected function getOutputValue(AbstractType $type, $value) |
|
| 367 | |||
| 368 | 28 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 374 | |||
| 375 | 27 | public function getResponseData() |
|
| 376 | { |
||
| 377 | 27 | $result = []; |
|
| 378 | |||
| 379 | 27 | if (!empty($this->data)) { |
|
| 380 | 24 | $result['data'] = $this->data; |
|
| 381 | } |
||
| 382 | |||
| 383 | 27 | if ($this->executionContext->hasErrors()) { |
|
| 384 | 10 | $result['errors'] = $this->executionContext->getErrorsArray(); |
|
| 385 | } |
||
| 386 | |||
| 387 | 27 | return $result; |
|
| 388 | } |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Convenience function for attaching a MaxComplexityQueryVisitor($max) to the next processor run |
||
| 392 | * |
||
| 393 | * @param int $max |
||
| 394 | */ |
||
| 395 | 1 | public function setMaxComplexity($max) |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Apply all of $reducers to this query. Example reducer operations: checking for maximum query complexity, |
||
| 402 | * performing look-ahead query planning, etc. |
||
| 403 | * |
||
| 404 | * @param AbstractType $queryType |
||
| 405 | * @param AbstractType $mutationType |
||
| 406 | * @param AbstractQueryVisitor[] $reducers |
||
| 407 | */ |
||
| 408 | 27 | protected function reduceQuery($queryType, $mutationType, array $reducers) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Entry point for the `walkQuery` routine. Execution bounces between here, where the reducer's ->visit() method |
||
| 419 | * is invoked, and `walkQuery` where we send in the scores from the `visit` call. |
||
| 420 | * |
||
| 421 | * @param Query $query |
||
| 422 | * @param AbstractType $currentLevelSchema |
||
| 423 | * @param AbstractQueryVisitor $reducer |
||
| 424 | */ |
||
| 425 | 2 | protected function doVisit(Query $query, $currentLevelSchema, $reducer) |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Coroutine to walk the query and schema in DFS manner (see AbstractQueryVisitor docs for more info) and yield a |
||
| 455 | * tuple of (queryNode, schemaNode, childScore) |
||
| 456 | * |
||
| 457 | * childScore costs are accumulated via values sent into the coroutine. |
||
| 458 | * |
||
| 459 | * Most of the branching in this function is just to handle the different types in a query: Queries, Unions, |
||
| 460 | * Fragments (anonymous and named), and Fields. The core of the function is simple: recurse until we hit the base |
||
| 461 | * case of a Field and yield that back up to the visitor up in `doVisit`. |
||
| 462 | * |
||
| 463 | * @param Query|Field|FragmentInterface $queryNode |
||
| 464 | * @param AbstractField $currentLevelAST |
||
| 465 | * |
||
| 466 | * @return \Generator |
||
| 467 | */ |
||
| 468 | 2 | protected function walkQuery($queryNode, AbstractField $currentLevelAST) |
|
| 521 | } |
||
| 522 |
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.