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 |
||
| 13 | class Post_Meta_Container extends Container { |
||
| 14 | /** |
||
| 15 | * ID of the post the container is working with |
||
| 16 | * |
||
| 17 | * @see init() |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $post_id; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * List of default container settings |
||
| 24 | * |
||
| 25 | * @see init() |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $settings = array( |
||
| 29 | 'post_type' => array( 'post' ), |
||
| 30 | 'panel_context' => 'normal', |
||
| 31 | 'panel_priority' => 'high', |
||
| 32 | 'show_on' => array( |
||
| 33 | 'category' => null, |
||
| 34 | 'template_names' => array(), |
||
| 35 | 'not_in_template_names' => array(), |
||
| 36 | 'post_formats' => array(), |
||
| 37 | 'level_limit' => null, |
||
| 38 | 'tax_term_id' => null, |
||
| 39 | 'page_id' => null, |
||
| 40 | 'parent_page_id' => null, |
||
| 41 | 'post_path' => null, |
||
| 42 | ), |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new container |
||
| 47 | * |
||
| 48 | * @param string $unique_id Unique id of the container |
||
| 49 | * @param string $title title of the container |
||
| 50 | * @param string $type Type of the container |
||
| 51 | **/ |
||
| 52 | View Code Duplication | public function __construct( $unique_id, $title, $type ) { |
|
| 59 | |||
| 60 | /** |
||
| 61 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
| 62 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 63 | **/ |
||
| 64 | public function init() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Perform save operation after successful is_valid_save() check. |
||
| 85 | * The call is propagated to all fields in the container. |
||
| 86 | * |
||
| 87 | * @param int $post_id ID of the post against which save() is ran |
||
| 88 | **/ |
||
| 89 | View Code Duplication | public function save( $post_id ) { |
|
| 103 | |||
| 104 | /** |
||
| 105 | * Perform checks whether the current save() request is valid |
||
| 106 | * Possible errors are triggering save() for autosave requests |
||
| 107 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 108 | * |
||
| 109 | * @see is_valid_save_conditions() |
||
| 110 | * @param int $post_id ID of the post against which save() is ran |
||
| 111 | * @return bool |
||
| 112 | **/ |
||
| 113 | public function is_valid_save( $post_id = 0 ) { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Perform checks whether the current save() request is valid |
||
| 131 | * Possible errors are triggering save() for autosave requests |
||
| 132 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 133 | * |
||
| 134 | * @param int $post_id ID of the post against which save() is ran |
||
| 135 | * @return bool |
||
| 136 | **/ |
||
| 137 | public function is_valid_save_conditions( $post_id ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Perform checks whether the container should be attached during the current request |
||
| 246 | * |
||
| 247 | * @return bool True if the container is allowed to be attached |
||
| 248 | **/ |
||
| 249 | public function _is_valid_attach() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Add meta box for each of the container post types |
||
| 300 | **/ |
||
| 301 | public function attach() { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Classes to add to the post meta box |
||
| 320 | */ |
||
| 321 | public function add_postbox_classes( $classes ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Output the container markup |
||
| 328 | **/ |
||
| 329 | public function render() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set the post ID the container will operate with. |
||
| 335 | * |
||
| 336 | * @param int $post_id |
||
| 337 | **/ |
||
| 338 | public function set_post_id( $post_id ) { |
||
| 342 | |||
| 343 | /** |
||
| 344 | * COMMON USAGE METHODS |
||
| 345 | */ |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Show the container only on particular page referenced by it's path. |
||
| 349 | * |
||
| 350 | * @param int|string $page page ID or page path |
||
| 351 | * @return object $this |
||
| 352 | **/ |
||
| 353 | 4 | public function show_on_page( $page ) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 375 | * |
||
| 376 | * @param string $parent_page_path |
||
| 377 | * @return object $this |
||
| 378 | **/ |
||
| 379 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Show the container only on posts from the specified category. |
||
| 395 | * |
||
| 396 | * @see show_on_taxonomy_term() |
||
| 397 | * |
||
| 398 | * @param string $category_slug |
||
| 399 | * @return object $this |
||
| 400 | **/ |
||
| 401 | public function show_on_category( $category_slug ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Show the container only on pages whose template has filename $template_path. |
||
| 409 | * |
||
| 410 | * @param string|array $template_path |
||
| 411 | * @return object $this |
||
| 412 | **/ |
||
| 413 | 2 | public function show_on_template( $template_path ) { |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Hide the container from pages whose template has filename $template_path. |
||
| 434 | * |
||
| 435 | * @param string|array $template_path |
||
| 436 | * @return object $this |
||
| 437 | **/ |
||
| 438 | public function hide_on_template( $template_path ) { |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Show the container only on hierarchical posts of level $level. |
||
| 453 | * Levels start from 1 (top level post) |
||
| 454 | * |
||
| 455 | * @param int $level |
||
| 456 | * @return object $this |
||
| 457 | **/ |
||
| 458 | public function show_on_level( $level ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 470 | * |
||
| 471 | * @param string $taxonomy_slug |
||
| 472 | * @param string $term_slug |
||
| 473 | * @return object $this |
||
| 474 | **/ |
||
| 475 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Show the container only on posts from the specified format. |
||
| 487 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 488 | * |
||
| 489 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 490 | * @return object $this |
||
| 491 | **/ |
||
| 492 | public function show_on_post_format( $post_format ) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Show the container only on posts from the specified type(s). |
||
| 511 | * |
||
| 512 | * @param string|array $post_types |
||
| 513 | * @return object $this |
||
| 514 | **/ |
||
| 515 | public function show_on_post_type( $post_types ) { |
||
| 522 | |||
| 523 | /** |
||
| 524 | * Sets the meta box container context |
||
| 525 | * |
||
| 526 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 527 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 528 | */ |
||
| 529 | public function set_context( $context ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Sets the meta box container priority |
||
| 537 | * |
||
| 538 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 539 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 540 | */ |
||
| 541 | public function set_priority( $priority ) { |
||
| 546 | } |
||
| 547 |