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  | 
            ||
| 42 | class Processor  | 
            ||
| 43 | { | 
            ||
| 44 | |||
| 45 | const TYPE_NAME_QUERY = '__typename';  | 
            ||
| 46 | |||
| 47 | /** @var ExecutionContext */  | 
            ||
| 48 | protected $executionContext;  | 
            ||
| 49 | |||
| 50 | /** @var ResolveValidatorInterface */  | 
            ||
| 51 | protected $resolveValidator;  | 
            ||
| 52 | |||
| 53 | /** @var array */  | 
            ||
| 54 | protected $data;  | 
            ||
| 55 | |||
| 56 | /** @var int */  | 
            ||
| 57 | protected $maxComplexity;  | 
            ||
| 58 | |||
| 59 | 39 | public function __construct(AbstractSchema $schema)  | 
            |
| 60 |     { | 
            ||
| 61 | 39 |         if (empty($this->executionContext)) { | 
            |
| 62 | 39 | $this->executionContext = new ExecutionContext($schema);  | 
            |
| 63 | 39 | $this->executionContext->setContainer(new Container());  | 
            |
| 64 | }  | 
            ||
| 65 | |||
| 66 | 39 | $this->resolveValidator = new ResolveValidator($this->executionContext);  | 
            |
| 
                                                                                                    
                        
                         | 
                |||
| 67 | 39 | }  | 
            |
| 68 | |||
| 69 | 37 | public function processPayload($payload, $variables = [], $reducers = [])  | 
            |
| 70 |     { | 
            ||
| 71 | 37 | $this->data = [];  | 
            |
| 72 | |||
| 73 |         try { | 
            ||
| 74 | 37 | $this->parseAndCreateRequest($payload, $variables);  | 
            |
| 75 | |||
| 76 | 36 |             if ($this->maxComplexity) { | 
            |
| 77 | 1 | $reducers[] = new MaxComplexityQueryVisitor($this->maxComplexity);  | 
            |
| 78 | }  | 
            ||
| 79 | |||
| 80 | 36 |             if ($reducers) { | 
            |
| 81 | 2 | $reducer = new Reducer();  | 
            |
| 82 | 2 | $reducer->reduceQuery($this->executionContext, $reducers);  | 
            |
| 83 | }  | 
            ||
| 84 | |||
| 85 | 36 |             foreach ($this->executionContext->getRequest()->getAllOperations() as $query) { | 
            |
| 86 | 36 |                 if ($operationResult = $this->resolveQuery($query)) { | 
            |
| 87 | 36 | $this->data = array_merge($this->data, $operationResult);  | 
            |
| 88 | };  | 
            ||
| 89 | }  | 
            ||
| 90 | 5 |         } catch (\Exception $e) { | 
            |
| 91 | 5 | $this->executionContext->addError($e);  | 
            |
| 92 | }  | 
            ||
| 93 | |||
| 94 | 37 | return $this;  | 
            |
| 95 | }  | 
            ||
| 96 | |||
| 97 | 37 | public function getResponseData()  | 
            |
| 98 |     { | 
            ||
| 99 | 37 | $result = [];  | 
            |
| 100 | |||
| 101 | 37 |         if (!empty($this->data)) { | 
            |
| 102 | 36 | $result['data'] = $this->data;  | 
            |
| 103 | }  | 
            ||
| 104 | |||
| 105 | 37 |         if ($this->executionContext->hasErrors()) { | 
            |
| 106 | 15 | $result['errors'] = $this->executionContext->getErrorsArray();  | 
            |
| 107 | }  | 
            ||
| 108 | |||
| 109 | 37 | return $result;  | 
            |
| 110 | }  | 
            ||
| 111 | |||
| 112 | /**  | 
            ||
| 113 | * You can access ExecutionContext to check errors and inject dependencies  | 
            ||
| 114 | *  | 
            ||
| 115 | * @return ExecutionContext  | 
            ||
| 116 | */  | 
            ||
| 117 | 10 | public function getExecutionContext()  | 
            |
| 121 | |||
| 122 | /**  | 
            ||
| 123 | * @return int  | 
            ||
| 124 | */  | 
            ||
| 125 | public function getMaxComplexity()  | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * @param int $maxComplexity  | 
            ||
| 132 | */  | 
            ||
| 133 | 1 | public function setMaxComplexity($maxComplexity)  | 
            |
| 137 | |||
| 138 | 36 | protected function resolveQuery(AstQuery $query)  | 
            |
| 152 | |||
| 153 | 36 | protected function resolveField(FieldInterface $field, AstFieldInterface $ast, $parentValue = null, $fromObject = false)  | 
            |
| 154 |     { | 
            ||
| 155 |         try { | 
            ||
| 156 | /** @var AbstractObjectType $type */  | 
            ||
| 157 | 36 | $type = $field->getType();  | 
            |
| 158 | 36 | $nonNullType = $type->getNullableType();  | 
            |
| 159 | |||
| 160 | 36 |             if (self::TYPE_NAME_QUERY == $ast->getName()) { | 
            |
| 161 | 2 | return $nonNullType->getName();  | 
            |
| 162 | }  | 
            ||
| 163 | |||
| 164 | 36 | $this->resolveValidator->assetTypeHasField($nonNullType, $ast);  | 
            |
| 165 | |||
| 166 | 36 | $targetField = $nonNullType->getField($ast->getName());  | 
            |
| 167 | |||
| 168 | 36 | $this->prepareAstArguments($targetField, $ast, $this->executionContext->getRequest());  | 
            |
| 169 | 35 | $this->resolveValidator->assertValidArguments($targetField, $ast, $this->executionContext->getRequest());  | 
            |
| 170 | |||
| 171 | 33 |             switch ($kind = $targetField->getType()->getNullableType()->getKind()) { | 
            |
| 172 | 33 | case TypeMap::KIND_ENUM:  | 
            |
| 173 | 33 | case TypeMap::KIND_SCALAR:  | 
            |
| 174 | 27 |                     if ($ast instanceof AstQuery && $ast->hasFields()) { | 
            |
| 175 | 2 |                         throw new ResolveException(sprintf('You can\'t specify fields for scalar type "%s"', $targetField->getType()->getNullableType()->getName())); | 
            |
| 176 | }  | 
            ||
| 177 | |||
| 178 | 27 | return $this->resolveScalar($targetField, $ast, $parentValue);  | 
            |
| 179 | |||
| 180 | 28 | View Code Duplication | case TypeMap::KIND_OBJECT:  | 
            
| 181 | /** @var $type AbstractObjectType */  | 
            ||
| 182 | 20 |                     if (!$ast instanceof AstQuery) { | 
            |
| 183 | 1 |                         throw new ResolveException(sprintf('You have to specify fields for "%s"', $ast->getName())); | 
            |
| 184 | }  | 
            ||
| 185 | |||
| 186 | 20 | return $this->resolveObject($targetField, $ast, $parentValue);  | 
            |
| 187 | |||
| 188 | 17 | case TypeMap::KIND_LIST:  | 
            |
| 189 | 14 | return $this->resolveList($targetField, $ast, $parentValue);  | 
            |
| 190 | |||
| 191 | 6 | case TypeMap::KIND_UNION:  | 
            |
| 192 | 5 | View Code Duplication | case TypeMap::KIND_INTERFACE:  | 
            
| 193 | 6 |                     if (!$ast instanceof AstQuery) { | 
            |
| 194 |                         throw new ResolveException(sprintf('You have to specify fields for "%s"', $ast->getName())); | 
            ||
| 195 | }  | 
            ||
| 196 | |||
| 197 | 6 | return $this->resolveComposite($targetField, $ast, $parentValue);  | 
            |
| 198 | |||
| 199 | default:  | 
            ||
| 200 |                     throw new ResolveException(sprintf('Resolving type with kind "%s" not supported', $kind)); | 
            ||
| 201 | }  | 
            ||
| 202 | 12 |         } catch (\Exception $e) { | 
            |
| 203 | 12 | $this->executionContext->addError($e);  | 
            |
| 204 | |||
| 205 | 12 |             if ($fromObject) { | 
            |
| 206 | 4 | throw $e;  | 
            |
| 207 | }  | 
            ||
| 208 | |||
| 209 | 10 | return null;  | 
            |
| 210 | }  | 
            ||
| 211 | }  | 
            ||
| 212 | |||
| 213 | 36 | private function prepareAstArguments(FieldInterface $field, AstFieldInterface $query, Request $request)  | 
            |
| 214 |     { | 
            ||
| 215 | 36 |         foreach ($query->getArguments() as $astArgument) { | 
            |
| 216 | 15 |             if ($field->hasArgument($astArgument->getName())) { | 
            |
| 217 | 15 | $argumentType = $field->getArgument($astArgument->getName())->getType()->getNullableType();  | 
            |
| 218 | |||
| 219 | 15 | $astArgument->setValue($this->prepareArgumentValue($astArgument->getValue(), $argumentType, $request));  | 
            |
| 220 | }  | 
            ||
| 221 | }  | 
            ||
| 222 | 35 | }  | 
            |
| 223 | |||
| 224 | 15 | private function prepareArgumentValue($argumentValue, AbstractType $argumentType, Request $request)  | 
            |
| 225 |     { | 
            ||
| 226 | 15 |         switch ($argumentType->getKind()) { | 
            |
| 227 | 15 | case TypeMap::KIND_LIST:  | 
            |
| 228 | /** @var $argumentType AbstractListType */  | 
            ||
| 229 | 2 | $result = [];  | 
            |
| 230 | 2 |                 if ($argumentValue instanceof AstInputList || is_array($argumentValue)) { | 
            |
| 231 | 2 | $list = is_array($argumentValue) ? $argumentValue : $argumentValue->getValue();  | 
            |
| 232 | 2 |                     foreach ($list as $item) { | 
            |
| 233 | 2 | $result[] = $this->prepareArgumentValue($item, $argumentType->getItemType()->getNullableType(), $request);  | 
            |
| 234 | }  | 
            ||
| 235 | }  | 
            ||
| 236 | |||
| 237 | 2 | return $result;  | 
            |
| 238 | |||
| 239 | 15 | case TypeMap::KIND_INPUT_OBJECT:  | 
            |
| 240 | /** @var $argumentType AbstractInputObjectType */  | 
            ||
| 241 | 2 | $result = [];  | 
            |
| 242 | 2 |                 if ($argumentValue instanceof AstInputObject) { | 
            |
| 243 | 2 |                     foreach ($argumentValue->getValue() as $key => $item) { | 
            |
| 244 | 2 |                         if ($argumentType->hasField($key)) { | 
            |
| 245 | 2 | $result[$key] = $this->prepareArgumentValue($item, $argumentType->getField($key)->getType()->getNullableType(), $request);  | 
            |
| 246 |                         } else { | 
            ||
| 247 | 2 | $result[$key] = $item;  | 
            |
| 248 | }  | 
            ||
| 249 | }  | 
            ||
| 250 | }  | 
            ||
| 251 | |||
| 252 | 2 | return $result;  | 
            |
| 253 | |||
| 254 | 15 | case TypeMap::KIND_SCALAR:  | 
            |
| 255 | 2 | case TypeMap::KIND_ENUM:  | 
            |
| 256 | /** @var $argumentValue AstLiteral|VariableReference */  | 
            ||
| 257 | 15 |                 if ($argumentValue instanceof VariableReference) { | 
            |
| 258 | 4 | return $this->getVariableReferenceArgumentValue($argumentValue, $argumentType, $request);  | 
            |
| 259 | 12 |                 } else if ($argumentValue instanceof AstLiteral) { | 
            |
| 260 | 10 | return $argumentValue->getValue();  | 
            |
| 261 |                 } else { | 
            ||
| 262 | 3 | return $argumentValue;  | 
            |
| 263 | }  | 
            ||
| 264 | }  | 
            ||
| 265 | |||
| 266 |         throw new ResolveException('Argument type not supported'); | 
            ||
| 267 | }  | 
            ||
| 268 | |||
| 269 | 4 | private function getVariableReferenceArgumentValue(VariableReference $variableReference, AbstractType $argumentType, Request $request)  | 
            |
| 283 | |||
| 284 | 25 | protected function resolveObject(FieldInterface $field, AstFieldInterface $ast, $parentValue, $fromUnion = false)  | 
            |
| 285 |     { | 
            ||
| 286 | 25 |         if (!$fromUnion) { | 
            |
| 287 | 20 | $resolvedValue = $this->doResolve($field, $ast, $parentValue);  | 
            |
| 288 |         } else { | 
            ||
| 289 | 7 | $resolvedValue = $parentValue;  | 
            |
| 306 | |||
| 307 | 24 | private function collectResult(FieldInterface $field, AbstractObjectType $type, $ast, $resolvedValue)  | 
            |
| 363 | |||
| 364 | 28 | protected function resolveScalar(FieldInterface $field, AstFieldInterface $ast, $parentValue)  | 
            |
| 375 | |||
| 376 | 14 | protected function resolveList(FieldInterface $field, AstFieldInterface $ast, $parentValue)  | 
            |
| 441 | |||
| 442 | 7 | protected function resolveComposite(FieldInterface $field, AstFieldInterface $ast, $parentValue)  | 
            |
| 470 | |||
| 471 | 37 | protected function parseAndCreateRequest($payload, $variables = [])  | 
            |
| 484 | |||
| 485 | 33 | protected function doResolve(FieldInterface $field, AstFieldInterface $ast, $parentValue = null)  | 
            |
| 493 | |||
| 494 | 33 | protected function parseArgumentsValues(FieldInterface $field, AstFieldInterface $ast)  | 
            |
| 519 | |||
| 520 | 36 | private function getAlias(AstFieldInterface $ast)  | 
            |
| 524 | |||
| 525 | 33 | protected function createResolveInfo(FieldInterface $field, array $astFields)  | 
            |
| 529 | |||
| 530 | }  | 
            ||
| 531 | 
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignorePhpDoc annotation to the duplicate definition and it will be ignored.