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() { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Perform save operation after successful is_valid_save() check. |
||
| 136 | * The call is propagated to all fields in the container. |
||
| 137 | * |
||
| 138 | * @param int $post_id ID of the post against which save() is ran |
||
| 139 | **/ |
||
| 140 | public function save( $post_id ) { |
||
| 156 | |||
| 157 | /** |
||
| 158 | * Perform checks whether the current save() request is valid |
||
| 159 | * Possible errors are triggering save() for autosave requests |
||
| 160 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 161 | * |
||
| 162 | * @see is_valid_save_conditions() |
||
| 163 | * @param int $post_id ID of the post against which save() is ran |
||
| 164 | * @return bool |
||
| 165 | **/ |
||
| 166 | public function is_valid_save( $post_id = 0 ) { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Perform checks whether the current save() request is valid |
||
| 180 | * Possible errors are triggering save() for autosave requests |
||
| 181 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 182 | * |
||
| 183 | * @param int $post_id ID of the post against which save() is ran |
||
| 184 | * @return bool |
||
| 185 | **/ |
||
| 186 | public function is_valid_save_conditions( $post_id ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Add meta box for each of the container post types |
||
| 295 | **/ |
||
| 296 | public function attach() { |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Classes to add to the post meta box |
||
| 315 | */ |
||
| 316 | public function postbox_classes( $classes ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Perform checks whether the container should be attached during the current request |
||
| 323 | * |
||
| 324 | * @return bool True if the container is allowed to be attached |
||
| 325 | **/ |
||
| 326 | public function is_valid_attach() { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Revert the result of attach() |
||
| 376 | **/ |
||
| 377 | View Code Duplication | public function detach() { |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Output the container markup |
||
| 391 | **/ |
||
| 392 | public function render() { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Set the post ID the container will operate with. |
||
| 398 | * |
||
| 399 | * @param int $post_id |
||
| 400 | **/ |
||
| 401 | public function set_post_id( $post_id ) { |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Perform checks whether there is a field registered with the name $name. |
||
| 408 | * If not, the field name is recorded. |
||
| 409 | * |
||
| 410 | * @param string $name |
||
| 411 | **/ |
||
| 412 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 429 | |||
| 430 | /** |
||
| 431 | * Remove field name $name from the list of unique field names |
||
| 432 | * |
||
| 433 | * @param string $name |
||
| 434 | **/ |
||
| 435 | View Code Duplication | public function drop_unique_field_name( $name ) { |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 446 | * |
||
| 447 | * @param string $parent_page_path |
||
| 448 | * @return object $this |
||
| 449 | **/ |
||
| 450 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Show the container only on particular page referenced by it's path. |
||
| 465 | * |
||
| 466 | * @param int|string $page page ID or page path |
||
| 467 | * @return object $this |
||
| 468 | **/ |
||
| 469 | 2 | public function show_on_page( $page ) { |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Show the container only on posts from the specified category. |
||
| 488 | * |
||
| 489 | * @see show_on_taxonomy_term() |
||
| 490 | * |
||
| 491 | * @param string $category_slug |
||
| 492 | * @return object $this |
||
| 493 | **/ |
||
| 494 | public function show_on_category( $category_slug ) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Show the container only on pages whose template has filename $template_path. |
||
| 502 | * |
||
| 503 | * @param string|array $template_path |
||
| 504 | * @return object $this |
||
| 505 | **/ |
||
| 506 | 2 | public function show_on_template( $template_path ) { |
|
| 521 | |||
| 522 | /** |
||
| 523 | * Hide the container from pages whose template has filename $template_path. |
||
| 524 | * |
||
| 525 | * @param string|array $template_path |
||
| 526 | * @return object $this |
||
| 527 | **/ |
||
| 528 | public function hide_on_template( $template_path ) { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Show the container only on hierarchical posts of level $level. |
||
| 543 | * Levels start from 1 (top level post) |
||
| 544 | * |
||
| 545 | * @param int $level |
||
| 546 | * @return object $this |
||
| 547 | **/ |
||
| 548 | public function show_on_level( $level ) { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 560 | * |
||
| 561 | * @param string $taxonomy_slug |
||
| 562 | * @param string $term_slug |
||
| 563 | * @return object $this |
||
| 564 | **/ |
||
| 565 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Show the container only on posts from the specified format. |
||
| 577 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 578 | * |
||
| 579 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 580 | * @return object $this |
||
| 581 | **/ |
||
| 582 | public function show_on_post_format( $post_format ) { |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Show the container only on posts from the specified type(s). |
||
| 601 | * |
||
| 602 | * @param string|array $post_types |
||
| 603 | * @return object $this |
||
| 604 | **/ |
||
| 605 | public function show_on_post_type( $post_types ) { |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Sets the meta box container context |
||
| 615 | * |
||
| 616 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 617 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 618 | */ |
||
| 619 | public function set_context( $context ) { |
||
| 624 | |||
| 625 | /** |
||
| 626 | * Sets the meta box container priority |
||
| 627 | * |
||
| 628 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 629 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 630 | */ |
||
| 631 | public function set_priority( $priority ) { |
||
| 636 | } // END Post_Meta_Container |
||
| 637 |