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.2.4 |
||
| 81 | * @link http://core.trac.wordpress.org/ticket/18947 |
||
| 82 | * @global array $_wp_additional_image_sizes |
||
| 83 | * @return array 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.2.4 |
||
| 114 | * @param array|string $size Image size. Accepts an array of width and height (in that order) |
||
| 115 | * @return false|string Named image size e.g. 'thumbnail' |
||
| 116 | */ |
||
| 117 | 6 | public static function get_named_size( $size ) { |
|
| 187 | |||
| 188 | /** |
||
| 189 | * Utility method that returns time string offset by timezone |
||
| 190 | * @since 1.0.0 |
||
| 191 | * @param string $tzstring Time string |
||
| 192 | * @return string Offset time string |
||
| 193 | */ |
||
| 194 | 2 | public static function timezone_offset( $tzstring ) { |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
| 217 | * |
||
| 218 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
| 219 | * an empty string if no value has been set on the options page. |
||
| 220 | * A timezone string is required by the wp_timezone_choice() used by the |
||
| 221 | * select_timezone field. |
||
| 222 | * |
||
| 223 | * @since 1.0.0 |
||
| 224 | * @return string Timezone string |
||
| 225 | */ |
||
| 226 | 1 | public static function timezone_string() { |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Returns a timestamp, first checking if value already is a timestamp. |
||
| 250 | * @since 2.0.0 |
||
| 251 | * @param string|int $string Possible timestamp string |
||
| 252 | * @return int Time stamp |
||
| 253 | */ |
||
| 254 | 10 | public static function make_valid_time_stamp( $string ) { |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Determine if a value is a valid timestamp |
||
| 266 | * @since 2.0.0 |
||
| 267 | * @param mixed $timestamp Value to check |
||
| 268 | * @return boolean Whether value is a valid timestamp |
||
| 269 | */ |
||
| 270 | 10 | public static function is_valid_time_stamp( $timestamp ) { |
|
| 275 | |||
| 276 | /** |
||
| 277 | * Checks if a value is 'empty'. Still accepts 0. |
||
| 278 | * @since 2.0.0 |
||
| 279 | * @param mixed $value Value to check |
||
| 280 | * @return bool True or false |
||
| 281 | */ |
||
| 282 | 61 | public static function isempty( $value ) { |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
| 288 | * @since 2.2.2 |
||
| 289 | * @param mixed $value Value to check |
||
| 290 | * @return bool True or false |
||
| 291 | */ |
||
| 292 | 4 | public static function notempty( $value ){ |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Filters out empty values (not including 0). |
||
| 298 | * @since 2.2.2 |
||
| 299 | * @param mixed $value Value to check |
||
| 300 | * @return bool True or false |
||
| 301 | */ |
||
| 302 | 3 | public static function filter_empty( $value ) { |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Insert a single array item inside another array at a set position |
||
| 308 | * @since 2.0.2 |
||
| 309 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
| 310 | * @param array $new New array to insert |
||
| 311 | * @param int $position Position in the main array to insert the new array |
||
| 312 | */ |
||
| 313 | 2 | public static function array_insert( &$array, $new, $position ) { |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Defines the url which is used to load local resources. |
||
| 321 | * This may need to be filtered for local Window installations. |
||
| 322 | * If resources do not load, please check the wiki for details. |
||
| 323 | * @since 1.0.1 |
||
| 324 | * @return string URL to CMB2 resources |
||
| 325 | */ |
||
| 326 | 2 | public static function url( $path = '' ) { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * Converts a system path to a URL |
||
| 345 | * @since 2.2.2 |
||
| 346 | * @param string $dir Directory path to convert. |
||
| 347 | * @return string Converted URL. |
||
| 348 | */ |
||
| 349 | 2 | public static function get_url_from_dir( $dir ) { |
|
| 390 | |||
| 391 | /** |
||
| 392 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
| 393 | * |
||
| 394 | * On windows systems, replaces backslashes with forward slashes |
||
| 395 | * and forces upper-case drive letters. |
||
| 396 | * Allows for two leading slashes for Windows network shares, but |
||
| 397 | * ensures that all other duplicate slashes are reduced to a single. |
||
| 398 | * |
||
| 399 | * @since 2.2.0 |
||
| 400 | * |
||
| 401 | * @param string $path Path to normalize. |
||
| 402 | * @return string Normalized path. |
||
| 403 | */ |
||
| 404 | 2 | protected static function normalize_path( $path ) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Get timestamp from text date |
||
| 421 | * @since 2.2.0 |
||
| 422 | * @param string $value Date value |
||
| 423 | * @param string $date_format Expected date format |
||
| 424 | * @return mixed Unix timestamp representing the date. |
||
| 425 | */ |
||
| 426 | public static function get_timestamp_from_value( $value, $date_format ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
| 433 | * It will work with only with the following subset ot date() options: |
||
| 434 | * |
||
| 435 | * d, j, z, m, n, y, and Y. |
||
| 436 | * |
||
| 437 | * A slight effort is made to deal with escaped characters. |
||
| 438 | * |
||
| 439 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
| 440 | * bring even more translation troubles. |
||
| 441 | * |
||
| 442 | * @since 2.2.0 |
||
| 443 | * @param string $format php date format |
||
| 444 | * @return string reformatted string |
||
| 445 | */ |
||
| 446 | 5 | public static function php_to_js_dateformat( $format ) { |
|
| 480 | |||
| 481 | /** |
||
| 482 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
| 483 | * @since 2.2.0 |
||
| 484 | * @param $value Value to wrap/escape |
||
| 485 | * @return string Modified value |
||
| 486 | */ |
||
| 487 | 4 | public static function wrap_escaped_chars( $value ) { |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Send to debug.log if WP_DEBUG is defined and true |
||
| 493 | * |
||
| 494 | * @since 2.2.0 |
||
| 495 | * |
||
| 496 | * @param string $function Function name |
||
| 497 | * @param int $line Line number |
||
| 498 | * @param mixed $msg Message to output |
||
| 499 | * @param mixed $debug Variable to print_r |
||
| 500 | */ |
||
| 501 | public static function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Determine a file's extension |
||
| 509 | * @since 1.0.0 |
||
| 510 | * @param string $file File url |
||
| 511 | * @return string|false File extension or false |
||
| 512 | */ |
||
| 513 | 6 | public static function get_file_ext( $file ) { |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Get the file name from a url |
||
| 520 | * @since 2.0.0 |
||
| 521 | * @param string $value File url or path |
||
| 522 | * @return string File name |
||
| 523 | */ |
||
| 524 | 5 | public static function get_file_name_from_path( $value ) { |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Check if WP version is at least $version. |
||
| 531 | * @since 2.2.2 |
||
| 532 | * @param string $version WP version string to compare. |
||
| 533 | * @return bool Result of comparison check. |
||
| 534 | */ |
||
| 535 | 6 | public static function wp_at_least( $version ) { |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Combines attributes into a string for a form element. |
||
| 541 | * @since 1.1.0 |
||
| 542 | * @param array $attrs Attributes to concatenate. |
||
| 543 | * @param array $attr_exclude Attributes that should NOT be concatenated. |
||
| 544 | * @return string String of attributes for form element. |
||
| 545 | */ |
||
| 546 | 46 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Ensures value is an array. |
||
| 563 | * |
||
| 564 | * @since 2.2.3 |
||
| 565 | * |
||
| 566 | * @param mixed $value Value to ensure is array. |
||
| 567 | * @param array $default Default array. Defaults to empty array. |
||
| 568 | * |
||
| 569 | * @return array The array. |
||
| 570 | */ |
||
| 571 | 48 | public static function ensure_array( $value, $default = array() ) { |
|
| 587 | |||
| 588 | } |
||
| 589 |
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.