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_cb', |
||
90 | 'classes_cb', |
||
91 | 'options_cb', |
||
92 | 'text_cb', |
||
93 | 'label_cb', |
||
94 | 'render_row_cb', |
||
95 | 'display_cb', |
||
96 | 'before_group', |
||
97 | 'before_group_row', |
||
98 | 'before_row', |
||
99 | 'before', |
||
100 | 'before_field', |
||
101 | 'after_field', |
||
102 | 'after', |
||
103 | 'after_row', |
||
104 | 'after_group_row', |
||
105 | 'after_group', |
||
106 | ); |
||
107 | |||
108 | /** |
||
109 | * Constructs our field object |
||
110 | * @since 1.1.0 |
||
111 | * @param array $args Field arguments |
||
112 | */ |
||
113 | 111 | public function __construct( $args ) { |
|
114 | |||
115 | 111 | if ( ! empty( $args['group_field'] ) ) { |
|
116 | 4 | $this->group = $args['group_field']; |
|
117 | 4 | $this->object_id = $this->group->object_id; |
|
118 | 4 | $this->object_type = $this->group->object_type; |
|
119 | 4 | $this->cmb_id = $this->group->cmb_id; |
|
120 | 4 | } else { |
|
121 | 111 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
|
122 | 111 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
|
123 | |||
124 | 111 | if ( isset( $args['cmb_id'] ) ) { |
|
125 | 42 | $this->cmb_id = $args['cmb_id']; |
|
126 | 42 | } |
|
127 | } |
||
128 | |||
129 | 111 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
|
130 | |||
131 | 111 | if ( $this->object_id ) { |
|
132 | 103 | $this->value = $this->get_data(); |
|
133 | 103 | } |
|
134 | 111 | } |
|
135 | |||
136 | /** |
||
137 | * Non-existent methods fallback to checking for field arguments of the same name |
||
138 | * @since 1.1.0 |
||
139 | * @param string $name Method name |
||
140 | * @param array $arguments Array of passed-in arguments |
||
141 | * @return mixed Value of field argument |
||
142 | */ |
||
143 | 99 | public function __call( $name, $arguments ) { |
|
144 | 99 | if ( 'string' === $name ) { |
|
145 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
146 | } |
||
147 | |||
148 | 99 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
|
149 | 99 | return $this->args( $name, $key ); |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * Retrieves the field id |
||
154 | * @since 1.1.0 |
||
155 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
156 | * @return string Field id |
||
157 | */ |
||
158 | 109 | public function id( $raw = false ) { |
|
159 | 109 | $id = $raw ? '_id' : 'id'; |
|
160 | 109 | return $this->args( $id ); |
|
161 | 1 | } |
|
162 | |||
163 | /** |
||
164 | * Get a field argument |
||
165 | * @since 1.1.0 |
||
166 | * @param string $key Argument to check |
||
167 | * @param string $_key Sub argument to check |
||
168 | * @return mixed Argument value or false if non-existent |
||
169 | */ |
||
170 | 112 | public function args( $key = '', $_key = '' ) { |
|
184 | |||
185 | /** |
||
186 | * Retrieve a portion of a field property |
||
187 | * @since 1.1.0 |
||
188 | * @param string $var Field property to check |
||
189 | * @param string $key Field property array key to check |
||
190 | * @return mixed Queried property value or false |
||
191 | */ |
||
192 | 112 | public function _data( $var, $key = '' ) { |
|
193 | 112 | $vars = $this->{$var}; |
|
194 | 112 | if ( $key ) { |
|
195 | 112 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
|
196 | } |
||
197 | 61 | return $vars; |
|
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get Field's value |
||
202 | * @since 1.1.0 |
||
203 | * @param string $key If value is an array, is used to get array key->value |
||
204 | * @return mixed Field value or false if non-existent |
||
205 | */ |
||
206 | 46 | public function value( $key = '' ) { |
|
209 | |||
210 | /** |
||
211 | * Retrieves metadata/option data |
||
212 | * @since 1.0.1 |
||
213 | * @param string $field_id Meta key/Option array key |
||
214 | * @param array $args Override arguments |
||
215 | * @return mixed Meta/Option value |
||
216 | */ |
||
217 | 107 | public function get_data( $field_id = '', $args = array() ) { |
|
218 | 107 | if ( $field_id ) { |
|
219 | $args['field_id'] = $field_id; |
||
220 | 107 | } else if ( $this->group ) { |
|
221 | $args['field_id'] = $this->group->id(); |
||
222 | } |
||
223 | |||
224 | 107 | $a = $this->data_args( $args ); |
|
225 | |||
226 | /** |
||
227 | * Filter whether to override getting of meta value. |
||
228 | * Returning a non 'cmb2_field_no_override_val' value |
||
229 | * will effectively short-circuit the value retrieval. |
||
230 | * |
||
231 | * @since 2.0.0 |
||
232 | * |
||
233 | * @param mixed $value The value get_metadata() should |
||
234 | * return - a single metadata value, |
||
235 | * or an array of values. |
||
236 | * |
||
237 | * @param int $object_id Object ID. |
||
238 | * |
||
239 | * @param array $args { |
||
240 | * An array of arguments for retrieving data |
||
241 | * |
||
242 | * @type string $type The current object type |
||
243 | * @type int $id The current object ID |
||
244 | * @type string $field_id The ID of the field being requested |
||
245 | * @type bool $repeat Whether current field is repeatable |
||
246 | * @type bool $single Whether current field is a single database row |
||
247 | * } |
||
248 | * |
||
249 | * @param CMB2_Field object $field This field object |
||
250 | */ |
||
251 | 107 | $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); |
|
252 | |||
253 | /** |
||
254 | * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). |
||
255 | * |
||
256 | * The dynamic portion of the hook, $field_id, refers to the current |
||
257 | * field id paramater. Returning a non 'cmb2_field_no_override_val' value |
||
258 | * will effectively short-circuit the value retrieval. |
||
259 | * |
||
260 | * @since 2.0.0 |
||
261 | */ |
||
262 | 107 | $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); |
|
263 | |||
264 | // If no override, get value normally |
||
265 | 107 | if ( 'cmb2_field_no_override_val' === $data ) { |
|
266 | 104 | $data = 'options-page' === $a['type'] |
|
267 | 104 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
|
268 | 104 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
|
269 | 104 | } |
|
270 | |||
271 | 107 | if ( $this->group ) { |
|
272 | |||
273 | $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) |
||
274 | ? $data[ $this->group->index ][ $this->args( '_id' ) ] |
||
275 | : false; |
||
276 | } |
||
277 | |||
278 | 107 | return $data; |
|
279 | } |
||
280 | |||
281 | /** |
||
282 | * Updates metadata/option data |
||
283 | * @since 1.0.1 |
||
284 | * @param mixed $new_value Value to update data with |
||
285 | * @param bool $single Whether data is an array (add_metadata) |
||
286 | */ |
||
287 | 13 | public function update_data( $new_value, $single = true ) { |
|
351 | |||
352 | /** |
||
353 | * Removes/updates metadata/option data |
||
354 | * @since 1.0.1 |
||
355 | * @param string $old Old value |
||
356 | */ |
||
357 | 3 | public function remove_data( $old = '' ) { |
|
413 | |||
414 | /** |
||
415 | * Data variables for get/set data methods |
||
416 | * @since 1.1.0 |
||
417 | * @param array $args Override arguments |
||
418 | * @return array Updated arguments |
||
419 | */ |
||
420 | 107 | public function data_args( $args = array() ) { |
|
430 | |||
431 | /** |
||
432 | * Checks if field has a registered sanitization callback |
||
433 | * @since 1.0.1 |
||
434 | * @param mixed $meta_value Meta value |
||
435 | * @return mixed Possibly sanitized meta value |
||
436 | */ |
||
437 | 15 | public function sanitization_cb( $meta_value ) { |
|
480 | |||
481 | /** |
||
482 | * Process $_POST data to save this field's value |
||
483 | * @since 2.0.3 |
||
484 | * @param array $data_to_save $_POST data to check |
||
485 | * @return array|int|bool Result of save, false on failure |
||
486 | */ |
||
487 | 3 | public function save_field_from_data( array $data_to_save ) { |
|
496 | |||
497 | /** |
||
498 | * Sanitize/store a value to this field |
||
499 | * @since 2.0.0 |
||
500 | * @param array $meta_value Desired value to sanitize/store |
||
501 | * @return array|int|bool Result of save. false on failure |
||
502 | */ |
||
503 | 13 | public function save_field( $meta_value ) { |
|
504 | |||
505 | 13 | $updated = false; |
|
506 | 13 | $action = ''; |
|
507 | 13 | $new_value = $this->sanitization_cb( $meta_value ); |
|
508 | |||
509 | 13 | if ( ! $this->args( 'save_field' ) ) { |
|
510 | |||
511 | // Nothing to see here. |
||
512 | 1 | $action = 'disabled'; |
|
513 | |||
514 | 13 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
515 | |||
516 | 1 | $this->remove_data(); |
|
517 | 1 | $count = 0; |
|
518 | |||
519 | 1 | if ( ! empty( $new_value ) ) { |
|
520 | 1 | foreach ( $new_value as $add_new ) { |
|
521 | 1 | if ( $this->update_data( $add_new, false ) ) { |
|
522 | 1 | $count++; |
|
523 | 1 | } |
|
524 | 1 | } |
|
525 | 1 | } |
|
526 | |||
527 | 1 | $updated = $count ? $count : false; |
|
528 | 1 | $action = 'repeatable'; |
|
529 | |||
530 | 12 | } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { |
|
531 | 10 | $updated = $this->update_data( $new_value ); |
|
532 | 10 | $action = 'updated'; |
|
533 | 11 | } elseif ( CMB2_Utils::isempty( $new_value ) ) { |
|
534 | 2 | $updated = $this->remove_data(); |
|
535 | 2 | $action = 'removed'; |
|
536 | 2 | } |
|
537 | |||
538 | 13 | if ( $updated ) { |
|
539 | 12 | $this->value = $this->get_data(); |
|
540 | 12 | $this->escaped_value = null; |
|
541 | 12 | } |
|
542 | |||
543 | 13 | $field_id = $this->id( true ); |
|
544 | |||
545 | /** |
||
546 | * Hooks after save field action. |
||
547 | * |
||
548 | * @since 2.2.0 |
||
549 | * |
||
550 | * @param string $field_id the current field id paramater. |
||
551 | * @param bool $updated Whether the metadata update action occurred. |
||
552 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
553 | * @param CMB2_Field object $field This field object |
||
554 | */ |
||
555 | 13 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
|
556 | |||
557 | /** |
||
558 | * Hooks after save field action. |
||
559 | * |
||
560 | * The dynamic portion of the hook, $field_id, refers to the |
||
561 | * current field id paramater. |
||
562 | * |
||
563 | * @since 2.2.0 |
||
564 | * |
||
565 | * @param bool $updated Whether the metadata update action occurred. |
||
566 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
567 | * @param CMB2_Field object $field This field object |
||
568 | */ |
||
569 | 12 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
|
570 | |||
571 | 11 | return $updated; |
|
572 | } |
||
573 | |||
574 | /** |
||
575 | * Determine if current type is exempt from escaping |
||
576 | * @since 1.1.0 |
||
577 | * @return bool True if exempt |
||
578 | */ |
||
579 | 46 | public function escaping_exception() { |
|
587 | |||
588 | /** |
||
589 | * Determine if current type cannot be repeatable |
||
590 | * @since 1.1.0 |
||
591 | * @param string $type Field type to check |
||
592 | * @return bool True if type cannot be repeatable |
||
593 | */ |
||
594 | 5 | public function repeatable_exception( $type ) { |
|
623 | |||
624 | /** |
||
625 | * Escape the value before output. Defaults to 'esc_attr()' |
||
626 | * @since 1.0.1 |
||
627 | * @param callable $func Escaping function (if not esc_attr()) |
||
628 | * @param mixed $meta_value Meta value |
||
629 | * @return mixed Final value |
||
630 | */ |
||
631 | 46 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
671 | |||
672 | /** |
||
673 | * Return non-empty value or field default if value IS empty |
||
674 | * @since 2.0.0 |
||
675 | * @param mixed $meta_value Field value |
||
676 | * @return mixed Field value, or default value |
||
677 | */ |
||
678 | 46 | public function val_or_default( $meta_value ) { |
|
681 | |||
682 | /** |
||
683 | * Offset a time value based on timezone |
||
684 | * @since 1.0.0 |
||
685 | * @return string Offset time string |
||
686 | */ |
||
687 | public function field_timezone_offset() { |
||
690 | |||
691 | /** |
||
692 | * Return timezone string |
||
693 | * @since 1.0.0 |
||
694 | * @return string Timezone string |
||
695 | */ |
||
696 | public function field_timezone() { |
||
710 | |||
711 | /** |
||
712 | * Format the timestamp field value based on the field date/time format arg |
||
713 | * @since 2.0.0 |
||
714 | * @param int $meta_value Timestamp |
||
715 | * @param string $format Either date_format or time_format |
||
716 | * @return string Formatted date |
||
717 | */ |
||
718 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
721 | |||
722 | /** |
||
723 | * Return a formatted timestamp for a field |
||
724 | * @since 2.0.0 |
||
725 | * @param string $format Either date_format or time_format |
||
726 | * @param string $meta_value Optional meta value to check |
||
727 | * @return string Formatted date |
||
728 | */ |
||
729 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
741 | |||
742 | /** |
||
743 | * Get timestamp from text date |
||
744 | * @since 2.2.0 |
||
745 | * @param string $value Date value |
||
746 | * @return mixed Unix timestamp representing the date. |
||
747 | */ |
||
748 | public function get_timestamp_from_value( $value ) { |
||
751 | |||
752 | /** |
||
753 | * Get field render callback and Render the field row |
||
754 | * @since 1.0.0 |
||
755 | */ |
||
756 | 10 | public function render_field() { |
|
764 | |||
765 | /** |
||
766 | * Default field render callback |
||
767 | * @since 2.1.1 |
||
768 | */ |
||
769 | 9 | public function render_field_callback() { |
|
813 | |||
814 | /** |
||
815 | * The default label_cb callback (if not a title field) |
||
816 | * |
||
817 | * @since 2.1.1 |
||
818 | * @return string Label html markup |
||
819 | */ |
||
820 | 9 | public function label() { |
|
829 | |||
830 | /** |
||
831 | * Defines the classes for the current CMB2 field row |
||
832 | * |
||
833 | * @since 2.0.0 |
||
834 | * @return string Space concatenated list of classes |
||
835 | */ |
||
836 | 45 | public function row_classes() { |
|
886 | |||
887 | |||
888 | |||
889 | /** |
||
890 | * Get field display callback and render the display value in the column. |
||
891 | * @since 2.2.2 |
||
892 | */ |
||
893 | 33 | public function render_column() { |
|
901 | |||
902 | /** |
||
903 | * Default callback to outputs field value in a display format. |
||
904 | * @since 2.2.2 |
||
905 | */ |
||
906 | 33 | public function display_value_callback() { |
|
949 | |||
950 | /** |
||
951 | * Replaces a hash key - {#} - with the repeatable index |
||
952 | * @since 1.2.0 |
||
953 | * @param string $value Value to update |
||
954 | * @return string Updated value |
||
955 | */ |
||
956 | 2 | public function replace_hash( $value ) { |
|
960 | |||
961 | /** |
||
962 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
963 | * For back-compatibility, falls back to checking the options array. |
||
964 | * |
||
965 | * @since 2.2.2 |
||
966 | * @param string $text_key Key in field's text array |
||
967 | * @param string $fallback Fallback text |
||
968 | * @return string Text |
||
969 | */ |
||
970 | 9 | public function get_string( $text_key, $fallback ) { |
|
994 | |||
995 | /** |
||
996 | * Retrieve options args. Calls options_cb if it exists. |
||
997 | * @since 2.0.0 |
||
998 | * @param string $key Specific option to retrieve |
||
999 | * @return array Array of options |
||
1000 | */ |
||
1001 | 31 | public function options( $key = '' ) { |
|
1026 | |||
1027 | /** |
||
1028 | * Store JS dependencies as part of the field args. |
||
1029 | * @since 2.2.0 |
||
1030 | * @param array $dependencies Dependies to register for this field. |
||
1031 | */ |
||
1032 | 12 | public function add_js_dependencies( $dependencies = array() ) { |
|
1039 | |||
1040 | /** |
||
1041 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1042 | * |
||
1043 | * @since 0.2.2 |
||
1044 | * |
||
1045 | * @return mixed Default field value |
||
1046 | */ |
||
1047 | 34 | public function get_default() { |
|
1060 | |||
1061 | /** |
||
1062 | * Fills in empty field parameters with defaults |
||
1063 | * @since 1.1.0 |
||
1064 | * @param array $args Metabox field config array |
||
1065 | * @param array Modified field config array. |
||
1066 | */ |
||
1067 | 111 | public function _set_field_defaults( $args ) { |
|
1163 | |||
1164 | /** |
||
1165 | * Get default field arguments specific to this CMB2 object. |
||
1166 | * @since 2.2.0 |
||
1167 | * @param array $field_args Metabox field config array. |
||
1168 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1169 | * @return array Array of field arguments. |
||
1170 | */ |
||
1171 | 6 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1182 | |||
1183 | /** |
||
1184 | * Returns a cloned version of this field object with, but with |
||
1185 | * modified/overridden field arguments. |
||
1186 | * |
||
1187 | * @since 2.2.2 |
||
1188 | * @param array $field_args Array of field arguments, or entire array of |
||
1189 | * arguments for CMB2_Field |
||
1190 | * |
||
1191 | * @return CMB2_Field The new CMB2_Field instance. |
||
1192 | */ |
||
1193 | 6 | public function get_field_clone( $field_args ) { |
|
1196 | |||
1197 | /** |
||
1198 | * Returns the CMB2 instance this field is registered to. |
||
1199 | * |
||
1200 | * @since 2.2.2 |
||
1201 | * |
||
1202 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1203 | */ |
||
1204 | 1 | public function get_cmb() { |
|
1211 | |||
1212 | /** |
||
1213 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1214 | * @since 2.2.3 |
||
1215 | * @param array $args Metabox field config array. |
||
1216 | * @param array Modified field config array. |
||
1217 | */ |
||
1218 | 111 | protected function convert_deprecated_params( $args ) { |
|
1261 | |||
1262 | } |
||
1263 |
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.