Complex classes like CMB2_Sanitize 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_Sanitize, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class CMB2_Sanitize { |
||
|
|||
16 | |||
17 | /** |
||
18 | * A CMB field object |
||
19 | * |
||
20 | * @var CMB2_Field object |
||
21 | */ |
||
22 | public $field; |
||
23 | |||
24 | /** |
||
25 | * Field's value |
||
26 | * |
||
27 | * @var mixed |
||
28 | */ |
||
29 | public $value; |
||
30 | |||
31 | /** |
||
32 | * Setup our class vars |
||
33 | * |
||
34 | * @since 1.1.0 |
||
35 | * @param CMB2_Field $field A CMB2 field object |
||
36 | * @param mixed $value Field value |
||
37 | */ |
||
38 | 15 | public function __construct( CMB2_Field $field, $value ) { |
|
42 | |||
43 | /** |
||
44 | * Catchall method if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method |
||
45 | * |
||
46 | * @since 1.0.0 |
||
47 | * @param string $name Non-existent method name |
||
48 | * @param array $arguments All arguments passed to the method |
||
49 | */ |
||
50 | 12 | public function __call( $name, $arguments ) { |
|
53 | |||
54 | /** |
||
55 | * Default fallback sanitization method. Applies filters. |
||
56 | * |
||
57 | * @since 1.0.2 |
||
58 | */ |
||
59 | 12 | public function default_sanitization() { |
|
107 | |||
108 | /** |
||
109 | * Default sanitization method, sanitize_text_field. Checks if value is array. |
||
110 | * |
||
111 | * @since 2.2.4 |
||
112 | * @return mixed Sanitized value. |
||
113 | */ |
||
114 | 11 | protected function _default_sanitization() { |
|
118 | |||
119 | /** |
||
120 | * Sets the object terms to the object (if not options-page) and optionally returns the sanitized term values. |
||
121 | * |
||
122 | * @since 2.2.4 |
||
123 | * @return mixed Blank value, or sanitized term values if "cmb2_return_taxonomy_values_{$cmb_id}" is true. |
||
124 | */ |
||
125 | 1 | protected function _taxonomy() { |
|
162 | |||
163 | /** |
||
164 | * Simple checkbox validation |
||
165 | * |
||
166 | * @since 1.0.1 |
||
167 | * @return string|false 'on' or false |
||
168 | */ |
||
169 | public function checkbox() { |
||
172 | |||
173 | /** |
||
174 | * Validate url in a meta value |
||
175 | * |
||
176 | * @since 1.0.1 |
||
177 | * @return string Empty string or escaped url |
||
178 | */ |
||
179 | 2 | public function text_url() { |
|
192 | |||
193 | public function colorpicker() { |
||
208 | |||
209 | /** |
||
210 | * Validate email in a meta value |
||
211 | * |
||
212 | * @since 1.0.1 |
||
213 | * @return string Empty string or sanitized email |
||
214 | */ |
||
215 | public function text_email() { |
||
229 | |||
230 | /** |
||
231 | * Validate money in a meta value |
||
232 | * |
||
233 | * @since 1.0.1 |
||
234 | * @return string Empty string or sanitized money value |
||
235 | */ |
||
236 | 1 | public function text_money() { |
|
259 | |||
260 | /** |
||
261 | * Converts text date to timestamp |
||
262 | * |
||
263 | * @since 1.0.2 |
||
264 | * @return string Timestring |
||
265 | */ |
||
266 | public function text_date_timestamp() { |
||
271 | |||
272 | /** |
||
273 | * Datetime to timestamp |
||
274 | * |
||
275 | * @since 1.0.1 |
||
276 | * @return string|array Timestring |
||
277 | */ |
||
278 | public function text_datetime_timestamp( $repeat = false ) { |
||
300 | |||
301 | /** |
||
302 | * Datetime to timestamp with timezone |
||
303 | * |
||
304 | * @since 1.0.1 |
||
305 | * @return string Timestring |
||
306 | */ |
||
307 | 2 | public function text_datetime_timestamp_timezone( $repeat = false ) { |
|
386 | |||
387 | /** |
||
388 | * Sanitize textareas and wysiwyg fields |
||
389 | * |
||
390 | * @since 1.0.1 |
||
391 | * @return string Sanitized data |
||
392 | */ |
||
393 | 2 | public function textarea() { |
|
396 | |||
397 | /** |
||
398 | * Sanitize code textareas |
||
399 | * |
||
400 | * @since 1.0.2 |
||
401 | * @return string Sanitized data |
||
402 | */ |
||
403 | public function textarea_code( $repeat = false ) { |
||
411 | |||
412 | /** |
||
413 | * Handles saving of attachment post ID and sanitizing file url |
||
414 | * |
||
415 | * @since 1.1.0 |
||
416 | * @return string Sanitized url |
||
417 | */ |
||
418 | 2 | public function file() { |
|
431 | |||
432 | /** |
||
433 | * Gets the values for the `file` field type from the data being saved. |
||
434 | * |
||
435 | * @since 2.2.0 |
||
436 | */ |
||
437 | 2 | public function _get_group_file_value_array( $id_key ) { |
|
458 | |||
459 | /** |
||
460 | * Peforms saving of `file` attachement's ID |
||
461 | * |
||
462 | * @since 1.1.0 |
||
463 | */ |
||
464 | public function _save_file_id_value( $file_id_key ) { |
||
479 | |||
480 | /** |
||
481 | * Peforms saving of `text_datetime_timestamp_timezone` utc timestamp |
||
482 | * |
||
483 | * @since 2.2.0 |
||
484 | */ |
||
485 | 1 | public function _save_utc_value( $utc_key, $utc_stamp ) { |
|
488 | |||
489 | /** |
||
490 | * Returns a new, supporting, CMB2_Field object based on a new field id. |
||
491 | * |
||
492 | * @since 2.2.0 |
||
493 | */ |
||
494 | 1 | public function _new_supporting_field( $new_field_id ) { |
|
500 | |||
501 | /** |
||
502 | * If repeating, loop through and re-apply sanitization method |
||
503 | * |
||
504 | * @since 1.1.0 |
||
505 | * @param string $method Class method |
||
506 | * @param bool $repeat Whether repeating or not |
||
507 | * @return mixed Sanitized value |
||
508 | */ |
||
509 | 2 | public function _check_repeat( $method, $repeat ) { |
|
530 | |||
531 | /** |
||
532 | * Determine if passed value is an empty array |
||
533 | * |
||
534 | * @since 2.0.6 |
||
535 | * @param mixed $to_check Value to check |
||
536 | * @return boolean Whether value is an array that's empty |
||
537 | */ |
||
538 | 12 | public function _is_empty_array( $to_check ) { |
|
545 | |||
546 | } |
||
547 |
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.