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 ) { |
|
112 | |||
113 | 109 | if ( ! empty( $args['group_field'] ) ) { |
|
114 | 3 | $this->group = $args['group_field']; |
|
115 | 3 | $this->object_id = $this->group->object_id; |
|
116 | 3 | $this->object_type = $this->group->object_type; |
|
117 | 3 | $this->cmb_id = $this->group->cmb_id; |
|
118 | 3 | } else { |
|
119 | 109 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
|
120 | 109 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
|
121 | |||
122 | 109 | if ( isset( $args['cmb_id'] ) ) { |
|
123 | 40 | $this->cmb_id = $args['cmb_id']; |
|
124 | 40 | } |
|
125 | } |
||
126 | |||
127 | 109 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
|
128 | |||
129 | 109 | if ( $this->object_id ) { |
|
130 | 103 | $this->value = $this->get_data(); |
|
131 | 103 | } |
|
132 | 109 | } |
|
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 ) { |
|
142 | 97 | if ( 'string' === $name ) { |
|
143 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
144 | } |
||
145 | |||
146 | 97 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
|
147 | 97 | return $this->args( $name, $key ); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * Retrieves the field id |
||
152 | * @since 1.1.0 |
||
153 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
154 | * @return string Field id |
||
155 | */ |
||
156 | 107 | public function id( $raw = false ) { |
|
157 | 107 | $id = $raw ? '_id' : 'id'; |
|
158 | 107 | return $this->args( $id ); |
|
159 | } |
||
160 | |||
161 | /** |
||
162 | * Get a field argument |
||
163 | * @since 1.1.0 |
||
164 | * @param string $key Argument to check |
||
165 | * @param string $_key Sub argument to check |
||
166 | * @return mixed Argument value or false if non-existent |
||
167 | */ |
||
168 | 110 | public function args( $key = '', $_key = '' ) { |
|
169 | 110 | $arg = $this->_data( 'args', $key ); |
|
170 | |||
171 | 110 | if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { |
|
172 | |||
173 | 1 | $arg = $this->get_default(); |
|
174 | |||
175 | 110 | } elseif ( $_key ) { |
|
176 | |||
177 | $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; |
||
178 | } |
||
179 | |||
180 | 110 | return $arg; |
|
181 | } |
||
182 | |||
183 | /** |
||
184 | * Retrieve a portion of a field property |
||
185 | * @since 1.1.0 |
||
186 | * @param string $var Field property to check |
||
187 | * @param string $key Field property array key to check |
||
188 | * @return mixed Queried property value or false |
||
189 | */ |
||
190 | 110 | public function _data( $var, $key = '' ) { |
|
191 | 110 | $vars = $this->{$var}; |
|
192 | 110 | if ( $key ) { |
|
193 | 110 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
|
194 | } |
||
195 | 59 | return $vars; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get Field's value |
||
200 | * @since 1.1.0 |
||
201 | * @param string $key If value is an array, is used to get array key->value |
||
202 | * @return mixed Field value or false if non-existent |
||
203 | */ |
||
204 | 46 | public function value( $key = '' ) { |
|
207 | |||
208 | /** |
||
209 | * Retrieves metadata/option data |
||
210 | * @since 1.0.1 |
||
211 | * @param string $field_id Meta key/Option array key |
||
212 | * @param array $args Override arguments |
||
213 | * @return mixed Meta/Option value |
||
214 | */ |
||
215 | 105 | public function get_data( $field_id = '', $args = array() ) { |
|
216 | 105 | if ( $field_id ) { |
|
217 | $args['field_id'] = $field_id; |
||
218 | 105 | } else if ( $this->group ) { |
|
219 | $args['field_id'] = $this->group->id(); |
||
220 | } |
||
221 | |||
222 | 105 | $a = $this->data_args( $args ); |
|
223 | |||
224 | /** |
||
225 | * Filter whether to override getting of meta value. |
||
226 | * Returning a non 'cmb2_field_no_override_val' value |
||
227 | * will effectively short-circuit the value retrieval. |
||
228 | * |
||
229 | * @since 2.0.0 |
||
230 | * |
||
231 | * @param mixed $value The value get_metadata() should |
||
232 | * return - a single metadata value, |
||
233 | * or an array of values. |
||
234 | * |
||
235 | * @param int $object_id Object ID. |
||
236 | * |
||
237 | * @param array $args { |
||
238 | * An array of arguments for retrieving data |
||
239 | * |
||
240 | * @type string $type The current object type |
||
241 | * @type int $id The current object ID |
||
242 | * @type string $field_id The ID of the field being requested |
||
243 | * @type bool $repeat Whether current field is repeatable |
||
244 | * @type bool $single Whether current field is a single database row |
||
245 | * } |
||
246 | * |
||
247 | * @param CMB2_Field object $field This field object |
||
248 | */ |
||
249 | 105 | $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); |
|
250 | |||
251 | /** |
||
252 | * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). |
||
253 | * |
||
254 | * The dynamic portion of the hook, $field_id, refers to the current |
||
255 | * field id paramater. Returning a non 'cmb2_field_no_override_val' value |
||
256 | * will effectively short-circuit the value retrieval. |
||
257 | * |
||
258 | * @since 2.0.0 |
||
259 | */ |
||
260 | 105 | $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); |
|
261 | |||
262 | // If no override, get value normally |
||
263 | 105 | if ( 'cmb2_field_no_override_val' === $data ) { |
|
264 | 102 | $data = 'options-page' === $a['type'] |
|
265 | 102 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
|
266 | 102 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
|
267 | 102 | } |
|
268 | |||
269 | 105 | if ( $this->group ) { |
|
270 | |||
271 | $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) |
||
272 | ? $data[ $this->group->index ][ $this->args( '_id' ) ] |
||
273 | : false; |
||
274 | } |
||
275 | |||
276 | 105 | return $data; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * Updates metadata/option data |
||
281 | * @since 1.0.1 |
||
282 | * @param mixed $new_value Value to update data with |
||
283 | * @param bool $single Whether data is an array (add_metadata) |
||
284 | */ |
||
285 | 11 | public function update_data( $new_value, $single = true ) { |
|
349 | |||
350 | /** |
||
351 | * Removes/updates metadata/option data |
||
352 | * @since 1.0.1 |
||
353 | * @param string $old Old value |
||
354 | */ |
||
355 | 3 | public function remove_data( $old = '' ) { |
|
411 | |||
412 | /** |
||
413 | * Data variables for get/set data methods |
||
414 | * @since 1.1.0 |
||
415 | * @param array $args Override arguments |
||
416 | * @return array Updated arguments |
||
417 | */ |
||
418 | 105 | public function data_args( $args = array() ) { |
|
428 | |||
429 | /** |
||
430 | * Checks if field has a registered sanitization callback |
||
431 | * @since 1.0.1 |
||
432 | * @param mixed $meta_value Meta value |
||
433 | * @return mixed Possibly sanitized meta value |
||
434 | */ |
||
435 | 13 | public function sanitization_cb( $meta_value ) { |
|
478 | |||
479 | /** |
||
480 | * Process $_POST data to save this field's value |
||
481 | * @since 2.0.3 |
||
482 | * @param array $data_to_save $_POST data to check |
||
483 | * @return array|int|bool Result of save, false on failure |
||
484 | */ |
||
485 | 2 | public function save_field_from_data( array $data_to_save ) { |
|
494 | |||
495 | /** |
||
496 | * Sanitize/store a value to this field |
||
497 | * @since 2.0.0 |
||
498 | * @param array $meta_value Desired value to sanitize/store |
||
499 | * @return array|int|bool Result of save. false on failure |
||
500 | */ |
||
501 | 12 | public function save_field( $meta_value ) { |
|
571 | |||
572 | /** |
||
573 | * Determine if current type is exempt from escaping |
||
574 | * @since 1.1.0 |
||
575 | * @return bool True if exempt |
||
576 | */ |
||
577 | 46 | public function escaping_exception() { |
|
585 | |||
586 | /** |
||
587 | * Determine if current type cannot be repeatable |
||
588 | * @since 1.1.0 |
||
589 | * @param string $type Field type to check |
||
590 | * @return bool True if type cannot be repeatable |
||
591 | */ |
||
592 | 4 | public function repeatable_exception( $type ) { |
|
622 | |||
623 | /** |
||
624 | * Escape the value before output. Defaults to 'esc_attr()' |
||
625 | * @since 1.0.1 |
||
626 | * @param callable $func Escaping function (if not esc_attr()) |
||
627 | * @param mixed $meta_value Meta value |
||
628 | * @return mixed Final value |
||
629 | */ |
||
630 | 46 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
670 | |||
671 | /** |
||
672 | * Return non-empty value or field default if value IS empty |
||
673 | * @since 2.0.0 |
||
674 | * @param mixed $meta_value Field value |
||
675 | * @return mixed Field value, or default value |
||
676 | */ |
||
677 | 46 | public function val_or_default( $meta_value ) { |
|
680 | |||
681 | /** |
||
682 | * Offset a time value based on timezone |
||
683 | * @since 1.0.0 |
||
684 | * @return string Offset time string |
||
685 | */ |
||
686 | public function field_timezone_offset() { |
||
689 | |||
690 | /** |
||
691 | * Return timezone string |
||
692 | * @since 1.0.0 |
||
693 | * @return string Timezone string |
||
694 | */ |
||
695 | public function field_timezone() { |
||
709 | |||
710 | /** |
||
711 | * Format the timestamp field value based on the field date/time format arg |
||
712 | * @since 2.0.0 |
||
713 | * @param int $meta_value Timestamp |
||
714 | * @param string $format Either date_format or time_format |
||
715 | * @return string Formatted date |
||
716 | */ |
||
717 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
720 | |||
721 | /** |
||
722 | * Return a formatted timestamp for a field |
||
723 | * @since 2.0.0 |
||
724 | * @param string $format Either date_format or time_format |
||
725 | * @param string $meta_value Optional meta value to check |
||
726 | * @return string Formatted date |
||
727 | */ |
||
728 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
740 | |||
741 | /** |
||
742 | * Get timestamp from text date |
||
743 | * @since 2.2.0 |
||
744 | * @param string $value Date value |
||
745 | * @return mixed Unix timestamp representing the date. |
||
746 | */ |
||
747 | public function get_timestamp_from_value( $value ) { |
||
750 | |||
751 | /** |
||
752 | * Get field render callback and Render the field row |
||
753 | * @since 1.0.0 |
||
754 | */ |
||
755 | 10 | public function render_field() { |
|
763 | |||
764 | /** |
||
765 | * Default field render callback |
||
766 | * @since 2.1.1 |
||
767 | */ |
||
768 | 9 | public function render_field_callback() { |
|
812 | |||
813 | /** |
||
814 | * The default label_cb callback (if not a title field) |
||
815 | * |
||
816 | * @since 2.1.1 |
||
817 | * @return string Label html markup |
||
818 | */ |
||
819 | 9 | public function label() { |
|
828 | |||
829 | /** |
||
830 | * Defines the classes for the current CMB2 field row |
||
831 | * |
||
832 | * @since 2.0.0 |
||
833 | * @return string Space concatenated list of classes |
||
834 | */ |
||
835 | 45 | public function row_classes() { |
|
836 | |||
837 | 45 | $classes = array(); |
|
838 | |||
839 | /** |
||
840 | * By default, 'text_url' and 'text' fields get table-like styling |
||
841 | * |
||
842 | * @since 2.0.0 |
||
843 | * |
||
844 | * @param array $field_types The types of fields which should get the 'table-layout' class |
||
845 | */ |
||
846 | 45 | $repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array( |
|
847 | 45 | 'text_url', 'text', |
|
848 | 45 | ) ); |
|
849 | |||
850 | $conditional_classes = array( |
||
851 | 45 | 'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true, |
|
852 | 45 | 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) ) => true, |
|
853 | 45 | 'cmb-repeat' => $this->args( 'repeatable' ), |
|
854 | 45 | 'cmb-repeat-group-field' => $this->group, |
|
855 | 45 | 'cmb-inline' => $this->args( 'inline' ), |
|
856 | 45 | 'table-layout' => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ), |
|
857 | 45 | ); |
|
858 | |||
859 | 45 | foreach ( $conditional_classes as $class => $condition ) { |
|
860 | 45 | if ( $condition ) { |
|
861 | 45 | $classes[] = $class; |
|
862 | 45 | } |
|
863 | 45 | } |
|
864 | |||
865 | 45 | if ( $added_classes = $this->args( 'classes' ) ) { |
|
866 | 1 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
|
867 | 45 | } elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
|
868 | 2 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
|
869 | 2 | } |
|
870 | |||
871 | 45 | if ( $added_classes ) { |
|
872 | 3 | $classes[] = esc_attr( $added_classes ); |
|
873 | 3 | } |
|
874 | |||
875 | /** |
||
876 | * Globally filter row classes |
||
877 | * |
||
878 | * @since 2.0.0 |
||
879 | * |
||
880 | * @param string $classes Space-separated list of row classes |
||
881 | * @param CMB2_Field object $field This field object |
||
882 | */ |
||
883 | 45 | return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this ); |
|
884 | } |
||
885 | |||
886 | |||
887 | |||
888 | /** |
||
889 | * Get field display callback and render the display value in the column. |
||
890 | * @since 2.2.2 |
||
891 | */ |
||
892 | 33 | public function render_column() { |
|
900 | |||
901 | /** |
||
902 | * Default callback to outputs field value in a display format. |
||
903 | * @since 2.2.2 |
||
904 | */ |
||
905 | 33 | public function display_value_callback() { |
|
948 | |||
949 | /** |
||
950 | * Replaces a hash key - {#} - with the repeatable index |
||
951 | * @since 1.2.0 |
||
952 | * @param string $value Value to update |
||
953 | * @return string Updated value |
||
954 | */ |
||
955 | 2 | public function replace_hash( $value ) { |
|
959 | |||
960 | /** |
||
961 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
962 | * For back-compatibility, falls back to checking the options array. |
||
963 | * |
||
964 | * @since 2.2.2 |
||
965 | * @param string $text_key Key in field's text array |
||
966 | * @param string $fallback Fallback text |
||
967 | * @return string Text |
||
968 | */ |
||
969 | 8 | public function get_string( $text_key, $fallback ) { |
|
993 | |||
994 | /** |
||
995 | * Retrieve options args. Calls options_cb if it exists. |
||
996 | * @since 2.0.0 |
||
997 | * @param string $key Specific option to retrieve |
||
998 | * @return array Array of options |
||
999 | */ |
||
1000 | 31 | public function options( $key = '' ) { |
|
1025 | |||
1026 | /** |
||
1027 | * Store JS dependencies as part of the field args. |
||
1028 | * @since 2.2.0 |
||
1029 | * @param array $dependencies Dependies to register for this field. |
||
1030 | */ |
||
1031 | 12 | public function add_js_dependencies( $dependencies = array() ) { |
|
1038 | |||
1039 | /** |
||
1040 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1041 | * |
||
1042 | * @since 0.2.2 |
||
1043 | * |
||
1044 | * @return mixed Default field value |
||
1045 | */ |
||
1046 | 33 | public function get_default() { |
|
1059 | |||
1060 | /** |
||
1061 | * Fills in empty field parameters with defaults |
||
1062 | * @since 1.1.0 |
||
1063 | * @param array $args Metabox field config array |
||
1064 | * @param array Modified field config array. |
||
1065 | */ |
||
1066 | 109 | public function _set_field_defaults( $args ) { |
|
1165 | |||
1166 | /** |
||
1167 | * Get default field arguments specific to this CMB2 object. |
||
1168 | * @since 2.2.0 |
||
1169 | * @param array $field_args Metabox field config array. |
||
1170 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1171 | * @return array Array of field arguments. |
||
1172 | */ |
||
1173 | 5 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1184 | |||
1185 | /** |
||
1186 | * Returns a cloned version of this field object with, but with |
||
1187 | * modified/overridden field arguments. |
||
1188 | * |
||
1189 | * @since 2.2.2 |
||
1190 | * @param array $field_args Array of field arguments, or entire array of |
||
1191 | * arguments for CMB2_Field |
||
1192 | * |
||
1193 | * @return CMB2_Field The new CMB2_Field instance. |
||
1194 | */ |
||
1195 | 5 | public function get_field_clone( $field_args ) { |
|
1198 | |||
1199 | /** |
||
1200 | * Returns the CMB2 instance this field is registered to. |
||
1201 | * |
||
1202 | * @since 2.2.2 |
||
1203 | * |
||
1204 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1205 | */ |
||
1206 | 1 | public function get_cmb() { |
|
1213 | |||
1214 | /** |
||
1215 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1216 | * @since 2.2.3 |
||
1217 | * @param array $args Metabox field config array. |
||
1218 | * @param array Modified field config array. |
||
1219 | */ |
||
1220 | 109 | protected function convert_deprecated_params( $args ) { |
|
1263 | |||
1264 | } |
||
1265 |
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.