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 mixed |
||
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() { |
||
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 getFieldMap() { |
|
414 | |||
415 | /** |
||
416 | * @return array |
||
417 | */ |
||
418 | protected function buildFieldMap() { |
||
437 | |||
438 | /** |
||
439 | * @return array |
||
440 | */ |
||
441 | View Code Duplication | protected function getMutationMap() { |
|
452 | |||
453 | /** |
||
454 | * @return array |
||
455 | */ |
||
456 | protected function buildMutationMap() { |
||
481 | |||
482 | /** |
||
483 | * @param $id |
||
484 | * @param $data |
||
485 | */ |
||
486 | protected function cacheSet($id, $data) { |
||
490 | |||
491 | /** |
||
492 | * @param $id |
||
493 | * |
||
494 | * @return mixed |
||
495 | */ |
||
496 | protected function cacheGet($id) { |
||
515 | } |
||
516 |
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.