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 Post_Meta_Container 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 Post_Meta_Container, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Post_Meta_Container extends Container { |
||
15 | /** |
||
16 | * List of registered unique field names |
||
17 | * |
||
18 | * @see verify_unique_field_name() |
||
19 | * @var array |
||
20 | */ |
||
21 | protected static $registered_field_names; |
||
22 | |||
23 | /** |
||
24 | * ID of the post the container is working with |
||
25 | * |
||
26 | * @see init() |
||
27 | * @var int |
||
28 | */ |
||
29 | protected $post_id; |
||
30 | |||
31 | /** |
||
32 | * List of default container settings |
||
33 | * |
||
34 | * @see init() |
||
35 | * @var array |
||
36 | */ |
||
37 | public $settings = array( |
||
38 | 'post_type' => array( 'post' ), |
||
39 | 'panel_context' => 'normal', |
||
40 | 'panel_priority' => 'high', |
||
41 | 'show_on' => array( |
||
42 | 'category' => null, |
||
43 | 'template_names' => array(), |
||
44 | 'not_in_template_names' => array(), |
||
45 | 'post_formats' => array(), |
||
46 | 'level_limit' => null, |
||
47 | 'tax_term_id' => null, |
||
48 | 'page_id' => null, |
||
49 | 'parent_page_id' => null, |
||
50 | 'post_path' => null, |
||
51 | ), |
||
52 | ); |
||
53 | |||
54 | /** |
||
55 | * Create a new post meta fields container |
||
56 | * |
||
57 | * @param string $title Unique title of the container |
||
58 | **/ |
||
59 | public function __construct( $title ) { |
||
66 | |||
67 | /** |
||
68 | * Check if all required container settings have been specified |
||
69 | * |
||
70 | * @param array $settings Container settings |
||
71 | **/ |
||
72 | public function check_setup_settings( &$settings = array() ) { |
||
107 | |||
108 | /** |
||
109 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
110 | * Bind attach() and save() to the appropriate WordPress actions. |
||
111 | **/ |
||
112 | public function init() { |
||
129 | |||
130 | /** |
||
131 | * Perform save operation after successful is_valid_save() check. |
||
132 | * The call is propagated to all fields in the container. |
||
133 | * |
||
134 | * @param int $post_id ID of the post against which save() is ran |
||
135 | **/ |
||
136 | View Code Duplication | public function save( $post_id ) { |
|
150 | |||
151 | /** |
||
152 | * Perform checks whether the current save() request is valid |
||
153 | * Possible errors are triggering save() for autosave requests |
||
154 | * or performing post save outside of the post edit page (like Quick Edit) |
||
155 | * |
||
156 | * @see is_valid_save_conditions() |
||
157 | * @param int $post_id ID of the post against which save() is ran |
||
158 | * @return bool |
||
159 | **/ |
||
160 | public function is_valid_save( $post_id = 0 ) { |
||
171 | |||
172 | /** |
||
173 | * Perform checks whether the current save() request is valid |
||
174 | * Possible errors are triggering save() for autosave requests |
||
175 | * or performing post save outside of the post edit page (like Quick Edit) |
||
176 | * |
||
177 | * @param int $post_id ID of the post against which save() is ran |
||
178 | * @return bool |
||
179 | **/ |
||
180 | public function is_valid_save_conditions( $post_id ) { |
||
286 | |||
287 | /** |
||
288 | * Add meta box for each of the container post types |
||
289 | **/ |
||
290 | public function attach() { |
||
306 | |||
307 | /** |
||
308 | * Classes to add to the post meta box |
||
309 | */ |
||
310 | public function postbox_classes( $classes ) { |
||
314 | |||
315 | /** |
||
316 | * Perform checks whether the container should be attached during the current request |
||
317 | * |
||
318 | * @return bool True if the container is allowed to be attached |
||
319 | **/ |
||
320 | public function is_valid_attach() { |
||
367 | |||
368 | /** |
||
369 | * Revert the result of attach() |
||
370 | **/ |
||
371 | View Code Duplication | public function detach() { |
|
382 | |||
383 | /** |
||
384 | * Output the container markup |
||
385 | **/ |
||
386 | public function render() { |
||
389 | |||
390 | /** |
||
391 | * Set the post ID the container will operate with. |
||
392 | * |
||
393 | * @param int $post_id |
||
394 | **/ |
||
395 | public function set_post_id( $post_id ) { |
||
399 | |||
400 | /** |
||
401 | * Perform checks whether there is a field registered with the name $name. |
||
402 | * If not, the field name is recorded. |
||
403 | * |
||
404 | * @param string $name |
||
405 | **/ |
||
406 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
423 | |||
424 | /** |
||
425 | * Remove field name $name from the list of unique field names |
||
426 | * |
||
427 | * @param string $name |
||
428 | **/ |
||
429 | View Code Duplication | public function drop_unique_field_name( $name ) { |
|
437 | |||
438 | /** |
||
439 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
440 | * |
||
441 | * @param string $parent_page_path |
||
442 | * @return object $this |
||
443 | **/ |
||
444 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
456 | |||
457 | /** |
||
458 | * Show the container only on particular page referenced by it's path. |
||
459 | * |
||
460 | * @param int|string $page page ID or page path |
||
461 | * @return object $this |
||
462 | **/ |
||
463 | 2 | public function show_on_page( $page ) { |
|
479 | |||
480 | /** |
||
481 | * Show the container only on posts from the specified category. |
||
482 | * |
||
483 | * @see show_on_taxonomy_term() |
||
484 | * |
||
485 | * @param string $category_slug |
||
486 | * @return object $this |
||
487 | **/ |
||
488 | public function show_on_category( $category_slug ) { |
||
493 | |||
494 | /** |
||
495 | * Show the container only on pages whose template has filename $template_path. |
||
496 | * |
||
497 | * @param string|array $template_path |
||
498 | * @return object $this |
||
499 | **/ |
||
500 | 2 | public function show_on_template( $template_path ) { |
|
515 | |||
516 | /** |
||
517 | * Hide the container from pages whose template has filename $template_path. |
||
518 | * |
||
519 | * @param string|array $template_path |
||
520 | * @return object $this |
||
521 | **/ |
||
522 | public function hide_on_template( $template_path ) { |
||
534 | |||
535 | /** |
||
536 | * Show the container only on hierarchical posts of level $level. |
||
537 | * Levels start from 1 (top level post) |
||
538 | * |
||
539 | * @param int $level |
||
540 | * @return object $this |
||
541 | **/ |
||
542 | public function show_on_level( $level ) { |
||
551 | |||
552 | /** |
||
553 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
554 | * |
||
555 | * @param string $taxonomy_slug |
||
556 | * @param string $term_slug |
||
557 | * @return object $this |
||
558 | **/ |
||
559 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
568 | |||
569 | /** |
||
570 | * Show the container only on posts from the specified format. |
||
571 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
572 | * |
||
573 | * @param string|array $post_format Name of the format as listed on Codex |
||
574 | * @return object $this |
||
575 | **/ |
||
576 | public function show_on_post_format( $post_format ) { |
||
592 | |||
593 | /** |
||
594 | * Show the container only on posts from the specified type(s). |
||
595 | * |
||
596 | * @param string|array $post_types |
||
597 | * @return object $this |
||
598 | **/ |
||
599 | public function show_on_post_type( $post_types ) { |
||
606 | |||
607 | /** |
||
608 | * Sets the meta box container context |
||
609 | * |
||
610 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
611 | * @param string $context ('normal', 'advanced' or 'side') |
||
612 | */ |
||
613 | public function set_context( $context ) { |
||
618 | |||
619 | /** |
||
620 | * Sets the meta box container priority |
||
621 | * |
||
622 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
623 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
624 | */ |
||
625 | public function set_priority( $priority ) { |
||
630 | } // END Post_Meta_Container |
||
631 |