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 CMB2_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 CMB2_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2_Field extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'args'; |
||
25 | |||
26 | /** |
||
27 | * Field arguments |
||
28 | * @var mixed |
||
29 | * @since 1.1.0 |
||
30 | */ |
||
31 | public $args = array(); |
||
32 | |||
33 | /** |
||
34 | * Field group object or false (if no group) |
||
35 | * @var mixed |
||
36 | * @since 1.1.0 |
||
37 | */ |
||
38 | public $group = false; |
||
39 | |||
40 | /** |
||
41 | * Field meta value |
||
42 | * @var mixed |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | public $value = null; |
||
46 | |||
47 | /** |
||
48 | * Field meta value |
||
49 | * @var mixed |
||
50 | * @since 1.1.0 |
||
51 | */ |
||
52 | public $escaped_value = null; |
||
53 | |||
54 | /** |
||
55 | * Grouped Field's current numeric index during the save process |
||
56 | * @var mixed |
||
57 | * @since 2.0.0 |
||
58 | */ |
||
59 | public $index = 0; |
||
60 | |||
61 | /** |
||
62 | * Array of field options |
||
63 | * @var array |
||
64 | * @since 2.0.0 |
||
65 | */ |
||
66 | protected $field_options = array(); |
||
67 | |||
68 | /** |
||
69 | * Array of provided field text strings |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $strings; |
||
74 | |||
75 | /** |
||
76 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
77 | * @var string |
||
78 | * @since 2.2.2 |
||
79 | */ |
||
80 | public $render_context = 'edit'; |
||
81 | |||
82 | /** |
||
83 | * All CMB2_Field callable field arguments. |
||
84 | * Can be used to determine if a field argument is callable. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | public static $callable_fields = array( |
||
89 | 'default', |
||
90 | 'row_classes', |
||
91 | 'options_cb', |
||
92 | 'label_cb', |
||
93 | 'render_row_cb', |
||
94 | 'before_group', |
||
95 | 'before_group_row', |
||
96 | 'before_row', |
||
97 | 'before', |
||
98 | 'before_field', |
||
99 | 'after_field', |
||
100 | 'after', |
||
101 | 'after_row', |
||
102 | 'after_group_row', |
||
103 | 'after_group', |
||
104 | ); |
||
105 | |||
106 | /** |
||
107 | * Constructs our field object |
||
108 | * @since 1.1.0 |
||
109 | * @param array $args Field arguments |
||
110 | */ |
||
111 | 109 | public function __construct( $args ) { |
|
133 | |||
134 | /** |
||
135 | * Non-existent methods fallback to checking for field arguments of the same name |
||
136 | * @since 1.1.0 |
||
137 | * @param string $name Method name |
||
138 | * @param array $arguments Array of passed-in arguments |
||
139 | * @return mixed Value of field argument |
||
140 | */ |
||
141 | 97 | public function __call( $name, $arguments ) { |
|
145 | |||
146 | /** |
||
147 | * Retrieves the field id |
||
148 | * @since 1.1.0 |
||
149 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
150 | * @return string Field id |
||
151 | */ |
||
152 | 107 | public function id( $raw = false ) { |
|
156 | |||
157 | /** |
||
158 | * Get a field argument |
||
159 | * @since 1.1.0 |
||
160 | * @param string $key Argument to check |
||
161 | * @param string $_key Sub argument to check |
||
162 | * @return mixed Argument value or false if non-existent |
||
163 | */ |
||
164 | 110 | public function args( $key = '', $_key = '' ) { |
|
178 | |||
179 | /** |
||
180 | * Retrieve a portion of a field property |
||
181 | * @since 1.1.0 |
||
182 | * @param string $var Field property to check |
||
183 | * @param string $key Field property array key to check |
||
184 | * @return mixed Queried property value or false |
||
185 | */ |
||
186 | 110 | public function _data( $var, $key = '' ) { |
|
193 | |||
194 | /** |
||
195 | * Get Field's value |
||
196 | * @since 1.1.0 |
||
197 | * @param string $key If value is an array, is used to get array key->value |
||
198 | * @return mixed Field value or false if non-existent |
||
199 | */ |
||
200 | 46 | public function value( $key = '' ) { |
|
203 | |||
204 | /** |
||
205 | * Retrieves metadata/option data |
||
206 | * @since 1.0.1 |
||
207 | * @param string $field_id Meta key/Option array key |
||
208 | * @param array $args Override arguments |
||
209 | * @return mixed Meta/Option value |
||
210 | */ |
||
211 | 105 | public function get_data( $field_id = '', $args = array() ) { |
|
274 | |||
275 | /** |
||
276 | * Updates metadata/option data |
||
277 | * @since 1.0.1 |
||
278 | * @param mixed $new_value Value to update data with |
||
279 | * @param bool $single Whether data is an array (add_metadata) |
||
280 | */ |
||
281 | 11 | public function update_data( $new_value, $single = true ) { |
|
345 | |||
346 | /** |
||
347 | * Removes/updates metadata/option data |
||
348 | * @since 1.0.1 |
||
349 | * @param string $old Old value |
||
350 | */ |
||
351 | 3 | public function remove_data( $old = '' ) { |
|
407 | |||
408 | /** |
||
409 | * Data variables for get/set data methods |
||
410 | * @since 1.1.0 |
||
411 | * @param array $args Override arguments |
||
412 | * @return array Updated arguments |
||
413 | */ |
||
414 | 105 | public function data_args( $args = array() ) { |
|
424 | |||
425 | /** |
||
426 | * Checks if field has a registered sanitization callback |
||
427 | * @since 1.0.1 |
||
428 | * @param mixed $meta_value Meta value |
||
429 | * @return mixed Possibly sanitized meta value |
||
430 | */ |
||
431 | 13 | public function sanitization_cb( $meta_value ) { |
|
474 | |||
475 | /** |
||
476 | * Process $_POST data to save this field's value |
||
477 | * @since 2.0.3 |
||
478 | * @param array $data_to_save $_POST data to check |
||
479 | * @return array|int|bool Result of save, false on failure |
||
480 | */ |
||
481 | 2 | public function save_field_from_data( array $data_to_save ) { |
|
490 | |||
491 | /** |
||
492 | * Sanitize/store a value to this field |
||
493 | * @since 2.0.0 |
||
494 | * @param array $meta_value Desired value to sanitize/store |
||
495 | * @return array|int|bool Result of save. false on failure |
||
496 | */ |
||
497 | 12 | public function save_field( $meta_value ) { |
|
498 | |||
499 | 12 | $updated = false; |
|
500 | 12 | $action = ''; |
|
501 | 12 | $new_value = $this->sanitization_cb( $meta_value ); |
|
502 | |||
503 | 12 | if ( ! $this->args( 'save_field' ) ) { |
|
504 | |||
505 | // Nothing to see here. |
||
506 | 1 | $action = 'disabled'; |
|
507 | |||
508 | 12 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
509 | |||
510 | 1 | $this->remove_data(); |
|
511 | 1 | $count = 0; |
|
512 | |||
513 | 1 | if ( ! empty( $new_value ) ) { |
|
514 | 1 | foreach ( $new_value as $add_new ) { |
|
515 | 1 | if ( $this->update_data( $add_new, false ) ) { |
|
516 | 1 | $count++; |
|
517 | 1 | } |
|
518 | 1 | } |
|
519 | 1 | } |
|
520 | |||
521 | 1 | $updated = $count ? $count : false; |
|
522 | 1 | $action = 'repeatable'; |
|
523 | |||
524 | 11 | } elseif ( ! cmb2_utils()->isempty( $new_value ) && $new_value !== $this->get_data() ) { |
|
525 | 9 | $updated = $this->update_data( $new_value ); |
|
526 | 9 | $action = 'updated'; |
|
527 | 10 | } elseif ( cmb2_utils()->isempty( $new_value ) ) { |
|
528 | 2 | $updated = $this->remove_data(); |
|
529 | 2 | $action = 'removed'; |
|
530 | 2 | } |
|
531 | |||
532 | 12 | if ( $updated ) { |
|
533 | 11 | $this->value = $this->get_data(); |
|
534 | 11 | $this->escaped_value = null; |
|
535 | 11 | } |
|
536 | |||
537 | 12 | $field_id = $this->id( true ); |
|
538 | |||
539 | /** |
||
540 | * Hooks after save field action. |
||
541 | * |
||
542 | * @since 2.2.0 |
||
543 | * |
||
544 | * @param string $field_id the current field id paramater. |
||
545 | * @param bool $updated Whether the metadata update action occurred. |
||
546 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
547 | * @param CMB2_Field object $field This field object |
||
548 | */ |
||
549 | 12 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
|
550 | |||
551 | /** |
||
552 | * Hooks after save field action. |
||
553 | * |
||
554 | * The dynamic portion of the hook, $field_id, refers to the |
||
555 | * current field id paramater. |
||
556 | * |
||
557 | * @since 2.2.0 |
||
558 | * |
||
559 | * @param bool $updated Whether the metadata update action occurred. |
||
560 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
561 | * @param CMB2_Field object $field This field object |
||
562 | */ |
||
563 | 11 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
|
564 | |||
565 | 10 | return $updated; |
|
566 | } |
||
567 | |||
568 | /** |
||
569 | * Determine if current type is exempt from escaping |
||
570 | * @since 1.1.0 |
||
571 | * @return bool True if exempt |
||
572 | */ |
||
573 | 46 | public function escaping_exception() { |
|
581 | |||
582 | /** |
||
583 | * Determine if current type cannot be repeatable |
||
584 | * @since 1.1.0 |
||
585 | * @param string $type Field type to check |
||
586 | * @return bool True if type cannot be repeatable |
||
587 | */ |
||
588 | 4 | public function repeatable_exception( $type ) { |
|
618 | |||
619 | /** |
||
620 | * Escape the value before output. Defaults to 'esc_attr()' |
||
621 | * @since 1.0.1 |
||
622 | * @param callable $func Escaping function (if not esc_attr()) |
||
623 | * @param mixed $meta_value Meta value |
||
624 | * @return mixed Final value |
||
625 | */ |
||
626 | 46 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
666 | |||
667 | /** |
||
668 | * Return non-empty value or field default if value IS empty |
||
669 | * @since 2.0.0 |
||
670 | * @param mixed $meta_value Field value |
||
671 | * @return mixed Field value, or default value |
||
672 | */ |
||
673 | 46 | public function val_or_default( $meta_value ) { |
|
676 | |||
677 | /** |
||
678 | * Offset a time value based on timezone |
||
679 | * @since 1.0.0 |
||
680 | * @return string Offset time string |
||
681 | */ |
||
682 | public function field_timezone_offset() { |
||
685 | |||
686 | /** |
||
687 | * Return timezone string |
||
688 | * @since 1.0.0 |
||
689 | * @return string Timezone string |
||
690 | */ |
||
691 | public function field_timezone() { |
||
705 | |||
706 | /** |
||
707 | * Format the timestamp field value based on the field date/time format arg |
||
708 | * @since 2.0.0 |
||
709 | * @param int $meta_value Timestamp |
||
710 | * @param string $format Either date_format or time_format |
||
711 | * @return string Formatted date |
||
712 | */ |
||
713 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
716 | |||
717 | /** |
||
718 | * Return a formatted timestamp for a field |
||
719 | * @since 2.0.0 |
||
720 | * @param string $format Either date_format or time_format |
||
721 | * @param string $meta_value Optional meta value to check |
||
722 | * @return string Formatted date |
||
723 | */ |
||
724 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
736 | |||
737 | /** |
||
738 | * Get timestamp from text date |
||
739 | * @since 2.2.0 |
||
740 | * @param string $value Date value |
||
741 | * @return mixed Unix timestamp representing the date. |
||
742 | */ |
||
743 | public function get_timestamp_from_value( $value ) { |
||
746 | |||
747 | /** |
||
748 | * Get field render callback and Render the field row |
||
749 | * @since 1.0.0 |
||
750 | */ |
||
751 | 10 | public function render_field() { |
|
759 | |||
760 | /** |
||
761 | * Default field render callback |
||
762 | * @since 2.1.1 |
||
763 | */ |
||
764 | 9 | public function render_field_callback() { |
|
808 | |||
809 | /** |
||
810 | * The default label_cb callback (if not a title field) |
||
811 | * |
||
812 | * @since 2.1.1 |
||
813 | * @return string Label html markup |
||
814 | */ |
||
815 | 9 | public function label() { |
|
824 | |||
825 | /** |
||
826 | * Defines the classes for the current CMB2 field row |
||
827 | * |
||
828 | * @since 2.0.0 |
||
829 | * @return string Space concatenated list of classes |
||
830 | */ |
||
831 | 45 | public function row_classes() { |
|
881 | |||
882 | |||
883 | |||
884 | /** |
||
885 | * Get field display callback and render the display value in the column. |
||
886 | * @since 2.2.2 |
||
887 | */ |
||
888 | 33 | public function render_column() { |
|
896 | |||
897 | /** |
||
898 | * Default callback to outputs field value in a display format. |
||
899 | * @since 2.2.2 |
||
900 | */ |
||
901 | 33 | public function display_value_callback() { |
|
944 | |||
945 | /** |
||
946 | * Replaces a hash key - {#} - with the repeatable index |
||
947 | * @since 1.2.0 |
||
948 | * @param string $value Value to update |
||
949 | * @return string Updated value |
||
950 | */ |
||
951 | 2 | public function replace_hash( $value ) { |
|
955 | |||
956 | /** |
||
957 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
958 | * For back-compatibility, falls back to checking the options array. |
||
959 | * |
||
960 | * @since 2.2.2 |
||
961 | * @param string $text_key Key in field's text array |
||
962 | * @param string $fallback Fallback text |
||
963 | * @return string Text |
||
964 | */ |
||
965 | 8 | public function string( $text_key, $fallback ) { |
|
989 | |||
990 | /** |
||
991 | * Retrieve options args. Calls options_cb if it exists. |
||
992 | * @since 2.0.0 |
||
993 | * @param string $key Specific option to retrieve |
||
994 | * @return array Array of options |
||
995 | */ |
||
996 | 31 | public function options( $key = '' ) { |
|
1021 | |||
1022 | /** |
||
1023 | * Store JS dependencies as part of the field args. |
||
1024 | * @since 2.2.0 |
||
1025 | * @param array $dependencies Dependies to register for this field. |
||
1026 | */ |
||
1027 | 12 | public function add_js_dependencies( $dependencies = array() ) { |
|
1028 | 12 | foreach ( (array) $dependencies as $dependency ) { |
|
1029 | 12 | $this->args['js_dependencies'][ $dependency ] = $dependency; |
|
1030 | 12 | } |
|
1031 | |||
1032 | 12 | CMB2_JS::add_dependencies( $dependencies ); |
|
1033 | 12 | } |
|
1034 | |||
1035 | /** |
||
1036 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1037 | * |
||
1038 | * @since 0.2.2 |
||
1039 | * |
||
1040 | * @return mixed Default field value |
||
1041 | */ |
||
1042 | 33 | public function get_default() { |
|
1055 | |||
1056 | /** |
||
1057 | * Fills in empty field parameters with defaults |
||
1058 | * @since 1.1.0 |
||
1059 | * @param array $args Metabox field config array |
||
1060 | */ |
||
1061 | 109 | public function _set_field_defaults( $args ) { |
|
1189 | |||
1190 | /** |
||
1191 | * Get default field arguments specific to this CMB2 object. |
||
1192 | * @since 2.2.0 |
||
1193 | * @param array $field_args Metabox field config array. |
||
1194 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1195 | * @return array Array of field arguments. |
||
1196 | */ |
||
1197 | 5 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1208 | |||
1209 | /** |
||
1210 | * Returns a cloned version of this field object with, but with |
||
1211 | * modified/overridden field arguments. |
||
1212 | * |
||
1213 | * @since 2.2.2 |
||
1214 | * @param array $field_args Array of field arguments, or entire array of |
||
1215 | * arguments for CMB2_Field |
||
1216 | * |
||
1217 | * @return CMB2_Field The new CMB2_Field instance. |
||
1218 | */ |
||
1219 | 5 | public function get_field_clone( $field_args ) { |
|
1222 | |||
1223 | /** |
||
1224 | * Returns the CMB2 instance this field is registered to. |
||
1225 | * |
||
1226 | * @since 2.2.2 |
||
1227 | * |
||
1228 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1229 | */ |
||
1230 | 1 | public function get_cmb() { |
|
1237 | |||
1238 | } |
||
1239 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.