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 | |||
| 39 | const TYPE_NAME_QUERY = '__typename'; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | protected $data; |
||
| 43 | |||
| 44 | /** @var ResolveValidatorInterface */ |
||
| 45 | protected $resolveValidator; |
||
| 46 | |||
| 47 | /** @var ExecutionContext */ |
||
| 48 | protected $executionContext; |
||
| 49 | |||
| 50 | 22 | public function __construct(AbstractSchema $schema) |
|
| 60 | |||
| 61 | |||
| 62 | 21 | public function processPayload($payload, $variables = []) |
|
| 63 | { |
||
| 64 | 21 | if ($this->executionContext->hasErrors()) { |
|
| 65 | 4 | $this->executionContext->clearErrors(); |
|
| 66 | } |
||
| 67 | |||
| 68 | 21 | $this->data = []; |
|
| 69 | |||
| 70 | try { |
||
| 71 | 21 | $this->parseAndCreateRequest($payload, $variables); |
|
| 72 | |||
| 73 | 21 | $queryType = $this->executionContext->getSchema()->getQueryType(); |
|
| 74 | 21 | $mutationType = $this->executionContext->getSchema()->getMutationType(); |
|
| 75 | 21 | foreach ($this->executionContext->getRequest()->getOperationsInOrder() as $operation) { |
|
| 76 | 21 | if ($operationResult = $this->executeOperation($operation, $operation instanceof Mutation ? $mutationType : $queryType)) { |
|
| 77 | 21 | $this->data = array_merge($this->data, $operationResult); |
|
| 78 | }; |
||
| 79 | } |
||
| 80 | |||
| 81 | 3 | } catch (\Exception $e) { |
|
| 82 | 3 | $this->executionContext->addError($e); |
|
| 83 | } |
||
| 84 | |||
| 85 | 21 | return $this; |
|
| 86 | } |
||
| 87 | |||
| 88 | 21 | protected function parseAndCreateRequest($payload, $variables = []) |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @param Query|Field $query |
||
| 101 | * @param AbstractObjectType $currentLevelSchema |
||
| 102 | * @return array|bool|mixed |
||
| 103 | */ |
||
| 104 | 21 | protected function executeOperation(Query $query, $currentLevelSchema) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param Query $query |
||
| 123 | * @param AbstractField $field |
||
| 124 | * @param $contextValue |
||
| 125 | * @return array|mixed|null |
||
| 126 | */ |
||
| 127 | 19 | protected function processQueryAST(Query $query, AbstractField $field, $contextValue = null) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * @param Query|Mutation $query |
||
| 140 | * @param AbstractType $fieldType |
||
| 141 | * @param mixed $resolvedValue |
||
| 142 | * @return array|mixed |
||
| 143 | */ |
||
| 144 | 19 | protected function collectValueForQueryWithType(Query $query, AbstractType $fieldType, $resolvedValue) |
|
| 145 | { |
||
| 146 | 19 | $fieldType = $this->resolveValidator->resolveTypeIfAbstract($fieldType, $resolvedValue); |
|
|
|
|||
| 147 | 19 | if (is_null($resolvedValue)) return null; |
|
| 148 | |||
| 149 | 17 | $value = []; |
|
| 150 | 17 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 151 | 8 | foreach ((array)$resolvedValue as $resolvedValueItem) { |
|
| 152 | 7 | $value[] = []; |
|
| 153 | 7 | $index = count($value) - 1; |
|
| 154 | |||
| 155 | |||
| 156 | 7 | $namedType = $fieldType->getNamedType(); |
|
| 157 | 7 | $namedType = $this->resolveValidator->resolveTypeIfAbstract($namedType, $resolvedValueItem); |
|
| 158 | 7 | if (!$namedType->isValidValue($resolvedValueItem)) { |
|
| 159 | 1 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $query->getName()))); |
|
| 160 | 1 | $value[$index] = null; |
|
| 161 | 1 | continue; |
|
| 162 | } |
||
| 163 | |||
| 164 | 8 | $value[$index] = $this->processQueryFields($query, $namedType, $resolvedValueItem, $value[$index]); |
|
| 165 | } |
||
| 166 | } else { |
||
| 167 | 17 | if (!$query->hasFields()) { |
|
| 168 | 2 | return $this->getOutputValue($fieldType, $resolvedValue); |
|
| 169 | } |
||
| 170 | |||
| 171 | 17 | $value = $this->processQueryFields($query, $fieldType, $resolvedValue, $value); |
|
| 172 | } |
||
| 173 | |||
| 174 | 17 | return $value; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param FieldAst $fieldAst |
||
| 179 | * @param AbstractField $field |
||
| 180 | * |
||
| 181 | * @param mixed $contextValue |
||
| 182 | * @return array|mixed|null |
||
| 183 | * @throws ResolveException |
||
| 184 | * @throws \Exception |
||
| 185 | */ |
||
| 186 | 16 | protected function processFieldAST(FieldAst $fieldAst, AbstractField $field, $contextValue) |
|
| 187 | { |
||
| 188 | 16 | $value = null; |
|
| 189 | 16 | $fieldType = $field->getType(); |
|
| 190 | 16 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $fieldAst, $field); |
|
| 191 | |||
| 192 | 16 | if ($fieldType->getKind() == TypeMap::KIND_LIST) { |
|
| 193 | 1 | $listValue = []; |
|
| 194 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
| 195 | 1 | $type = $fieldType->getNamedType(); |
|
| 196 | |||
| 197 | 1 | if (!$type->isValidValue($resolvedValueItem)) { |
|
| 198 | $this->executionContext->addError(new ResolveException(sprintf('Not valid resolve value in %s field', $field->getName()))); |
||
| 199 | |||
| 200 | $listValue = null; |
||
| 201 | break; |
||
| 202 | } |
||
| 203 | 1 | $listValue[] = $this->getOutputValue($type, $resolvedValueItem); |
|
| 204 | } |
||
| 205 | |||
| 206 | 1 | $value = $listValue; |
|
| 207 | } else { |
||
| 208 | 16 | $value = $this->getFieldValidatedValue($field, $preResolvedValue); |
|
| 209 | } |
||
| 210 | |||
| 211 | 16 | return $value; |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param AbstractField $field |
||
| 216 | * @param mixed $contextValue |
||
| 217 | * @param Query $query |
||
| 218 | * |
||
| 219 | * @return mixed |
||
| 220 | */ |
||
| 221 | 19 | protected function resolveFieldValue(AbstractField $field, $contextValue, Query $query) |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @param $contextValue |
||
| 236 | * @param FieldAst $fieldAst |
||
| 237 | * @param AbstractField $field |
||
| 238 | * |
||
| 239 | * @throws \Exception |
||
| 240 | * |
||
| 241 | * @return mixed |
||
| 242 | */ |
||
| 243 | 16 | protected function getPreResolvedValue($contextValue, FieldAst $fieldAst, AbstractField $field) |
|
| 272 | |||
| 273 | /** |
||
| 274 | * @param $field AbstractField |
||
| 275 | * @param $query Query |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | 19 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
| 290 | |||
| 291 | /** |
||
| 292 | * @param $query Query|FragmentInterface |
||
| 293 | * @param $queryType AbstractObjectType|TypeInterface|Field|AbstractType |
||
| 294 | * @param $resolvedValue mixed |
||
| 295 | * @param $value array |
||
| 296 | * |
||
| 297 | * @throws \Exception |
||
| 298 | * |
||
| 299 | * @return array |
||
| 300 | */ |
||
| 301 | 17 | protected function processQueryFields($query, AbstractType $queryType, $resolvedValue, $value) |
|
| 349 | |||
| 350 | 16 | protected function getFieldValidatedValue(AbstractField $field, $value) |
|
| 354 | |||
| 355 | 16 | protected function getOutputValue(AbstractType $type, $value) |
|
| 359 | |||
| 360 | 17 | protected function collectValue($value, $queryValue) |
|
| 370 | |||
| 371 | 21 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
| 379 | |||
| 380 | 21 | public function getResponseData() |
|
| 394 | } |
||
| 395 |