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 |
||
| 16 | abstract class Container implements Datastore_Holder_Interface { |
||
| 17 | /** |
||
| 18 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
| 19 | */ |
||
| 20 | const TABS_TAIL = 1; |
||
| 21 | const TABS_HEAD = 2; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Stores if the container is active on the current page |
||
| 25 | * |
||
| 26 | * @see activate() |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $active = false; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * List of registered unique field names for this container instance |
||
| 33 | * |
||
| 34 | * @see verify_unique_field_name() |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $registered_field_names = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Stores all the container Backbone templates |
||
| 41 | * |
||
| 42 | * @see factory() |
||
| 43 | * @see add_template() |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $templates = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Tabs available |
||
| 50 | */ |
||
| 51 | protected $tabs = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * List of default container settings |
||
| 55 | * |
||
| 56 | * @see init() |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | public $settings = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Title of the container |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | public $title = ''; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * List of notification messages to be displayed on the front-end |
||
| 70 | * |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $notifications = array(); |
||
| 74 | |||
| 75 | /** |
||
| 76 | * List of error messages to be displayed on the front-end |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $errors = array(); |
||
| 81 | |||
| 82 | /** |
||
| 83 | * List of container fields |
||
| 84 | * |
||
| 85 | * @see add_fields() |
||
| 86 | * @var array |
||
| 87 | */ |
||
| 88 | protected $fields = array(); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Container datastores. Propagated to all container fields |
||
| 92 | * |
||
| 93 | * @see set_datastore() |
||
| 94 | * @see get_datastore() |
||
| 95 | * @var object |
||
| 96 | */ |
||
| 97 | protected $datastore; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Flag whether the datastore is the default one or replaced with a custom one |
||
| 101 | * |
||
| 102 | * @see set_datastore() |
||
| 103 | * @see get_datastore() |
||
| 104 | * @var boolean |
||
| 105 | */ |
||
| 106 | protected $has_default_datastore = true; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Normalizes a container type string to an expected format |
||
| 110 | * |
||
| 111 | * @param string $type |
||
| 112 | * @return string $normalized_type |
||
| 113 | **/ |
||
| 114 | protected static function normalize_container_type( $type ) { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Resolves a string-based type to a fully qualified container class name |
||
| 126 | * |
||
| 127 | * @param string $type |
||
| 128 | * @return string $class_name |
||
| 129 | **/ |
||
| 130 | protected static function container_type_to_class( $type ) { |
||
| 131 | $class = __NAMESPACE__ . '\\' . $type . '_Container'; |
||
| 132 | if ( ! class_exists( $class ) ) { |
||
| 133 | Incorrect_Syntax_Exception::raise( 'Unknown container "' . $type . '".' ); |
||
| 134 | $class = __NAMESPACE__ . '\\Broken_Container'; |
||
| 135 | } |
||
| 136 | return $class; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Create a new container of type $type and name $name. |
||
| 141 | * |
||
| 142 | * @param string $type |
||
| 143 | * @param string $name Human-readable name of the container |
||
| 144 | * @return object $container |
||
| 145 | **/ |
||
| 146 | 9 | public static function factory( $type, $name ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * An alias of factory(). |
||
| 160 | * |
||
| 161 | * @see Container::factory() |
||
| 162 | **/ |
||
| 163 | public static function make( $type, $name ) { |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Create a new container |
||
| 169 | * |
||
| 170 | * @param string $unique_id Unique id of the container |
||
| 171 | * @param string $title title of the container |
||
| 172 | * @param string $type Type of the container |
||
| 173 | **/ |
||
| 174 | 2 | public function __construct( $unique_id, $title, $type ) { |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Return whether the container is active |
||
| 186 | **/ |
||
| 187 | public function active() { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Activate the container and trigger an action |
||
| 193 | **/ |
||
| 194 | protected function activate() { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Activates and boots a field recursively |
||
| 202 | **/ |
||
| 203 | protected function activate_field( $field ) { |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Perform instance initialization |
||
| 219 | **/ |
||
| 220 | abstract public function init(); |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Prints the container Underscore template |
||
| 224 | **/ |
||
| 225 | public function template() { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Boot the container once it's attached. |
||
| 255 | **/ |
||
| 256 | protected function boot() { |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Called first as part of the container save procedure. |
||
| 265 | * Responsible for checking the request validity and |
||
| 266 | * calling the container-specific save() method |
||
| 267 | * |
||
| 268 | * @see save() |
||
| 269 | * @see is_valid_save() |
||
| 270 | **/ |
||
| 271 | public function _save() { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Load submitted data and save each field in the container |
||
| 280 | * |
||
| 281 | * @see is_valid_save() |
||
| 282 | **/ |
||
| 283 | public function save( $data ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Checks whether the current request is valid |
||
| 292 | * |
||
| 293 | * @return bool |
||
| 294 | **/ |
||
| 295 | public function is_valid_save() { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Load the value for each field in the container. |
||
| 301 | * Could be used internally during container rendering |
||
| 302 | **/ |
||
| 303 | public function load() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Called first as part of the container attachment procedure. |
||
| 311 | * Responsible for checking it's OK to attach the container |
||
| 312 | * and if it is, calling the container-specific attach() method |
||
| 313 | * |
||
| 314 | * @see attach() |
||
| 315 | * @see is_valid_attach() |
||
| 316 | **/ |
||
| 317 | public function _attach() { |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Returns all the Backbone templates |
||
| 335 | * |
||
| 336 | * @return array |
||
| 337 | **/ |
||
| 338 | public function get_templates() { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Adds a new Backbone template |
||
| 344 | **/ |
||
| 345 | protected function add_template( $name, $callback ) { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Attach the container rendering and helping methods |
||
| 351 | * to concrete WordPress Action hooks |
||
| 352 | **/ |
||
| 353 | public function attach() {} |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Perform checks whether the container should be attached during the current request |
||
| 357 | * |
||
| 358 | * @return bool True if the container is allowed to be attached |
||
| 359 | **/ |
||
| 360 | final public function is_valid_attach() { |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Require extending containers to define their own attach rules |
||
| 367 | * |
||
| 368 | * @return bool True if the container is allowed to be attached |
||
| 369 | **/ |
||
| 370 | abstract protected function _is_valid_attach(); |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Whether this container is currently viewed. |
||
| 374 | **/ |
||
| 375 | public function is_active() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Perform a check whether the current container has fields |
||
| 381 | * |
||
| 382 | * @return bool |
||
| 383 | **/ |
||
| 384 | public function has_fields() { |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Returns the private container array of fields. |
||
| 390 | * Use only if you are completely aware of what you are doing. |
||
| 391 | * |
||
| 392 | * @return array |
||
| 393 | **/ |
||
| 394 | public function get_fields() { |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Return root field from container with specified name |
||
| 400 | * |
||
| 401 | * @example crb_complex |
||
| 402 | * |
||
| 403 | * @param string $field_name |
||
| 404 | * @return Field |
||
| 405 | **/ |
||
| 406 | public function get_root_field_by_name( $field_name ) { |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Return field from container with specified name |
||
| 418 | * |
||
| 419 | * @example crb_complex/text_field |
||
| 420 | * @example crb_complex/complex_2 |
||
| 421 | * @example crb_complex/complex_2:text_group/text_field |
||
| 422 | * |
||
| 423 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
| 424 | * @return Field |
||
| 425 | **/ |
||
| 426 | public function get_field_by_name( $field_name ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Perform checks whether there is a field registered with the name $name. |
||
| 464 | * If not, the field name is recorded. |
||
| 465 | * |
||
| 466 | * @param string $name |
||
| 467 | **/ |
||
| 468 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 475 | |||
| 476 | /** |
||
| 477 | * Remove field name $name from the list of unique field names |
||
| 478 | * |
||
| 479 | * @param string $name |
||
| 480 | **/ |
||
| 481 | public function drop_unique_field_name( $name ) { |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Return whether the datastore instance is the default one or has been overriden |
||
| 491 | * |
||
| 492 | * @return boolean |
||
| 493 | **/ |
||
| 494 | 6 | public function has_default_datastore() { |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Set datastore instance |
||
| 500 | * |
||
| 501 | * @param Datastore_Interface $datastore |
||
| 502 | * @return object $this |
||
| 503 | **/ |
||
| 504 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
| 516 | |||
| 517 | /** |
||
| 518 | * Get the DataStore instance |
||
| 519 | * |
||
| 520 | * @return Datastore_Interface $datastore |
||
| 521 | **/ |
||
| 522 | 6 | public function get_datastore() { |
|
| 525 | |||
| 526 | /** |
||
| 527 | * Return WordPress nonce name used to identify the current container instance |
||
| 528 | * |
||
| 529 | * @return string |
||
| 530 | **/ |
||
| 531 | public function get_nonce_name() { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Return WordPress nonce field |
||
| 537 | * |
||
| 538 | * @return string |
||
| 539 | **/ |
||
| 540 | public function get_nonce_field() { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Check if the nonce is present in the request and that it is verified |
||
| 546 | * |
||
| 547 | * @return bool |
||
| 548 | **/ |
||
| 549 | protected function verified_nonce_in_request() { |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Internal function that creates the tab and associates it with particular field set |
||
| 557 | * |
||
| 558 | * @param string $tab_name |
||
| 559 | * @param array $fields |
||
| 560 | * @param int $queue_end |
||
| 561 | * @return object $this |
||
| 562 | */ |
||
| 563 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Whether the container is tabbed or not |
||
| 587 | * |
||
| 588 | * @return bool |
||
| 589 | */ |
||
| 590 | public function is_tabbed() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Retrieve all fields that are not defined under a specific tab |
||
| 596 | * |
||
| 597 | * @return array |
||
| 598 | */ |
||
| 599 | protected function get_untabbed_fields() { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Retrieve all tabs. |
||
| 624 | * Create a default tab if there are any untabbed fields. |
||
| 625 | * |
||
| 626 | * @return array |
||
| 627 | */ |
||
| 628 | protected function get_tabs() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Build the tabs JSON |
||
| 640 | * |
||
| 641 | * @return array |
||
| 642 | */ |
||
| 643 | protected function get_tabs_json() { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * Underscore template for tabs |
||
| 658 | */ |
||
| 659 | public function template_tabs() { |
||
| 678 | |||
| 679 | /** |
||
| 680 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 681 | * This data will be available in the Underscore template and the Backbone Model. |
||
| 682 | * |
||
| 683 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 684 | * @return array |
||
| 685 | */ |
||
| 686 | public function to_json( $load ) { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Enqueue admin scripts |
||
| 706 | */ |
||
| 707 | public static function admin_hook_scripts() { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Enqueue admin styles |
||
| 720 | */ |
||
| 721 | public static function admin_hook_styles() { |
||
| 724 | |||
| 725 | /** |
||
| 726 | * COMMON USAGE METHODS |
||
| 727 | */ |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Append array of fields to the current fields set. All items of the array |
||
| 731 | * must be instances of Field and their names should be unique for all |
||
| 732 | * Carbon containers. |
||
| 733 | * If a field does not have DataStore already, the container datastore is |
||
| 734 | * assigned to them instead. |
||
| 735 | * |
||
| 736 | * @param array $fields |
||
| 737 | * @return object $this |
||
| 738 | **/ |
||
| 739 | public function add_fields( $fields ) { |
||
| 757 | |||
| 758 | /** |
||
| 759 | * Configuration function for adding tab with fields |
||
| 760 | * |
||
| 761 | * @param string $tab_name |
||
| 762 | * @param array $fields |
||
| 763 | * @return object $this |
||
| 764 | */ |
||
| 765 | public function add_tab( $tab_name, $fields ) { |
||
| 773 | } |
||
| 774 |