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 | * |
||
18 | * @var string |
||
19 | * @since 2.2.3 |
||
20 | */ |
||
21 | protected static $ABSPATH = ABSPATH; |
||
22 | |||
23 | /** |
||
24 | * The url which is used to load local resources. |
||
25 | * |
||
26 | * @var string |
||
27 | * @since 2.0.0 |
||
28 | */ |
||
29 | protected static $url = ''; |
||
30 | |||
31 | /** |
||
32 | * Utility method that attempts to get an attachment's ID by it's url |
||
33 | * |
||
34 | * @since 1.0.0 |
||
35 | * @param string $img_url Attachment url |
||
36 | * @return int|false Attachment ID or false |
||
37 | */ |
||
38 | 1 | public static function image_id_from_url( $img_url ) { |
|
79 | |||
80 | /** |
||
81 | * Utility method to get a combined list of default and custom registered image sizes |
||
82 | * |
||
83 | * @since 2.2.4 |
||
84 | * @link http://core.trac.wordpress.org/ticket/18947 |
||
85 | * @global array $_wp_additional_image_sizes |
||
86 | * @return array The image sizes |
||
87 | */ |
||
88 | 6 | static function get_available_image_sizes() { |
|
106 | |||
107 | /** |
||
108 | * Utility method to return the closest named size from an array of values |
||
109 | * |
||
110 | * Based off of WordPress's image_get_intermediate_size() |
||
111 | * If the size matches an existing size then it will be used. If there is no |
||
112 | * direct match, then the nearest image size larger than the specified size |
||
113 | * will be used. If nothing is found, then the function will return false. |
||
114 | * Uses get_available_image_sizes() to get all available sizes. |
||
115 | * |
||
116 | * @since 2.2.4 |
||
117 | * @param array|string $size Image size. Accepts an array of width and height (in that order) |
||
118 | * @return false|string Named image size e.g. 'thumbnail' |
||
119 | */ |
||
120 | 6 | public static function get_named_size( $size ) { |
|
188 | |||
189 | /** |
||
190 | * Utility method that returns time string offset by timezone |
||
191 | * |
||
192 | * @since 1.0.0 |
||
193 | * @param string $tzstring Time string |
||
194 | * @return string Offset time string |
||
195 | */ |
||
196 | 2 | public static function timezone_offset( $tzstring ) { |
|
215 | |||
216 | /** |
||
217 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
218 | * |
||
219 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
220 | * an empty string if no value has been set on the options page. |
||
221 | * A timezone string is required by the wp_timezone_choice() used by the |
||
222 | * select_timezone field. |
||
223 | * |
||
224 | * @since 1.0.0 |
||
225 | * @return string Timezone string |
||
226 | */ |
||
227 | public static function timezone_string() { |
||
248 | |||
249 | /** |
||
250 | * Returns a timestamp, first checking if value already is a timestamp. |
||
251 | * |
||
252 | * @since 2.0.0 |
||
253 | * @param string|int $string Possible timestamp string |
||
254 | * @return int Time stamp |
||
255 | */ |
||
256 | public static function make_valid_time_stamp( $string ) { |
||
265 | |||
266 | /** |
||
267 | * Determine if a value is a valid timestamp |
||
268 | * |
||
269 | * @since 2.0.0 |
||
270 | * @param mixed $timestamp Value to check |
||
271 | * @return boolean Whether value is a valid timestamp |
||
272 | */ |
||
273 | public static function is_valid_time_stamp( $timestamp ) { |
||
278 | |||
279 | /** |
||
280 | * Checks if a value is 'empty'. Still accepts 0. |
||
281 | * |
||
282 | * @since 2.0.0 |
||
283 | * @param mixed $value Value to check |
||
284 | * @return bool True or false |
||
285 | */ |
||
286 | public static function isempty( $value ) { |
||
289 | |||
290 | /** |
||
291 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
292 | * |
||
293 | * @since 2.2.2 |
||
294 | * @param mixed $value Value to check |
||
295 | * @return bool True or false |
||
296 | */ |
||
297 | public static function notempty( $value ) { |
||
300 | |||
301 | /** |
||
302 | * Filters out empty values (not including 0). |
||
303 | * |
||
304 | * @since 2.2.2 |
||
305 | * @param mixed $value Value to check |
||
306 | * @return bool True or false |
||
307 | */ |
||
308 | public static function filter_empty( $value ) { |
||
311 | |||
312 | /** |
||
313 | * Insert a single array item inside another array at a set position |
||
314 | * |
||
315 | * @since 2.0.2 |
||
316 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
317 | * @param array $new New array to insert |
||
318 | * @param int $position Position in the main array to insert the new array |
||
319 | */ |
||
320 | public static function array_insert( &$array, $new, $position ) { |
||
325 | 2 | ||
326 | /** |
||
327 | * Defines the url which is used to load local resources. |
||
328 | * This may need to be filtered for local Window installations. |
||
329 | * If resources do not load, please check the wiki for details. |
||
330 | * |
||
331 | * @since 1.0.1 |
||
332 | * @return string URL to CMB2 resources |
||
333 | */ |
||
334 | public static function url( $path = '' ) { |
||
350 | |||
351 | /** |
||
352 | * Converts a system path to a URL |
||
353 | * |
||
354 | * @since 2.2.2 |
||
355 | * @param string $dir Directory path to convert. |
||
356 | * @return string Converted URL. |
||
357 | */ |
||
358 | public static function get_url_from_dir( $dir ) { |
||
398 | |||
399 | /** |
||
400 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
401 | * |
||
402 | * On windows systems, replaces backslashes with forward slashes |
||
403 | * and forces upper-case drive letters. |
||
404 | * Allows for two leading slashes for Windows network shares, but |
||
405 | * ensures that all other duplicate slashes are reduced to a single. |
||
406 | * |
||
407 | * @since 2.2.0 |
||
408 | * |
||
409 | * @param string $path Path to normalize. |
||
410 | * @return string Normalized path. |
||
411 | */ |
||
412 | protected static function normalize_path( $path ) { |
||
426 | |||
427 | /** |
||
428 | * Get timestamp from text date |
||
429 | * |
||
430 | * @since 2.2.0 |
||
431 | * @param string $value Date value |
||
432 | * @param string $date_format Expected date format |
||
433 | * @return mixed Unix timestamp representing the date. |
||
434 | */ |
||
435 | public static function get_timestamp_from_value( $value, $date_format ) { |
||
439 | |||
440 | /** |
||
441 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
442 | * It will work with only with the following subset ot date() options: |
||
443 | * |
||
444 | * d, j, z, m, n, y, and Y. |
||
445 | * |
||
446 | * A slight effort is made to deal with escaped characters. |
||
447 | * |
||
448 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
449 | * bring even more translation troubles. |
||
450 | * |
||
451 | * @since 2.2.0 |
||
452 | * @param string $format php date format |
||
453 | * @return string reformatted string |
||
454 | */ |
||
455 | public static function php_to_js_dateformat( $format ) { |
||
489 | |||
490 | /** |
||
491 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
492 | * |
||
493 | * @since 2.2.0 |
||
494 | * @param $value Value to wrap/escape |
||
495 | * @return string Modified value |
||
496 | */ |
||
497 | public static function wrap_escaped_chars( $value ) { |
||
500 | |||
501 | /** |
||
502 | * Send to debug.log if WP_DEBUG is defined and true |
||
503 | * |
||
504 | * @since 2.2.0 |
||
505 | * |
||
506 | * @param string $function Function name |
||
507 | * @param int $line Line number |
||
508 | * @param mixed $msg Message to output |
||
509 | * @param mixed $debug Variable to print_r |
||
510 | */ |
||
511 | public static function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
516 | |||
517 | /** |
||
518 | * Determine a file's extension |
||
519 | * |
||
520 | * @since 1.0.0 |
||
521 | * @param string $file File url |
||
522 | * @return string|false File extension or false |
||
523 | */ |
||
524 | public static function get_file_ext( $file ) { |
||
528 | |||
529 | /** |
||
530 | * Get the file name from a url |
||
531 | * |
||
532 | * @since 2.0.0 |
||
533 | * @param string $value File url or path |
||
534 | * @return string File name |
||
535 | */ |
||
536 | public static function get_file_name_from_path( $value ) { |
||
540 | |||
541 | /** |
||
542 | * Check if WP version is at least $version. |
||
543 | * |
||
544 | * @since 2.2.2 |
||
545 | * @param string $version WP version string to compare. |
||
546 | * @return bool Result of comparison check. |
||
547 | */ |
||
548 | public static function wp_at_least( $version ) { |
||
551 | |||
552 | /** |
||
553 | * Combines attributes into a string for a form element. |
||
554 | * |
||
555 | * @since 1.1.0 |
||
556 | * @param array $attrs Attributes to concatenate. |
||
557 | * @param array $attr_exclude Attributes that should NOT be concatenated. |
||
558 | * @return string String of attributes for form element. |
||
559 | */ |
||
560 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
||
574 | |||
575 | /** |
||
576 | * Ensures value is an array. |
||
577 | * |
||
578 | * @since 2.2.3 |
||
579 | * |
||
580 | * @param mixed $value Value to ensure is array. |
||
581 | * @param array $default Default array. Defaults to empty array. |
||
582 | * |
||
583 | * @return array The array. |
||
584 | */ |
||
585 | public static function ensure_array( $value, $default = array() ) { |
||
601 | |||
602 | } |
||
603 |
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.