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 |
||
31 | class Processor |
||
32 | { |
||
33 | |||
34 | const TYPE_NAME_QUERY = '__typename'; |
||
35 | |||
36 | /** @var array */ |
||
37 | protected $data; |
||
38 | |||
39 | /** @var ResolveValidatorInterface */ |
||
40 | protected $resolveValidator; |
||
41 | |||
42 | /** @var Schema */ |
||
43 | protected $schema; |
||
44 | |||
45 | /** @var PropertyAccessor */ |
||
46 | protected $propertyAccessor; |
||
47 | |||
48 | /** @var Request */ |
||
49 | protected $request; |
||
50 | |||
51 | 17 | public function __construct(ResolveValidatorInterface $validator) |
|
52 | { |
||
53 | 17 | $this->resolveValidator = $validator; |
|
54 | |||
55 | 17 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
56 | 17 | } |
|
57 | |||
58 | 17 | public function processQuery($queryString, $variables = []) |
|
59 | { |
||
60 | 17 | $this->resolveValidator->clearErrors(); |
|
61 | 17 | $this->data = []; |
|
62 | |||
63 | 1 | try { |
|
64 | 17 | $this->parseAndCreateRequest($queryString, $variables); |
|
65 | |||
66 | 17 | if ($this->request->hasQueries()) { |
|
67 | 17 | foreach ($this->request->getQueries() as $query) { |
|
68 | 17 | if ($queryResult = $this->executeQuery($query, $this->getSchema()->getQueryType())) { |
|
69 | 16 | $this->data = array_merge($this->data, $queryResult); |
|
70 | 16 | }; |
|
71 | 17 | } |
|
72 | 17 | } |
|
73 | |||
74 | 17 | if ($this->request->hasMutations()) { |
|
75 | foreach ($this->request->getMutations() as $mutation) { |
||
76 | if ($mutationResult = $this->executeMutation($mutation, $this->getSchema()->getMutationType())) { |
||
77 | $this->data = array_merge($this->data, $mutationResult); |
||
78 | 1 | } |
|
79 | } |
||
80 | } |
||
81 | 17 | } catch (\Exception $e) { |
|
82 | $this->resolveValidator->clearErrors(); |
||
83 | |||
84 | $this->resolveValidator->addError($e); |
||
85 | } |
||
86 | 17 | } |
|
87 | |||
88 | /** |
||
89 | * @param Mutation $mutation |
||
90 | * @param InputObjectType $objectType |
||
91 | * |
||
92 | * @return array|bool|mixed |
||
93 | */ |
||
94 | protected function executeMutation($mutation, $objectType) |
||
95 | { |
||
96 | if (!$this->checkFieldExist($objectType, $mutation)) { |
||
97 | |||
98 | return null; |
||
99 | } |
||
100 | |||
101 | /** @var Field $field */ |
||
102 | $field = $objectType->getConfig()->getField($mutation->getName()); |
||
103 | |||
104 | if (!$this->resolveValidator->validateArguments($field, $mutation, $this->request)) { |
||
105 | return null; |
||
106 | } |
||
107 | |||
108 | $alias = $mutation->hasAlias() ? $mutation->getAlias() : $mutation->getName(); |
||
109 | $resolvedValue = $this->resolveValue($field, null, $mutation); |
||
110 | |||
111 | if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType()->getKind())) { |
||
|
|||
112 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for mutation "%s"', $field->getType()->getName()))); |
||
113 | |||
114 | return [$alias => null]; |
||
115 | } |
||
116 | |||
117 | $value = null; |
||
118 | if ($mutation->hasFields()) { |
||
119 | $outputType = $field->getType()->getConfig()->getOutputType(); |
||
120 | |||
121 | $value = $this->processQueryFields($mutation, $outputType, $resolvedValue, []); |
||
122 | } |
||
123 | |||
124 | return [$alias => $value]; |
||
125 | } |
||
126 | |||
127 | 17 | protected function parseAndCreateRequest($query, $variables = []) |
|
128 | { |
||
129 | 17 | $parser = new Parser(); |
|
130 | |||
131 | 17 | $parser->setSource($query); |
|
132 | 17 | $data = $parser->parse(); |
|
133 | |||
134 | 17 | $this->request = new Request($data); |
|
135 | 17 | $this->request->setVariables($variables); |
|
136 | 17 | } |
|
137 | |||
138 | /** |
||
139 | * @param Query|Field $query |
||
140 | * @param ObjectType $currentLevelSchema |
||
141 | * @param null $contextValue |
||
142 | * @return array|bool|mixed |
||
143 | */ |
||
144 | 17 | protected function executeQuery($query, $currentLevelSchema, $contextValue = null) |
|
145 | { |
||
146 | 17 | if (!$this->checkFieldExist($currentLevelSchema, $query)) { |
|
147 | return null; |
||
148 | } |
||
149 | |||
150 | /** @var Field $field */ |
||
151 | 17 | $field = $currentLevelSchema->getConfig()->getField($query->getName()); |
|
152 | 17 | if (get_class($query) == 'Youshido\GraphQL\Parser\Ast\Field') { |
|
153 | 15 | $alias = $query->getName(); |
|
154 | 15 | $preResolvedValue = $this->getPreResolvedValue($contextValue, $query); |
|
155 | |||
156 | 15 | if ($field->getConfig()->getType()->getKind() == TypeMap::KIND_LIST) { |
|
157 | 1 | if(!is_array($preResolvedValue)){ |
|
158 | $value = null; |
||
159 | $this->resolveValidator->addError(new ResolveException('Not valid resolve value for list type')); |
||
160 | } |
||
161 | |||
162 | |||
163 | 1 | $listValue = []; |
|
164 | 1 | foreach ($preResolvedValue as $resolvedValueItem) { |
|
165 | /** @var TypeInterface $type */ |
||
166 | 1 | $type = $field->getType()->getConfig()->getItem(); |
|
167 | |||
168 | 1 | if ($type->getKind() == TypeMap::KIND_ENUM) { |
|
169 | /** @var $type AbstractEnumType */ |
||
170 | 1 | if(!$type->isValidValue($resolvedValueItem)) { |
|
171 | $this->resolveValidator->addError(new ResolveException('Not valid value for enum type')); |
||
172 | |||
173 | $listValue = null; |
||
174 | break; |
||
175 | } |
||
176 | |||
177 | 1 | $listValue[] = $type->resolve($resolvedValueItem); |
|
178 | 1 | } else { |
|
179 | /** @var AbstractScalarType $type */ |
||
180 | $listValue[] = $type->serialize($preResolvedValue); |
||
181 | } |
||
182 | 1 | } |
|
183 | |||
184 | 1 | $value = $listValue; |
|
185 | 1 | } else { |
|
186 | 15 | if ($field->getType()->getKind() == TypeMap::KIND_ENUM) { |
|
187 | if(!$field->getType()->isValidValue($preResolvedValue)) { |
||
188 | $this->resolveValidator->addError(new ResolveException('Not valid value for enum type')); |
||
189 | $value = null; |
||
190 | } else { |
||
191 | $value = $field->getType()->resolve($preResolvedValue); |
||
192 | } |
||
193 | } else { |
||
194 | 15 | $value = $field->getType()->serialize($preResolvedValue); |
|
195 | } |
||
196 | } |
||
197 | 15 | } else { |
|
198 | 17 | if (!$this->resolveValidator->validateArguments($field, $query, $this->request)) { |
|
199 | 1 | return null; |
|
200 | } |
||
201 | |||
202 | 16 | $resolvedValue = $this->resolveValue($field, $contextValue, $query); |
|
203 | 16 | $alias = $query->hasAlias() ? $query->getAlias() : $query->getName(); |
|
204 | |||
205 | 16 | if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType()->getKind())) { |
|
206 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for query "%s"', $field->getType()->getName()))); |
||
207 | |||
208 | return [$alias => null]; |
||
209 | } |
||
210 | |||
211 | 16 | $value = []; |
|
212 | 16 | if ($resolvedValue) { |
|
213 | 15 | if ($field->getType()->getKind() == TypeMap::KIND_LIST) { |
|
214 | 6 | foreach ($resolvedValue as $resolvedValueItem) { |
|
215 | 6 | $value[] = []; |
|
216 | 6 | $index = count($value) - 1; |
|
217 | |||
218 | 6 | if(in_array($field->getConfig()->getType()->getConfig()->getItem()->getKind(), [TypeMap::KIND_UNION, TypeMap::KIND_INTERFACE]) ) { |
|
219 | 4 | $type = $field->getConfig()->getType()->getConfig()->getItemConfig()->resolveType($resolvedValueItem); |
|
220 | 4 | } else { |
|
221 | 2 | $type = $field->getType(); |
|
222 | } |
||
223 | |||
224 | 6 | $value[$index] = $this->processQueryFields($query, $type, $resolvedValueItem, $value[$index]); |
|
225 | 6 | } |
|
226 | 6 | } else { |
|
227 | 13 | $value = $this->processQueryFields($query, $field->getType(), $resolvedValue, $value); |
|
228 | } |
||
229 | 15 | } else { |
|
230 | 3 | $value = $resolvedValue; |
|
231 | } |
||
232 | } |
||
233 | |||
234 | 16 | return [$alias => $value]; |
|
235 | } |
||
236 | |||
237 | /** |
||
238 | * @param $objectType InputObjectType|ObjectType |
||
239 | * @param $query Mutation|Query |
||
240 | * @return null |
||
241 | */ |
||
242 | 17 | private function checkFieldExist($objectType, $query) |
|
243 | { |
||
244 | 17 | if (!$objectType->getConfig()->hasField($query->getName())) { |
|
245 | $this->resolveValidator->addError(new ResolveException(sprintf('Field "%s" not found in schema', $query->getName()))); |
||
246 | |||
247 | return false; |
||
248 | } |
||
249 | |||
250 | 17 | return true; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * @param $value |
||
255 | * @param $query Field |
||
256 | * |
||
257 | * @throws \Exception |
||
258 | * |
||
259 | * @return mixed |
||
260 | */ |
||
261 | 15 | protected function getPreResolvedValue($value, $query) |
|
262 | { |
||
263 | 15 | if (is_array($value)) { |
|
264 | 11 | if (array_key_exists($query->getName(), $value)) { |
|
265 | 11 | return $value[$query->getName()]; |
|
266 | } else { |
||
267 | throw new \Exception('Not found in resolve result', $query->getName()); |
||
268 | } |
||
269 | 4 | } elseif (is_object($value)) { |
|
270 | 4 | return $this->propertyAccessor->getValue($value, $query->getName()); |
|
271 | } |
||
272 | |||
273 | return $value; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * @param $field Field |
||
278 | * @param $contextValue mixed |
||
279 | * @param $query Query |
||
280 | * |
||
281 | * @return mixed |
||
282 | */ |
||
283 | 16 | protected function resolveValue($field, $contextValue, $query) |
|
284 | { |
||
285 | 16 | $resolvedValue = $field->getConfig()->resolve($contextValue, $this->parseArgumentsValues($field, $query)); |
|
286 | |||
287 | 16 | if(in_array($field->getType()->getKind(), [TypeMap::KIND_UNION, TypeMap::KIND_INTERFACE])){ |
|
288 | 6 | $resolvedType = $field->getType()->resolveType($resolvedValue); |
|
289 | 6 | $field->setType($resolvedType); |
|
290 | 6 | } |
|
291 | |||
292 | 16 | return $resolvedValue; |
|
293 | } |
||
294 | |||
295 | /** |
||
296 | * @param $field Field |
||
297 | * @param $query Query |
||
298 | * |
||
299 | * @return array |
||
300 | */ |
||
301 | 16 | public function parseArgumentsValues($field, $query) |
|
314 | |||
315 | /** |
||
316 | * @param $query Query |
||
317 | * @param $queryType ObjectType|TypeInterface|Field |
||
318 | * @param $resolvedValue mixed |
||
319 | * @param $value array |
||
320 | * |
||
321 | * @throws \Exception |
||
322 | * |
||
323 | * @return array |
||
324 | */ |
||
325 | 15 | protected function processQueryFields($query, $queryType, $resolvedValue, $value) |
|
357 | |||
358 | 15 | protected function collectValue($value, $queryValue) |
|
368 | |||
369 | 17 | public function getSchema() |
|
373 | |||
374 | 17 | public function setSchema(Schema $schema) |
|
387 | |||
388 | /** |
||
389 | * @return ResolveValidatorInterface |
||
390 | */ |
||
391 | public function getResolveValidator() |
||
395 | |||
396 | 17 | public function getResponseData() |
|
410 | } |
||
411 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.