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 |
||
7 | class SchemaBuilder { |
||
8 | |||
9 | /** |
||
10 | * @var \Drupal\graphql\Plugin\FieldPluginManager |
||
11 | */ |
||
12 | protected $fieldManager; |
||
13 | |||
14 | /** |
||
15 | * @var \Drupal\graphql\Plugin\MutationPluginManager |
||
16 | */ |
||
17 | protected $mutationManager; |
||
18 | |||
19 | /** |
||
20 | * @var \Drupal\graphql\Plugin\TypePluginManager[] |
||
21 | */ |
||
22 | protected $typeManagers; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $fields; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $mutations; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $types; |
||
38 | |||
39 | /** |
||
40 | * SchemaBuilder constructor. |
||
41 | * |
||
42 | * @param \Drupal\graphql\Plugin\FieldPluginManager $fieldManager |
||
43 | * @param \Drupal\graphql\Plugin\MutationPluginManager $mutationManager |
||
44 | */ |
||
45 | public function __construct( |
||
52 | |||
53 | /** |
||
54 | * Registers a plugin manager. |
||
55 | * |
||
56 | * @param \Drupal\Component\Plugin\PluginManagerInterface $pluginManager |
||
57 | * The plugin manager to register. |
||
58 | * @param $id |
||
59 | * The id of the service. |
||
60 | */ |
||
61 | public function addTypeManager(PluginManagerInterface $pluginManager, $id) { |
||
67 | |||
68 | /** |
||
69 | * @return bool |
||
70 | */ |
||
71 | public function hasFields($parent) { |
||
74 | |||
75 | /** |
||
76 | * @return bool |
||
77 | */ |
||
78 | public function hasMutations() { |
||
81 | |||
82 | /** |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function hasType($name) { |
||
88 | |||
89 | /** |
||
90 | * @return array |
||
91 | */ |
||
92 | public function getFields($parent) { |
||
103 | |||
104 | /** |
||
105 | * @return array |
||
106 | */ |
||
107 | public function getMutations() { |
||
110 | |||
111 | /** |
||
112 | * @return array |
||
113 | */ |
||
114 | public function getTypes() { |
||
119 | |||
120 | /** |
||
121 | * @param $name |
||
122 | * |
||
123 | * @return mixed |
||
124 | */ |
||
125 | public function getType($name) { |
||
140 | |||
141 | /** |
||
142 | * @param $fields |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function processFields($fields) { |
||
149 | |||
150 | /** |
||
151 | * @param $args |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | public function processArguments($args) { |
||
162 | |||
163 | /** |
||
164 | * @param $type |
||
165 | * |
||
166 | * @return mixed |
||
167 | */ |
||
168 | public function processType($type) { |
||
175 | |||
176 | /** |
||
177 | * @return array |
||
178 | */ |
||
179 | protected function getTypeMap() { |
||
186 | |||
187 | /** |
||
188 | * @param $type |
||
189 | * |
||
190 | * @return mixed |
||
191 | */ |
||
192 | View Code Duplication | protected function buildType($type) { |
|
200 | |||
201 | /** |
||
202 | * @param $field |
||
203 | * |
||
204 | * @return mixed |
||
205 | */ |
||
206 | View Code Duplication | protected function buildField($field) { |
|
214 | |||
215 | /** |
||
216 | * @param $mutation |
||
217 | * |
||
218 | * @return mixed |
||
219 | */ |
||
220 | View Code Duplication | protected function buildMutation($mutation) { |
|
228 | |||
229 | /** |
||
230 | * @return array |
||
231 | */ |
||
232 | protected function buildTypeMap() { |
||
274 | |||
275 | /** |
||
276 | * @return array |
||
277 | */ |
||
278 | protected function getTypeReferenceMap() { |
||
285 | |||
286 | /** |
||
287 | * @return array |
||
288 | */ |
||
289 | protected function buildTypeReferenceMap() { |
||
309 | |||
310 | /** |
||
311 | * @return array |
||
312 | */ |
||
313 | protected function getFieldAssociationMap() { |
||
320 | |||
321 | /** |
||
322 | * @return array |
||
323 | */ |
||
324 | protected function buildFieldAssociationMap() { |
||
362 | |||
363 | /** |
||
364 | * @return array |
||
365 | */ |
||
366 | protected function getFieldMap() { |
||
373 | |||
374 | /** |
||
375 | * @return array |
||
376 | */ |
||
377 | protected function buildFieldMap() { |
||
396 | |||
397 | /** |
||
398 | * @return array |
||
399 | */ |
||
400 | protected function getMutationMap() { |
||
407 | |||
408 | /** |
||
409 | * @return array |
||
410 | */ |
||
411 | protected function buildMutationMap() { |
||
438 | } |
||
439 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.