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) |
|
244 | { |
||
245 | 16 | $resolved = false; |
|
246 | 16 | $resolverValue = null; |
|
247 | |||
248 | 16 | if (is_array($contextValue) && array_key_exists($fieldAst->getName(), $contextValue)) { |
|
249 | 12 | $resolverValue = $contextValue[$fieldAst->getName()]; |
|
250 | 12 | $resolved = true; |
|
251 | 7 | } elseif (is_object($contextValue)) { |
|
252 | 6 | $resolverValue = TypeService::getPropertyValue($contextValue, $fieldAst->getName()); |
|
253 | 6 | $resolved = true; |
|
254 | } |
||
255 | |||
256 | 16 | if (!$resolved && $field->getType()->getNamedType()->getKind() == TypeMap::KIND_SCALAR) { |
|
257 | 1 | $resolved = true; |
|
258 | } |
||
259 | |||
260 | 16 | if ($resolveFunction = $field->getConfig()->getResolveFunction()) { |
|
261 | 1 | $resolveInfo = new ResolveInfo($field, [$fieldAst], $field->getType(), $this->executionContext); |
|
262 | |||
263 | 1 | $resolverValue = $resolveFunction($resolved ? $resolverValue : $contextValue, $fieldAst->getKeyValueArguments(), $resolveInfo); |
|
264 | } |
||
265 | |||
266 | 16 | if (!$resolverValue && !$resolved) { |
|
267 | 1 | throw new \Exception(sprintf('Property "%s" not found in resolve result', $fieldAst->getName())); |
|
268 | } |
||
269 | |||
270 | 16 | return $resolverValue; |
|
271 | } |
||
272 | |||
273 | /** |
||
274 | * @param $field AbstractField |
||
275 | * @param $query Query |
||
276 | * |
||
277 | * @return array |
||
278 | */ |
||
279 | 19 | protected function parseArgumentsValues(AbstractField $field, Query $query) |
|
280 | { |
||
281 | 19 | $args = []; |
|
282 | 19 | foreach ($query->getArguments() as $argument) { |
|
283 | 7 | if ($configArgument = $field->getConfig()->getArgument($argument->getName())) { |
|
284 | 7 | $args[$argument->getName()] = $configArgument->getType()->parseValue($argument->getValue()->getValue()); |
|
285 | } |
||
286 | } |
||
287 | |||
288 | 19 | return $args; |
|
289 | } |
||
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) |
|
302 | { |
||
303 | 17 | foreach ($query->getFields() as $fieldAst) { |
|
304 | 17 | $fieldResolvedValue = null; |
|
305 | |||
306 | 17 | if ($fieldAst instanceof FragmentInterface) { |
|
307 | /** @var TypedFragmentReference $fragment */ |
||
308 | 3 | $fragment = $fieldAst; |
|
309 | 3 | if ($fieldAst instanceof FragmentReference) { |
|
310 | /** @var Fragment $fragment */ |
||
311 | 2 | $fragment = $this->executionContext->getRequest()->getFragment($fieldAst->getName()); |
|
312 | 2 | $this->resolveValidator->assertValidFragmentForField($fragment, $fieldAst, $queryType); |
|
313 | 1 | } elseif ($fragment->getTypeName() !== $queryType->getName()) { |
|
314 | 1 | continue; |
|
315 | } |
||
316 | |||
317 | 3 | $fragmentValue = $this->processQueryFields($fragment, $queryType, $resolvedValue, $value); |
|
318 | 3 | $fieldResolvedValue = is_array($fragmentValue) ? $fragmentValue : []; |
|
319 | } else { |
||
320 | 17 | $alias = $fieldAst->getAlias() ?: $fieldAst->getName(); |
|
321 | 17 | $currentType = $queryType->getNullableType(); |
|
322 | |||
323 | 17 | if ($fieldAst->getName() == self::TYPE_NAME_QUERY) { |
|
324 | 1 | $fieldResolvedValue = [$alias => $queryType->getName()]; |
|
325 | } elseif ($fieldAst instanceof Query) { |
||
326 | 9 | $fieldValue = $this->processQueryAST($fieldAst, $currentType->getField($fieldAst->getName()), $resolvedValue); |
|
327 | 9 | $fieldResolvedValue = [$alias => $fieldValue]; |
|
328 | } elseif ($fieldAst instanceof FieldAst) { |
||
329 | |||
330 | 16 | if (!$this->resolveValidator->objectHasField($currentType, $fieldAst)) { |
|
331 | 2 | $fieldResolvedValue = null; |
|
332 | } else { |
||
333 | $fieldResolvedValue = [ |
||
334 | 16 | $alias => $this->processFieldAST($fieldAst, $currentType->getField($fieldAst->getName()), $resolvedValue) |
|
335 | ]; |
||
336 | } |
||
337 | } |
||
338 | } |
||
339 | |||
340 | 17 | $value = $this->collectValue($value, $fieldResolvedValue); |
|
341 | } |
||
342 | |||
343 | 17 | return $value; |
|
344 | } |
||
345 | |||
346 | 16 | protected function getFieldValidatedValue(AbstractField $field, $value) |
|
350 | |||
351 | 16 | protected function getOutputValue(AbstractType $type, $value) |
|
355 | |||
356 | 17 | protected function collectValue($value, $queryValue) |
|
357 | { |
||
358 | 17 | if ($queryValue && is_array($queryValue)) { |
|
359 | 17 | $value = array_merge(is_array($value) ? $value : [], $queryValue); |
|
360 | } else { |
||
361 | 2 | $value = $queryValue; |
|
362 | } |
||
363 | |||
364 | 17 | return $value; |
|
365 | } |
||
366 | |||
367 | 21 | protected function introduceIntrospectionFields(AbstractSchema $schema) |
|
375 | |||
376 | 21 | public function getResponseData() |
|
377 | { |
||
378 | 21 | $result = []; |
|
379 | |||
380 | 21 | if (!empty($this->data)) { |
|
381 | 19 | $result['data'] = $this->data; |
|
382 | } |
||
390 | } |
||
391 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.