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 | const TYPE_PROPERTY = '_type'; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Default field value |
||
| 31 | * |
||
| 32 | * @var array |
||
| 33 | */ |
||
| 34 | protected $default_value = array(); |
||
| 35 | |||
| 36 | /** |
||
| 37 | * Complex field layout |
||
| 38 | * |
||
| 39 | * @var string static::LAYOUT_* constant |
||
| 40 | */ |
||
| 41 | protected $layout = self::LAYOUT_GRID; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Value tree describing the complex values and all groups with their child fields |
||
| 45 | * |
||
| 46 | * @var array |
||
| 47 | */ |
||
| 48 | protected $value_tree = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Array of groups registered for this complex field |
||
| 52 | * |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | protected $groups = array(); |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Minimum number of entries. -1 for no limit |
||
| 59 | * |
||
| 60 | * @var integer |
||
| 61 | */ |
||
| 62 | protected $values_min = -1; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Maximum number of entries. -1 for no limit |
||
| 66 | * |
||
| 67 | * @var integer |
||
| 68 | */ |
||
| 69 | protected $values_max = -1; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Default entry state - collapsed or not |
||
| 73 | * |
||
| 74 | * @var boolean |
||
| 75 | */ |
||
| 76 | protected $collapsed = false; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Defines whether duplicate groups are allowed or not |
||
| 80 | * |
||
| 81 | * @var boolean |
||
| 82 | */ |
||
| 83 | protected $duplicate_groups_allowed = true; |
||
|
1 ignored issue
–
show
|
|||
| 84 | |||
| 85 | /** |
||
| 86 | * Entry labels |
||
| 87 | * These are translated in init() |
||
| 88 | * |
||
| 89 | * @var array |
||
| 90 | */ |
||
| 91 | public $labels = array( |
||
| 92 | 'singular_name' => 'Entry', |
||
| 93 | 'plural_name' => 'Entries', |
||
| 94 | ); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create a field from a certain type with the specified label. |
||
| 98 | * |
||
| 99 | * @param string $type Field type |
||
| 100 | * @param string $name Field name |
||
| 101 | * @param string $label Field label |
||
| 102 | */ |
||
| 103 | public function __construct( $type, $name, $label ) { |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Initialization tasks. |
||
| 110 | */ |
||
| 111 | public function init() { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Set array of hierarchy field names |
||
| 121 | */ |
||
| 122 | public function set_hierarchy( $hierarchy ) { |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Propagate hierarchy to child fields |
||
| 129 | */ |
||
| 130 | public function update_child_hierarchy() { |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Activate the field once the container is attached. |
||
| 140 | */ |
||
| 141 | public function activate() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Set the datastore of this field and propagate it to children |
||
| 151 | * |
||
| 152 | * @param Datastore_Interface $datastore |
||
| 153 | * @param boolean $set_as_default |
||
| 154 | * @return object $this |
||
| 155 | */ |
||
| 156 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Propagate the datastore down the hierarchy |
||
| 169 | * |
||
| 170 | * @param Datastore_Interface $datastore |
||
| 171 | * @param boolean $set_as_default |
||
| 172 | */ |
||
| 173 | protected function update_child_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Retrieve all groups of fields. |
||
| 181 | * |
||
| 182 | * @return array $fields |
||
| 183 | */ |
||
| 184 | public function get_fields() { |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Add a set/group of fields. |
||
| 198 | * |
||
| 199 | * Accepted param variations: |
||
| 200 | * - array<Field> $fields |
||
| 201 | * - string $group_name, array<Field> $fields |
||
| 202 | * - string $group_name, string $group_label, array<Field> $fields |
||
| 203 | * |
||
| 204 | * @return $this |
||
| 205 | */ |
||
| 206 | public function add_fields() { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Retrieve the groups of this field. |
||
| 249 | * |
||
| 250 | * @return array |
||
| 251 | */ |
||
| 252 | public function get_group_names() { |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Retrieve a group by its name. |
||
| 258 | * |
||
| 259 | * @param string $group_name Group name |
||
| 260 | * @return Group_Field $group_object Group object |
||
| 261 | */ |
||
| 262 | public function get_group_by_name( $group_name ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Set the group label Underscore template. |
||
| 276 | * |
||
| 277 | * @param string|callable $template |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function set_header_template( $template ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Set the field labels. |
||
| 298 | * Currently supported values: |
||
| 299 | * - singular_name - the singular entry label |
||
| 300 | * - plural_name - the plural entries label |
||
| 301 | * |
||
| 302 | * @param array $labels Labels |
||
| 303 | */ |
||
| 304 | public function setup_labels( $labels ) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return a clone of a field with hierarchy settings applied |
||
| 311 | * |
||
| 312 | * @param Field $field |
||
| 313 | * @param Field $parent_field |
||
| 314 | * @param int $group_index |
||
| 315 | * @return Field |
||
| 316 | */ |
||
| 317 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $group_index = 0 ) { |
||
| 323 | |||
| 324 | protected function get_prefilled_group_fields( $group_fields, $group_values, $group_index ) { |
||
| 337 | |||
| 338 | protected function get_prefilled_groups( $value_tree ) { |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Load the field value from an input array based on it's name. |
||
| 361 | * |
||
| 362 | * @param array $input Array of field names and values. |
||
| 363 | */ |
||
| 364 | public function set_value_from_input( $input ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Save all contained groups of fields. |
||
| 406 | */ |
||
| 407 | public function save() { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritDoc} |
||
| 433 | */ |
||
| 434 | public function get_formatted_value() { |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Convert an externally-keyed value array ('_type' => ...) |
||
| 457 | * to an internally-keyed one ('value' => ...) |
||
| 458 | * |
||
| 459 | * @param mixed $value |
||
| 460 | * @return mixed |
||
| 461 | */ |
||
| 462 | protected function external_to_internal_value( $value ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * {@inheritDoc} |
||
| 477 | */ |
||
| 478 | public function set_value( $value ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * {@inheritDoc} |
||
| 490 | */ |
||
| 491 | public function set_default_value( $default_value ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Return the full value tree of all groups and their fields |
||
| 503 | * |
||
| 504 | * @return mixed |
||
| 505 | */ |
||
| 506 | public function get_value_tree() { |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Set the full value tree of all groups and their fields |
||
| 512 | * |
||
| 513 | * @see Internal Glossary in DEVELOPMENT.MD |
||
| 514 | */ |
||
| 515 | public function set_value_tree( $value_tree ) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 521 | * |
||
| 522 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 523 | * @return array |
||
| 524 | */ |
||
| 525 | public function to_json( $load ) { |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Modify the layout of this field. |
||
| 585 | * |
||
| 586 | * @param string $layout |
||
| 587 | */ |
||
| 588 | public function set_layout( $layout ) { |
||
| 607 | |||
| 608 | /** |
||
| 609 | * Get the minimum number of entries. |
||
| 610 | * |
||
| 611 | * @return int $min |
||
| 612 | */ |
||
| 613 | public function get_min() { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Set the minimum number of entries. |
||
| 619 | * |
||
| 620 | * @param int $min |
||
| 621 | */ |
||
| 622 | public function set_min( $min ) { |
||
| 626 | |||
| 627 | /** |
||
| 628 | * Get the maximum number of entries. |
||
| 629 | * |
||
| 630 | * @return int $max |
||
| 631 | */ |
||
| 632 | public function get_max() { |
||
| 635 | |||
| 636 | /** |
||
| 637 | * Set the maximum number of entries. |
||
| 638 | * |
||
| 639 | * @param int $max |
||
| 640 | */ |
||
| 641 | public function set_max( $max ) { |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Get collapsed state |
||
| 648 | * |
||
| 649 | * @return bool |
||
| 650 | */ |
||
| 651 | public function get_collapsed() { |
||
| 654 | |||
| 655 | /** |
||
| 656 | * Change the groups initial collapse state. |
||
| 657 | * This state relates to the state of which the groups are rendered. |
||
| 658 | * |
||
| 659 | * @param bool $collapsed |
||
| 660 | */ |
||
| 661 | public function set_collapsed( $collapsed = true ) { |
||
| 665 | |||
| 666 | /** |
||
| 667 | * Get whether duplicate groups are allowed. |
||
| 668 | * |
||
| 669 | * @return bool |
||
| 670 | */ |
||
| 671 | public function get_duplicate_groups_allowed() { |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Set whether duplicate groups are allowed. |
||
| 677 | * |
||
| 678 | * @param bool $allowed |
||
| 679 | */ |
||
| 680 | public function set_duplicate_groups_allowed( $allowed ) { |
||
| 684 | } |
||
| 685 |
Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.