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 ) { |
||
| 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 ) { |
|
| 175 | 2 | \Carbon_Fields\App::verify_boot(); |
|
| 176 | |||
| 177 | 2 | if ( empty( $title ) ) { |
|
| 178 | 1 | Incorrect_Syntax_Exception::raise( 'Empty container title is not supported' ); |
|
| 179 | } |
||
| 180 | |||
| 181 | 1 | $this->id = $unique_id; |
|
| 182 | 1 | $this->title = $title; |
|
| 183 | 1 | $this->type = $type; |
|
| 184 | 1 | } |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Return whether the container is active |
||
| 188 | **/ |
||
| 189 | public function active() { |
||
| 190 | return $this->active; |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Activate the container and trigger an action |
||
| 195 | **/ |
||
| 196 | protected function activate() { |
||
| 197 | $this->active = true; |
||
| 198 | $this->boot(); |
||
| 199 | do_action( 'crb_container_activated', $this ); |
||
| 200 | |||
| 201 | $fields = $this->get_fields(); |
||
| 202 | foreach ( $fields as $field ) { |
||
| 203 | $field->activate(); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Perform instance initialization |
||
| 209 | **/ |
||
| 210 | abstract public function init(); |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Prints the container Underscore template |
||
| 214 | **/ |
||
| 215 | public function template() { |
||
| 216 | ?> |
||
| 217 | <div class="{{{ classes.join(' ') }}}"> |
||
| 218 | <# _.each(fields, function(field) { #> |
||
| 219 | <div class="{{{ field.classes.join(' ') }}}"> |
||
| 220 | <label for="{{{ field.id }}}"> |
||
| 221 | {{ field.label }} |
||
| 222 | |||
| 223 | <# if (field.required) { #> |
||
| 224 | <span class="carbon-required">*</span> |
||
| 225 | <# } #> |
||
| 226 | </label> |
||
| 227 | |||
| 228 | <div class="field-holder {{{ field.id }}}"></div> |
||
| 229 | |||
| 230 | <# if (field.help_text) { #> |
||
| 231 | <em class="help-text"> |
||
| 232 | {{{ field.help_text }}} |
||
| 233 | </em> |
||
| 234 | <# } #> |
||
| 235 | |||
| 236 | <em class="carbon-error"></em> |
||
| 237 | </div> |
||
| 238 | <# }); #> |
||
| 239 | </div> |
||
| 240 | <?php |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Boot the container once it's attached. |
||
| 245 | **/ |
||
| 246 | protected function boot() { |
||
| 247 | $this->add_template( $this->type, array( $this, 'template' ) ); |
||
| 248 | |||
| 249 | add_action( 'admin_footer', array( get_class(), 'admin_hook_scripts' ), 5 ); |
||
| 250 | add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 ); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Load the value for each field in the container. |
||
| 255 | * Could be used internally during container rendering |
||
| 256 | **/ |
||
| 257 | public function load() { |
||
| 258 | foreach ( $this->fields as $field ) { |
||
| 259 | $field->load(); |
||
| 260 | } |
||
| 261 | } |
||
| 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 | * Called first as part of the container attachment procedure. |
||
| 301 | * Responsible for checking it's OK to attach the container |
||
| 302 | * and if it is, calling the container-specific attach() method |
||
| 303 | * |
||
| 304 | * @see attach() |
||
| 305 | * @see is_valid_attach() |
||
| 306 | **/ |
||
| 307 | public function _attach() { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Attach the container rendering and helping methods |
||
| 321 | * to concrete WordPress Action hooks |
||
| 322 | **/ |
||
| 323 | public function attach() {} |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Perform checks whether the container should be attached during the current request |
||
| 327 | * |
||
| 328 | * @return bool True if the container is allowed to be attached |
||
| 329 | **/ |
||
| 330 | final public function is_valid_attach() { |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Check container attachment rules against current page request (in admin) |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | **/ |
||
| 340 | abstract protected function is_valid_attach_for_request(); |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Check container attachment rules against object id |
||
| 344 | * |
||
| 345 | * @param int $object_id |
||
| 346 | * @return bool |
||
| 347 | **/ |
||
| 348 | abstract public function is_valid_attach_for_object( $object_id = null ); |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Whether this container is currently viewed. |
||
| 352 | **/ |
||
| 353 | public function should_activate() { |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Returns all the Backbone templates |
||
| 359 | * |
||
| 360 | * @return array |
||
| 361 | **/ |
||
| 362 | public function get_templates() { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Adds a new Backbone template |
||
| 368 | **/ |
||
| 369 | protected function add_template( $name, $callback ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Perform a check whether the current container has fields |
||
| 375 | * |
||
| 376 | * @return bool |
||
| 377 | **/ |
||
| 378 | public function has_fields() { |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Returns the private container array of fields. |
||
| 384 | * Use only if you are completely aware of what you are doing. |
||
| 385 | * |
||
| 386 | * @return array |
||
| 387 | **/ |
||
| 388 | public function get_fields() { |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Return root field from container with specified name |
||
| 394 | * |
||
| 395 | * @example crb_complex |
||
| 396 | * |
||
| 397 | * @param string $field_name |
||
| 398 | * @return Field |
||
| 399 | **/ |
||
| 400 | public function get_root_field_by_name( $field_name ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return field from container with specified name |
||
| 412 | * |
||
| 413 | * @example crb_complex/text_field |
||
| 414 | * @example crb_complex/complex_2 |
||
| 415 | * @example crb_complex/complex_2:text_group/text_field |
||
| 416 | * |
||
| 417 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
| 418 | * @return Field |
||
| 419 | **/ |
||
| 420 | public function get_field_by_name( $field_name ) { |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Perform checks whether there is a field registered with the name $name. |
||
| 458 | * If not, the field name is recorded. |
||
| 459 | * |
||
| 460 | * @param string $name |
||
| 461 | **/ |
||
| 462 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 469 | |||
| 470 | /** |
||
| 471 | * Remove field name $name from the list of unique field names |
||
| 472 | * |
||
| 473 | * @param string $name |
||
| 474 | **/ |
||
| 475 | public function drop_unique_field_name( $name ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Return whether the datastore instance is the default one or has been overriden |
||
| 485 | * |
||
| 486 | * @return boolean |
||
| 487 | **/ |
||
| 488 | 6 | public function has_default_datastore() { |
|
| 491 | |||
| 492 | /** |
||
| 493 | * Set datastore instance |
||
| 494 | * |
||
| 495 | * @param Datastore_Interface $datastore |
||
| 496 | * @return object $this |
||
| 497 | **/ |
||
| 498 | 6 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
| 510 | |||
| 511 | /** |
||
| 512 | * Get the DataStore instance |
||
| 513 | * |
||
| 514 | * @return Datastore_Interface $datastore |
||
| 515 | **/ |
||
| 516 | 6 | public function get_datastore() { |
|
| 519 | |||
| 520 | /** |
||
| 521 | * Return WordPress nonce name used to identify the current container instance |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | **/ |
||
| 525 | public function get_nonce_name() { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Return WordPress nonce field |
||
| 531 | * |
||
| 532 | * @return string |
||
| 533 | **/ |
||
| 534 | public function get_nonce_field() { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Check if the nonce is present in the request and that it is verified |
||
| 540 | * |
||
| 541 | * @return bool |
||
| 542 | **/ |
||
| 543 | protected function verified_nonce_in_request() { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Internal function that creates the tab and associates it with particular field set |
||
| 551 | * |
||
| 552 | * @param string $tab_name |
||
| 553 | * @param array $fields |
||
| 554 | * @param int $queue_end |
||
| 555 | * @return object $this |
||
| 556 | */ |
||
| 557 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Whether the container is tabbed or not |
||
| 581 | * |
||
| 582 | * @return bool |
||
| 583 | */ |
||
| 584 | public function is_tabbed() { |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Retrieve all fields that are not defined under a specific tab |
||
| 590 | * |
||
| 591 | * @return array |
||
| 592 | */ |
||
| 593 | protected function get_untabbed_fields() { |
||
| 615 | |||
| 616 | /** |
||
| 617 | * Retrieve all tabs. |
||
| 618 | * Create a default tab if there are any untabbed fields. |
||
| 619 | * |
||
| 620 | * @return array |
||
| 621 | */ |
||
| 622 | protected function get_tabs() { |
||
| 631 | |||
| 632 | /** |
||
| 633 | * Build the tabs JSON |
||
| 634 | * |
||
| 635 | * @return array |
||
| 636 | */ |
||
| 637 | protected function get_tabs_json() { |
||
| 649 | |||
| 650 | /** |
||
| 651 | * Underscore template for tabs |
||
| 652 | */ |
||
| 653 | public function template_tabs() { |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 675 | * This data will be available in the Underscore template and the Backbone Model. |
||
| 676 | * |
||
| 677 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 678 | * @return array |
||
| 679 | */ |
||
| 680 | public function to_json( $load ) { |
||
| 697 | |||
| 698 | /** |
||
| 699 | * Enqueue admin scripts |
||
| 700 | */ |
||
| 701 | public static function admin_hook_scripts() { |
||
| 711 | |||
| 712 | /** |
||
| 713 | * Enqueue admin styles |
||
| 714 | */ |
||
| 715 | public static function admin_hook_styles() { |
||
| 718 | |||
| 719 | /** |
||
| 720 | * COMMON USAGE METHODS |
||
| 721 | */ |
||
| 722 | |||
| 723 | /** |
||
| 724 | * Append array of fields to the current fields set. All items of the array |
||
| 725 | * must be instances of Field and their names should be unique for all |
||
| 726 | * Carbon containers. |
||
| 727 | * If a field does not have DataStore already, the container datastore is |
||
| 728 | * assigned to them instead. |
||
| 729 | * |
||
| 730 | * @param array $fields |
||
| 731 | * @return object $this |
||
| 732 | **/ |
||
| 733 | public function add_fields( $fields ) { |
||
| 751 | |||
| 752 | /** |
||
| 753 | * Configuration function for adding tab with fields |
||
| 754 | * |
||
| 755 | * @param string $tab_name |
||
| 756 | * @param array $fields |
||
| 757 | * @return object $this |
||
| 758 | */ |
||
| 759 | public function add_tab( $tab_name, $fields ) { |
||
| 767 | } |
||
| 768 |