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 | * @var CMB2_Field object |
||
| 20 | */ |
||
| 21 | public $field; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Field's value |
||
| 25 | * @var mixed |
||
| 26 | */ |
||
| 27 | public $value; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup our class vars |
||
| 31 | * @since 1.1.0 |
||
| 32 | * @param CMB2_Field $field A CMB2 field object |
||
| 33 | * @param mixed $value Field value |
||
| 34 | */ |
||
| 35 | 9 | public function __construct( CMB2_Field $field, $value ) { |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Catchall method if field's 'sanitization_cb' is NOT defined, or field type does not have a corresponding validation method |
||
| 42 | * @since 1.0.0 |
||
| 43 | * @param string $name Non-existent method name |
||
| 44 | * @param array $arguments All arguments passed to the method |
||
| 45 | */ |
||
| 46 | 8 | public function __call( $name, $arguments ) { |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Default fallback sanitization method. Applies filters. |
||
| 52 | * @since 1.0.2 |
||
| 53 | */ |
||
| 54 | 8 | public function default_sanitization() { |
|
| 106 | |||
| 107 | /** |
||
| 108 | * Simple checkbox validation |
||
| 109 | * @since 1.0.1 |
||
| 110 | * @return string|false 'on' or false |
||
| 111 | */ |
||
| 112 | public function checkbox() { |
||
| 115 | |||
| 116 | 1 | /** |
|
| 117 | 1 | * Validate url in a meta value |
|
| 118 | * @since 1.0.1 |
||
| 119 | 1 | * @return string Empty string or escaped url |
|
| 120 | */ |
||
| 121 | public function text_url() { |
||
| 122 | $protocols = $this->field->args( 'protocols' ); |
||
| 123 | // for repeatable |
||
| 124 | 1 | if ( is_array( $this->value ) ) { |
|
| 125 | foreach ( $this->value as $key => $val ) { |
||
| 126 | $this->value[ $key ] = $val ? esc_url_raw( $val, $protocols ) : $this->field->get_default(); |
||
| 127 | 1 | } |
|
| 128 | } else { |
||
| 129 | $this->value = $this->value ? esc_url_raw( $this->value, $protocols ) : $this->field->get_default(); |
||
| 130 | } |
||
| 131 | |||
| 132 | return $this->value; |
||
| 133 | } |
||
| 134 | |||
| 135 | public function colorpicker() { |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Validate email in a meta value |
||
| 153 | * @since 1.0.1 |
||
| 154 | * @return string Empty string or sanitized email |
||
| 155 | */ |
||
| 156 | public function text_email() { |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Validate money in a meta value |
||
| 173 | * @since 1.0.1 |
||
| 174 | * @return string Empty string or sanitized money value |
||
| 175 | */ |
||
| 176 | public function text_money() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Converts text date to timestamp |
||
| 197 | * @since 1.0.2 |
||
| 198 | * @return string Timestring |
||
| 199 | */ |
||
| 200 | public function text_date_timestamp() { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Datetime to timestamp |
||
| 208 | * @since 1.0.1 |
||
| 209 | * @return string Timestring |
||
| 210 | */ |
||
| 211 | public function text_datetime_timestamp( $repeat = false ) { |
||
| 233 | |||
| 234 | 1 | /** |
|
| 235 | 1 | * Datetime to timestamp with timezone |
|
| 236 | * @since 1.0.1 |
||
| 237 | 1 | * @return string Timestring |
|
| 238 | 1 | */ |
|
| 239 | 1 | public function text_datetime_timestamp_timezone( $repeat = false ) { |
|
| 319 | |||
| 320 | 2 | /** |
|
| 321 | 2 | * Sanitize textareas and wysiwyg fields |
|
| 322 | * @since 1.0.1 |
||
| 323 | * @return string Sanitized data |
||
| 324 | */ |
||
| 325 | public function textarea() { |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Sanitize code textareas |
||
| 331 | * @since 1.0.2 |
||
| 332 | * @return string Sanitized data |
||
| 333 | */ |
||
| 334 | public function textarea_code( $repeat = false ) { |
||
| 342 | |||
| 343 | 1 | /** |
|
| 344 | 1 | * Validate a timezone against the available options. |
|
| 345 | * @since 2.2.2 |
||
| 346 | 1 | * @return mixed Timezone string or false if invalid. |
|
| 347 | */ |
||
| 348 | 1 | public function select_timezone() { |
|
| 363 | 1 | ||
| 364 | 1 | /** |
|
| 365 | * Handles saving of attachment post ID and sanitizing file url |
||
| 366 | * @since 1.1.0 |
||
| 367 | 1 | * @return string Sanitized url |
|
| 368 | 1 | */ |
|
| 369 | 1 | public function file() { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Gets the values for the `file` field type from the data being saved. |
||
| 385 | * @since 2.2.0 |
||
| 386 | */ |
||
| 387 | public function _get_group_file_value_array( $id_key ) { |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Peforms saving of `file` attachement's ID |
||
| 406 | * @since 1.1.0 |
||
| 407 | */ |
||
| 408 | public function _save_file_id_value( $file_id_key ) { |
||
| 423 | |||
| 424 | 1 | /** |
|
| 425 | 1 | * Peforms saving of `text_datetime_timestamp_timezone` utc timestamp |
|
| 426 | 1 | * @since 2.2.0 |
|
| 427 | */ |
||
| 428 | public function _save_utc_value( $utc_key, $utc_stamp ) { |
||
| 431 | 1 | ||
| 432 | 1 | /** |
|
| 433 | 1 | * Returns a new, supporting, CMB2_Field object based on a new field id. |
|
| 434 | 1 | * @since 2.2.0 |
|
| 435 | 1 | */ |
|
| 436 | 1 | public function _new_supporting_field( $new_field_id ) { |
|
| 442 | |||
| 443 | 1 | /** |
|
| 444 | * If repeating, loop through and re-apply sanitization method |
||
| 445 | * @since 1.1.0 |
||
| 446 | * @param string $method Class method |
||
| 447 | * @param bool $repeat Whether repeating or not |
||
| 448 | * @return mixed Sanitized value |
||
| 449 | */ |
||
| 450 | public function _check_repeat( $method, $repeat ) { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Determine if passed value is an empty array |
||
| 474 | * @since 2.0.6 |
||
| 475 | * @param mixed $to_check Value to check |
||
| 476 | * @return boolean Whether value is an array that's empty |
||
| 477 | */ |
||
| 478 | public function _is_empty_array( $to_check ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Validate a value against a set of allowed options. |
||
| 488 | * |
||
| 489 | * @since 2.2.2 |
||
| 490 | * @param array Options to validate against. Defaults to the `options` field argument. |
||
| 491 | * @return mixed Validated option or false if the value is invalid. |
||
| 492 | * The validated option may be empty if `show_option_none` was set to true. |
||
| 493 | */ |
||
| 494 | protected function validate_against_options( $options = array() ) { |
||
| 527 | |||
| 528 | } |
||
| 529 |
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.