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 PluggableSchemaBuilder 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 PluggableSchemaBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class PluggableSchemaBuilder implements PluggableSchemaBuilderInterface { |
||
12 | use DependencySerializationTrait; |
||
13 | |||
14 | /** |
||
15 | * The type system plugin manager aggregator service. |
||
16 | * |
||
17 | * @var \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator |
||
18 | */ |
||
19 | protected $pluginManagers; |
||
20 | |||
21 | /** |
||
22 | * PluggableSchemaBuilder constructor. |
||
23 | * |
||
24 | * @param \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerAggregator $pluginManagers |
||
25 | * Type system plugin manager aggregator service. |
||
26 | */ |
||
27 | public function __construct(TypeSystemPluginManagerAggregator $pluginManagers) { |
||
30 | |||
31 | /** |
||
32 | * @param $type |
||
33 | * @param $id |
||
34 | * |
||
35 | * @return \Drupal\graphql\Plugin\GraphQL\TypeSystemPluginManagerInterface |
||
36 | */ |
||
37 | public function getPluginManager($type, $id) { |
||
47 | |||
48 | /** |
||
49 | * @param $type |
||
50 | * @param $id |
||
51 | * |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function getPluginDefinition($type, $id) { |
||
57 | |||
58 | /** |
||
59 | * @param $type |
||
60 | * @param $id |
||
61 | * |
||
62 | * @return object |
||
63 | */ |
||
64 | public function getPluginInstance($type, $id) { |
||
71 | |||
72 | /** |
||
73 | * @param $type |
||
74 | * @param $id |
||
75 | * |
||
76 | * @return mixed |
||
77 | */ |
||
78 | public function getDefinition($type, $id) { |
||
81 | |||
82 | /** |
||
83 | * @param $type |
||
84 | * @param $id |
||
85 | * |
||
86 | * @return mixed |
||
87 | */ |
||
88 | public function getType($type, $id) { |
||
97 | |||
98 | /** |
||
99 | * @return array |
||
100 | */ |
||
101 | public function getTypeMap() { |
||
134 | |||
135 | /** |
||
136 | * @return array |
||
137 | */ |
||
138 | public function getFieldMap() { |
||
166 | |||
167 | /** |
||
168 | * @return array |
||
169 | */ |
||
170 | public function getMutationMap() { |
||
194 | |||
195 | /** |
||
196 | * @param $name |
||
197 | * |
||
198 | * @return mixed |
||
199 | */ |
||
200 | public function getTypeByName($name) { |
||
214 | |||
215 | /** |
||
216 | * @param $name |
||
217 | * |
||
218 | * @return array |
||
219 | */ |
||
220 | public function getFieldsByType($name) { |
||
228 | |||
229 | /** |
||
230 | * @param $fields |
||
231 | * |
||
232 | * @return array |
||
233 | */ |
||
234 | public function resolveFields($fields, $pluginType = GRAPHQL_FIELD_PLUGIN) { |
||
248 | |||
249 | /** |
||
250 | * @param $args |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | public function resolveArgs($args) { |
||
267 | |||
268 | } |
||
269 |
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.