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 | const LAYOUT_GRID = 'grid'; // default |
||
19 | |||
20 | const LAYOUT_TABBED_HORIZONTAL = 'tabbed-horizontal'; |
||
21 | |||
22 | const LAYOUT_TABBED_VERTICAL = 'tabbed-vertical'; |
||
23 | |||
24 | const GROUP_TYPE_KEY = '_type'; |
||
25 | |||
26 | /** |
||
27 | * Default field value |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $default_value = array(); |
||
32 | |||
33 | protected $layout = self::LAYOUT_GRID; |
||
34 | |||
35 | protected $value_tree = array(); |
||
36 | |||
37 | protected $fields = array(); |
||
38 | |||
39 | protected $groups = array(); |
||
40 | |||
41 | protected $values_min = -1; |
||
42 | |||
43 | protected $values_max = -1; |
||
44 | |||
45 | protected $collapsed = false; |
||
46 | |||
47 | public $labels = array( |
||
48 | 'singular_name' => 'Entry', |
||
49 | 'plural_name' => 'Entries', |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * Create a field from a certain type with the specified label. |
||
54 | * |
||
55 | * @param string $type Field type |
||
56 | * @param string $name Field name |
||
57 | * @param string $label Field label |
||
58 | */ |
||
59 | protected function __construct( $type, $name, $label ) { |
||
63 | |||
64 | /** |
||
65 | * Set array of hierarchy field names |
||
66 | **/ |
||
67 | public function set_hierarchy( $hierarchy ) { |
||
71 | |||
72 | /** |
||
73 | * Propagate hierarchy to child fields |
||
74 | **/ |
||
75 | public function update_child_hierarchy() { |
||
82 | |||
83 | /** |
||
84 | * Activate the field once the container is attached. |
||
85 | */ |
||
86 | public function activate() { |
||
93 | |||
94 | /** |
||
95 | * Initialization tasks. |
||
96 | */ |
||
97 | public function init() { |
||
109 | |||
110 | /** |
||
111 | * Add a set/group of fields. |
||
112 | * |
||
113 | * Accepted param variations: |
||
114 | * - array<Field> $fields |
||
115 | * - string $group_name, array<Field> $fields |
||
116 | * - string $group_name, string $group_label, array<Field> $fields |
||
117 | * |
||
118 | * @return $this |
||
119 | */ |
||
120 | public function add_fields() { |
||
154 | |||
155 | /** |
||
156 | * Set the group label Underscore template. |
||
157 | * |
||
158 | * @param string|callable $template |
||
159 | * @return $this |
||
160 | */ |
||
161 | public function set_header_template( $template ) { |
||
180 | |||
181 | /** |
||
182 | * Retrieve all groups of fields. |
||
183 | * |
||
184 | * @return array $fields |
||
185 | */ |
||
186 | public function get_fields() { |
||
197 | |||
198 | /** |
||
199 | * Set the field labels. |
||
200 | * Currently supported values: |
||
201 | * - singular_name - the singular entry label |
||
202 | * - plural_name - the plural entries label |
||
203 | * |
||
204 | * @param array $labels Labels |
||
205 | */ |
||
206 | public function setup_labels( $labels ) { |
||
210 | |||
211 | /** |
||
212 | * Set the datastore of this field. |
||
213 | * |
||
214 | * @param Datastore_Interface $datastore |
||
215 | */ |
||
216 | View Code Duplication | public function set_datastore( Datastore_Interface $datastore, $set_as_default = false ) { |
|
228 | |||
229 | /** |
||
230 | * Return a clone of a field with hierarchy settings applied |
||
231 | * |
||
232 | * @param Field $field |
||
233 | * @param Field $parent_field |
||
234 | * @param int $entry_index |
||
235 | * @return Field |
||
236 | **/ |
||
237 | public function get_clone_under_field_in_hierarchy( $field, $parent_field, $entry_index = 0 ) { |
||
243 | |||
244 | /** |
||
245 | * Load the field value from an input array based on it's name. |
||
246 | * |
||
247 | * @param array $input Array of field names and values. |
||
248 | **/ |
||
249 | public function set_value_from_input( $input ) { |
||
295 | |||
296 | protected function get_prefilled_field_groups( $value_tree ) { |
||
334 | |||
335 | /** |
||
336 | * Load all groups of fields and their data. |
||
337 | */ |
||
338 | public function load() { |
||
352 | |||
353 | /** |
||
354 | * Save all contained groups of fields. |
||
355 | */ |
||
356 | public function save() { |
||
376 | |||
377 | /** |
||
378 | * Return the full value tree of all groups and their fields |
||
379 | * |
||
380 | * @return mixed |
||
381 | **/ |
||
382 | public function get_value_tree() { |
||
385 | |||
386 | /** |
||
387 | * Set the full value tree of all groups and their fields |
||
388 | * |
||
389 | * Tree Schema |
||
390 | * 'value_set' => array( |
||
391 | * array( |
||
392 | * 'value' => '_', |
||
393 | * ), |
||
394 | * ... |
||
395 | * ), |
||
396 | * 'groups' => array( |
||
397 | * array( |
||
398 | * 'field1' => array( |
||
399 | * 'value_set'=>array( |
||
400 | * array( |
||
401 | * 'value' => ..., |
||
402 | * ... |
||
403 | * ), |
||
404 | * ... |
||
405 | * ), |
||
406 | * ), |
||
407 | * ... |
||
408 | * ), |
||
409 | * ... |
||
410 | * ), |
||
411 | * |
||
412 | * @return mixed |
||
413 | **/ |
||
414 | public function set_value_tree( $value_tree ) { |
||
417 | |||
418 | /** |
||
419 | * Return a differently formatted value for end-users |
||
420 | * |
||
421 | * @return mixed |
||
422 | **/ |
||
423 | public function get_formatted_value() { |
||
439 | |||
440 | /** |
||
441 | * Returns an array that holds the field data, suitable for JSON representation. |
||
442 | * This data will be available in the Underscore template and the Backbone Model. |
||
443 | * |
||
444 | * @param bool $load Should the value be loaded from the database or use the value from the current instance. |
||
445 | * @return array |
||
446 | */ |
||
447 | public function to_json( $load ) { |
||
489 | |||
490 | /** |
||
491 | * The main Underscore template. |
||
492 | */ |
||
493 | public function template() { |
||
545 | |||
546 | /** |
||
547 | * The Underscore template for the complex field group. |
||
548 | */ |
||
549 | public function template_group() { |
||
598 | |||
599 | /** |
||
600 | * The Underscore template for the group item tab. |
||
601 | */ |
||
602 | public function template_group_tab_item() { |
||
617 | |||
618 | /** |
||
619 | * Modify the layout of this field. |
||
620 | * |
||
621 | * @param string $layout |
||
622 | */ |
||
623 | public function set_layout( $layout ) { |
||
641 | |||
642 | /** |
||
643 | * Set the minimum number of entries. |
||
644 | * |
||
645 | * @param int $min |
||
646 | */ |
||
647 | public function set_min( $min ) { |
||
651 | |||
652 | /** |
||
653 | * Get the minimum number of entries. |
||
654 | * |
||
655 | * @return int $min |
||
656 | */ |
||
657 | public function get_min() { |
||
660 | |||
661 | /** |
||
662 | * Set the maximum number of entries. |
||
663 | * |
||
664 | * @param int $max |
||
665 | */ |
||
666 | public function set_max( $max ) { |
||
670 | |||
671 | /** |
||
672 | * Get the maximum number of entries. |
||
673 | * |
||
674 | * @return int $max |
||
675 | */ |
||
676 | public function get_max() { |
||
679 | |||
680 | /** |
||
681 | * Change the groups initial collapse state. |
||
682 | * This state relates to the state of which the groups are rendered. |
||
683 | * |
||
684 | * @param bool $collapsed |
||
685 | */ |
||
686 | public function set_collapsed( $collapsed = true ) { |
||
691 | |||
692 | /** |
||
693 | * Retrieve the groups of this field. |
||
694 | * |
||
695 | * @return array |
||
696 | */ |
||
697 | public function get_group_names() { |
||
700 | |||
701 | /** |
||
702 | * Retrieve a group by its name. |
||
703 | * |
||
704 | * @param string $group_name Group name |
||
705 | * @return Group_Field $group_object Group object |
||
706 | */ |
||
707 | public function get_group_by_name( $group_name ) { |
||
718 | } |
||
719 |