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 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 Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | abstract class Container implements Datastore_Holder_Interface { |
||
| 15 | /** |
||
| 16 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
| 17 | */ |
||
| 18 | const TABS_TAIL = 1; |
||
| 19 | const TABS_HEAD = 2; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * List of registered unique panel identificators |
||
| 23 | * |
||
| 24 | * @see get_unique_panel_id() |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | public static $registered_panel_ids = array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * List of containers created via factory that |
||
| 31 | * should be initialized |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $init_containers = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * List of containers attached to the current page view |
||
| 39 | * |
||
| 40 | * @see _attach() |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | public static $active_containers = array(); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * List of fields attached to the current page view |
||
| 47 | * |
||
| 48 | * @see _attach() |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected static $active_fields = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * List of registered unique field names for this container instance |
||
| 55 | * |
||
| 56 | * @see verify_unique_field_name() |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | protected $registered_field_names = array(); |
||
|
1 ignored issue
–
show
|
|||
| 60 | |||
| 61 | /** |
||
| 62 | * Stores all the container Backbone templates |
||
| 63 | * |
||
| 64 | * @see factory() |
||
| 65 | * @see add_template() |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $templates = array(); |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Tabs available |
||
| 72 | */ |
||
| 73 | protected $tabs = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * List of default container settings |
||
| 77 | * |
||
| 78 | * @see init() |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | public $settings = array(); |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Title of the container |
||
| 85 | * |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | public $title = ''; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Whether the container was setup |
||
| 92 | * |
||
| 93 | * @var bool |
||
| 94 | */ |
||
| 95 | public $setup_ready = false; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * List of notification messages to be displayed on the front-end |
||
| 99 | * |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | protected $notifications = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * List of error messages to be displayed on the front-end |
||
| 106 | * |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $errors = array(); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * List of container fields |
||
| 113 | * |
||
| 114 | * @see add_fields() |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | protected $fields = array(); |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Container datastores. Propagated to all container fields |
||
| 121 | * |
||
| 122 | * @see set_datastore() |
||
| 123 | * @see get_datastore() |
||
| 124 | * @var object |
||
| 125 | */ |
||
| 126 | protected $datastore; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Flag whether the datastore is the default one or replaced with a custom one |
||
| 130 | * |
||
| 131 | * @see set_datastore() |
||
| 132 | * @see get_datastore() |
||
| 133 | * @var object |
||
| 134 | */ |
||
| 135 | protected $has_default_datastore = true; |
||
|
1 ignored issue
–
show
|
|||
| 136 | |||
| 137 | /** |
||
| 138 | * Create a new container of type $type and name $name and label $label. |
||
| 139 | * |
||
| 140 | * @param string $type |
||
| 141 | * @param string $name Human-readable name of the container |
||
| 142 | * @return object $container |
||
| 143 | **/ |
||
| 144 | 11 | View Code Duplication | public static function factory( $type, $name ) { |
| 166 | |||
| 167 | /** |
||
| 168 | * An alias of factory(). |
||
| 169 | * |
||
| 170 | * @see Container::factory() |
||
| 171 | **/ |
||
| 172 | 11 | public static function make( $type, $name ) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Initialize containers created via factory |
||
| 178 | * |
||
| 179 | * @return object |
||
| 180 | **/ |
||
| 181 | public static function init_containers() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Returns all the active containers created via factory |
||
| 191 | * |
||
| 192 | * @return array |
||
| 193 | **/ |
||
| 194 | public static function get_active_containers() { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Adds a container to the active containers array and triggers an action |
||
| 200 | **/ |
||
| 201 | public static function activate_container( $container ) { |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Returns all the active fields created via factory |
||
| 211 | * |
||
| 212 | * @return array |
||
| 213 | **/ |
||
| 214 | public static function get_active_fields() { |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Adds a field to the active fields array and triggers an action |
||
| 220 | **/ |
||
| 221 | public static function activate_field( $field ) { |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Perform instance initialization after calling setup() |
||
| 239 | **/ |
||
| 240 | abstract public function init(); |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Prints the container Underscore template |
||
| 244 | **/ |
||
| 245 | public function template() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Create a new container |
||
| 275 | * |
||
| 276 | * @param string $title Unique title of the container |
||
| 277 | **/ |
||
| 278 | 9 | public function __construct( $title ) { |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Boot the container once it's attached. |
||
| 292 | **/ |
||
| 293 | public function boot() { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Update container settings and begin initialization |
||
| 302 | * |
||
| 303 | * @see init() |
||
| 304 | * @param array $settings |
||
| 305 | * @return object $this |
||
| 306 | **/ |
||
| 307 | public function setup( $settings = array() ) { |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Check if all required container settings have been specified |
||
| 329 | * |
||
| 330 | * @param array $settings Container settings |
||
| 331 | **/ |
||
| 332 | public function check_setup_settings( &$settings = array() ) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * Called first as part of the container save procedure. |
||
| 341 | * Responsible for checking the request validity and |
||
| 342 | * calling the container-specific save() method |
||
| 343 | * |
||
| 344 | * @see save() |
||
| 345 | * @see is_valid_save() |
||
| 346 | **/ |
||
| 347 | public function _save() { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Load submitted data and save each field in the container |
||
| 356 | * |
||
| 357 | * @see is_valid_save() |
||
| 358 | **/ |
||
| 359 | public function save( $data ) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Checks whether the current request is valid |
||
| 368 | * |
||
| 369 | * @return bool |
||
| 370 | **/ |
||
| 371 | public function is_valid_save() { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Load the value for each field in the container. |
||
| 377 | * Could be used internally during container rendering |
||
| 378 | **/ |
||
| 379 | public function load() { |
||
| 384 | |||
| 385 | |||
| 386 | /** |
||
| 387 | * Called first as part of the container attachment procedure. |
||
| 388 | * Responsible for checking it's OK to attach the container |
||
| 389 | * and if it is, calling the container-specific attach() method |
||
| 390 | * |
||
| 391 | * @see attach() |
||
| 392 | * @see is_valid_attach() |
||
| 393 | **/ |
||
| 394 | public function _attach() { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Returns all the Backbone templates |
||
| 412 | * |
||
| 413 | * @return array |
||
| 414 | **/ |
||
| 415 | public function get_templates() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Adds a new Backbone template |
||
| 421 | **/ |
||
| 422 | public function add_template( $name, $callback ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Attach the container rendering and helping methods |
||
| 428 | * to concrete WordPress Action hooks |
||
| 429 | **/ |
||
| 430 | public function attach() {} |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Perform checks whether the container is active for current request |
||
| 434 | * |
||
| 435 | * @return bool True if the container is active |
||
| 436 | **/ |
||
| 437 | public function is_active() { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Perform checks whether the container should be attached during the current request |
||
| 443 | * |
||
| 444 | * @return bool True if the container is allowed to be attached |
||
| 445 | **/ |
||
| 446 | public function is_valid_attach() { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Revert the result of attach() |
||
| 452 | **/ |
||
| 453 | public function detach() { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Append array of fields to the current fields set. All items of the array |
||
| 464 | * must be instances of Field and their names should be unique for all |
||
| 465 | * Carbon containers. |
||
| 466 | * If a field does not have DataStore already, the container datastore is |
||
| 467 | * assigned to them instead. |
||
| 468 | * |
||
| 469 | * @param array $fields |
||
| 470 | * @return object $this |
||
| 471 | **/ |
||
| 472 | public function add_fields( $fields ) { |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Configuration function for adding tab with fields |
||
| 493 | * |
||
| 494 | * @param string $tab_name |
||
| 495 | * @param array $fields |
||
| 496 | * @return object $this |
||
| 497 | */ |
||
| 498 | public function add_tab( $tab_name, $fields ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Internal function that creates the tab and associates it with particular field set |
||
| 509 | * |
||
| 510 | * @param string $tab_name |
||
| 511 | * @param array $fields |
||
| 512 | * @param int $queue_end |
||
| 513 | * @return object $this |
||
| 514 | */ |
||
| 515 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Whether the container is tabbed or not |
||
| 539 | * |
||
| 540 | * @return bool |
||
| 541 | */ |
||
| 542 | public function is_tabbed() { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Retrieve all fields that are not defined under a specific tab |
||
| 548 | * |
||
| 549 | * @return array |
||
| 550 | */ |
||
| 551 | public function get_untabbed_fields() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Retrieve all tabs. |
||
| 576 | * Create a default tab if there are any untabbed fields. |
||
| 577 | * |
||
| 578 | * @return array |
||
| 579 | */ |
||
| 580 | public function get_tabs() { |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Build the tabs JSON |
||
| 592 | * |
||
| 593 | * @return array |
||
| 594 | */ |
||
| 595 | public function get_tabs_json() { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Returns the private container array of fields. |
||
| 610 | * Use only if you are completely aware of what you are doing. |
||
| 611 | * |
||
| 612 | * @return array |
||
| 613 | **/ |
||
| 614 | public function get_fields() { |
||
| 617 | |||
| 618 | /** |
||
| 619 | * Perform a check whether the current container has fields |
||
| 620 | * |
||
| 621 | * @return bool |
||
| 622 | **/ |
||
| 623 | public function has_fields() { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Perform checks whether there is a container registered with identificator $id |
||
| 629 | */ |
||
| 630 | public static function get_unique_panel_id( $id ) { |
||
| 641 | |||
| 642 | |||
| 643 | /** |
||
| 644 | * Remove container identificator $id from the list of unique container ids |
||
| 645 | * |
||
| 646 | * @param string $id |
||
| 647 | **/ |
||
| 648 | public static function drop_unique_panel_id( $id ) { |
||
| 653 | |||
| 654 | /** |
||
| 655 | * Perform checks whether there is a field registered with the name $name. |
||
| 656 | * If not, the field name is recorded. |
||
| 657 | * |
||
| 658 | * @param string $name |
||
| 659 | **/ |
||
| 660 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 667 | |||
| 668 | /** |
||
| 669 | * Remove field name $name from the list of unique field names |
||
| 670 | * |
||
| 671 | * @param string $name |
||
| 672 | **/ |
||
| 673 | public function drop_unique_field_name( $name ) { |
||
| 680 | |||
| 681 | /** |
||
| 682 | * Return whether the datastore instance is the default one or has been overriden |
||
| 683 | * |
||
| 684 | * @return Datastore_Interface $datastore |
||
| 685 | **/ |
||
| 686 | public function has_default_datastore() { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Assign datastore instance for use by the container fields |
||
| 692 | * |
||
| 693 | * @param Datastore_Interface $datastore |
||
| 694 | * @return object $this |
||
| 695 | **/ |
||
| 696 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 708 | |||
| 709 | /** |
||
| 710 | * Return the DataStore instance used by container fields |
||
| 711 | * |
||
| 712 | * @return Datastore_Interface $datastore |
||
| 713 | **/ |
||
| 714 | public function get_datastore() { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Return WordPress nonce name used to identify the current container instance |
||
| 720 | * |
||
| 721 | * @return string |
||
| 722 | **/ |
||
| 723 | public function get_nonce_name() { |
||
| 726 | |||
| 727 | /** |
||
| 728 | * Return WordPress nonce field |
||
| 729 | * |
||
| 730 | * @return string |
||
| 731 | **/ |
||
| 732 | public function get_nonce_field() { |
||
| 735 | |||
| 736 | /** |
||
| 737 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 738 | * This data will be available in the Underscore template and the Backbone Model. |
||
| 739 | * |
||
| 740 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 741 | * @return array |
||
| 742 | */ |
||
| 743 | public function to_json( $load ) { |
||
| 760 | |||
| 761 | /** |
||
| 762 | * Underscore template for tabs |
||
| 763 | */ |
||
| 764 | public function template_tabs() { |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Enqueue admin scripts |
||
| 786 | */ |
||
| 787 | public static function admin_hook_scripts() { |
||
| 797 | |||
| 798 | /** |
||
| 799 | * Enqueue admin styles |
||
| 800 | */ |
||
| 801 | public static function admin_hook_styles() { |
||
| 804 | } // END Container |
||
| 805 | |||
| 806 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.