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 | * Array of custom CSS classes. |
||
| 97 | * |
||
| 98 | * @see add_class() |
||
| 99 | * @see get_classes() |
||
| 100 | * @var array<string> |
||
| 101 | */ |
||
| 102 | protected $classes = array(); |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Container datastores. Propagated to all container fields |
||
| 106 | * |
||
| 107 | * @see set_datastore() |
||
| 108 | * @see get_datastore() |
||
| 109 | * @var object |
||
| 110 | */ |
||
| 111 | protected $datastore; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Flag whether the datastore is the default one or replaced with a custom one |
||
| 115 | * |
||
| 116 | * @see set_datastore() |
||
| 117 | * @see get_datastore() |
||
| 118 | * @var boolean |
||
| 119 | */ |
||
| 120 | protected $has_default_datastore = true; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Fulfillable_Collection to use when checking attachment/saving conditions |
||
| 124 | * |
||
| 125 | * @var Fulfillable_Collection |
||
| 126 | */ |
||
| 127 | protected $condition_collection; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Get array of all static condition types |
||
| 131 | * |
||
| 132 | * @param boolean $static |
||
| 133 | * @return array<string> |
||
| 134 | */ |
||
| 135 | protected function get_condition_types( $static ) { |
||
| 136 | $group = $static ? 'static' : 'dynamic'; |
||
| 137 | $container_type = Helper::class_to_type( get_class( $this ), '_Container' ); |
||
| 138 | |||
| 139 | $condition_types = array(); |
||
| 140 | $condition_types = apply_filters( 'carbon_fields_' . $container_type . '_container_' . $group . '_condition_types', $condition_types, $container_type, $this ); |
||
| 141 | $condition_types = apply_filters( 'carbon_fields_container_' . $group . '_condition_types', $condition_types, $container_type, $this ); |
||
| 142 | |||
| 143 | return $condition_types; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Create a new container of type $type and name $name. |
||
| 148 | * |
||
| 149 | * @param string $type |
||
| 150 | * @param string $name Human-readable name of the container |
||
| 151 | * @return object $container |
||
| 152 | */ |
||
| 153 | public static function factory( $type, $name ) { |
||
| 154 | $normalized_type = Helper::normalize_type( $type ); |
||
| 155 | $class = Helper::type_to_class( $normalized_type, __NAMESPACE__, '_Container' ); |
||
| 156 | |||
| 157 | View Code Duplication | if ( ! class_exists( $class ) ) { |
|
| 158 | Incorrect_Syntax_Exception::raise( 'Unknown container "' . $type . '".' ); |
||
| 159 | $class = __NAMESPACE__ . '\\Broken_Container'; |
||
| 160 | } |
||
| 161 | |||
| 162 | $repository = App::resolve( 'container_repository' ); |
||
| 163 | $unique_id = $repository->get_unique_panel_id( $name ); |
||
| 164 | $container = new $class( $unique_id, $name, $normalized_type ); |
||
| 165 | $repository->register_container( $container ); |
||
| 166 | |||
| 167 | return $container; |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * An alias of factory(). |
||
| 172 | * |
||
| 173 | * @see Container::factory() |
||
| 174 | */ |
||
| 175 | public static function make( $type, $name ) { |
||
| 176 | return static::factory( $type, $name ); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Create a new container |
||
| 181 | * |
||
| 182 | * @param string $unique_id Unique id of the container |
||
| 183 | * @param string $title title of the container |
||
| 184 | * @param string $type Type of the container |
||
| 185 | */ |
||
| 186 | public function __construct( $unique_id, $title, $type ) { |
||
| 187 | App::verify_boot(); |
||
| 188 | |||
| 189 | if ( empty( $title ) ) { |
||
| 190 | Incorrect_Syntax_Exception::raise( 'Empty container title is not supported' ); |
||
| 191 | } |
||
| 192 | |||
| 193 | $this->id = $unique_id; |
||
| 194 | $this->title = $title; |
||
| 195 | $this->type = $type; |
||
| 196 | 9 | $this->condition_collection = App::resolve( 'container_condition_fulfillable_collection' ); |
|
| 197 | 9 | $this->condition_collection->set_condition_type_list( |
|
| 198 | 9 | array_merge( $this->get_condition_types( true ), $this->get_condition_types( false ) ), |
|
| 199 | true |
||
| 200 | 9 | ); |
|
| 201 | 9 | } |
|
| 202 | 7 | ||
| 203 | 7 | /** |
|
| 204 | * Return whether the container is active |
||
| 205 | 7 | */ |
|
| 206 | public function active() { |
||
| 207 | return $this->active; |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Activate the container and trigger an action |
||
| 212 | */ |
||
| 213 | protected function activate() { |
||
| 214 | $this->active = true; |
||
| 215 | $this->boot(); |
||
| 216 | do_action( 'crb_container_activated', $this ); |
||
| 217 | |||
| 218 | $fields = $this->get_fields(); |
||
| 219 | foreach ( $fields as $field ) { |
||
| 220 | $field->activate(); |
||
| 221 | } |
||
| 222 | } |
||
| 223 | |||
| 224 | 2 | /** |
|
| 225 | 2 | * Perform instance initialization |
|
| 226 | */ |
||
| 227 | 2 | abstract public function init(); |
|
| 228 | 1 | ||
| 229 | /** |
||
| 230 | * Boot the container once it's attached. |
||
| 231 | 1 | */ |
|
| 232 | 1 | protected function boot() { |
|
| 233 | 1 | add_action( 'admin_footer', array( get_class(), 'admin_hook_styles' ), 5 ); |
|
| 234 | 1 | } |
|
| 235 | 1 | ||
| 236 | 1 | /** |
|
| 237 | * Load the value for each field in the container. |
||
| 238 | 1 | * Could be used internally during container rendering |
|
| 239 | 1 | */ |
|
| 240 | public function load() { |
||
| 241 | foreach ( $this->fields as $field ) { |
||
| 242 | $field->load(); |
||
| 243 | } |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Called first as part of the container save procedure. |
||
| 248 | * Responsible for checking the request validity and |
||
| 249 | * calling the container-specific save() method |
||
| 250 | * |
||
| 251 | * @see save() |
||
| 252 | * @see is_valid_save() |
||
| 253 | */ |
||
| 254 | public function _save() { |
||
| 255 | $param = func_get_args(); |
||
| 256 | if ( call_user_func_array( array( $this, '_is_valid_save' ), $param ) ) { |
||
| 257 | call_user_func_array( array( $this, 'save' ), $param ); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Load submitted data and save each field in the container |
||
| 263 | * |
||
| 264 | * @see is_valid_save() |
||
| 265 | */ |
||
| 266 | public function save( $data = null ) { |
||
| 267 | foreach ( $this->fields as $field ) { |
||
| 268 | $field->set_value_from_input( stripslashes_deep( $_POST ) ); |
||
|
1 ignored issue
–
show
|
|||
| 269 | $field->save(); |
||
| 270 | } |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Checks whether the current save request is valid |
||
| 275 | * |
||
| 276 | * @return bool |
||
| 277 | **/ |
||
| 278 | final protected function _is_valid_save() { |
||
| 279 | $param = func_get_args(); |
||
| 280 | $is_valid_save = call_user_func_array( array( $this, 'is_valid_save' ), $param ); |
||
| 281 | return apply_filters( 'carbon_fields_container_is_valid_save', $is_valid_save, $this ); |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Checks whether the current save request is valid |
||
| 286 | * |
||
| 287 | * @return bool |
||
| 288 | **/ |
||
| 289 | abstract protected function is_valid_save(); |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Called first as part of the container attachment procedure. |
||
| 293 | * Responsible for checking it's OK to attach the container |
||
| 294 | * and if it is, calling the container-specific attach() method |
||
| 295 | * |
||
| 296 | * @see attach() |
||
| 297 | * @see is_valid_attach() |
||
| 298 | */ |
||
| 299 | public function _attach() { |
||
| 300 | $param = func_get_args(); |
||
| 301 | if ( $this->is_valid_attach() ) { |
||
| 302 | call_user_func_array( array( $this, 'attach' ), $param ); |
||
| 303 | |||
| 304 | // Allow containers to activate but not load (useful in cases such as theme options) |
||
| 305 | if ( $this->should_activate() ) { |
||
| 306 | $this->activate(); |
||
| 307 | } |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Attach the container rendering and helping methods |
||
| 313 | * to concrete WordPress Action hooks |
||
| 314 | */ |
||
| 315 | public function attach() {} |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Perform checks whether the container should be attached during the current request |
||
| 319 | * |
||
| 320 | * @return bool True if the container is allowed to be attached |
||
| 321 | */ |
||
| 322 | final public function is_valid_attach() { |
||
| 323 | $is_valid_attach = $this->is_valid_attach_for_request(); |
||
| 324 | return apply_filters( 'carbon_fields_container_is_valid_attach', $is_valid_attach, $this ); |
||
| 325 | } |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Get environment array for page request (in admin) |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | abstract protected function get_environment_for_request(); |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Check container attachment rules against current page request (in admin) |
||
| 336 | * |
||
| 337 | * @return bool |
||
| 338 | */ |
||
| 339 | abstract protected function is_valid_attach_for_request(); |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if conditions pass for request |
||
| 343 | * |
||
| 344 | * @return bool |
||
| 345 | */ |
||
| 346 | protected function static_conditions_pass() { |
||
| 347 | $environment = $this->get_environment_for_request(); |
||
| 348 | $static_condition_collection = $this->condition_collection->evaluate( |
||
| 349 | $this->get_condition_types( false ), |
||
| 350 | true |
||
| 351 | ); |
||
| 352 | return $static_condition_collection->is_fulfilled( $environment ); |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Get environment array for object id |
||
| 357 | * |
||
| 358 | * @param integer $object_id |
||
| 359 | * @return array |
||
| 360 | */ |
||
| 361 | abstract protected function get_environment_for_object( $object_id ); |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Check container attachment rules against object id |
||
| 365 | * |
||
| 366 | * @param int $object_id |
||
| 367 | * @return bool |
||
| 368 | */ |
||
| 369 | abstract public function is_valid_attach_for_object( $object_id ); |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Check if all conditions pass for object |
||
| 373 | * |
||
| 374 | * @return bool |
||
| 375 | */ |
||
| 376 | protected function all_conditions_pass( $object_id ) { |
||
| 377 | $environment = $this->get_environment_for_object( $object_id ); |
||
| 378 | return $this->condition_collection->is_fulfilled( $environment ); |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Whether this container is currently viewed. |
||
| 383 | */ |
||
| 384 | public function should_activate() { |
||
| 385 | return $this->is_valid_attach(); |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Perform a check whether the current container has fields |
||
| 390 | * |
||
| 391 | * @return bool |
||
| 392 | */ |
||
| 393 | public function has_fields() { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Returns the private container array of fields. |
||
| 399 | * Use only if you are completely aware of what you are doing. |
||
| 400 | * |
||
| 401 | * @return array |
||
| 402 | */ |
||
| 403 | public function get_fields() { |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Return root field from container with specified name |
||
| 409 | * |
||
| 410 | * @example crb_complex |
||
| 411 | * |
||
| 412 | * @param string $field_name |
||
| 413 | * @return Field |
||
| 414 | */ |
||
| 415 | public function get_root_field_by_name( $field_name ) { |
||
| 416 | $fields = $this->get_fields(); |
||
| 417 | foreach ( $fields as $field ) { |
||
| 418 | if ( $field->get_base_name() === $field_name ) { |
||
| 419 | return $field; |
||
| 420 | } |
||
| 421 | } |
||
| 422 | return null; |
||
| 423 | } |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get a regex to match field name patterns used to fetch specific fields |
||
| 427 | * |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | protected function get_field_pattern_regex() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Return field from container with specified name |
||
| 448 | * |
||
| 449 | * @example crb_complex/text_field |
||
| 450 | * @example crb_complex/complex_2 |
||
| 451 | * @example crb_complex/complex_2:text_group/text_field |
||
| 452 | * |
||
| 453 | * @param string $field_name Can specify a field inside a complex with a / (slash) separator |
||
| 454 | * @return Field |
||
| 455 | */ |
||
| 456 | public function get_field_by_name( $field_name ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Perform checks whether there is a field registered with the name $name. |
||
| 503 | * If not, the field name is recorded. |
||
| 504 | * |
||
| 505 | * @param string $name |
||
| 506 | */ |
||
| 507 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 514 | |||
| 515 | /** |
||
| 516 | * Remove field name $name from the list of unique field names |
||
| 517 | * |
||
| 518 | * @param string $name |
||
| 519 | */ |
||
| 520 | public function drop_unique_field_name( $name ) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Return whether the datastore instance is the default one or has been overriden |
||
| 530 | * |
||
| 531 | * @return boolean |
||
| 532 | */ |
||
| 533 | public function has_default_datastore() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | 6 | * Set datastore instance |
|
| 539 | 6 | * |
|
| 540 | * @param Datastore_Interface $datastore |
||
| 541 | * @return object $this |
||
| 542 | */ |
||
| 543 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 555 | 6 | ||
| 556 | /** |
||
| 557 | 6 | * Get the DataStore instance |
|
| 558 | 6 | * |
|
| 559 | * @return Datastore_Interface $datastore |
||
| 560 | */ |
||
| 561 | public function get_datastore() { |
||
| 564 | |||
| 565 | /** |
||
| 566 | 6 | * Return WordPress nonce name used to identify the current container instance |
|
| 567 | 6 | * |
|
| 568 | * @return string |
||
| 569 | */ |
||
| 570 | public function get_nonce_name() { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Return WordPress nonce name used to identify the current container instance |
||
| 576 | * |
||
| 577 | * @return string |
||
| 578 | */ |
||
| 579 | public function get_nonce_value() { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Check if the nonce is present in the request and that it is verified |
||
| 585 | * |
||
| 586 | * @return bool |
||
| 587 | */ |
||
| 588 | protected function verified_nonce_in_request() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Internal function that creates the tab and associates it with particular field set |
||
| 596 | * |
||
| 597 | * @param string $tab_name |
||
| 598 | * @param array $fields |
||
| 599 | * @param int $queue_end |
||
| 600 | * @return object $this |
||
| 601 | */ |
||
| 602 | private function create_tab( $tab_name, $fields, $queue_end = self::TABS_TAIL ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Whether the container is tabbed or not |
||
| 626 | * |
||
| 627 | * @return bool |
||
| 628 | */ |
||
| 629 | public function is_tabbed() { |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Retrieve all fields that are not defined under a specific tab |
||
| 635 | * |
||
| 636 | * @return array |
||
| 637 | */ |
||
| 638 | protected function get_untabbed_fields() { |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Retrieve all tabs. |
||
| 663 | * Create a default tab if there are any untabbed fields. |
||
| 664 | * |
||
| 665 | * @return array |
||
| 666 | */ |
||
| 667 | protected function get_tabs() { |
||
| 676 | |||
| 677 | /** |
||
| 678 | * Build the tabs JSON |
||
| 679 | * |
||
| 680 | * @return array |
||
| 681 | */ |
||
| 682 | protected function get_tabs_json() { |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Get custom CSS classes. |
||
| 697 | * |
||
| 698 | * @return array<string> |
||
| 699 | */ |
||
| 700 | public function get_classes() { |
||
| 703 | |||
| 704 | /** |
||
| 705 | * Set CSS classes that the container should use. |
||
| 706 | * |
||
| 707 | * @param string|array $classes |
||
| 708 | * @return object $this |
||
| 709 | */ |
||
| 710 | public function set_classes( $classes ) { |
||
| 714 | |||
| 715 | /** |
||
| 716 | * Returns an array that holds the container data, suitable for JSON representation. |
||
| 717 | * |
||
| 718 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 719 | * @return array |
||
| 720 | */ |
||
| 721 | public function to_json( $load ) { |
||
| 748 | |||
| 749 | /** |
||
| 750 | * Enqueue admin styles |
||
| 751 | */ |
||
| 752 | public static function admin_hook_styles() { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * COMMON USAGE METHODS |
||
| 758 | */ |
||
| 759 | |||
| 760 | /** |
||
| 761 | * Append array of fields to the current fields set. All items of the array |
||
| 762 | * must be instances of Field and their names should be unique for all |
||
| 763 | * Carbon containers. |
||
| 764 | * If a field does not have DataStore already, the container datastore is |
||
| 765 | * assigned to them instead. |
||
| 766 | * |
||
| 767 | * @param array $fields |
||
| 768 | * @return object $this |
||
| 769 | */ |
||
| 770 | public function add_fields( $fields ) { |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Configuration function for adding tab with fields |
||
| 791 | * |
||
| 792 | * @param string $tab_name |
||
| 793 | * @param array $fields |
||
| 794 | * @return object $this |
||
| 795 | */ |
||
| 796 | public function add_tab( $tab_name, $fields ) { |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Proxy function to set attachment conditions |
||
| 804 | * |
||
| 805 | * @see Fulfillable_Collection::when() |
||
| 806 | * @return Container $this |
||
| 807 | */ |
||
| 808 | public function when() { |
||
| 812 | |||
| 813 | /** |
||
| 814 | * Proxy function to set attachment conditions |
||
| 815 | * |
||
| 816 | * @see Fulfillable_Collection::and_when() |
||
| 817 | * @return Container $this |
||
| 818 | */ |
||
| 819 | public function and_when() { |
||
| 823 | |||
| 824 | /** |
||
| 825 | * Proxy function to set attachment conditions |
||
| 826 | * |
||
| 827 | * @see Fulfillable_Collection::or_when() |
||
| 828 | * @return Container $this |
||
| 829 | */ |
||
| 830 | public function or_when() { |
||
| 834 | } |
||
| 835 |