Complex classes like CMB2_Utils 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_Utils, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | class CMB2_Utils { |
||
|
|||
14 | |||
15 | /** |
||
16 | * The WordPress ABSPATH constant. |
||
17 | * @var string |
||
18 | * @since 2.2.3 |
||
19 | */ |
||
20 | protected static $ABSPATH = ABSPATH; |
||
21 | |||
22 | /** |
||
23 | * The url which is used to load local resources. |
||
24 | * @var string |
||
25 | * @since 2.0.0 |
||
26 | */ |
||
27 | protected static $url = ''; |
||
28 | |||
29 | /** |
||
30 | * Utility method that attempts to get an attachment's ID by it's url |
||
31 | * @since 1.0.0 |
||
32 | * @param string $img_url Attachment url |
||
33 | * @return int|false Attachment ID or false |
||
34 | */ |
||
35 | 1 | public static function image_id_from_url( $img_url ) { |
|
77 | |||
78 | /** |
||
79 | * Utility method to get a combined list of default and custom registered image sizes |
||
80 | * @since 2.x.x.x |
||
81 | * @link http://core.trac.wordpress.org/ticket/18947 |
||
82 | * @global array $_wp_additional_image_sizes |
||
83 | * @return array $image_sizes The image sizes |
||
84 | */ |
||
85 | 6 | static function get_available_image_sizes() { |
|
103 | |||
104 | /** |
||
105 | * Utility method to return the closest named size from an array of values |
||
106 | * |
||
107 | * Based off of WordPress's image_get_intermediate_size() |
||
108 | * If the size matches an existing size then it will be used. If there is no |
||
109 | * direct match, then the nearest image size larger than the specified size |
||
110 | * will be used. If nothing is found, then the function will return false. |
||
111 | * Uses get_available_image_sizes() to get all available sizes. |
||
112 | * |
||
113 | * @since 2.x.x.x |
||
114 | * @param array|string $size Image size. Accepts an array of width and height (in that order) |
||
115 | * @return false|string $data Named image size e.g. 'thumbnail' |
||
116 | */ |
||
117 | 6 | public static function get_named_size( $size ) { |
|
186 | |||
187 | /** |
||
188 | * Utility method that returns time string offset by timezone |
||
189 | * @since 1.0.0 |
||
190 | * @param string $tzstring Time string |
||
191 | * @return string Offset time string |
||
192 | */ |
||
193 | 2 | public static function timezone_offset( $tzstring ) { |
|
213 | |||
214 | /** |
||
215 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
216 | * |
||
217 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
218 | * an empty string if no value has been set on the options page. |
||
219 | * A timezone string is required by the wp_timezone_choice() used by the |
||
220 | * select_timezone field. |
||
221 | * |
||
222 | * @since 1.0.0 |
||
223 | * @return string Timezone string |
||
224 | */ |
||
225 | 1 | public static function timezone_string() { |
|
246 | |||
247 | /** |
||
248 | * Returns a timestamp, first checking if value already is a timestamp. |
||
249 | * @since 2.0.0 |
||
250 | * @param string|int $string Possible timestamp string |
||
251 | * @return int Time stamp |
||
252 | */ |
||
253 | 10 | public static function make_valid_time_stamp( $string ) { |
|
262 | |||
263 | /** |
||
264 | * Determine if a value is a valid timestamp |
||
265 | * @since 2.0.0 |
||
266 | * @param mixed $timestamp Value to check |
||
267 | * @return boolean Whether value is a valid timestamp |
||
268 | */ |
||
269 | 10 | public static function is_valid_time_stamp( $timestamp ) { |
|
274 | |||
275 | /** |
||
276 | * Checks if a value is 'empty'. Still accepts 0. |
||
277 | * @since 2.0.0 |
||
278 | * @param mixed $value Value to check |
||
279 | * @return bool True or false |
||
280 | */ |
||
281 | 61 | public static function isempty( $value ) { |
|
284 | |||
285 | /** |
||
286 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
287 | * @since 2.2.2 |
||
288 | * @param mixed $value Value to check |
||
289 | * @return bool True or false |
||
290 | */ |
||
291 | 4 | public static function notempty( $value ){ |
|
294 | |||
295 | /** |
||
296 | * Filters out empty values (not including 0). |
||
297 | * @since 2.2.2 |
||
298 | * @param mixed $value Value to check |
||
299 | * @return bool True or false |
||
300 | */ |
||
301 | 3 | public static function filter_empty( $value ) { |
|
304 | |||
305 | /** |
||
306 | * Insert a single array item inside another array at a set position |
||
307 | * @since 2.0.2 |
||
308 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
309 | * @param array $new New array to insert |
||
310 | * @param int $position Position in the main array to insert the new array |
||
311 | */ |
||
312 | 2 | public static function array_insert( &$array, $new, $position ) { |
|
317 | |||
318 | /** |
||
319 | * Defines the url which is used to load local resources. |
||
320 | * This may need to be filtered for local Window installations. |
||
321 | * If resources do not load, please check the wiki for details. |
||
322 | * @since 1.0.1 |
||
323 | * @return string URL to CMB2 resources |
||
324 | */ |
||
325 | 2 | public static function url( $path = '' ) { |
|
341 | |||
342 | /** |
||
343 | * Converts a system path to a URL |
||
344 | * @since 2.2.2 |
||
345 | * @param string $dir Directory path to convert. |
||
346 | * @return string Converted URL. |
||
347 | */ |
||
348 | 2 | public static function get_url_from_dir( $dir ) { |
|
389 | |||
390 | /** |
||
391 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
392 | * |
||
393 | * On windows systems, replaces backslashes with forward slashes |
||
394 | * and forces upper-case drive letters. |
||
395 | * Allows for two leading slashes for Windows network shares, but |
||
396 | * ensures that all other duplicate slashes are reduced to a single. |
||
397 | * |
||
398 | * @since 2.2.0 |
||
399 | * |
||
400 | * @param string $path Path to normalize. |
||
401 | * @return string Normalized path. |
||
402 | */ |
||
403 | 2 | protected static function normalize_path( $path ) { |
|
417 | |||
418 | /** |
||
419 | * Get timestamp from text date |
||
420 | * @since 2.2.0 |
||
421 | * @param string $value Date value |
||
422 | * @param string $date_format Expected date format |
||
423 | * @return mixed Unix timestamp representing the date. |
||
424 | */ |
||
425 | public static function get_timestamp_from_value( $value, $date_format ) { |
||
429 | |||
430 | /** |
||
431 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
432 | * It will work with only with the following subset ot date() options: |
||
433 | * |
||
434 | * d, j, z, m, n, y, and Y. |
||
435 | * |
||
436 | * A slight effort is made to deal with escaped characters. |
||
437 | * |
||
438 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
439 | * bring even more translation troubles. |
||
440 | * |
||
441 | * @since 2.2.0 |
||
442 | * @param string $format php date format |
||
443 | * @return string reformatted string |
||
444 | */ |
||
445 | 5 | public static function php_to_js_dateformat( $format ) { |
|
479 | |||
480 | /** |
||
481 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
482 | * @since 2.2.0 |
||
483 | * @param $value Value to wrap/escape |
||
484 | * @return string Modified value |
||
485 | */ |
||
486 | 4 | public static function wrap_escaped_chars( $value ) { |
|
489 | |||
490 | /** |
||
491 | * Send to debug.log if WP_DEBUG is defined and true |
||
492 | * |
||
493 | * @since 2.2.0 |
||
494 | * |
||
495 | * @param string $function Function name |
||
496 | * @param int $line Line number |
||
497 | * @param mixed $msg Message to output |
||
498 | * @param mixed $debug Variable to print_r |
||
499 | */ |
||
500 | public static function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
505 | |||
506 | /** |
||
507 | * Determine a file's extension |
||
508 | * @since 1.0.0 |
||
509 | * @param string $file File url |
||
510 | * @return string|false File extension or false |
||
511 | */ |
||
512 | 6 | public static function get_file_ext( $file ) { |
|
516 | |||
517 | /** |
||
518 | * Get the file name from a url |
||
519 | * @since 2.0.0 |
||
520 | * @param string $value File url or path |
||
521 | * @return string File name |
||
522 | */ |
||
523 | 5 | public static function get_file_name_from_path( $value ) { |
|
527 | |||
528 | /** |
||
529 | * Check if WP version is at least $version. |
||
530 | * @since 2.2.2 |
||
531 | * @param string $version WP version string to compare. |
||
532 | * @return bool Result of comparison check. |
||
533 | */ |
||
534 | 6 | public static function wp_at_least( $version ) { |
|
537 | |||
538 | /** |
||
539 | * Combines attributes into a string for a form element. |
||
540 | * @since 1.1.0 |
||
541 | * @param array $attrs Attributes to concatenate. |
||
542 | * @param array $attr_exclude Attributes that should NOT be concatenated. |
||
543 | * @return string String of attributes for form element. |
||
544 | */ |
||
545 | 46 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
|
559 | |||
560 | /** |
||
561 | * Ensures value is an array. |
||
562 | * |
||
563 | * @since 2.2.3 |
||
564 | * |
||
565 | * @param mixed $value Value to ensure is array. |
||
566 | * @param array $default Default array. Defaults to empty array. |
||
567 | * |
||
568 | * @return array The array. |
||
569 | */ |
||
570 | 48 | public static function ensure_array( $value, $default = array() ) { |
|
586 | |||
587 | } |
||
588 |
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.