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_group_fields( $group_fields, $group_values, $group_index ) { | ||
| 335 | |||
| 336 | 	protected function get_prefilled_groups( $value_tree ) { | ||
| 356 | |||
| 357 | /** | ||
| 358 | * Load the field value from an input array based on it's name. | ||
| 359 | * | ||
| 360 | * @param array $input Array of field names and values. | ||
| 361 | */ | ||
| 362 | 	public function set_value_from_input( $input ) { | ||
| 411 | |||
| 412 | /** | ||
| 413 | * Load all groups of fields and their data. | ||
| 414 | */ | ||
| 415 | 	public function load() { | ||
| 427 | |||
| 428 | /** | ||
| 429 | * Save all contained groups of fields. | ||
| 430 | */ | ||
| 431 | 	public function save() { | ||
| 451 | |||
| 452 | /** | ||
| 453 | * Return the full value tree of all groups and their fields | ||
| 454 | * | ||
| 455 | * @return mixed | ||
| 456 | */ | ||
| 457 | 	public function get_value_tree() { | ||
| 460 | |||
| 461 | /** | ||
| 462 | * Set the full value tree of all groups and their fields | ||
| 463 | * | ||
| 464 | * @see Internal Glossary in DEVELOPMENT.MD | ||
| 465 | */ | ||
| 466 | 	public function set_value_tree( $value_tree ) { | ||
| 469 | |||
| 470 | /** | ||
| 471 | * Return a differently formatted value for end-users | ||
| 472 | * | ||
| 473 | * @return mixed | ||
| 474 | */ | ||
| 475 | 	public function get_formatted_value() { | ||
| 491 | |||
| 492 | /** | ||
| 493 | * Returns an array that holds the field data, suitable for JSON representation. | ||
| 494 | * This data will be available in the Underscore template and the Backbone Model. | ||
| 495 | * | ||
| 496 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. | ||
| 497 | * @return array | ||
| 498 | */ | ||
| 499 | 	public function to_json( $load ) { | ||
| 541 | |||
| 542 | /** | ||
| 543 | * The main Underscore template. | ||
| 544 | */ | ||
| 545 | 	public function template() { | ||
| 597 | |||
| 598 | /** | ||
| 599 | * The Underscore template for the complex field group. | ||
| 600 | */ | ||
| 601 | 	public function template_group() { | ||
| 650 | |||
| 651 | /** | ||
| 652 | * The Underscore template for the group item tab. | ||
| 653 | */ | ||
| 654 | 	public function template_group_tab_item() { | ||
| 669 | |||
| 670 | /** | ||
| 671 | * Modify the layout of this field. | ||
| 672 | * | ||
| 673 | * @param string $layout | ||
| 674 | */ | ||
| 675 | 	public function set_layout( $layout ) { | ||
| 693 | |||
| 694 | /** | ||
| 695 | * Get the minimum number of entries. | ||
| 696 | * | ||
| 697 | * @return int $min | ||
| 698 | */ | ||
| 699 | 	public function get_min() { | ||
| 702 | |||
| 703 | /** | ||
| 704 | * Set the minimum number of entries. | ||
| 705 | * | ||
| 706 | * @param int $min | ||
| 707 | */ | ||
| 708 | 	public function set_min( $min ) { | ||
| 712 | |||
| 713 | /** | ||
| 714 | * Get the maximum number of entries. | ||
| 715 | * | ||
| 716 | * @return int $max | ||
| 717 | */ | ||
| 718 | 	public function get_max() { | ||
| 721 | |||
| 722 | /** | ||
| 723 | * Set the maximum number of entries. | ||
| 724 | * | ||
| 725 | * @param int $max | ||
| 726 | */ | ||
| 727 | 	public function set_max( $max ) { | ||
| 731 | |||
| 732 | /** | ||
| 733 | * Get collapsed state | ||
| 734 | * | ||
| 735 | * @return bool | ||
| 736 | */ | ||
| 737 | 	public function get_collapsed() { | ||
| 740 | |||
| 741 | /** | ||
| 742 | * Change the groups initial collapse state. | ||
| 743 | * This state relates to the state of which the groups are rendered. | ||
| 744 | * | ||
| 745 | * @param bool $collapsed | ||
| 746 | */ | ||
| 747 | 	public function set_collapsed( $collapsed = true ) { | ||
| 751 | } | ||
| 752 |