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 | * Entry labels |
||
| 80 | * These are translated in init() |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | public $labels = array( |
||
| 85 | 'singular_name' => 'Entry', |
||
| 86 | 'plural_name' => 'Entries', |
||
| 87 | ); |
||
| 88 | |||
| 89 | /** |
||
| 90 | * Create a field from a certain type with the specified label. |
||
| 91 | * |
||
| 92 | * @param string $type Field type |
||
| 93 | * @param string $name Field name |
||
| 94 | * @param string $label Field label |
||
| 95 | */ |
||
| 96 | public function __construct( $type, $name, $label ) { |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Initialization tasks. |
||
| 103 | */ |
||
| 104 | public function init() { |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set array of hierarchy field names |
||
| 114 | */ |
||
| 115 | public function set_hierarchy( $hierarchy ) { |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Propagate hierarchy to child fields |
||
| 122 | */ |
||
| 123 | public function update_child_hierarchy() { |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Activate the field once the container is attached. |
||
| 133 | */ |
||
| 134 | public function activate() { |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Set the datastore of this field and propagate it to children |
||
| 144 | * |
||
| 145 | * @param Datastore_Interface $datastore |
||
| 146 | * @param boolean $set_as_default |
||
| 147 | * @return object $this |
||
| 148 | */ |
||
| 149 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Propagate the datastore down the hierarchy |
||
| 162 | * |
||
| 163 | * @param Datastore_Interface $datastore |
||
| 164 | * @param boolean $set_as_default |
||
| 165 | */ |
||
| 166 | protected function update_child_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Retrieve all groups of fields. |
||
| 174 | * |
||
| 175 | * @return array $fields |
||
| 176 | */ |
||
| 177 | public function get_fields() { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Add a set/group of fields. |
||
| 191 | * |
||
| 192 | * Accepted param variations: |
||
| 193 | * - array<Field> $fields |
||
| 194 | * - string $group_name, array<Field> $fields |
||
| 195 | * - string $group_name, string $group_label, array<Field> $fields |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | */ |
||
| 199 | public function add_fields() { |
||
| 200 | $argv = func_get_args(); |
||
| 201 | $argc = count( $argv ); |
||
| 202 | $fields = $argv[ $argc - 1 ]; |
||
| 203 | $name = ''; |
||
| 204 | $label = null; |
||
| 205 | |||
| 206 | if ( $argc >= 2 ) { |
||
| 207 | $name = $argv[0]; |
||
| 208 | } |
||
| 209 | |||
| 210 | if ( $argc >= 3 ) { |
||
| 211 | $label = $argv[1]; |
||
| 212 | } |
||
| 213 | |||
| 214 | $name = ! empty( $name ) ? $name : Group_Field::DEFAULT_GROUP_NAME; |
||
| 215 | |||
| 216 | if ( array_key_exists( $name, $this->groups ) ) { |
||
| 217 | Incorrect_Syntax_Exception::raise( 'Group with name "' . $name . '" in Complex Field "' . $this->get_label() . '" already exists.' ); |
||
| 218 | return $this; |
||
| 219 | } |
||
| 220 | |||
| 221 | $reserved_names = array( Value_Set::VALUE_PROPERTY, static::TYPE_PROPERTY ); |
||
| 222 | foreach ( $fields as $field ) { |
||
| 223 | if ( in_array( $field->get_base_name(), $reserved_names ) ) { |
||
| 224 | Incorrect_Syntax_Exception::raise( '"' . $field->get_base_name() . '" is a reserved keyword for Complex fields and cannot be used for a field name.' ); |
||
| 225 | return $this; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | $group = new Group_Field( $name, $label, $fields ); |
||
| 230 | $this->groups[ $group->get_name() ] = $group; |
||
| 231 | |||
| 232 | $this->update_child_hierarchy(); |
||
| 233 | if ( $this->get_datastore() !== null ) { |
||
| 234 | $this->update_child_datastore( $this->get_datastore(), true ); |
||
| 235 | } |
||
| 236 | |||
| 237 | return $this; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Retrieve the groups of this field. |
||
| 242 | * |
||
| 243 | * @return array |
||
| 244 | */ |
||
| 245 | public function get_group_names() { |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Retrieve a group by its name. |
||
| 251 | * |
||
| 252 | * @param string $group_name Group name |
||
| 253 | * @return Group_Field $group_object Group object |
||
| 254 | */ |
||
| 255 | public function get_group_by_name( $group_name ) { |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Set the group label Underscore template. |
||
| 269 | * |
||
| 270 | * @param string|callable $template |
||
| 271 | * @return $this |
||
| 272 | */ |
||
| 273 | public function set_header_template( $template ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Set the field labels. |
||
| 291 | * Currently supported values: |
||
| 292 | * - singular_name - the singular entry label |
||
| 293 | * - plural_name - the plural entries label |
||
| 294 | * |
||
| 295 | * @param array $labels Labels |
||
| 296 | */ |
||
| 297 | public function setup_labels( $labels ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Return a clone of a field with hierarchy settings applied |
||
| 304 | * |
||
| 305 | * @param Field $field |
||
| 306 | * @param Field $parent_field |
||
| 307 | * @param int $group_index |
||
| 308 | * @return Field |
||
| 309 | */ |
||
| 310 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $group_index = 0 ) { |
||
| 316 | |||
| 317 | protected function get_prefilled_group_fields( $group_fields, $group_values, $group_index ) { |
||
| 330 | |||
| 331 | protected function get_prefilled_groups( $value_tree ) { |
||
| 332 | $fields = array(); |
||
| 333 | |||
| 334 | foreach ( $value_tree as $group_index => $value ) { |
||
| 335 | $group_name = $value[ Value_Set::VALUE_PROPERTY ]; |
||
| 336 | $group = $this->get_group_by_name( $group_name ); |
||
| 337 | if ( ! $group ) { |
||
| 338 | // Failed to find group - sombody has been messing with the database or group definitions |
||
| 339 | continue; |
||
| 340 | } |
||
| 341 | $group_fields = $group->get_fields(); |
||
| 342 | $group_values = array(); |
||
| 343 | if ( isset( $value_tree[ $group_index ] ) ) { |
||
| 344 | $group_values = $value_tree[ $group_index ]; |
||
| 345 | } |
||
| 346 | $fields[ $group_index ] = array( Value_Set::VALUE_PROPERTY => $group->get_name() ) + $this->get_prefilled_group_fields( $group_fields, $group_values, $group_index ); |
||
| 347 | } |
||
| 348 | |||
| 349 | return $fields; |
||
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Load the field value from an input array based on it's name. |
||
| 354 | * |
||
| 355 | * @param array $input Array of field names and values. |
||
| 356 | */ |
||
| 357 | public function set_value_from_input( $input ) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Save all contained groups of fields. |
||
| 399 | */ |
||
| 400 | public function save() { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritDoc} |
||
| 426 | */ |
||
| 427 | public function get_formatted_value() { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Convert an externally-keyed value array ('_type' => ...) |
||
| 450 | * to an internally-keyed one ('value' => ...) |
||
| 451 | * |
||
| 452 | * @param mixed $value |
||
| 453 | * @return mixed |
||
| 454 | */ |
||
| 455 | protected function external_to_internal_value( $value ) { |
||
| 467 | |||
| 468 | /** |
||
| 469 | * {@inheritDoc} |
||
| 470 | */ |
||
| 471 | public function set_value( $value ) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * {@inheritDoc} |
||
| 483 | */ |
||
| 484 | public function set_default_value( $default_value ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Return the full value tree of all groups and their fields |
||
| 496 | * |
||
| 497 | * @return mixed |
||
| 498 | */ |
||
| 499 | public function get_value_tree() { |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Set the full value tree of all groups and their fields |
||
| 505 | * |
||
| 506 | * @see Internal Glossary in DEVELOPMENT.MD |
||
| 507 | */ |
||
| 508 | public function set_value_tree( $value_tree ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 514 | * |
||
| 515 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | public function to_json( $load ) { |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Modify the layout of this field. |
||
| 568 | * |
||
| 569 | * @param string $layout |
||
| 570 | */ |
||
| 571 | public function set_layout( $layout ) { |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Get the minimum number of entries. |
||
| 593 | * |
||
| 594 | * @return int $min |
||
| 595 | */ |
||
| 596 | public function get_min() { |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Set the minimum number of entries. |
||
| 602 | * |
||
| 603 | * @param int $min |
||
| 604 | */ |
||
| 605 | public function set_min( $min ) { |
||
| 609 | |||
| 610 | /** |
||
| 611 | * Get the maximum number of entries. |
||
| 612 | * |
||
| 613 | * @return int $max |
||
| 614 | */ |
||
| 615 | public function get_max() { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Set the maximum number of entries. |
||
| 621 | * |
||
| 622 | * @param int $max |
||
| 623 | */ |
||
| 624 | public function set_max( $max ) { |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Get collapsed state |
||
| 631 | * |
||
| 632 | * @return bool |
||
| 633 | */ |
||
| 634 | public function get_collapsed() { |
||
| 637 | |||
| 638 | /** |
||
| 639 | * Change the groups initial collapse state. |
||
| 640 | * This state relates to the state of which the groups are rendered. |
||
| 641 | * |
||
| 642 | * @param bool $collapsed |
||
| 643 | */ |
||
| 644 | public function set_collapsed( $collapsed = true ) { |
||
| 648 | } |
||
| 649 |