Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 | 1 | /** |
|
| 23 | 1 | * The url which is used to load local resources. |
|
| 24 | * @var string |
||
| 25 | 1 | * @since 2.0.0 |
|
| 26 | */ |
||
| 27 | 1 | protected static $url = ''; |
|
| 28 | 1 | ||
| 29 | 1 | /** |
|
| 30 | 1 | * 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 | 1 | * @return int|false Attachment ID or false |
|
| 34 | */ |
||
| 35 | public static function image_id_from_url( $img_url ) { |
||
| 77 | 2 | ||
| 78 | 2 | /** |
|
| 79 | 2 | * Utility method to get a combined list of default and custom registered image sizes |
|
| 80 | * @since 2.x.x.x |
||
| 81 | 2 | * @link http://core.trac.wordpress.org/ticket/18947 |
|
| 82 | 2 | * @global array $_wp_additional_image_sizes |
|
| 83 | 2 | * @return array $image_sizes The image sizes |
|
| 84 | 2 | */ |
|
| 85 | static function get_available_image_sizes() { |
||
| 103 | |||
| 104 | /** |
||
| 105 | 5 | * Utility method to return the closest named size from an array of values |
|
| 106 | 5 | * |
|
| 107 | 5 | * 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 | 5 | */ |
|
| 117 | 5 | public static function get_named_size( $size ) { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Utility method that returns time string offset by timezone |
||
| 173 | * @since 1.0.0 |
||
| 174 | * @param string $tzstring Time string |
||
| 175 | * @return string Offset time string |
||
| 176 | */ |
||
| 177 | public static function timezone_offset( $tzstring ) { |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
| 200 | * |
||
| 201 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
| 202 | * an empty string if no value has been set on the options page. |
||
| 203 | * A timezone string is required by the wp_timezone_choice() used by the |
||
| 204 | * select_timezone field. |
||
| 205 | * |
||
| 206 | * @since 1.0.0 |
||
| 207 | * @return string Timezone string |
||
| 208 | */ |
||
| 209 | public static function timezone_string() { |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns a timestamp, first checking if value already is a timestamp. |
||
| 233 | * @since 2.0.0 |
||
| 234 | * @param string|int $string Possible timestamp string |
||
| 235 | * @return int Time stamp |
||
| 236 | */ |
||
| 237 | public static function make_valid_time_stamp( $string ) { |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Determine if a value is a valid timestamp |
||
| 249 | * @since 2.0.0 |
||
| 250 | * @param mixed $timestamp Value to check |
||
| 251 | * @return boolean Whether value is a valid timestamp |
||
| 252 | */ |
||
| 253 | public static function is_valid_time_stamp( $timestamp ) { |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Checks if a value is 'empty'. Still accepts 0. |
||
| 261 | * @since 2.0.0 |
||
| 262 | * @param mixed $value Value to check |
||
| 263 | * @return bool True or false |
||
| 264 | */ |
||
| 265 | public static function isempty( $value ) { |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
| 271 | * @since 2.2.2 |
||
| 272 | * @param mixed $value Value to check |
||
| 273 | * @return bool True or false |
||
| 274 | */ |
||
| 275 | public static function notempty( $value ){ |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Filters out empty values (not including 0). |
||
| 281 | * @since 2.2.2 |
||
| 282 | * @param mixed $value Value to check |
||
| 283 | * @return bool True or false |
||
| 284 | */ |
||
| 285 | public static function filter_empty( $value ) { |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Insert a single array item inside another array at a set position |
||
| 291 | * @since 2.0.2 |
||
| 292 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
| 293 | * @param array $new New array to insert |
||
| 294 | * @param int $position Position in the main array to insert the new array |
||
| 295 | */ |
||
| 296 | public static function array_insert( &$array, $new, $position ) { |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Defines the url which is used to load local resources. |
||
| 304 | * This may need to be filtered for local Window installations. |
||
| 305 | * If resources do not load, please check the wiki for details. |
||
| 306 | * @since 1.0.1 |
||
| 307 | * @return string URL to CMB2 resources |
||
| 308 | */ |
||
| 309 | public static function url( $path = '' ) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Converts a system path to a URL |
||
| 328 | * @since 2.2.2 |
||
| 329 | * @param string $dir Directory path to convert. |
||
| 330 | * @return string Converted URL. |
||
| 331 | */ |
||
| 332 | public static function get_url_from_dir( $dir ) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
| 376 | * |
||
| 377 | * On windows systems, replaces backslashes with forward slashes |
||
| 378 | * and forces upper-case drive letters. |
||
| 379 | * Allows for two leading slashes for Windows network shares, but |
||
| 380 | * ensures that all other duplicate slashes are reduced to a single. |
||
| 381 | * |
||
| 382 | * @since 2.2.0 |
||
| 383 | * |
||
| 384 | * @param string $path Path to normalize. |
||
| 385 | * @return string Normalized path. |
||
| 386 | */ |
||
| 387 | protected static function normalize_path( $path ) { |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Get timestamp from text date |
||
| 404 | * @since 2.2.0 |
||
| 405 | * @param string $value Date value |
||
| 406 | * @param string $date_format Expected date format |
||
| 407 | * @return mixed Unix timestamp representing the date. |
||
| 408 | */ |
||
| 409 | public static function get_timestamp_from_value( $value, $date_format ) { |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
| 416 | * It will work with only with the following subset ot date() options: |
||
| 417 | * |
||
| 418 | * d, j, z, m, n, y, and Y. |
||
| 419 | * |
||
| 420 | * A slight effort is made to deal with escaped characters. |
||
| 421 | * |
||
| 422 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
| 423 | * bring even more translation troubles. |
||
| 424 | * |
||
| 425 | * @since 2.2.0 |
||
| 426 | * @param string $format php date format |
||
| 427 | * @return string reformatted string |
||
| 428 | */ |
||
| 429 | public static function php_to_js_dateformat( $format ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
| 466 | * @since 2.2.0 |
||
| 467 | * @param $value Value to wrap/escape |
||
| 468 | * @return string Modified value |
||
| 469 | */ |
||
| 470 | public static function wrap_escaped_chars( $value ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Send to debug.log if WP_DEBUG is defined and true |
||
| 476 | * |
||
| 477 | * @since 2.2.0 |
||
| 478 | * |
||
| 479 | * @param string $function Function name |
||
| 480 | * @param int $line Line number |
||
| 481 | * @param mixed $msg Message to output |
||
| 482 | * @param mixed $debug Variable to print_r |
||
| 483 | */ |
||
| 484 | public static function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Determine a file's extension |
||
| 492 | * @since 1.0.0 |
||
| 493 | * @param string $file File url |
||
| 494 | * @return string|false File extension or false |
||
| 495 | */ |
||
| 496 | public static function get_file_ext( $file ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Get the file name from a url |
||
| 503 | * @since 2.0.0 |
||
| 504 | * @param string $value File url or path |
||
| 505 | * @return string File name |
||
| 506 | */ |
||
| 507 | public static function get_file_name_from_path( $value ) { |
||
| 511 | |||
| 512 | /** |
||
| 513 | * Check if WP version is at least $version. |
||
| 514 | * @since 2.2.2 |
||
| 515 | * @param string $version WP version string to compare. |
||
| 516 | * @return bool Result of comparison check. |
||
| 517 | */ |
||
| 518 | public static function wp_at_least( $version ) { |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Combines attributes into a string for a form element. |
||
| 524 | * @since 1.1.0 |
||
| 525 | * @param array $attrs Attributes to concatenate. |
||
| 526 | * @param array $attr_exclude Attributes that should NOT be concatenated. |
||
| 527 | * @return string String of attributes for form element. |
||
| 528 | */ |
||
| 529 | public static function concat_attrs( $attrs, $attr_exclude = array() ) { |
||
| 543 | |||
| 544 | /** |
||
| 545 | * Ensures value is an array. |
||
| 546 | * |
||
| 547 | * @since 2.2.3 |
||
| 548 | * |
||
| 549 | * @param mixed $value Value to ensure is array. |
||
| 550 | * @param array $default Default array. Defaults to empty array. |
||
| 551 | * |
||
| 552 | * @return array The array. |
||
| 553 | */ |
||
| 554 | public static function ensure_array( $value, $default = array() ) { |
||
| 570 | |||
| 571 | } |
||
| 572 |
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.