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 | * @param int $post_id ID of the post against which save() is ran |
||
| 110 | * @return bool |
||
| 111 | **/ |
||
| 112 | public function is_valid_save( $post_id = 0 ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Perform checks whether the container should be attached during the current request |
||
| 126 | * |
||
| 127 | * @return bool True if the container is allowed to be attached |
||
| 128 | **/ |
||
| 129 | public function is_valid_attach_for_request() { |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Check container attachment rules against object id |
||
| 180 | * |
||
| 181 | * @return bool |
||
| 182 | **/ |
||
| 183 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Add meta box for each of the container post types |
||
| 297 | **/ |
||
| 298 | public function attach() { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Classes to add to the post meta box |
||
| 317 | */ |
||
| 318 | public function add_postbox_classes( $classes ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Output the container markup |
||
| 325 | **/ |
||
| 326 | public function render() { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Set the post ID the container will operate with. |
||
| 332 | * |
||
| 333 | * @param int $post_id |
||
| 334 | **/ |
||
| 335 | public function set_post_id( $post_id ) { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * COMMON USAGE METHODS |
||
| 342 | */ |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Show the container only on particular page referenced by it's path. |
||
| 346 | * |
||
| 347 | * @param int|string $page page ID or page path |
||
| 348 | * @return object $this |
||
| 349 | **/ |
||
| 350 | public function show_on_page( $page ) { |
||
| 369 | |||
| 370 | 4 | /** |
|
| 371 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 372 | * |
||
| 373 | * @param string $parent_page_path |
||
| 374 | * @return object $this |
||
| 375 | **/ |
||
| 376 | public function show_on_page_children( $parent_page_path ) { |
||
| 389 | |||
| 390 | 1 | /** |
|
| 391 | * Show the container only on posts from the specified category. |
||
| 392 | * |
||
| 393 | * @see show_on_taxonomy_term() |
||
| 394 | * |
||
| 395 | * @param string $category_slug |
||
| 396 | * @return object $this |
||
| 397 | **/ |
||
| 398 | public function show_on_category( $category_slug ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Show the container only on pages whose template has filename $template_path. |
||
| 406 | * |
||
| 407 | * @param string|array $template_path |
||
| 408 | * @return object $this |
||
| 409 | **/ |
||
| 410 | public function show_on_template( $template_path ) { |
||
| 428 | |||
| 429 | 2 | /** |
|
| 430 | * Hide the container from pages whose template has filename $template_path. |
||
| 431 | * |
||
| 432 | * @param string|array $template_path |
||
| 433 | * @return object $this |
||
| 434 | **/ |
||
| 435 | public function hide_on_template( $template_path ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Show the container only on hierarchical posts of level $level. |
||
| 450 | * Levels start from 1 (top level post) |
||
| 451 | * |
||
| 452 | * @param int $level |
||
| 453 | * @return object $this |
||
| 454 | **/ |
||
| 455 | public function show_on_level( $level ) { |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 467 | * |
||
| 468 | * @param string $taxonomy_slug |
||
| 469 | * @param string $term_slug |
||
| 470 | * @return object $this |
||
| 471 | **/ |
||
| 472 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 481 | |||
| 482 | /** |
||
| 483 | * Show the container only on posts from the specified format. |
||
| 484 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 485 | * |
||
| 486 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 487 | * @return object $this |
||
| 488 | **/ |
||
| 489 | public function show_on_post_format( $post_format ) { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * Show the container only on posts from the specified type(s). |
||
| 508 | * |
||
| 509 | * @param string|array $post_types |
||
| 510 | * @return object $this |
||
| 511 | **/ |
||
| 512 | public function show_on_post_type( $post_types ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Sets the meta box container context |
||
| 522 | * |
||
| 523 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 524 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 525 | */ |
||
| 526 | public function set_context( $context ) { |
||
| 531 | |||
| 532 | /** |
||
| 533 | * Sets the meta box container priority |
||
| 534 | * |
||
| 535 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 536 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 537 | */ |
||
| 538 | public function set_priority( $priority ) { |
||
| 543 | } |
||
| 544 |