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 | * |
||
22 | * @var string |
||
23 | * @since 2.2.3 |
||
24 | */ |
||
25 | protected $properties_name = 'args'; |
||
26 | |||
27 | /** |
||
28 | * Field arguments |
||
29 | * |
||
30 | * @var mixed |
||
31 | * @since 1.1.0 |
||
32 | */ |
||
33 | public $args = array(); |
||
34 | |||
35 | /** |
||
36 | * Field group object or false (if no group) |
||
37 | * |
||
38 | * @var mixed |
||
39 | * @since 1.1.0 |
||
40 | */ |
||
41 | public $group = false; |
||
42 | |||
43 | /** |
||
44 | * Field meta value |
||
45 | * |
||
46 | * @var mixed |
||
47 | * @since 1.1.0 |
||
48 | */ |
||
49 | public $value = null; |
||
50 | |||
51 | /** |
||
52 | * Field meta value |
||
53 | * |
||
54 | * @var mixed |
||
55 | * @since 1.1.0 |
||
56 | */ |
||
57 | public $escaped_value = null; |
||
58 | |||
59 | /** |
||
60 | * Grouped Field's current numeric index during the save process |
||
61 | * |
||
62 | * @var mixed |
||
63 | * @since 2.0.0 |
||
64 | */ |
||
65 | public $index = 0; |
||
66 | |||
67 | /** |
||
68 | * Array of field options |
||
69 | * |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $field_options = array(); |
||
74 | |||
75 | /** |
||
76 | * Array of provided field text strings |
||
77 | * |
||
78 | * @var array |
||
79 | * @since 2.0.0 |
||
80 | */ |
||
81 | protected $strings; |
||
82 | |||
83 | /** |
||
84 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
85 | * |
||
86 | * @var string |
||
87 | * @since 2.2.2 |
||
88 | */ |
||
89 | public $render_context = 'edit'; |
||
90 | |||
91 | /** |
||
92 | * All CMB2_Field callable field arguments. |
||
93 | * Can be used to determine if a field argument is callable. |
||
94 | * |
||
95 | * @var array |
||
96 | */ |
||
97 | public static $callable_fields = array( |
||
98 | 'default_cb', |
||
99 | 'classes_cb', |
||
100 | 'options_cb', |
||
101 | 'text_cb', |
||
102 | 'label_cb', |
||
103 | 'render_row_cb', |
||
104 | 'display_cb', |
||
105 | 'before_group', |
||
106 | 'before_group_row', |
||
107 | 'before_row', |
||
108 | 'before', |
||
109 | 'before_field', |
||
110 | 'after_field', |
||
111 | 'after', |
||
112 | 'after_row', |
||
113 | 'after_group_row', |
||
114 | 'after_group', |
||
115 | ); |
||
116 | |||
117 | /** |
||
118 | * Constructs our field object |
||
119 | * |
||
120 | * @since 1.1.0 |
||
121 | * @param array $args Field arguments |
||
122 | */ |
||
123 | 111 | public function __construct( $args ) { |
|
124 | |||
125 | 111 | if ( ! empty( $args['group_field'] ) ) { |
|
126 | 4 | $this->group = $args['group_field']; |
|
127 | 4 | $this->object_id = $this->group->object_id; |
|
128 | 4 | $this->object_type = $this->group->object_type; |
|
129 | 4 | $this->cmb_id = $this->group->cmb_id; |
|
130 | 4 | } else { |
|
131 | 111 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
|
132 | 111 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
|
133 | |||
134 | 111 | if ( isset( $args['cmb_id'] ) ) { |
|
135 | 43 | $this->cmb_id = $args['cmb_id']; |
|
136 | 43 | } |
|
137 | } |
||
138 | |||
139 | 111 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
|
140 | |||
141 | 111 | if ( $this->object_id ) { |
|
142 | 103 | $this->value = $this->get_data(); |
|
143 | 103 | } |
|
144 | 111 | } |
|
145 | |||
146 | /** |
||
147 | * Non-existent methods fallback to checking for field arguments of the same name |
||
148 | * |
||
149 | * @since 1.1.0 |
||
150 | * @param string $name Method name |
||
151 | * @param array $arguments Array of passed-in arguments |
||
152 | * @return mixed Value of field argument |
||
153 | */ |
||
154 | 100 | public function __call( $name, $arguments ) { |
|
155 | 100 | if ( 'string' === $name ) { |
|
156 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
157 | } |
||
158 | |||
159 | 100 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
|
160 | 100 | return $this->args( $name, $key ); |
|
161 | 1 | } |
|
162 | |||
163 | /** |
||
164 | * Retrieves the field id |
||
165 | * |
||
166 | * @since 1.1.0 |
||
167 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
168 | * @return string Field id |
||
169 | */ |
||
170 | 109 | public function id( $raw = false ) { |
|
171 | 109 | $id = $raw ? '_id' : 'id'; |
|
172 | 109 | return $this->args( $id ); |
|
173 | } |
||
174 | |||
175 | /** |
||
176 | * Get a field argument |
||
177 | * |
||
178 | * @since 1.1.0 |
||
179 | * @param string $key Argument to check |
||
180 | * @param string $_key Sub argument to check |
||
181 | * @return mixed Argument value or false if non-existent |
||
182 | */ |
||
183 | 112 | public function args( $key = '', $_key = '' ) { |
|
184 | 112 | $arg = $this->_data( 'args', $key ); |
|
185 | |||
186 | 112 | if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) { |
|
187 | |||
188 | 1 | $arg = $this->get_default(); |
|
189 | |||
190 | 112 | } elseif ( $_key ) { |
|
191 | |||
192 | $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false; |
||
193 | } |
||
194 | |||
195 | 112 | return $arg; |
|
196 | } |
||
197 | |||
198 | /** |
||
199 | * Retrieve a portion of a field property |
||
200 | * |
||
201 | * @since 1.1.0 |
||
202 | * @param string $var Field property to check |
||
203 | * @param string $key Field property array key to check |
||
204 | * @return mixed Queried property value or false |
||
205 | */ |
||
206 | 112 | public function _data( $var, $key = '' ) { |
|
207 | 112 | $vars = $this->{$var}; |
|
208 | 112 | if ( $key ) { |
|
209 | 112 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
|
210 | } |
||
211 | 62 | return $vars; |
|
212 | } |
||
213 | |||
214 | /** |
||
215 | * Get Field's value |
||
216 | * |
||
217 | * @since 1.1.0 |
||
218 | * @param string $key If value is an array, is used to get array key->value |
||
219 | * @return mixed Field value or false if non-existent |
||
220 | */ |
||
221 | 47 | public function value( $key = '' ) { |
|
224 | |||
225 | /** |
||
226 | * Retrieves metadata/option data |
||
227 | * |
||
228 | * @since 1.0.1 |
||
229 | * @param string $field_id Meta key/Option array key |
||
230 | * @param array $args Override arguments |
||
231 | * @return mixed Meta/Option value |
||
232 | */ |
||
233 | 107 | public function get_data( $field_id = '', $args = array() ) { |
|
296 | |||
297 | /** |
||
298 | * Updates metadata/option data |
||
299 | * |
||
300 | * @since 1.0.1 |
||
301 | * @param mixed $new_value Value to update data with |
||
302 | * @param bool $single Whether data is an array (add_metadata) |
||
303 | */ |
||
304 | 13 | public function update_data( $new_value, $single = true ) { |
|
368 | |||
369 | /** |
||
370 | * Removes/updates metadata/option data |
||
371 | * |
||
372 | * @since 1.0.1 |
||
373 | * @param string $old Old value |
||
374 | */ |
||
375 | 3 | public function remove_data( $old = '' ) { |
|
430 | |||
431 | /** |
||
432 | * Data variables for get/set data methods |
||
433 | * |
||
434 | * @since 1.1.0 |
||
435 | * @param array $args Override arguments |
||
436 | * @return array Updated arguments |
||
437 | */ |
||
438 | 107 | public function data_args( $args = array() ) { |
|
448 | |||
449 | /** |
||
450 | * Checks if field has a registered sanitization callback |
||
451 | * |
||
452 | * @since 1.0.1 |
||
453 | * @param mixed $meta_value Meta value |
||
454 | * @return mixed Possibly sanitized meta value |
||
455 | */ |
||
456 | 15 | public function sanitization_cb( $meta_value ) { |
|
499 | |||
500 | /** |
||
501 | * Process $_POST data to save this field's value |
||
502 | * |
||
503 | * @since 2.0.3 |
||
504 | * @param array $data_to_save $_POST data to check |
||
505 | * @return array|int|bool Result of save, false on failure |
||
506 | */ |
||
507 | 3 | public function save_field_from_data( array $data_to_save ) { |
|
516 | |||
517 | /** |
||
518 | * Sanitize/store a value to this field |
||
519 | * |
||
520 | * @since 2.0.0 |
||
521 | * @param array $meta_value Desired value to sanitize/store |
||
522 | * @return array|int|bool Result of save. false on failure |
||
523 | */ |
||
524 | 13 | public function save_field( $meta_value ) { |
|
594 | |||
595 | /** |
||
596 | * Determine if current type is exempt from escaping |
||
597 | * |
||
598 | * @since 1.1.0 |
||
599 | * @return bool True if exempt |
||
600 | */ |
||
601 | 47 | public function escaping_exception() { |
|
609 | |||
610 | /** |
||
611 | * Determine if current type cannot be repeatable |
||
612 | * |
||
613 | * @since 1.1.0 |
||
614 | * @param string $type Field type to check |
||
615 | * @return bool True if type cannot be repeatable |
||
616 | */ |
||
617 | 5 | public function repeatable_exception( $type ) { |
|
646 | |||
647 | /** |
||
648 | * Escape the value before output. Defaults to 'esc_attr()' |
||
649 | * |
||
650 | * @since 1.0.1 |
||
651 | * @param callable $func Escaping function (if not esc_attr()) |
||
652 | * @param mixed $meta_value Meta value |
||
653 | * @return mixed Final value |
||
654 | */ |
||
655 | 47 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
695 | |||
696 | /** |
||
697 | * Return non-empty value or field default if value IS empty |
||
698 | * |
||
699 | * @since 2.0.0 |
||
700 | * @param mixed $meta_value Field value |
||
701 | * @return mixed Field value, or default value |
||
702 | */ |
||
703 | 47 | public function val_or_default( $meta_value ) { |
|
706 | |||
707 | /** |
||
708 | * Offset a time value based on timezone |
||
709 | * |
||
710 | * @since 1.0.0 |
||
711 | * @return string Offset time string |
||
712 | */ |
||
713 | public function field_timezone_offset() { |
||
716 | |||
717 | /** |
||
718 | * Return timezone string |
||
719 | * |
||
720 | * @since 1.0.0 |
||
721 | * @return string Timezone string |
||
722 | */ |
||
723 | public function field_timezone() { |
||
736 | |||
737 | /** |
||
738 | * Format the timestamp field value based on the field date/time format arg |
||
739 | * |
||
740 | * @since 2.0.0 |
||
741 | * @param int $meta_value Timestamp |
||
742 | * @param string $format Either date_format or time_format |
||
743 | * @return string Formatted date |
||
744 | */ |
||
745 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
748 | |||
749 | /** |
||
750 | * Return a formatted timestamp for a field |
||
751 | * |
||
752 | * @since 2.0.0 |
||
753 | * @param string $format Either date_format or time_format |
||
754 | * @param string $meta_value Optional meta value to check |
||
755 | * @return string Formatted date |
||
756 | */ |
||
757 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
776 | |||
777 | /** |
||
778 | * Get timestamp from text date |
||
779 | * |
||
780 | * @since 2.2.0 |
||
781 | * @param string $value Date value |
||
782 | * @return mixed Unix timestamp representing the date. |
||
783 | */ |
||
784 | public function get_timestamp_from_value( $value ) { |
||
787 | |||
788 | /** |
||
789 | * Get field render callback and Render the field row |
||
790 | * |
||
791 | * @since 1.0.0 |
||
792 | */ |
||
793 | 10 | public function render_field() { |
|
801 | |||
802 | /** |
||
803 | * Default field render callback |
||
804 | * |
||
805 | * @since 2.1.1 |
||
806 | */ |
||
807 | 9 | public function render_field_callback() { |
|
851 | |||
852 | /** |
||
853 | * The default label_cb callback (if not a title field) |
||
854 | * |
||
855 | * @since 2.1.1 |
||
856 | * @return string Label html markup |
||
857 | */ |
||
858 | 9 | public function label() { |
|
867 | |||
868 | /** |
||
869 | * Defines the classes for the current CMB2 field row |
||
870 | * |
||
871 | * @since 2.0.0 |
||
872 | * @return string Space concatenated list of classes |
||
873 | */ |
||
874 | 45 | public function row_classes() { |
|
925 | |||
926 | |||
927 | |||
928 | /** |
||
929 | * Get field display callback and render the display value in the column. |
||
930 | * |
||
931 | * @since 2.2.2 |
||
932 | */ |
||
933 | 33 | public function render_column() { |
|
941 | |||
942 | /** |
||
943 | * Default callback to outputs field value in a display format. |
||
944 | * |
||
945 | * @since 2.2.2 |
||
946 | */ |
||
947 | 33 | public function display_value_callback() { |
|
990 | |||
991 | /** |
||
992 | * Replaces a hash key - {#} - with the repeatable index |
||
993 | * |
||
994 | * @since 1.2.0 |
||
995 | * @param string $value Value to update |
||
996 | * @return string Updated value |
||
997 | */ |
||
998 | 2 | public function replace_hash( $value ) { |
|
1002 | |||
1003 | /** |
||
1004 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
1005 | * For back-compatibility, falls back to checking the options array. |
||
1006 | * |
||
1007 | * @since 2.2.2 |
||
1008 | * @param string $text_key Key in field's text array |
||
1009 | * @param string $fallback Fallback text |
||
1010 | * @return string Text |
||
1011 | */ |
||
1012 | 10 | public function get_string( $text_key, $fallback ) { |
|
1036 | |||
1037 | /** |
||
1038 | * Retrieve options args. Calls options_cb if it exists. |
||
1039 | * |
||
1040 | * @since 2.0.0 |
||
1041 | * @param string $key Specific option to retrieve |
||
1042 | * @return array Array of options |
||
1043 | */ |
||
1044 | 32 | public function options( $key = '' ) { |
|
1069 | |||
1070 | /** |
||
1071 | * Store JS dependencies as part of the field args. |
||
1072 | * |
||
1073 | * @since 2.2.0 |
||
1074 | * @param array $dependencies Dependies to register for this field. |
||
1075 | */ |
||
1076 | 13 | public function add_js_dependencies( $dependencies = array() ) { |
|
1083 | |||
1084 | /** |
||
1085 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1086 | * |
||
1087 | * @since 0.2.2 |
||
1088 | * |
||
1089 | * @return mixed Default field value |
||
1090 | */ |
||
1091 | 35 | public function get_default() { |
|
1104 | |||
1105 | /** |
||
1106 | * Fills in empty field parameters with defaults |
||
1107 | * |
||
1108 | * @since 1.1.0 |
||
1109 | * @param array $args Metabox field config array |
||
1110 | * @param array Modified field config array. |
||
1111 | */ |
||
1112 | 111 | public function _set_field_defaults( $args ) { |
|
1207 | |||
1208 | /** |
||
1209 | * Get default field arguments specific to this CMB2 object. |
||
1210 | * |
||
1211 | * @since 2.2.0 |
||
1212 | * @param array $field_args Metabox field config array. |
||
1213 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1214 | * @return array Array of field arguments. |
||
1215 | */ |
||
1216 | 7 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1227 | |||
1228 | /** |
||
1229 | * Returns a cloned version of this field object with, but with |
||
1230 | * modified/overridden field arguments. |
||
1231 | * |
||
1232 | * @since 2.2.2 |
||
1233 | * @param array $field_args Array of field arguments, or entire array of |
||
1234 | * arguments for CMB2_Field |
||
1235 | * |
||
1236 | * @return CMB2_Field The new CMB2_Field instance. |
||
1237 | */ |
||
1238 | 7 | public function get_field_clone( $field_args ) { |
|
1241 | |||
1242 | /** |
||
1243 | * Returns the CMB2 instance this field is registered to. |
||
1244 | * |
||
1245 | * @since 2.2.2 |
||
1246 | * |
||
1247 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1248 | */ |
||
1249 | 1 | public function get_cmb() { |
|
1256 | |||
1257 | /** |
||
1258 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1259 | * |
||
1260 | * @since 2.2.3 |
||
1261 | * @param array $args Metabox field config array. |
||
1262 | * @param array Modified field config array. |
||
1263 | */ |
||
1264 | 111 | protected function convert_deprecated_params( $args ) { |
|
1305 | |||
1306 | } |
||
1307 |
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.