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 |
||
29 | class Processor |
||
30 | { |
||
31 | |||
32 | /** @var array */ |
||
33 | protected $data; |
||
34 | |||
35 | /** @var ResolveValidatorInterface */ |
||
36 | protected $resolveValidator; |
||
37 | |||
38 | /** @var Schema */ |
||
39 | protected $schema; |
||
40 | |||
41 | /** @var PropertyAccessor */ |
||
42 | protected $propertyAccessor; |
||
43 | |||
44 | /** @var Request */ |
||
45 | protected $request; |
||
46 | |||
47 | 5 | public function __construct(ResolveValidatorInterface $validator) |
|
48 | { |
||
49 | 5 | $this->resolveValidator = $validator; |
|
50 | |||
51 | 5 | $this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
52 | 5 | } |
|
53 | |||
54 | 5 | public function processQuery($queryString, $variables = []) |
|
55 | { |
||
56 | 5 | $this->resolveValidator->clearErrors(); |
|
57 | 5 | $this->data = []; |
|
58 | |||
59 | try { |
||
60 | 5 | $this->parseAndCreateRequest($queryString, $variables); |
|
61 | |||
62 | 5 | if ($this->request->hasQueries()) { |
|
63 | 5 | foreach ($this->request->getQueries() as $query) { |
|
64 | 5 | if ($queryResult = $this->executeQuery($query, $this->getSchema()->getQueryType())) { |
|
65 | 5 | $this->data = array_merge($this->data, $queryResult); |
|
66 | 5 | }; |
|
67 | 5 | } |
|
68 | 5 | } |
|
69 | |||
70 | 5 | if ($this->request->hasMutations()) { |
|
71 | foreach ($this->request->getMutations() as $mutation) { |
||
72 | if ($mutationResult = $this->executeMutation($mutation, $this->getSchema()->getMutationType())) { |
||
73 | $this->data = array_merge($this->data, $mutationResult); |
||
74 | } |
||
75 | } |
||
76 | } |
||
77 | 5 | } catch (\Exception $e) { |
|
78 | 1 | $this->resolveValidator->clearErrors(); |
|
79 | |||
80 | $this->resolveValidator->addError($e); |
||
81 | } |
||
82 | 5 | } |
|
83 | |||
84 | /** |
||
85 | * @param Mutation $mutation |
||
86 | * @param InputObjectType $objectType |
||
87 | * |
||
88 | * @return array|bool|mixed |
||
89 | */ |
||
90 | protected function executeMutation($mutation, $objectType) |
||
91 | { |
||
92 | if (!$this->checkFieldExist($objectType, $mutation)) { |
||
93 | |||
94 | return null; |
||
95 | } |
||
96 | |||
97 | /** @var Field $field */ |
||
98 | $field = $objectType->getConfig()->getField($mutation->getName()); |
||
99 | |||
100 | if (!$this->resolveValidator->validateArguments($field->getType(), $mutation, $this->request)) { |
||
101 | return null; |
||
102 | } |
||
103 | |||
104 | $alias = $mutation->hasAlias() ? $mutation->getAlias() : $mutation->getName(); |
||
105 | $resolvedValue = $this->resolveValue($field, null, $mutation); |
||
106 | |||
107 | View Code Duplication | if (!$this->resolveValidator->validateResolvedValue($resolvedValue, $field->getType()->getKind())) { |
|
|
|||
108 | $this->resolveValidator->addError(new ResolveException(sprintf('Not valid resolved value for mutation "%s"', $field->getType()->getName()))); |
||
109 | |||
110 | return [$alias => null]; |
||
111 | } |
||
112 | |||
113 | $value = null; |
||
114 | if ($mutation->hasFields()) { |
||
115 | $outputType = $field->getType()->getConfig()->getOutputType(); |
||
116 | |||
117 | $value = $this->processQueryFields($mutation, $outputType, $resolvedValue, []); |
||
118 | } |
||
119 | |||
120 | return [$alias => $value]; |
||
121 | } |
||
122 | |||
123 | 5 | protected function parseAndCreateRequest($query, $variables = []) |
|
133 | |||
134 | /** |
||
135 | * @param Query|Field $query |
||
136 | * @param ObjectType $currentLevelSchema |
||
137 | * @param null $contextValue |
||
138 | * @return array|bool|mixed |
||
139 | */ |
||
140 | 5 | protected function executeQuery($query, $currentLevelSchema, $contextValue = null) |
|
188 | |||
189 | /** |
||
190 | * @param $objectType InputObjectType|ObjectType |
||
191 | * @param $query Mutation|Query |
||
192 | * @return null |
||
193 | */ |
||
194 | 5 | private function checkFieldExist($objectType, $query) |
|
204 | |||
205 | /** |
||
206 | * @param $value |
||
207 | * @param $query Field |
||
208 | * |
||
209 | * @throws \Exception |
||
210 | * |
||
211 | * @return mixed |
||
212 | */ |
||
213 | 4 | protected function getPreResolvedValue($value, $query) |
|
227 | |||
228 | /** |
||
229 | * @param $field Field |
||
230 | * @param $contextValue mixed |
||
231 | * @param $query Query |
||
232 | * |
||
233 | * @return mixed |
||
234 | */ |
||
235 | 5 | protected function resolveValue($field, $contextValue, $query) |
|
239 | |||
240 | /** |
||
241 | * @param $type TypeInterface|AbstractObjectType |
||
242 | * @param $contextValue mixed |
||
243 | * @param $query Query |
||
244 | * |
||
245 | * @return mixed |
||
246 | */ |
||
247 | protected function resolveValueByType($type, $contextValue, $query) |
||
251 | |||
252 | /** |
||
253 | * @param $queryType AbstractObjectType |
||
254 | * @param $query Query |
||
255 | * |
||
256 | * @return array |
||
257 | */ |
||
258 | 5 | public function parseArgumentsValues($queryType, $query) |
|
275 | |||
276 | /** |
||
277 | * @param $query Query |
||
278 | * @param $queryType ObjectType|TypeInterface|Field |
||
279 | * @param $resolvedValue mixed |
||
280 | * @param $value array |
||
281 | * |
||
282 | * @throws \Exception |
||
283 | * |
||
284 | * @return array |
||
285 | */ |
||
286 | 4 | protected function processQueryFields($query, $queryType, $resolvedValue, $value) |
|
308 | |||
309 | 4 | protected function collectValue($value, $queryValue) |
|
319 | |||
320 | 5 | public function getSchema() |
|
324 | |||
325 | 5 | public function setSchema(Schema $schema) |
|
338 | |||
339 | /** |
||
340 | * @return ResolveValidatorInterface |
||
341 | */ |
||
342 | public function getResolveValidator() |
||
346 | |||
347 | 5 | public function getResponseData() |
|
361 | } |
||
362 |
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.