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 SchemaBuilder 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 SchemaBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
10 | class SchemaBuilder { |
||
11 | |||
12 | /** |
||
13 | * @var \Drupal\Core\Cache\CacheBackendInterface |
||
14 | */ |
||
15 | protected $cacheBackend; |
||
16 | |||
17 | /** |
||
18 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
19 | */ |
||
20 | protected $fieldManager; |
||
21 | |||
22 | /** |
||
23 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
24 | */ |
||
25 | protected $mutationManager; |
||
26 | |||
27 | /** |
||
28 | * @var \Drupal\graphql\Plugin\TypePluginManager[] |
||
29 | */ |
||
30 | protected $typeManagers; |
||
31 | |||
32 | /** |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $cache; |
||
36 | |||
37 | /** |
||
38 | * @var array |
||
39 | */ |
||
40 | protected $fields; |
||
41 | |||
42 | /** |
||
43 | * @var array |
||
44 | */ |
||
45 | protected $mutations; |
||
46 | |||
47 | /** |
||
48 | * @var array |
||
49 | */ |
||
50 | protected $types; |
||
51 | |||
52 | /** |
||
53 | * SchemaBuilder constructor. |
||
54 | * |
||
55 | * @param \Drupal\Core\Cache\CacheBackendInterface $cacheBackend |
||
56 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
57 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
58 | */ |
||
59 | public function __construct( |
||
68 | |||
69 | /** |
||
70 | * Registers a plugin manager. |
||
71 | * |
||
72 | * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager |
||
73 | * The plugin manager to register. |
||
74 | * @param $id |
||
75 | * The id of the service. |
||
76 | */ |
||
77 | public function addTypeManager(PluginManagerInterface $pluginManager, $id) { |
||
83 | |||
84 | /** |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function hasFields($parent) { |
||
90 | |||
91 | /** |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function hasMutations() { |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function hasType($name) { |
||
104 | |||
105 | /** |
||
106 | * @return array |
||
107 | */ |
||
108 | public function getFields($parent) { |
||
119 | |||
120 | /** |
||
121 | * @return array |
||
122 | */ |
||
123 | public function getMutations() { |
||
126 | |||
127 | /** |
||
128 | * @return array |
||
129 | */ |
||
130 | public function getTypes() { |
||
135 | |||
136 | /** |
||
137 | * @param $name |
||
138 | * |
||
139 | * @return mixed |
||
140 | */ |
||
141 | public function getType($name) { |
||
156 | |||
157 | /** |
||
158 | * @param $mutations |
||
159 | * |
||
160 | * @return array |
||
161 | */ |
||
162 | public function processMutations($mutations) { |
||
165 | |||
166 | /** |
||
167 | * @param $fields |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | public function processFields($fields) { |
||
174 | |||
175 | /** |
||
176 | * @param $args |
||
177 | * |
||
178 | * @return array |
||
179 | */ |
||
180 | public function processArguments($args) { |
||
187 | |||
188 | /** |
||
189 | * @param $type |
||
190 | * |
||
191 | * @return mixed |
||
192 | */ |
||
193 | public function processType($type) { |
||
200 | |||
201 | /** |
||
202 | * @param $type |
||
203 | * |
||
204 | * @return \Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase |
||
205 | */ |
||
206 | View Code Duplication | protected function buildType($type) { |
|
214 | |||
215 | /** |
||
216 | * @param $field |
||
217 | * |
||
218 | * @return mixed |
||
219 | */ |
||
220 | View Code Duplication | protected function buildField($field) { |
|
228 | |||
229 | /** |
||
230 | * @param $mutation |
||
231 | * |
||
232 | * @return mixed |
||
233 | */ |
||
234 | View Code Duplication | protected function buildMutation($mutation) { |
|
242 | |||
243 | /** |
||
244 | * @return array |
||
245 | */ |
||
246 | View Code Duplication | protected function getTypeMap() { |
|
257 | |||
258 | /** |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function buildTypeMap() { |
||
262 | // First collect all definitions by their name, overwriting those with |
||
263 | // lower weights by their higher weighted counterparts. We also collect |
||
264 | // the class from the plugin definition to be able to statically create |
||
265 | // the type instance without loading the plugin managers at all at |
||
266 | // run-time. |
||
267 | $types = array_reduce(array_keys($this->typeManagers), function ($carry, $type) { |
||
268 | $manager = $this->typeManagers[$type]; |
||
269 | $definitions = $manager->getDefinitions(); |
||
270 | |||
271 | return array_reduce(array_keys($definitions), function ($carry, $id) use ($type, $definitions) { |
||
272 | $current = $definitions[$id]; |
||
273 | $name = $current['name']; |
||
274 | |||
275 | if (empty($carry[$name]) || $carry[$name]['weight'] < $current['weight']) { |
||
276 | $carry[$name] = [ |
||
277 | 'type' => $type, |
||
278 | 'id' => $id, |
||
279 | 'class' => $current['class'], |
||
280 | 'weight' => !empty($current['weight']) ? $current['weight'] : 0, |
||
281 | 'reference' => !empty($current['type']) ? $current['type'] : NULL, |
||
282 | ]; |
||
283 | } |
||
284 | |||
285 | return $carry; |
||
286 | }, $carry); |
||
287 | }, []); |
||
288 | |||
289 | // Retrieve the plugins run-time definitions. These will help us to prevent |
||
290 | // plugin instantiation at run-time unless a plugin is actually called from |
||
291 | // the graphql query execution. Plugins should take care of not having to |
||
292 | // instantiate their plugin instances during schema composition. |
||
293 | return array_map(function ($type) { |
||
294 | $manager = $this->typeManagers[$type['type']]; |
||
295 | /** @var \Drupal\graphql\Plugin\TypePluginInterface $instance */ |
||
296 | $instance = $manager->getInstance(['id' => $type['id']]); |
||
297 | |||
298 | return $type + [ |
||
299 | 'definition' => $instance->getDefinition(), |
||
300 | ] + $type; |
||
301 | }, $types); |
||
302 | } |
||
303 | |||
304 | /** |
||
305 | * @return array |
||
306 | */ |
||
307 | View Code Duplication | protected function getTypeReferenceMap() { |
|
318 | |||
319 | /** |
||
320 | * @return array |
||
321 | */ |
||
322 | protected function buildTypeReferenceMap() { |
||
342 | |||
343 | /** |
||
344 | * @return array |
||
345 | */ |
||
346 | View Code Duplication | protected function getFieldAssociationMap() { |
|
357 | |||
358 | /** |
||
359 | * @return array |
||
360 | */ |
||
361 | protected function buildFieldAssociationMap() { |
||
399 | |||
400 | /** |
||
401 | * @return array |
||
402 | */ |
||
403 | View Code Duplication | protected function getTypeAssociationMap() { |
|
414 | |||
415 | /** |
||
416 | * @return array |
||
417 | */ |
||
418 | protected function buildTypeAssociationMap() { |
||
457 | |||
458 | /** |
||
459 | * Retrieve the list of derivatives associated with a composite type. |
||
460 | * |
||
461 | * @return string[] |
||
462 | * The list of possible sub typenames. |
||
463 | */ |
||
464 | public function getSubTypes($definition) { |
||
468 | |||
469 | /** |
||
470 | * Resolve the matching type. |
||
471 | */ |
||
472 | public function resolveType($definition, $value, $context, $info) { |
||
489 | |||
490 | /** |
||
491 | * @return array |
||
492 | */ |
||
493 | View Code Duplication | protected function getFieldMap() { |
|
504 | |||
505 | /** |
||
506 | * @return array |
||
507 | */ |
||
508 | protected function buildFieldMap() { |
||
527 | |||
528 | /** |
||
529 | * @return array |
||
530 | */ |
||
531 | View Code Duplication | protected function getMutationMap() { |
|
542 | |||
543 | /** |
||
544 | * @return array |
||
545 | */ |
||
546 | protected function buildMutationMap() { |
||
571 | |||
572 | /** |
||
573 | * @param $id |
||
574 | * @param $data |
||
575 | */ |
||
576 | protected function cacheSet($id, $data) { |
||
580 | |||
581 | /** |
||
582 | * @param $id |
||
583 | * |
||
584 | * @return mixed |
||
585 | */ |
||
586 | protected function cacheGet($id) { |
||
605 | } |
||
606 |
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.