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 Complex_Field 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 Complex_Field, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Complex_Field extends Field { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Visual layout type constants |
||
| 20 | */ |
||
| 21 | const LAYOUT_GRID = 'grid'; // default |
||
| 22 | |||
| 23 | const LAYOUT_TABBED_HORIZONTAL = 'tabbed-horizontal'; |
||
| 24 | |||
| 25 | const LAYOUT_TABBED_VERTICAL = 'tabbed-vertical'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * Default field value |
||
| 29 | * |
||
| 30 | * @var array |
||
| 31 | */ |
||
| 32 | protected $default_value = array(); |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Complex field layout |
||
| 36 | * |
||
| 37 | * @var string static::LAYOUT_* constant |
||
| 38 | */ |
||
| 39 | protected $layout = self::LAYOUT_GRID; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Value tree describing the complex values and all groups with their child fields |
||
| 43 | * |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $value_tree = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Array of groups registered for this complex field |
||
| 50 | * |
||
| 51 | * @var array |
||
| 52 | */ |
||
| 53 | protected $groups = array(); |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Minimum number of entries. -1 for no limit |
||
| 57 | * |
||
| 58 | * @var integer |
||
| 59 | */ |
||
| 60 | protected $values_min = -1; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Maximum number of entries. -1 for no limit |
||
| 64 | * |
||
| 65 | * @var integer |
||
| 66 | */ |
||
| 67 | protected $values_max = -1; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Default entry state - collapsed or not |
||
| 71 | * |
||
| 72 | * @var boolean |
||
| 73 | */ |
||
| 74 | protected $collapsed = false; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Entry labels |
||
| 78 | * These are translated in init() |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | public $labels = array( |
||
| 83 | 'singular_name' => 'Entry', |
||
| 84 | 'plural_name' => 'Entries', |
||
| 85 | ); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Create a field from a certain type with the specified label. |
||
| 89 | * |
||
| 90 | * @param string $type Field type |
||
| 91 | * @param string $name Field name |
||
| 92 | * @param string $label Field label |
||
| 93 | */ |
||
| 94 | public function __construct( $type, $name, $label ) { |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialization tasks. |
||
| 101 | */ |
||
| 102 | public function init() { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set array of hierarchy field names |
||
| 112 | */ |
||
| 113 | public function set_hierarchy( $hierarchy ) { |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Propagate hierarchy to child fields |
||
| 120 | */ |
||
| 121 | public function update_child_hierarchy() { |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Activate the field once the container is attached. |
||
| 131 | */ |
||
| 132 | public function activate() { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the datastore of this field and propagate it to children |
||
| 142 | * |
||
| 143 | * @param Datastore_Interface $datastore |
||
| 144 | */ |
||
| 145 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 157 | |||
| 158 | /** |
||
| 159 | * Retrieve all groups of fields. |
||
| 160 | * |
||
| 161 | * @return array $fields |
||
| 162 | */ |
||
| 163 | public function get_fields() { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Add a set/group of fields. |
||
| 177 | * |
||
| 178 | * Accepted param variations: |
||
| 179 | * - array<Field> $fields |
||
| 180 | * - string $group_name, array<Field> $fields |
||
| 181 | * - string $group_name, string $group_label, array<Field> $fields |
||
| 182 | * |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function add_fields() { |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Retrieve the groups of this field. |
||
| 224 | * |
||
| 225 | * @return array |
||
| 226 | */ |
||
| 227 | public function get_group_names() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Retrieve a group by its name. |
||
| 233 | * |
||
| 234 | * @param string $group_name Group name |
||
| 235 | * @return Group_Field $group_object Group object |
||
| 236 | */ |
||
| 237 | public function get_group_by_name( $group_name ) { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Set the group label Underscore template. |
||
| 251 | * |
||
| 252 | * @param string|callable $template |
||
| 253 | * @return $this |
||
| 254 | */ |
||
| 255 | public function set_header_template( $template ) { |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Set the field labels. |
||
| 273 | * Currently supported values: |
||
| 274 | * - singular_name - the singular entry label |
||
| 275 | * - plural_name - the plural entries label |
||
| 276 | * |
||
| 277 | * @param array $labels Labels |
||
| 278 | */ |
||
| 279 | public function setup_labels( $labels ) { |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return a clone of a field with hierarchy settings applied |
||
| 286 | * |
||
| 287 | * @param Field $field |
||
| 288 | * @param Field $parent_field |
||
| 289 | * @param int $group_index |
||
| 290 | * @return Field |
||
| 291 | */ |
||
| 292 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $group_index = 0 ) { |
||
| 298 | |||
| 299 | protected function get_prefilled_group_fields( $group_fields, $group_values, $group_index ) { |
||
| 312 | |||
| 313 | protected function get_prefilled_groups( $value_tree ) { |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Load the field value from an input array based on it's name. |
||
| 332 | * |
||
| 333 | * @param array $input Array of field names and values. |
||
| 334 | */ |
||
| 335 | public function set_value_from_input( $input ) { |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Save all contained groups of fields. |
||
| 377 | */ |
||
| 378 | public function save() { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritDoc} |
||
| 404 | */ |
||
| 405 | public function set_value( $value ) { |
||
| 409 | |||
| 410 | /** |
||
| 411 | * Return the full value tree of all groups and their fields |
||
| 412 | * |
||
| 413 | * @return mixed |
||
| 414 | */ |
||
| 415 | protected function get_value_tree() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set the full value tree of all groups and their fields |
||
| 421 | * |
||
| 422 | * @see Internal Glossary in DEVELOPMENT.MD |
||
| 423 | */ |
||
| 424 | protected function set_value_tree( $value_tree ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return a differently formatted value for end-users |
||
| 430 | * |
||
| 431 | * @return mixed |
||
| 432 | */ |
||
| 433 | public function get_formatted_value() { |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 456 | * |
||
| 457 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | public function to_json( $load ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Modify the layout of this field. |
||
| 510 | * |
||
| 511 | * @param string $layout |
||
| 512 | */ |
||
| 513 | public function set_layout( $layout ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Get the minimum number of entries. |
||
| 535 | * |
||
| 536 | * @return int $min |
||
| 537 | */ |
||
| 538 | public function get_min() { |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Set the minimum number of entries. |
||
| 544 | * |
||
| 545 | * @param int $min |
||
| 546 | */ |
||
| 547 | public function set_min( $min ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Get the maximum number of entries. |
||
| 554 | * |
||
| 555 | * @return int $max |
||
| 556 | */ |
||
| 557 | public function get_max() { |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Set the maximum number of entries. |
||
| 563 | * |
||
| 564 | * @param int $max |
||
| 565 | */ |
||
| 566 | public function set_max( $max ) { |
||
| 570 | |||
| 571 | /** |
||
| 572 | * Get collapsed state |
||
| 573 | * |
||
| 574 | * @return bool |
||
| 575 | */ |
||
| 576 | public function get_collapsed() { |
||
| 579 | |||
| 580 | /** |
||
| 581 | * Change the groups initial collapse state. |
||
| 582 | * This state relates to the state of which the groups are rendered. |
||
| 583 | * |
||
| 584 | * @param bool $collapsed |
||
| 585 | */ |
||
| 586 | public function set_collapsed( $collapsed = true ) { |
||
| 590 | } |
||
| 591 |