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 acf_form_post 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 acf_form_post, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class acf_form_post { |
||
16 | |||
17 | var $post_id = 0, |
||
18 | $typenow = '', |
||
19 | $style = ''; |
||
20 | |||
21 | |||
22 | /* |
||
23 | * __construct |
||
24 | * |
||
25 | * This function will setup the class functionality |
||
26 | * |
||
27 | * @type function |
||
28 | * @date 5/03/2014 |
||
29 | * @since 5.0.0 |
||
30 | * |
||
31 | * @param n/a |
||
32 | * @return n/a |
||
33 | */ |
||
|
|||
34 | |||
35 | View Code Duplication | function __construct() { |
|
50 | |||
51 | |||
52 | /* |
||
53 | * validate_page |
||
54 | * |
||
55 | * This function will check if the current page is for a post/page edit form |
||
56 | * |
||
57 | * @type function |
||
58 | * @date 23/06/12 |
||
59 | * @since 3.1.8 |
||
60 | * |
||
61 | * @param n/a |
||
62 | * @return (boolean) |
||
63 | */ |
||
64 | |||
65 | function validate_page() { |
||
114 | |||
115 | |||
116 | /* |
||
117 | * admin_enqueue_scripts |
||
118 | * |
||
119 | * This action is run after post query but before any admin script / head actions. |
||
120 | * It is a good place to register all actions. |
||
121 | * |
||
122 | * @type action (admin_enqueue_scripts) |
||
123 | * @date 26/01/13 |
||
124 | * @since 3.6.0 |
||
125 | * |
||
126 | * @param n/a |
||
127 | * @return n/a |
||
128 | */ |
||
129 | |||
130 | View Code Duplication | function admin_enqueue_scripts() { |
|
144 | |||
145 | |||
146 | /* |
||
147 | * admin_head |
||
148 | * |
||
149 | * This action will find and add field groups to the current edit page |
||
150 | * |
||
151 | * @type action (admin_head) |
||
152 | * @date 23/06/12 |
||
153 | * @since 3.1.8 |
||
154 | * |
||
155 | * @param n/a |
||
156 | * @return n/a |
||
157 | */ |
||
158 | |||
159 | function admin_head() { |
||
229 | |||
230 | |||
231 | /* |
||
232 | * edit_form_after_title |
||
233 | * |
||
234 | * This action will allow ACF to render metaboxes after the title |
||
235 | * |
||
236 | * @type action |
||
237 | * @date 17/08/13 |
||
238 | * |
||
239 | * @param n/a |
||
240 | * @return n/a |
||
241 | */ |
||
242 | |||
243 | function edit_form_after_title() { |
||
265 | |||
266 | |||
267 | /* |
||
268 | * render_meta_box |
||
269 | * |
||
270 | * description |
||
271 | * |
||
272 | * @type function |
||
273 | * @date 20/10/13 |
||
274 | * @since 5.0.0 |
||
275 | * |
||
276 | * @param $post_id (int) |
||
277 | * @return $post_id (int) |
||
278 | */ |
||
279 | |||
280 | function render_meta_box( $post, $args ) { |
||
335 | |||
336 | |||
337 | /* |
||
338 | * admin_footer |
||
339 | * |
||
340 | * description |
||
341 | * |
||
342 | * @type function |
||
343 | * @date 21/10/13 |
||
344 | * @since 5.0.0 |
||
345 | * |
||
346 | * @param $post_id (int) |
||
347 | * @return $post_id (int) |
||
348 | */ |
||
349 | |||
350 | function admin_footer(){ |
||
356 | |||
357 | |||
358 | /* |
||
359 | * get_field_groups |
||
360 | * |
||
361 | * This function will return all the JSON data needed to render new metaboxes |
||
362 | * |
||
363 | * @type function |
||
364 | * @date 21/10/13 |
||
365 | * @since 5.0.0 |
||
366 | * |
||
367 | * @param n/a |
||
368 | * @return n/a |
||
369 | */ |
||
370 | |||
371 | function get_field_groups() { |
||
460 | |||
461 | |||
462 | /* |
||
463 | * wp_insert_post_empty_content |
||
464 | * |
||
465 | * This function will allow WP to insert a new post without title / content if ACF data exists |
||
466 | * |
||
467 | * @type function |
||
468 | * @date 16/07/2014 |
||
469 | * @since 5.0.1 |
||
470 | * |
||
471 | * @param $maybe_empty (bool) whether the post should be considered "empty" |
||
472 | * @param $postarr (array) Array of post data |
||
473 | * @return $maybe_empty |
||
474 | */ |
||
475 | |||
476 | function wp_insert_post_empty_content( $maybe_empty, $postarr ) { |
||
488 | |||
489 | |||
490 | /* |
||
491 | * save_post |
||
492 | * |
||
493 | * This function will validate and save the $_POST data |
||
494 | * |
||
495 | * @type function |
||
496 | * @date 23/06/12 |
||
497 | * @since 1.0.0 |
||
498 | * |
||
499 | * @param $post_id (int) |
||
500 | * @return $post_id (int) |
||
501 | */ |
||
502 | |||
503 | function save_post( $post_id, $post ) { |
||
550 | |||
551 | |||
552 | /* |
||
553 | * is_protected_meta |
||
554 | * |
||
555 | * This function will remove any ACF meta from showing in the meta postbox |
||
556 | * |
||
557 | * @type function |
||
558 | * @date 12/04/2014 |
||
559 | * @since 5.0.0 |
||
560 | * |
||
561 | * @param $post_id (int) |
||
562 | * @return $post_id (int) |
||
563 | */ |
||
564 | |||
565 | function is_protected_meta( $protected, $meta_key, $meta_type ) { |
||
585 | |||
586 | } |
||
587 | |||
593 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.