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 MetaboxTypes 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 MetaboxTypes, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class MetaboxTypes { |
||
12 | |||
13 | /** |
||
14 | * Decide which cmb library to try and loop to get our metaboxes. |
||
15 | * |
||
16 | * Due to supporting multiple CMB libraries, we need to check which library |
||
17 | * is used on our site and then run the appropriate function. Currently |
||
18 | * supported libraries are CMB2 & Custom Metaboxes and Fields. |
||
19 | * |
||
20 | * @see get_cmb2_metaboxes, get_cmb1_metaboxes |
||
21 | * |
||
22 | * @param string $slug Post Type slug ID. |
||
23 | * @return array Fields to fill in for our post object. |
||
24 | */ |
||
25 | public function get_metaboxes( $slug ) { |
||
47 | |||
48 | |||
49 | /** |
||
50 | * Gets the metaboxes assigned to a custom post type in ACF. |
||
51 | * |
||
52 | * @access private |
||
53 | * |
||
54 | * @see get_all_acf_field_groups, is_acf_field_in_post_type |
||
55 | * |
||
56 | * @param string $slug Post type. |
||
57 | * @return array Fields array. |
||
58 | */ |
||
59 | private function get_acf_free_metaboxes( $slug ) { |
||
91 | |||
92 | |||
93 | /** |
||
94 | * Check if a group of fields is in a custom post type. |
||
95 | * |
||
96 | * @access private |
||
97 | * |
||
98 | * @param string $slug Post type slug. |
||
99 | * @param object $fieldset Fieldset group. |
||
100 | * @return boolean Whether or not the grouping is assigned to the post type. |
||
101 | */ |
||
102 | private function is_acf_field_in_post_type( $slug, $fieldset ) { |
||
120 | |||
121 | |||
122 | /** |
||
123 | * Loop through and retrive all acf cpts and cmb. |
||
124 | * |
||
125 | * ACF stores their data in custom post types and unfortunately named cmbs. |
||
126 | * Therefore, we have to loop through all acfs and sort through the mish-mash |
||
127 | * of messy data and make something clean of it. |
||
128 | * |
||
129 | * @access private |
||
130 | * |
||
131 | * @return array All acf fieldsets. |
||
132 | */ |
||
133 | private function get_all_acf_field_groups() { |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Gets all CMB2 custom metaboxes associated with a post type. |
||
187 | * |
||
188 | * Loops through all custom metabox fields registered with CMB2 and |
||
189 | * looks through them for matches on the given post type ID. Returns a single |
||
190 | * array of all boxes associated with the post type. |
||
191 | * |
||
192 | * @access private |
||
193 | * |
||
194 | * @see cmb2_meta_boxes |
||
195 | * |
||
196 | * @param string $slug a custom post type ID. |
||
197 | * @return array Array of fields. |
||
198 | */ |
||
199 | private function get_cmb2_metaboxes( $slug ) { |
||
257 | |||
258 | |||
259 | /** |
||
260 | * Gets all CMB1 custom metaboxes associated with a post type. |
||
261 | * |
||
262 | * Loops through all custom metabox fields registered with CMB2 and |
||
263 | * looks through them for matches on the given post type ID. Returns a single |
||
264 | * array of all boxes associated with the post type. |
||
265 | * |
||
266 | * @access private |
||
267 | * |
||
268 | * @see cmb_meta_boxes |
||
269 | * |
||
270 | * @param string $slug a custom post type ID. |
||
271 | * @return array Array of fields. |
||
272 | */ |
||
273 | private function get_cmb1_metaboxes( $slug ) { |
||
308 | |||
309 | private function add_source( $fields, $source ) { |
||
322 | |||
323 | } |
||
324 |
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.