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 ) { |
||
| 127 | $group = $static ? 'static' : 'dynamic'; |
||
| 128 | $container_type = Helper::class_to_type( get_class( $this ), '_Container' ); |
||
| 129 | |||
| 130 | $condition_types = array(); |
||
| 131 | $condition_types = apply_filters( 'carbon_fields_' . $container_type . '_container_' . $group . '_condition_types', $condition_types, $container_type, $this ); |
||
| 132 | $condition_types = apply_filters( 'carbon_fields_container_' . $group . '_condition_types', $condition_types, $container_type, $this ); |
||
| 133 | |||
| 134 | return $condition_types; |
||
| 135 | } |
||
| 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 ) { |
||
| 145 | $normalized_type = Helper::normalize_type( $type ); |
||
| 146 | $class = Helper::type_to_class( $normalized_type, __NAMESPACE__, '_Container' ); |
||
| 147 | |||
| 148 | View Code Duplication | if ( ! class_exists( $class ) ) { |
|
| 149 | Incorrect_Syntax_Exception::raise( 'Unknown container "' . $type . '".' ); |
||
| 150 | $class = __NAMESPACE__ . '\\Broken_Container'; |
||
| 151 | } |
||
| 152 | |||
| 153 | $repository = App::resolve( 'container_repository' ); |
||
| 154 | $unique_id = $repository->get_unique_panel_id( $name ); |
||
| 155 | $container = new $class( $unique_id, $name, $normalized_type ); |
||
| 156 | $repository->register_container( $container ); |
||
| 157 | |||
| 158 | return $container; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * An alias of factory(). |
||
| 163 | * |
||
| 164 | * @see Container::factory() |
||
| 165 | */ |
||
| 166 | public static function make( $type, $name ) { |
||
| 167 | return static::factory( $type, $name ); |
||
| 168 | } |
||
| 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 ) { |
||
| 178 | App::verify_boot(); |
||
| 179 | |||
| 180 | if ( empty( $title ) ) { |
||
| 181 | Incorrect_Syntax_Exception::raise( 'Empty container title is not supported' ); |
||
| 182 | } |
||
| 183 | |||
| 184 | $this->id = $unique_id; |
||
| 185 | $this->title = $title; |
||
| 186 | $this->type = $type; |
||
| 187 | $this->condition_collection = App::resolve( 'container_condition_fulfillable_collection' ); |
||
| 188 | $this->condition_collection->set_condition_type_list( |
||
| 189 | array_merge( $this->get_condition_types( true ), $this->get_condition_types( false ) ), |
||
| 190 | true |
||
| 191 | ); |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Return whether the container is active |
||
| 196 | 9 | */ |
|
| 197 | 9 | public function active() { |
|
| 198 | 9 | return $this->active; |
|
| 199 | } |
||
| 200 | 9 | ||
| 201 | 9 | /** |
|
| 202 | 7 | * Activate the container and trigger an action |
|
| 203 | 7 | */ |
|
| 204 | protected function activate() { |
||
| 205 | 7 | $this->active = true; |
|
| 206 | $this->boot(); |
||
| 207 | do_action( 'crb_container_activated', $this ); |
||
| 208 | |||
| 209 | $fields = $this->get_fields(); |
||
| 210 | foreach ( $fields as $field ) { |
||
| 211 | $field->activate(); |
||
| 212 | } |
||
| 213 | } |
||
| 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() { |
||
| 224 | 2 | add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 ); |
|
| 225 | 2 | } |
|
| 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() { |
|
| 232 | 1 | foreach ( $this->fields as $field ) { |
|
| 233 | 1 | $field->load(); |
|
| 234 | 1 | } |
|
| 235 | 1 | } |
|
| 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() { |
||
| 246 | $param = func_get_args(); |
||
| 247 | if ( call_user_func_array( array( $this, '_is_valid_save' ), $param ) ) { |
||
| 248 | call_user_func_array( array( $this, 'save' ), $param ); |
||
| 249 | } |
||
| 250 | } |
||
| 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 ) { |
||
| 258 | foreach ( $this->fields as $field ) { |
||
| 259 | $field->set_value_from_input( stripslashes_deep( $_POST ) ); |
||
|
1 ignored issue
–
show
|
|||
| 260 | $field->save(); |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Checks whether the current save request is valid |
||
| 266 | * |
||
| 267 | * @return bool |
||
| 268 | **/ |
||
| 269 | final protected function _is_valid_save() { |
||
| 270 | $param = func_get_args(); |
||
| 271 | $is_valid_save = call_user_func_array( array( $this, 'is_valid_save' ), $param ); |
||
| 272 | return apply_filters( 'carbon_fields_container_is_valid_save', $is_valid_save, $this ); |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Checks whether the current save request is valid |
||
| 277 | * |
||
| 278 | * @return bool |
||
| 279 | **/ |
||
| 280 | abstract protected function is_valid_save(); |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Called first as part of the container attachment procedure. |
||
| 284 | * Responsible for checking it's OK to attach the container |
||
| 285 | * and if it is, calling the container-specific attach() method |
||
| 286 | * |
||
| 287 | * @see attach() |
||
| 288 | * @see is_valid_attach() |
||
| 289 | */ |
||
| 290 | public function _attach() { |
||
| 291 | $param = func_get_args(); |
||
| 292 | if ( $this->is_valid_attach() ) { |
||
| 293 | call_user_func_array( array( $this, 'attach' ), $param ); |
||
| 294 | |||
| 295 | // Allow containers to activate but not load (useful in cases such as theme options) |
||
| 296 | if ( $this->should_activate() ) { |
||
| 297 | $this->activate(); |
||
| 298 | } |
||
| 299 | } |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Attach the container rendering and helping methods |
||
| 304 | * to concrete WordPress Action hooks |
||
| 305 | */ |
||
| 306 | public function attach() {} |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Perform checks whether the container should be attached during the current request |
||
| 310 | * |
||
| 311 | * @return bool True if the container is allowed to be attached |
||
| 312 | */ |
||
| 313 | final public function is_valid_attach() { |
||
| 314 | $is_valid_attach = $this->is_valid_attach_for_request(); |
||
| 315 | return apply_filters( 'carbon_fields_container_is_valid_attach', $is_valid_attach, $this ); |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get environment array for page request (in admin) |
||
| 320 | * |
||
| 321 | * @return array |
||
| 322 | */ |
||
| 323 | abstract protected function get_environment_for_request(); |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Check container attachment rules against current page request (in admin) |
||
| 327 | * |
||
| 328 | * @return bool |
||
| 329 | */ |
||
| 330 | abstract protected function is_valid_attach_for_request(); |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Check if conditions pass for request |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | protected function static_conditions_pass() { |
||
| 338 | $environment = $this->get_environment_for_request(); |
||
| 339 | $static_condition_collection = $this->condition_collection->evaluate( |
||
| 340 | $this->get_condition_types( false ), |
||
| 341 | true |
||
| 342 | ); |
||
| 343 | return $static_condition_collection->is_fulfilled( $environment ); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Get environment array for object id |
||
| 348 | * |
||
| 349 | * @param integer $object_id |
||
| 350 | * @return array |
||
| 351 | */ |
||
| 352 | abstract protected function get_environment_for_object( $object_id ); |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Check container attachment rules against object id |
||
| 356 | * |
||
| 357 | * @param int $object_id |
||
| 358 | * @return bool |
||
| 359 | */ |
||
| 360 | abstract public function is_valid_attach_for_object( $object_id ); |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Check if all conditions pass for object |
||
| 364 | * |
||
| 365 | * @return bool |
||
| 366 | */ |
||
| 367 | protected function all_conditions_pass( $object_id ) { |
||
| 368 | $environment = $this->get_environment_for_object( $object_id ); |
||
| 369 | return $this->condition_collection->is_fulfilled( $environment ); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Whether this container is currently viewed. |
||
| 374 | */ |
||
| 375 | public function should_activate() { |
||
| 376 | return $this->is_valid_attach(); |
||
| 377 | } |
||
| 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 | * Get a regex to match field name patterns used to fetch specific fields |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | protected function get_field_pattern_regex() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return field from container with specified name |
||
| 439 | * |
||
| 440 | * @example crb_complex/text_field |
||
| 441 | * @example crb_complex/complex_2 |
||
| 442 | * @example crb_complex/complex_2:text_group/text_field |
||
| 443 | * |
||
| 444 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
| 445 | * @return Field |
||
| 446 | */ |
||
| 447 | public function get_field_by_name( $field_name ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Perform checks whether there is a field registered with the name $name. |
||
| 494 | * If not, the field name is recorded. |
||
| 495 | * |
||
| 496 | * @param string $name |
||
| 497 | */ |
||
| 498 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 505 | |||
| 506 | /** |
||
| 507 | * Remove field name $name from the list of unique field names |
||
| 508 | * |
||
| 509 | * @param string $name |
||
| 510 | */ |
||
| 511 | public function drop_unique_field_name( $name ) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Return whether the datastore instance is the default one or has been overriden |
||
| 521 | * |
||
| 522 | * @return boolean |
||
| 523 | */ |
||
| 524 | public function has_default_datastore() { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Set datastore instance |
||
| 530 | * |
||
| 531 | * @param Datastore_Interface $datastore |
||
| 532 | * @return object $this |
||
| 533 | */ |
||
| 534 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 546 | |||
| 547 | /** |
||
| 548 | 6 | * Get the DataStore instance |
|
| 549 | 6 | * |
|
| 550 | 1 | * @return Datastore_Interface $datastore |
|
| 551 | */ |
||
| 552 | 6 | public function get_datastore() { |
|
| 555 | 6 | ||
| 556 | /** |
||
| 557 | 6 | * Return WordPress nonce name used to identify the current container instance |
|
| 558 | 6 | * |
|
| 559 | * @return string |
||
| 560 | */ |
||
| 561 | public function get_nonce_name() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | 6 | * Return WordPress nonce field |
|
| 567 | 6 | * |
|
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function get_nonce_field() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Check if the nonce is present in the request and that it is verified |
||
| 576 | * |
||
| 577 | * @return bool |
||
| 578 | */ |
||
| 579 | protected function verified_nonce_in_request() { |
||
| 584 | |||
| 585 | /** |
||
| 586 | * Internal function that creates the tab and associates it with particular field set |
||
| 587 | * |
||
| 588 | * @param string $tab_name |
||
| 589 | * @param array $fields |
||
| 590 | * @param int $queue_end |
||
| 591 | * @return object $this |
||
| 592 | */ |
||
| 593 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 614 | |||
| 615 | /** |
||
| 616 | * Whether the container is tabbed or not |
||
| 617 | * |
||
| 618 | * @return bool |
||
| 619 | */ |
||
| 620 | public function is_tabbed() { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Retrieve all fields that are not defined under a specific tab |
||
| 626 | * |
||
| 627 | * @return array |
||
| 628 | */ |
||
| 629 | protected function get_untabbed_fields() { |
||
| 651 | |||
| 652 | /** |
||
| 653 | * Retrieve all tabs. |
||
| 654 | * Create a default tab if there are any untabbed fields. |
||
| 655 | * |
||
| 656 | * @return array |
||
| 657 | */ |
||
| 658 | protected function get_tabs() { |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Build the tabs JSON |
||
| 670 | * |
||
| 671 | * @return array |
||
| 672 | */ |
||
| 673 | protected function get_tabs_json() { |
||
| 685 | |||
| 686 | /** |
||
| 687 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 688 | * |
||
| 689 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 690 | * @return array |
||
| 691 | */ |
||
| 692 | public function to_json( $load ) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Enqueue admin styles |
||
| 717 | */ |
||
| 718 | public static function admin_hook_styles() { |
||
| 721 | |||
| 722 | /** |
||
| 723 | * COMMON USAGE METHODS |
||
| 724 | */ |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Append array of fields to the current fields set. All items of the array |
||
| 728 | * must be instances of Field and their names should be unique for all |
||
| 729 | * Carbon containers. |
||
| 730 | * If a field does not have DataStore already, the container datastore is |
||
| 731 | * assigned to them instead. |
||
| 732 | * |
||
| 733 | * @param array $fields |
||
| 734 | * @return object $this |
||
| 735 | */ |
||
| 736 | public function add_fields( $fields ) { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * Configuration function for adding tab with fields |
||
| 757 | * |
||
| 758 | * @param string $tab_name |
||
| 759 | * @param array $fields |
||
| 760 | * @return object $this |
||
| 761 | */ |
||
| 762 | public function add_tab( $tab_name, $fields ) { |
||
| 767 | |||
| 768 | /** |
||
| 769 | * Proxy function to set attachment conditions |
||
| 770 | * |
||
| 771 | * @see Fulfillable_Collection::when() |
||
| 772 | * @return Container $this |
||
| 773 | */ |
||
| 774 | public function when() { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * Proxy function to set attachment conditions |
||
| 781 | * |
||
| 782 | * @see Fulfillable_Collection::and_when() |
||
| 783 | * @return Container $this |
||
| 784 | */ |
||
| 785 | public function and_when() { |
||
| 789 | |||
| 790 | /** |
||
| 791 | * Proxy function to set attachment conditions |
||
| 792 | * |
||
| 793 | * @see Fulfillable_Collection::or_when() |
||
| 794 | * @return Container $this |
||
| 795 | */ |
||
| 796 | public function or_when() { |
||
| 800 | } |
||
| 801 |