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 CreatePost 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 CreatePost, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class CreatePost{ |
||
12 | |||
13 | /** |
||
14 | * Create test data posts. |
||
15 | * |
||
16 | * This is where the magic begins. We accept a cpt id (slug) and potntially |
||
17 | * a number of posts to create. We then fetch the supports & metaboxes |
||
18 | * for that cpt and feed them into a function to create each post individually. |
||
19 | * |
||
20 | * @access private |
||
21 | * |
||
22 | * @see $this->get_cpt_supports, $this->get_metaboxes, $this->create_test_object |
||
23 | * |
||
24 | * @param string $slug a custom post type ID. |
||
25 | * @param boolean $echo Whether or not to echo. Optional. |
||
26 | * @param int $num Optional. Number of posts to create. |
||
27 | */ |
||
28 | public function create_post_type_content( $slug, $echo = false, $num = '' ){ |
||
56 | |||
57 | |||
58 | /** |
||
59 | * Creates the individual test data post. |
||
60 | * |
||
61 | * Create individual posts for testing with. Gathers basic information such |
||
62 | * as title, content, thumbnail, etc. and inserts them with the post. Also |
||
63 | * adds metaboxes if applicable . |
||
64 | * |
||
65 | * @access private |
||
66 | * |
||
67 | * @see TestContent, wp_insert_post, add_post_meta, update_post_meta, $this->random_metabox_content |
||
68 | * |
||
69 | * @param string $slug a custom post type ID. |
||
70 | * @param array $supports Features that the post type supports. |
||
71 | * @param array $supports All CMB2 metaboxes attached to the post type. |
||
72 | */ |
||
73 | private function create_test_object( $slug, $supports, $metaboxes ){ |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Assemble supports statements for a particular post type. |
||
142 | * |
||
143 | * @access private |
||
144 | * |
||
145 | * @see post_type_supports |
||
146 | * |
||
147 | * @param string $slug a custom post type ID. |
||
148 | * @return array Array of necessary supports booleans. |
||
149 | */ |
||
150 | private function get_cpt_supports( $slug ){ |
||
161 | |||
162 | |||
163 | /** |
||
164 | * Assigns taxonomies to the new post. |
||
165 | * |
||
166 | * Loop through every taxonomy type associated with a custom post type & |
||
167 | * assign the post to a random item out of each taxonomy. Taxonomies must |
||
168 | * have at least one term in them for this to work. |
||
169 | * |
||
170 | * @access private |
||
171 | * |
||
172 | * @param int $post_id a custom post type ID. |
||
173 | * @param array $taxonomies taxonomies assigned to this cpt. |
||
174 | * @return object WP Error if there is one. |
||
175 | */ |
||
176 | private function assign_terms( $post_id, $taxonomies ){ |
||
216 | |||
217 | |||
218 | /** |
||
219 | * Decide which cmb library to try and loop to get our metaboxes. |
||
220 | * |
||
221 | * Due to supporting multiple CMB libraries, we need to check which library |
||
222 | * is used on our site and then run the appropriate function. Currently |
||
223 | * supported libraries are CMB2 & Custom Metaboxes and Fields. |
||
224 | * |
||
225 | * @access private |
||
226 | * |
||
227 | * @see get_cmb2_metaboxes, get_cmb1_metaboxes |
||
228 | * |
||
229 | * @param string $slug Post Type slug ID. |
||
230 | * @return array Fields to fill in for our post object. |
||
231 | */ |
||
232 | private function get_metaboxes( $slug ){ |
||
250 | |||
251 | |||
252 | /** |
||
253 | * Gets all CMB2 custom metaboxes associated with a post type. |
||
254 | * |
||
255 | * Loops through all custom metabox fields registered with CMB2 and |
||
256 | * looks through them for matches on the given post type ID. Returns a single |
||
257 | * array of all boxes associated with the post type. |
||
258 | * |
||
259 | * @access private |
||
260 | * |
||
261 | * @see cmb2_meta_boxes |
||
262 | * |
||
263 | * @param string $slug a custom post type ID. |
||
264 | * @return array Array of fields. |
||
265 | */ |
||
266 | private function get_cmb2_metaboxes( $slug ){ |
||
319 | |||
320 | |||
321 | /** |
||
322 | * Gets all CMB1 custom metaboxes associated with a post type. |
||
323 | * |
||
324 | * Loops through all custom metabox fields registered with CMB2 and |
||
325 | * looks through them for matches on the given post type ID. Returns a single |
||
326 | * array of all boxes associated with the post type. |
||
327 | * |
||
328 | * @access private |
||
329 | * |
||
330 | * @see cmb_meta_boxes |
||
331 | * |
||
332 | * @param string $slug a custom post type ID. |
||
333 | * @return array Array of fields. |
||
334 | */ |
||
335 | private function get_cmb1_metaboxes( $slug ){ |
||
362 | |||
363 | |||
364 | /** |
||
365 | * Assigns the proper testing data to a custom metabox. |
||
366 | * |
||
367 | * Swaps through the possible types of CMB2 supported fields and |
||
368 | * insert the appropriate data based on type & id. |
||
369 | * Some types are not yet supported due to low frequency of use. |
||
370 | * |
||
371 | * @access private |
||
372 | * |
||
373 | * @see TestContent, add_post_meta |
||
374 | * |
||
375 | * @param int $post_id Single post ID. |
||
376 | * @param array $cmb custom metabox array from CMB2. |
||
377 | */ |
||
378 | private function random_metabox_content( $post_id, $cmb ){ |
||
548 | |||
549 | } |
||
550 |
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.