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 | * Key which defines what a group's type/name is |
||
| 29 | */ |
||
| 30 | const GROUP_TYPE_KEY = '_type'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Default field value |
||
| 34 | * |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | protected $default_value = array(); |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Complex field layout |
||
| 41 | * |
||
| 42 | * @var string static::LAYOUT_* constant |
||
| 43 | */ |
||
| 44 | protected $layout = self::LAYOUT_GRID; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Value tree describing the complex values ( ['value_set'] ) and all groups with their child fields ( ['groups'] ) |
||
| 48 | * |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $value_tree = array( |
||
| 52 | 'value_set' => array(), |
||
| 53 | 'groups' => array(), |
||
| 54 | ); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Array of groups registered for this complex field |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | */ |
||
| 61 | protected $groups = array(); |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Minimum number of entries. -1 for no limit |
||
| 65 | * |
||
| 66 | * @var integer |
||
| 67 | */ |
||
| 68 | protected $values_min = -1; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * Maximum number of entries. -1 for no limit |
||
| 72 | * |
||
| 73 | * @var integer |
||
| 74 | */ |
||
| 75 | protected $values_max = -1; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Default entry state - collapsed or not |
||
| 79 | * |
||
| 80 | * @var boolean |
||
| 81 | */ |
||
| 82 | protected $collapsed = false; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Entry labels |
||
| 86 | * These are translated in init() |
||
| 87 | * |
||
| 88 | * @var array |
||
| 89 | */ |
||
| 90 | public $labels = array( |
||
| 91 | 'singular_name' => 'Entry', |
||
| 92 | 'plural_name' => 'Entries', |
||
| 93 | ); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Create a field from a certain type with the specified label. |
||
| 97 | * |
||
| 98 | * @param string $type Field type |
||
| 99 | * @param string $name Field name |
||
| 100 | * @param string $label Field label |
||
| 101 | */ |
||
| 102 | protected function __construct( $type, $name, $label ) { |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Initialization tasks. |
||
| 109 | */ |
||
| 110 | public function init() { |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Set array of hierarchy field names |
||
| 125 | */ |
||
| 126 | public function set_hierarchy( $hierarchy ) { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Propagate hierarchy to child fields |
||
| 133 | */ |
||
| 134 | public function update_child_hierarchy() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Activate the field once the container is attached. |
||
| 144 | */ |
||
| 145 | public function activate() { |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Set the datastore of this field and propagate it to children |
||
| 155 | * |
||
| 156 | * @param Datastore_Interface $datastore |
||
| 157 | */ |
||
| 158 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Retrieve all groups of fields. |
||
| 173 | * |
||
| 174 | * @return array $fields |
||
| 175 | */ |
||
| 176 | public function get_fields() { |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Add a set/group of fields. |
||
| 190 | * |
||
| 191 | * Accepted param variations: |
||
| 192 | * - array<Field> $fields |
||
| 193 | * - string $group_name, array<Field> $fields |
||
| 194 | * - string $group_name, string $group_label, array<Field> $fields |
||
| 195 | * |
||
| 196 | * @return $this |
||
| 197 | */ |
||
| 198 | public function add_fields() { |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Retrieve the groups of this field. |
||
| 235 | * |
||
| 236 | * @return array |
||
| 237 | */ |
||
| 238 | public function get_group_names() { |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Retrieve a group by its name. |
||
| 244 | * |
||
| 245 | * @param string $group_name Group name |
||
| 246 | * @return Group_Field $group_object Group object |
||
| 247 | */ |
||
| 248 | public function get_group_by_name( $group_name ) { |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Set the group label Underscore template. |
||
| 262 | * |
||
| 263 | * @param string|callable $template |
||
| 264 | * @return $this |
||
| 265 | */ |
||
| 266 | public function set_header_template( $template ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set the field labels. |
||
| 288 | * Currently supported values: |
||
| 289 | * - singular_name - the singular entry label |
||
| 290 | * - plural_name - the plural entries label |
||
| 291 | * |
||
| 292 | * @param array $labels Labels |
||
| 293 | */ |
||
| 294 | public function setup_labels( $labels ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return a clone of a field with hierarchy settings applied |
||
| 301 | * |
||
| 302 | * @param Field $field |
||
| 303 | * @param Field $parent_field |
||
| 304 | * @param int $group_index |
||
| 305 | * @return Field |
||
| 306 | */ |
||
| 307 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $group_index = 0 ) { |
||
| 313 | |||
| 314 | protected function get_prefilled_field_groups( $value_tree ) { |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Load the field value from an input array based on it's name. |
||
| 355 | * |
||
| 356 | * @param array $input Array of field names and values. |
||
| 357 | */ |
||
| 358 | public function set_value_from_input( $input ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Load all groups of fields and their data. |
||
| 410 | */ |
||
| 411 | public function load() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Save all contained groups of fields. |
||
| 426 | */ |
||
| 427 | public function save() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Return the full value tree of all groups and their fields |
||
| 450 | * |
||
| 451 | * @return mixed |
||
| 452 | */ |
||
| 453 | public function get_value_tree() { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Set the full value tree of all groups and their fields |
||
| 459 | * |
||
| 460 | * @see Internal Glossary in DEVELOPMENT.MD |
||
| 461 | */ |
||
| 462 | public function set_value_tree( $value_tree ) { |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Return a differently formatted value for end-users |
||
| 468 | * |
||
| 469 | * @return mixed |
||
| 470 | */ |
||
| 471 | public function get_formatted_value() { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 490 | * This data will be available in the Underscore template and the Backbone Model. |
||
| 491 | * |
||
| 492 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 493 | * @return array |
||
| 494 | */ |
||
| 495 | public function to_json( $load ) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * The main Underscore template. |
||
| 540 | */ |
||
| 541 | public function template() { |
||
| 593 | |||
| 594 | /** |
||
| 595 | * The Underscore template for the complex field group. |
||
| 596 | */ |
||
| 597 | public function template_group() { |
||
| 646 | |||
| 647 | /** |
||
| 648 | * The Underscore template for the group item tab. |
||
| 649 | */ |
||
| 650 | public function template_group_tab_item() { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Modify the layout of this field. |
||
| 668 | * |
||
| 669 | * @param string $layout |
||
| 670 | */ |
||
| 671 | public function set_layout( $layout ) { |
||
| 689 | |||
| 690 | /** |
||
| 691 | * Get the minimum number of entries. |
||
| 692 | * |
||
| 693 | * @return int $min |
||
| 694 | */ |
||
| 695 | public function get_min() { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Set the minimum number of entries. |
||
| 701 | * |
||
| 702 | * @param int $min |
||
| 703 | */ |
||
| 704 | public function set_min( $min ) { |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Get the maximum number of entries. |
||
| 711 | * |
||
| 712 | * @return int $max |
||
| 713 | */ |
||
| 714 | public function get_max() { |
||
| 717 | |||
| 718 | /** |
||
| 719 | * Set the maximum number of entries. |
||
| 720 | * |
||
| 721 | * @param int $max |
||
| 722 | */ |
||
| 723 | public function set_max( $max ) { |
||
| 727 | |||
| 728 | /** |
||
| 729 | * Get collapsed state |
||
| 730 | * |
||
| 731 | * @return bool |
||
| 732 | */ |
||
| 733 | public function get_collapsed() { |
||
| 736 | |||
| 737 | /** |
||
| 738 | * Change the groups initial collapse state. |
||
| 739 | * This state relates to the state of which the groups are rendered. |
||
| 740 | * |
||
| 741 | * @param bool $collapsed |
||
| 742 | */ |
||
| 743 | public function set_collapsed( $collapsed = true ) { |
||
| 747 | } |
||
| 748 |