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() { |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Checks whether the current save request is valid |
||
| 86 | * Possible errors are triggering save() for autosave requests |
||
| 87 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 88 | * |
||
| 89 | * @param int $post_id ID of the post against which save() is ran |
||
| 90 | * @return bool |
||
| 91 | **/ |
||
| 92 | public function is_valid_save( $post_id = 0 ) { |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Perform save operation after successful is_valid_save() check. |
||
| 106 | * The call is propagated to all fields in the container. |
||
| 107 | * |
||
| 108 | * @param int $post_id ID of the post against which save() is ran |
||
| 109 | **/ |
||
| 110 | View Code Duplication | public function save( $post_id = null ) { |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Perform checks whether the container should be attached during the current request |
||
| 127 | * |
||
| 128 | * @return bool True if the container is allowed to be attached |
||
| 129 | **/ |
||
| 130 | public function is_valid_attach_for_request() { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Check container attachment rules against object id |
||
| 174 | * |
||
| 175 | * @param int $object_id |
||
| 176 | * @return bool |
||
| 177 | **/ |
||
| 178 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Add meta box for each of the container post types |
||
| 206 | **/ |
||
| 207 | public function attach() { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Classes to add to the post meta box |
||
| 226 | */ |
||
| 227 | public function add_postbox_classes( $classes ) { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Output the container markup |
||
| 234 | **/ |
||
| 235 | public function render() { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Set the post ID the container will operate with. |
||
| 241 | * |
||
| 242 | * @param int $post_id |
||
| 243 | **/ |
||
| 244 | public function set_post_id( $post_id ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * CONDITION TOOLS |
||
| 251 | */ |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Check a condition against all supported conditions |
||
| 255 | * |
||
| 256 | * @param string $condition |
||
| 257 | * @param mixed $value |
||
| 258 | * @param WP_Post $post |
||
| 259 | * @return bool |
||
| 260 | */ |
||
| 261 | protected function is_condition_fullfilled( $condition, $value, $post ) { |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Check if a post is of a given post format |
||
| 326 | * |
||
| 327 | * @param WP_Post $post |
||
| 328 | * @param string $format |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | protected function condition_is_post_of_format( $post, $format ) { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Check if a post is on a specific level in it's hierarchy |
||
| 346 | * |
||
| 347 | * @param WP_Post $post |
||
| 348 | * @param int $level |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | protected function condition_is_post_on_level( $post, $level ) { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Check if a post uses one of a given array of templates |
||
| 359 | * |
||
| 360 | * @param WP_Post $post |
||
| 361 | * @param string|array<string> $templates |
||
| 362 | * @return bool |
||
| 363 | */ |
||
| 364 | protected function condition_is_post_using_template( $post, $templates ) { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * COMMON USAGE METHODS |
||
| 372 | */ |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Show the container only on particular page referenced by it's path. |
||
| 376 | * |
||
| 377 | * @param int|string $page page ID or page path |
||
| 378 | * @return object $this |
||
| 379 | **/ |
||
| 380 | 4 | public function show_on_page( $page ) { |
|
| 399 | |||
| 400 | /** |
||
| 401 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 402 | * |
||
| 403 | * @param string $parent_page_path |
||
| 404 | * @return object $this |
||
| 405 | **/ |
||
| 406 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 419 | |||
| 420 | /** |
||
| 421 | * Show the container only on posts from the specified category. |
||
| 422 | * |
||
| 423 | * @see show_on_taxonomy_term() |
||
| 424 | * |
||
| 425 | * @param string $category_slug |
||
| 426 | * @return object $this |
||
| 427 | **/ |
||
| 428 | public function show_on_category( $category_slug ) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Show the container only on pages whose template has filename $template_path. |
||
| 436 | * |
||
| 437 | * @param string|array $template_path |
||
| 438 | * @return object $this |
||
| 439 | **/ |
||
| 440 | 2 | public function show_on_template( $template_path ) { |
|
| 458 | |||
| 459 | /** |
||
| 460 | * Hide the container from pages whose template has filename $template_path. |
||
| 461 | * |
||
| 462 | * @param string|array $template_path |
||
| 463 | * @return object $this |
||
| 464 | **/ |
||
| 465 | public function hide_on_template( $template_path ) { |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Show the container only on hierarchical posts of level $level. |
||
| 480 | * Levels start from 1 (top level post) |
||
| 481 | * |
||
| 482 | * @param int $level |
||
| 483 | * @return object $this |
||
| 484 | **/ |
||
| 485 | public function show_on_level( $level ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 497 | * |
||
| 498 | * @param string $taxonomy_slug |
||
| 499 | * @param string $term_slug |
||
| 500 | * @return object $this |
||
| 501 | **/ |
||
| 502 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Show the container only on posts from the specified format. |
||
| 514 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 515 | * |
||
| 516 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 517 | * @return object $this |
||
| 518 | **/ |
||
| 519 | public function show_on_post_format( $post_format ) { |
||
| 535 | |||
| 536 | /** |
||
| 537 | * Show the container only on posts from the specified type(s). |
||
| 538 | * |
||
| 539 | * @param string|array $post_types |
||
| 540 | * @return object $this |
||
| 541 | **/ |
||
| 542 | public function show_on_post_type( $post_types ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Sets the meta box container context |
||
| 552 | * |
||
| 553 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 554 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 555 | */ |
||
| 556 | public function set_context( $context ) { |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Sets the meta box container priority |
||
| 564 | * |
||
| 565 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 566 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 567 | */ |
||
| 568 | public function set_priority( $priority ) { |
||
| 573 | } |
||
| 574 |