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() { |
||
117 | |||
118 | /** |
||
119 | * Set array of hierarchy field names |
||
120 | */ |
||
121 | public function set_hierarchy( $hierarchy ) { |
||
125 | |||
126 | /** |
||
127 | * Propagate hierarchy to child fields |
||
128 | */ |
||
129 | public function update_child_hierarchy() { |
||
136 | |||
137 | /** |
||
138 | * Activate the field once the container is attached. |
||
139 | */ |
||
140 | public function activate() { |
||
147 | |||
148 | /** |
||
149 | * Set the datastore of this field and propagate it to children |
||
150 | * |
||
151 | * @param Datastore_Interface $datastore |
||
152 | */ |
||
153 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
165 | |||
166 | /** |
||
167 | * Retrieve all groups of fields. |
||
168 | * |
||
169 | * @return array $fields |
||
170 | */ |
||
171 | public function get_fields() { |
||
182 | |||
183 | /** |
||
184 | * Add a set/group of fields. |
||
185 | * |
||
186 | * Accepted param variations: |
||
187 | * - array<Field> $fields |
||
188 | * - string $group_name, array<Field> $fields |
||
189 | * - string $group_name, string $group_label, array<Field> $fields |
||
190 | * |
||
191 | * @return $this |
||
192 | */ |
||
193 | public function add_fields() { |
||
227 | |||
228 | /** |
||
229 | * Retrieve the groups of this field. |
||
230 | * |
||
231 | * @return array |
||
232 | */ |
||
233 | public function get_group_names() { |
||
236 | |||
237 | /** |
||
238 | * Retrieve a group by its name. |
||
239 | * |
||
240 | * @param string $group_name Group name |
||
241 | * @return Group_Field $group_object Group object |
||
242 | */ |
||
243 | public function get_group_by_name( $group_name ) { |
||
254 | |||
255 | /** |
||
256 | * Set the group label Underscore template. |
||
257 | * |
||
258 | * @param string|callable $template |
||
259 | * @return $this |
||
260 | */ |
||
261 | public function set_header_template( $template ) { |
||
277 | |||
278 | /** |
||
279 | * Set the field labels. |
||
280 | * Currently supported values: |
||
281 | * - singular_name - the singular entry label |
||
282 | * - plural_name - the plural entries label |
||
283 | * |
||
284 | * @param array $labels Labels |
||
285 | */ |
||
286 | public function setup_labels( $labels ) { |
||
290 | |||
291 | /** |
||
292 | * Return a clone of a field with hierarchy settings applied |
||
293 | * |
||
294 | * @param Field $field |
||
295 | * @param Field $parent_field |
||
296 | * @param int $group_index |
||
297 | * @return Field |
||
298 | */ |
||
299 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $group_index = 0 ) { |
||
305 | |||
306 | protected function get_prefilled_group_fields( $group_fields, $group_values, $group_index ) { |
||
327 | |||
328 | protected function get_prefilled_groups( $value_tree ) { |
||
348 | |||
349 | /** |
||
350 | * Load the field value from an input array based on it's name. |
||
351 | * |
||
352 | * @param array $input Array of field names and values. |
||
353 | */ |
||
354 | public function set_value_from_input( $input ) { |
||
403 | |||
404 | /** |
||
405 | * Load all groups of fields and their data. |
||
406 | */ |
||
407 | public function load() { |
||
419 | |||
420 | /** |
||
421 | * Save all contained groups of fields. |
||
422 | */ |
||
423 | public function save() { |
||
443 | |||
444 | /** |
||
445 | * Return the full value tree of all groups and their fields |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | public function get_value_tree() { |
||
452 | |||
453 | /** |
||
454 | * Set the full value tree of all groups and their fields |
||
455 | * |
||
456 | * @see Internal Glossary in DEVELOPMENT.MD |
||
457 | */ |
||
458 | public function set_value_tree( $value_tree ) { |
||
461 | |||
462 | /** |
||
463 | * Return a differently formatted value for end-users |
||
464 | * |
||
465 | * @return mixed |
||
466 | */ |
||
467 | public function get_formatted_value() { |
||
483 | |||
484 | /** |
||
485 | * Returns an array that holds the field data, suitable for JSON representation. |
||
486 | * |
||
487 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
488 | * @return array |
||
489 | */ |
||
490 | public function to_json( $load ) { |
||
532 | |||
533 | /** |
||
534 | * Modify the layout of this field. |
||
535 | * |
||
536 | * @param string $layout |
||
537 | */ |
||
538 | public function set_layout( $layout ) { |
||
556 | |||
557 | /** |
||
558 | * Get the minimum number of entries. |
||
559 | * |
||
560 | * @return int $min |
||
561 | */ |
||
562 | public function get_min() { |
||
565 | |||
566 | /** |
||
567 | * Set the minimum number of entries. |
||
568 | * |
||
569 | * @param int $min |
||
570 | */ |
||
571 | public function set_min( $min ) { |
||
575 | |||
576 | /** |
||
577 | * Get the maximum number of entries. |
||
578 | * |
||
579 | * @return int $max |
||
580 | */ |
||
581 | public function get_max() { |
||
584 | |||
585 | /** |
||
586 | * Set the maximum number of entries. |
||
587 | * |
||
588 | * @param int $max |
||
589 | */ |
||
590 | public function set_max( $max ) { |
||
594 | |||
595 | /** |
||
596 | * Get collapsed state |
||
597 | * |
||
598 | * @return bool |
||
599 | */ |
||
600 | public function get_collapsed() { |
||
603 | |||
604 | /** |
||
605 | * Change the groups initial collapse state. |
||
606 | * This state relates to the state of which the groups are rendered. |
||
607 | * |
||
608 | * @param bool $collapsed |
||
609 | */ |
||
610 | public function set_collapsed( $collapsed = true ) { |
||
614 | } |
||
615 |