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 |
||
| 18 | abstract class Container implements Datastore_Holder_Interface { |
||
| 19 | /** |
||
| 20 | * Where to put a particular tab -- at the head or the tail. Tail by default |
||
| 21 | */ |
||
| 22 | const TABS_TAIL = 1; |
||
| 23 | const TABS_HEAD = 2; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Separator signifying field hierarchy relation |
||
| 27 | * Used when searching for fields in a specific complex field |
||
| 28 | */ |
||
| 29 | const HIERARCHY_FIELD_SEPARATOR = '/'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Separator signifying complex_field->group relation |
||
| 33 | * Used when searching for fields in a specific complex field group |
||
| 34 | */ |
||
| 35 | const HIERARCHY_GROUP_SEPARATOR = ':'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Stores if the container is active on the current page |
||
| 39 | * |
||
| 40 | * @see activate() |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $active = false; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * List of registered unique field names for this container instance |
||
| 47 | * |
||
| 48 | * @see verify_unique_field_name() |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $registered_field_names = array(); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Tabs available |
||
| 55 | */ |
||
| 56 | protected $tabs = array(); |
||
| 57 | |||
| 58 | /** |
||
| 59 | * List of default container settings |
||
| 60 | * |
||
| 61 | * @see init() |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | public $settings = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Title of the container |
||
| 68 | * |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | public $title = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * List of notification messages to be displayed on the front-end |
||
| 75 | * |
||
| 76 | * @var array |
||
| 77 | */ |
||
| 78 | protected $notifications = array(); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * List of error messages to be displayed on the front-end |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $errors = array(); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * List of container fields |
||
| 89 | * |
||
| 90 | * @see add_fields() |
||
| 91 | * @var array |
||
| 92 | */ |
||
| 93 | protected $fields = array(); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Container datastores. Propagated to all container fields |
||
| 97 | * |
||
| 98 | * @see set_datastore() |
||
| 99 | * @see get_datastore() |
||
| 100 | * @var object |
||
| 101 | */ |
||
| 102 | protected $datastore; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Flag whether the datastore is the default one or replaced with a custom one |
||
| 106 | * |
||
| 107 | * @see set_datastore() |
||
| 108 | * @see get_datastore() |
||
| 109 | * @var boolean |
||
| 110 | */ |
||
| 111 | protected $has_default_datastore = true; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Fulfillable_Collection to use when checking attachment/saving conditions |
||
| 115 | * |
||
| 116 | * @var Fulfillable_Collection |
||
| 117 | */ |
||
| 118 | protected $condition_collection; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get array of all static condition types |
||
| 122 | * |
||
| 123 | * @param boolean $static |
||
| 124 | * @return array<string> |
||
| 125 | */ |
||
| 126 | protected function get_condition_types( $static ) { |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Create a new container of type $type and name $name. |
||
| 139 | * |
||
| 140 | * @param string $type |
||
| 141 | * @param string $name Human-readable name of the container |
||
| 142 | * @return object $container |
||
| 143 | */ |
||
| 144 | public static function factory( $type, $name ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * An alias of factory(). |
||
| 163 | * |
||
| 164 | * @see Container::factory() |
||
| 165 | */ |
||
| 166 | public static function make( $type, $name ) { |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Create a new container |
||
| 172 | * |
||
| 173 | * @param string $unique_id Unique id of the container |
||
| 174 | * @param string $title title of the container |
||
| 175 | * @param string $type Type of the container |
||
| 176 | */ |
||
| 177 | public function __construct( $unique_id, $title, $type ) { |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return whether the container is active |
||
| 196 | 9 | */ |
|
| 197 | 9 | public function active() { |
|
| 200 | 9 | ||
| 201 | 9 | /** |
|
| 202 | 7 | * Activate the container and trigger an action |
|
| 203 | 7 | */ |
|
| 204 | protected function activate() { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Perform instance initialization |
||
| 217 | */ |
||
| 218 | abstract public function init(); |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Boot the container once it's attached. |
||
| 222 | */ |
||
| 223 | protected function boot() { |
||
| 226 | |||
| 227 | 2 | /** |
|
| 228 | 1 | * Load the value for each field in the container. |
|
| 229 | * Could be used internally during container rendering |
||
| 230 | */ |
||
| 231 | 1 | public function load() { |
|
| 236 | 1 | ||
| 237 | /** |
||
| 238 | 1 | * Called first as part of the container save procedure. |
|
| 239 | 1 | * Responsible for checking the request validity and |
|
| 240 | * calling the container-specific save() method |
||
| 241 | * |
||
| 242 | * @see save() |
||
| 243 | * @see is_valid_save() |
||
| 244 | */ |
||
| 245 | public function _save() { |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Load submitted data and save each field in the container |
||
| 254 | * |
||
| 255 | * @see is_valid_save() |
||
| 256 | */ |
||
| 257 | public function save( $data = null ) { |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Checks whether the current request is valid |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | */ |
||
| 269 | public function is_valid_save() { |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Called first as part of the container attachment procedure. |
||
| 275 | * Responsible for checking it's OK to attach the container |
||
| 276 | * and if it is, calling the container-specific attach() method |
||
| 277 | * |
||
| 278 | * @see attach() |
||
| 279 | * @see is_valid_attach() |
||
| 280 | */ |
||
| 281 | public function _attach() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Attach the container rendering and helping methods |
||
| 295 | * to concrete WordPress Action hooks |
||
| 296 | */ |
||
| 297 | public function attach() {} |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Perform checks whether the container should be attached during the current request |
||
| 301 | * |
||
| 302 | * @return bool True if the container is allowed to be attached |
||
| 303 | */ |
||
| 304 | final public function is_valid_attach() { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Get environment array for page request (in admin) |
||
| 311 | * |
||
| 312 | * @return array |
||
| 313 | */ |
||
| 314 | abstract protected function get_environment_for_request(); |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Check container attachment rules against current page request (in admin) |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | abstract protected function is_valid_attach_for_request(); |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Check if conditions pass for request |
||
| 325 | * |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | protected function static_conditions_pass() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get environment array for object id |
||
| 339 | * |
||
| 340 | * @param integer $object_id |
||
| 341 | * @return array |
||
| 342 | */ |
||
| 343 | abstract protected function get_environment_for_object( $object_id ); |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Check container attachment rules against object id |
||
| 347 | * |
||
| 348 | * @param int $object_id |
||
| 349 | * @return bool |
||
| 350 | */ |
||
| 351 | abstract public function is_valid_attach_for_object( $object_id ); |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Check if all conditions pass for object |
||
| 355 | * |
||
| 356 | * @return bool |
||
| 357 | */ |
||
| 358 | protected function all_conditions_pass( $object_id ) { |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Whether this container is currently viewed. |
||
| 365 | */ |
||
| 366 | public function should_activate() { |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Perform a check whether the current container has fields |
||
| 372 | * |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function has_fields() { |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Returns the private container array of fields. |
||
| 381 | * Use only if you are completely aware of what you are doing. |
||
| 382 | * |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | public function get_fields() { |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Return root field from container with specified name |
||
| 391 | * |
||
| 392 | * @example crb_complex |
||
| 393 | * |
||
| 394 | * @param string $field_name |
||
| 395 | * @return Field |
||
| 396 | */ |
||
| 397 | public function get_root_field_by_name( $field_name ) { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get a regex to match field name patterns used to fetch specific fields |
||
| 409 | * |
||
| 410 | * @return string |
||
| 411 | */ |
||
| 412 | protected function get_field_pattern_regex() { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return field from container with specified name |
||
| 430 | * |
||
| 431 | * @example crb_complex/text_field |
||
| 432 | * @example crb_complex/complex_2 |
||
| 433 | * @example crb_complex/complex_2:text_group/text_field |
||
| 434 | * |
||
| 435 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
| 436 | * @return Field |
||
| 437 | */ |
||
| 438 | public function get_field_by_name( $field_name ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Perform checks whether there is a field registered with the name $name. |
||
| 485 | * If not, the field name is recorded. |
||
| 486 | * |
||
| 487 | * @param string $name |
||
| 488 | */ |
||
| 489 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Remove field name $name from the list of unique field names |
||
| 499 | * |
||
| 500 | * @param string $name |
||
| 501 | */ |
||
| 502 | public function drop_unique_field_name( $name ) { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Return whether the datastore instance is the default one or has been overriden |
||
| 512 | * |
||
| 513 | * @return boolean |
||
| 514 | */ |
||
| 515 | public function has_default_datastore() { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Set datastore instance |
||
| 521 | * |
||
| 522 | * @param Datastore_Interface $datastore |
||
| 523 | * @return object $this |
||
| 524 | */ |
||
| 525 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 537 | |||
| 538 | 6 | /** |
|
| 539 | 6 | * Get the DataStore instance |
|
| 540 | * |
||
| 541 | * @return Datastore_Interface $datastore |
||
| 542 | */ |
||
| 543 | public function get_datastore() { |
||
| 546 | |||
| 547 | /** |
||
| 548 | 6 | * Return WordPress nonce name used to identify the current container instance |
|
| 549 | 6 | * |
|
| 550 | 1 | * @return string |
|
| 551 | */ |
||
| 552 | 6 | public function get_nonce_name() { |
|
| 555 | 6 | ||
| 556 | /** |
||
| 557 | 6 | * Return WordPress nonce field |
|
| 558 | 6 | * |
|
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function get_nonce_field() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | 6 | * Check if the nonce is present in the request and that it is verified |
|
| 567 | 6 | * |
|
| 568 | * @return bool |
||
| 569 | */ |
||
| 570 | protected function verified_nonce_in_request() { |
||
| 575 | |||
| 576 | /** |
||
| 577 | * Internal function that creates the tab and associates it with particular field set |
||
| 578 | * |
||
| 579 | * @param string $tab_name |
||
| 580 | * @param array $fields |
||
| 581 | * @param int $queue_end |
||
| 582 | * @return object $this |
||
| 583 | */ |
||
| 584 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Whether the container is tabbed or not |
||
| 608 | * |
||
| 609 | * @return bool |
||
| 610 | */ |
||
| 611 | public function is_tabbed() { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Retrieve all fields that are not defined under a specific tab |
||
| 617 | * |
||
| 618 | * @return array |
||
| 619 | */ |
||
| 620 | protected function get_untabbed_fields() { |
||
| 642 | |||
| 643 | /** |
||
| 644 | * Retrieve all tabs. |
||
| 645 | * Create a default tab if there are any untabbed fields. |
||
| 646 | * |
||
| 647 | * @return array |
||
| 648 | */ |
||
| 649 | protected function get_tabs() { |
||
| 658 | |||
| 659 | /** |
||
| 660 | * Build the tabs JSON |
||
| 661 | * |
||
| 662 | * @return array |
||
| 663 | */ |
||
| 664 | protected function get_tabs_json() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 679 | * |
||
| 680 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 681 | * @return array |
||
| 682 | */ |
||
| 683 | public function to_json( $load ) { |
||
| 705 | |||
| 706 | /** |
||
| 707 | * Enqueue admin styles |
||
| 708 | */ |
||
| 709 | public static function admin_hook_styles() { |
||
| 712 | |||
| 713 | /** |
||
| 714 | * COMMON USAGE METHODS |
||
| 715 | */ |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Append array of fields to the current fields set. All items of the array |
||
| 719 | * must be instances of Field and their names should be unique for all |
||
| 720 | * Carbon containers. |
||
| 721 | * If a field does not have DataStore already, the container datastore is |
||
| 722 | * assigned to them instead. |
||
| 723 | * |
||
| 724 | * @param array $fields |
||
| 725 | * @return object $this |
||
| 726 | */ |
||
| 727 | public function add_fields( $fields ) { |
||
| 745 | |||
| 746 | /** |
||
| 747 | * Configuration function for adding tab with fields |
||
| 748 | * |
||
| 749 | * @param string $tab_name |
||
| 750 | * @param array $fields |
||
| 751 | * @return object $this |
||
| 752 | */ |
||
| 753 | public function add_tab( $tab_name, $fields ) { |
||
| 758 | |||
| 759 | /** |
||
| 760 | * Proxy function to set attachment conditions |
||
| 761 | * |
||
| 762 | * @see Fulfillable_Collection::when() |
||
| 763 | * @return Container $this |
||
| 764 | */ |
||
| 765 | public function when() { |
||
| 769 | |||
| 770 | /** |
||
| 771 | * Proxy function to set attachment conditions |
||
| 772 | * |
||
| 773 | * @see Fulfillable_Collection::and_when() |
||
| 774 | * @return Container $this |
||
| 775 | */ |
||
| 776 | public function and_when() { |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Proxy function to set attachment conditions |
||
| 783 | * |
||
| 784 | * @see Fulfillable_Collection::or_when() |
||
| 785 | * @return Container $this |
||
| 786 | */ |
||
| 787 | public function or_when() { |
||
| 791 | } |
||
| 792 |