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 | * ID of the post the container is working with |
||
| 17 | * |
||
| 18 | * @see init() |
||
| 19 | * @var int |
||
| 20 | */ |
||
| 21 | protected $post_id; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * List of default container settings |
||
| 25 | * |
||
| 26 | * @see init() |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | public $settings = array( |
||
| 30 | 'post_type' => array( 'post' ), |
||
| 31 | 'panel_context' => 'normal', |
||
| 32 | 'panel_priority' => 'high', |
||
| 33 | 'show_on' => array( |
||
| 34 | 'category' => null, |
||
| 35 | 'template_names' => array(), |
||
| 36 | 'not_in_template_names' => array(), |
||
| 37 | 'post_formats' => array(), |
||
| 38 | 'level_limit' => null, |
||
| 39 | 'tax_term_id' => null, |
||
| 40 | 'page_id' => null, |
||
| 41 | 'parent_page_id' => null, |
||
| 42 | 'post_path' => null, |
||
| 43 | ), |
||
| 44 | ); |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Create a new post meta fields container |
||
| 48 | * |
||
| 49 | * @param string $title Unique title of the container |
||
| 50 | **/ |
||
| 51 | public function __construct( $title ) { |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Check if all required container settings have been specified |
||
| 61 | * |
||
| 62 | * @param array $settings Container settings |
||
| 63 | **/ |
||
| 64 | public function check_setup_settings( &$settings = array() ) { |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
| 102 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 103 | **/ |
||
| 104 | public function init() { |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Perform save operation after successful is_valid_save() check. |
||
| 124 | * The call is propagated to all fields in the container. |
||
| 125 | * |
||
| 126 | * @param int $post_id ID of the post against which save() is ran |
||
| 127 | **/ |
||
| 128 | View Code Duplication | public function save( $post_id ) { |
|
| 142 | |||
| 143 | /** |
||
| 144 | * Perform checks whether the current save() request is valid |
||
| 145 | * Possible errors are triggering save() for autosave requests |
||
| 146 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 147 | * |
||
| 148 | * @see is_valid_save_conditions() |
||
| 149 | * @param int $post_id ID of the post against which save() is ran |
||
| 150 | * @return bool |
||
| 151 | **/ |
||
| 152 | public function is_valid_save( $post_id = 0 ) { |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Perform checks whether the current save() request is valid |
||
| 166 | * Possible errors are triggering save() for autosave requests |
||
| 167 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 168 | * |
||
| 169 | * @param int $post_id ID of the post against which save() is ran |
||
| 170 | * @return bool |
||
| 171 | **/ |
||
| 172 | public function is_valid_save_conditions( $post_id ) { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Add meta box for each of the container post types |
||
| 281 | **/ |
||
| 282 | public function attach() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Classes to add to the post meta box |
||
| 301 | */ |
||
| 302 | public function postbox_classes( $classes ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Perform checks whether the container should be attached during the current request |
||
| 309 | * |
||
| 310 | * @return bool True if the container is allowed to be attached |
||
| 311 | **/ |
||
| 312 | public function is_valid_attach() { |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Revert the result of attach() |
||
| 362 | **/ |
||
| 363 | View Code Duplication | public function detach() { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Output the container markup |
||
| 377 | **/ |
||
| 378 | public function render() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Set the post ID the container will operate with. |
||
| 384 | * |
||
| 385 | * @param int $post_id |
||
| 386 | **/ |
||
| 387 | public function set_post_id( $post_id ) { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 394 | * |
||
| 395 | * @param string $parent_page_path |
||
| 396 | * @return object $this |
||
| 397 | **/ |
||
| 398 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Show the container only on particular page referenced by it's path. |
||
| 414 | * |
||
| 415 | * @param int|string $page page ID or page path |
||
| 416 | * @return object $this |
||
| 417 | **/ |
||
| 418 | 4 | public function show_on_page( $page ) { |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Show the container only on posts from the specified category. |
||
| 440 | * |
||
| 441 | * @see show_on_taxonomy_term() |
||
| 442 | * |
||
| 443 | * @param string $category_slug |
||
| 444 | * @return object $this |
||
| 445 | **/ |
||
| 446 | public function show_on_category( $category_slug ) { |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Show the container only on pages whose template has filename $template_path. |
||
| 454 | * |
||
| 455 | * @param string|array $template_path |
||
| 456 | * @return object $this |
||
| 457 | **/ |
||
| 458 | 2 | public function show_on_template( $template_path ) { |
|
| 473 | |||
| 474 | /** |
||
| 475 | * Hide the container from pages whose template has filename $template_path. |
||
| 476 | * |
||
| 477 | * @param string|array $template_path |
||
| 478 | * @return object $this |
||
| 479 | **/ |
||
| 480 | public function hide_on_template( $template_path ) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * Show the container only on hierarchical posts of level $level. |
||
| 495 | * Levels start from 1 (top level post) |
||
| 496 | * |
||
| 497 | * @param int $level |
||
| 498 | * @return object $this |
||
| 499 | **/ |
||
| 500 | public function show_on_level( $level ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 512 | * |
||
| 513 | * @param string $taxonomy_slug |
||
| 514 | * @param string $term_slug |
||
| 515 | * @return object $this |
||
| 516 | **/ |
||
| 517 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Show the container only on posts from the specified format. |
||
| 529 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 530 | * |
||
| 531 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 532 | * @return object $this |
||
| 533 | **/ |
||
| 534 | public function show_on_post_format( $post_format ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Show the container only on posts from the specified type(s). |
||
| 553 | * |
||
| 554 | * @param string|array $post_types |
||
| 555 | * @return object $this |
||
| 556 | **/ |
||
| 557 | public function show_on_post_type( $post_types ) { |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Sets the meta box container context |
||
| 567 | * |
||
| 568 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 569 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 570 | */ |
||
| 571 | public function set_context( $context ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Sets the meta box container priority |
||
| 579 | * |
||
| 580 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 581 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 582 | */ |
||
| 583 | public function set_priority( $priority ) { |
||
| 588 | } // END Post_Meta_Container |
||
| 589 |