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 ) { |
||
| 95 | $this->set_value_set( new Value_Set( Value_Set::TYPE_MULTIPLE_VALUES ) ); |
||
| 96 | parent::__construct( $type, $name, $label ); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Initialization tasks. |
||
| 101 | */ |
||
| 102 | public function init() { |
||
| 103 | $this->labels = array( |
||
| 104 | 'singular_name' => __( $this->labels['singular_name'], 'carbon-fields' ), |
||
| 105 | 'plural_name' => __( $this->labels['plural_name'], 'carbon-fields' ), |
||
| 106 | ); |
||
| 107 | parent::init(); |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Set array of hierarchy field names |
||
| 112 | */ |
||
| 113 | public function set_hierarchy( $hierarchy ) { |
||
| 114 | parent::set_hierarchy( $hierarchy ); |
||
| 115 | $this->update_child_hierarchy(); |
||
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Propagate hierarchy to child fields |
||
| 120 | */ |
||
| 121 | public function update_child_hierarchy() { |
||
| 122 | $hierarchy = array_merge( $this->get_hierarchy(), array( $this->get_base_name() ) ); |
||
| 123 | $fields = $this->get_fields(); |
||
| 124 | foreach ( $fields as $field ) { |
||
| 125 | $field->set_hierarchy( $hierarchy ); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Activate the field once the container is attached. |
||
| 131 | */ |
||
| 132 | public function activate() { |
||
| 133 | parent::activate(); |
||
| 134 | $fields = $this->get_fields(); |
||
| 135 | foreach ( $fields as $field ) { |
||
| 136 | $field->activate(); |
||
| 137 | } |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Set the datastore of this field and propagate it to children |
||
| 142 | * |
||
| 143 | * @param Datastore_Interface $datastore |
||
| 144 | * @param boolean $set_as_default |
||
| 145 | * @return object $this |
||
| 146 | */ |
||
| 147 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
| 148 | if ( $set_as_default && ! $this->has_default_datastore() ) { |
||
| 149 | return $this; // datastore has been overriden with a custom one - abort changing to a default one |
||
| 150 | } |
||
| 151 | $this->datastore = $datastore; |
||
| 152 | $this->has_default_datastore = $set_as_default; |
||
| 153 | |||
| 154 | $this->update_child_datastore( $this->get_datastore(), true ); |
||
| 155 | return $this; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Propagate the datastore down the hierarchy |
||
| 160 | * |
||
| 161 | * @param Datastore_Interface $datastore |
||
| 162 | * @param boolean $set_as_default |
||
| 163 | */ |
||
| 164 | protected function update_child_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
||
|
|
|||
| 165 | foreach ( $this->groups as $group ) { |
||
| 166 | $group->set_datastore( $this->get_datastore(), $set_as_default ); |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Retrieve all groups of fields. |
||
| 172 | * |
||
| 173 | * @return array $fields |
||
| 174 | */ |
||
| 175 | public function get_fields() { |
||
| 176 | $fields = array(); |
||
| 177 | |||
| 178 | foreach ( $this->groups as $group ) { |
||
| 179 | $group_fields = $group->get_fields(); |
||
| 180 | |||
| 181 | $fields = array_merge( $fields, $group_fields ); |
||
| 182 | } |
||
| 183 | |||
| 184 | return $fields; |
||
| 185 | } |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Add a set/group of fields. |
||
| 189 | * |
||
| 190 | * Accepted param variations: |
||
| 191 | * - array<Field> $fields |
||
| 192 | * - string $group_name, array<Field> $fields |
||
| 193 | * - string $group_name, string $group_label, array<Field> $fields |
||
| 194 | * |
||
| 195 | * @return $this |
||
| 196 | */ |
||
| 197 | public function add_fields() { |
||
| 198 | $argv = func_get_args(); |
||
| 199 | $argc = count( $argv ); |
||
| 200 | $fields = $argv[ $argc - 1 ]; |
||
| 201 | $name = ''; |
||
| 202 | $label = null; |
||
| 203 | |||
| 204 | if ( $argc >= 2 ) { |
||
| 205 | $name = $argv[0]; |
||
| 206 | } |
||
| 207 | |||
| 208 | if ( $argc >= 3 ) { |
||
| 209 | $label = $argv[1]; |
||
| 210 | } |
||
| 211 | |||
| 212 | $name = ! empty( $name ) ? $name : Group_Field::DEFAULT_GROUP_NAME; |
||
| 213 | |||
| 214 | if ( array_key_exists( $name, $this->groups ) ) { |
||
| 215 | Incorrect_Syntax_Exception::raise( 'Group with name "' . $name . '" in Complex Field "' . $this->get_label() . '" already exists.' ); |
||
| 216 | return $this; |
||
| 217 | } |
||
| 218 | |||
| 219 | foreach ( $fields as $field ) { |
||
| 220 | if ( $field->get_base_name() === Value_Set::VALUE_PROPERTY ) { |
||
| 221 | Incorrect_Syntax_Exception::raise( '"' . Value_Set::VALUE_PROPERTY . '" is a reserved keyword for Complex fields and cannot be used for a field name.' ); |
||
| 222 | return $this; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | $group = new Group_Field( $name, $label, $fields ); |
||
| 227 | $this->groups[ $group->get_name() ] = $group; |
||
| 228 | |||
| 229 | $this->update_child_hierarchy(); |
||
| 230 | if ( $this->get_datastore() !== null ) { |
||
| 231 | $this->update_child_datastore( $this->get_datastore(), true ); |
||
| 232 | } |
||
| 233 | |||
| 234 | return $this; |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Retrieve the groups of this field. |
||
| 239 | * |
||
| 240 | * @return array |
||
| 241 | */ |
||
| 242 | public function get_group_names() { |
||
| 243 | return array_keys( $this->groups ); |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Retrieve a group by its name. |
||
| 248 | * |
||
| 249 | * @param string $group_name Group name |
||
| 250 | * @return Group_Field $group_object Group object |
||
| 251 | */ |
||
| 252 | public function get_group_by_name( $group_name ) { |
||
| 253 | $group_object = null; |
||
| 254 | |||
| 255 | foreach ( $this->groups as $group ) { |
||
| 256 | if ( $group->get_name() == $group_name ) { |
||
| 257 | $group_object = $group; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | return $group_object; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Set the group label Underscore template. |
||
| 266 | * |
||
| 267 | * @param string|callable $template |
||
| 268 | * @return $this |
||
| 269 | */ |
||
| 270 | public function set_header_template( $template ) { |
||
| 271 | $template = is_callable( $template ) ? call_user_func( $template ) : $template; |
||
| 272 | |||
| 273 | // Assign the template to the group that was added last |
||
| 274 | $values = array_values( $this->groups ); |
||
| 275 | $group = end( $values ); |
||
| 276 | |||
| 277 | if ( $group ) { |
||
| 278 | $group->set_label_template( $template ); |
||
| 279 | |||
| 280 | $this->groups[ $group->get_name() ] = $group; |
||
| 281 | } |
||
| 282 | |||
| 283 | return $this; |
||
| 284 | } |
||
| 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 ) { |
||
| 315 | $fields = array(); |
||
| 316 | |||
| 317 | foreach ( $group_fields as $field ) { |
||
| 318 | $clone = $this->get_clone_under_field_in_hierarchy( $field, $this, $group_index ); |
||
| 319 | if ( isset( $group_values[ $clone->get_base_name() ] ) ) { |
||
| 320 | $clone->set_value( $group_values[ $clone->get_base_name() ] ); |
||
| 321 | } |
||
| 322 | $fields[] = $clone; |
||
| 323 | } |
||
| 324 | |||
| 325 | return $fields; |
||
| 327 | |||
| 328 | protected function get_prefilled_groups( $value_tree ) { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Load the field value from an input array based on it's name. |
||
| 347 | * |
||
| 348 | * @param array $input Array of field names and values. |
||
| 349 | */ |
||
| 350 | public function set_value_from_input( $input ) { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Save all contained groups of fields. |
||
| 392 | */ |
||
| 393 | public function save() { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * {@inheritDoc} |
||
| 420 | */ |
||
| 421 | public function set_value( $value ) { |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Return the full value tree of all groups and their fields |
||
| 428 | * |
||
| 429 | * @return mixed |
||
| 430 | */ |
||
| 431 | public function get_value_tree() { |
||
| 434 | |||
| 435 | /** |
||
| 436 | * Set the full value tree of all groups and their fields |
||
| 437 | * |
||
| 438 | * @see Internal Glossary in DEVELOPMENT.MD |
||
| 439 | */ |
||
| 440 | public function set_value_tree( $value_tree ) { |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Return a differently formatted value for end-users |
||
| 446 | * |
||
| 447 | * @return mixed |
||
| 448 | */ |
||
| 449 | public function get_formatted_value() { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Returns an array that holds the field data, suitable for JSON representation. |
||
| 472 | * |
||
| 473 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
| 474 | * @return array |
||
| 475 | */ |
||
| 476 | public function to_json( $load ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Modify the layout of this field. |
||
| 526 | * |
||
| 527 | * @param string $layout |
||
| 528 | */ |
||
| 529 | public function set_layout( $layout ) { |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Get the minimum number of entries. |
||
| 551 | * |
||
| 552 | * @return int $min |
||
| 553 | */ |
||
| 554 | public function get_min() { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * Set the minimum number of entries. |
||
| 560 | * |
||
| 561 | * @param int $min |
||
| 562 | */ |
||
| 563 | public function set_min( $min ) { |
||
| 567 | |||
| 568 | /** |
||
| 569 | * Get the maximum number of entries. |
||
| 570 | * |
||
| 571 | * @return int $max |
||
| 572 | */ |
||
| 573 | public function get_max() { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Set the maximum number of entries. |
||
| 579 | * |
||
| 580 | * @param int $max |
||
| 581 | */ |
||
| 582 | public function set_max( $max ) { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Get collapsed state |
||
| 589 | * |
||
| 590 | * @return bool |
||
| 591 | */ |
||
| 592 | public function get_collapsed() { |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Change the groups initial collapse state. |
||
| 598 | * This state relates to the state of which the groups are rendered. |
||
| 599 | * |
||
| 600 | * @param bool $collapsed |
||
| 601 | */ |
||
| 602 | public function set_collapsed( $collapsed = true ) { |
||
| 606 | } |
||
| 607 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.