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 $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 function image_id_from_url( $img_url ) { |
|
| 77 | |||
| 78 | /** |
||
| 79 | * Utility method that returns time string offset by timezone |
||
| 80 | * @since 1.0.0 |
||
| 81 | * @param string $tzstring Time string |
||
| 82 | * @return string Offset time string |
||
| 83 | */ |
||
| 84 | 2 | public function timezone_offset( $tzstring ) { |
|
| 104 | |||
| 105 | /** |
||
| 106 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
| 107 | * |
||
| 108 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
||
| 109 | * an empty string if no value has been set on the options page. |
||
| 110 | * A timezone string is required by the wp_timezone_choice() used by the |
||
| 111 | * select_timezone field. |
||
| 112 | * |
||
| 113 | * @since 1.0.0 |
||
| 114 | * @return string Timezone string |
||
| 115 | */ |
||
| 116 | 1 | public function timezone_string() { |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Returns a timestamp, first checking if value already is a timestamp. |
||
| 140 | * @since 2.0.0 |
||
| 141 | * @param string|int $string Possible timestamp string |
||
| 142 | * @return int Time stamp |
||
| 143 | */ |
||
| 144 | 10 | public function make_valid_time_stamp( $string ) { |
|
| 153 | |||
| 154 | /** |
||
| 155 | * Determine if a value is a valid timestamp |
||
| 156 | * @since 2.0.0 |
||
| 157 | * @param mixed $timestamp Value to check |
||
| 158 | * @return boolean Whether value is a valid timestamp |
||
| 159 | */ |
||
| 160 | 10 | public function is_valid_time_stamp( $timestamp ) { |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Checks if a value is 'empty'. Still accepts 0. |
||
| 168 | * @since 2.0.0 |
||
| 169 | * @param mixed $value Value to check |
||
| 170 | * @return bool True or false |
||
| 171 | */ |
||
| 172 | 58 | public function isempty( $value ) { |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Checks if a value is not 'empty'. 0 doesn't count as empty. |
||
| 178 | * @since 2.2.2 |
||
| 179 | * @param mixed $value Value to check |
||
| 180 | * @return bool True or false |
||
| 181 | */ |
||
| 182 | 3 | public function notempty( $value ){ |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Filters out empty values (not including 0). |
||
| 188 | * @since 2.2.2 |
||
| 189 | * @param mixed $value Value to check |
||
| 190 | * @return bool True or false |
||
| 191 | */ |
||
| 192 | 2 | function filter_empty( $value ) { |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Insert a single array item inside another array at a set position |
||
| 198 | * @since 2.0.2 |
||
| 199 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
| 200 | * @param array $new New array to insert |
||
| 201 | * @param int $position Position in the main array to insert the new array |
||
| 202 | */ |
||
| 203 | 2 | public function array_insert( &$array, $new, $position ) { |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Defines the url which is used to load local resources. |
||
| 211 | * This may need to be filtered for local Window installations. |
||
| 212 | * If resources do not load, please check the wiki for details. |
||
| 213 | * @since 1.0.1 |
||
| 214 | * @return string URL to CMB2 resources |
||
| 215 | */ |
||
| 216 | 2 | public function url( $path = '' ) { |
|
| 232 | |||
| 233 | /** |
||
| 234 | * Converts a system path to a URL |
||
| 235 | * @since 2.2.2 |
||
| 236 | * @param string $dir Directory path to convert. |
||
| 237 | * @return string Converted URL. |
||
| 238 | */ |
||
| 239 | 2 | public static function get_url_from_dir( $dir ) { |
|
| 280 | |||
| 281 | /** |
||
| 282 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
| 283 | * |
||
| 284 | * On windows systems, replaces backslashes with forward slashes |
||
| 285 | * and forces upper-case drive letters. |
||
| 286 | * Allows for two leading slashes for Windows network shares, but |
||
| 287 | * ensures that all other duplicate slashes are reduced to a single. |
||
| 288 | * |
||
| 289 | * @since 2.2.0 |
||
| 290 | * |
||
| 291 | * @param string $path Path to normalize. |
||
| 292 | * @return string Normalized path. |
||
| 293 | */ |
||
| 294 | 2 | protected static function normalize_path( $path ) { |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get timestamp from text date |
||
| 311 | * @since 2.2.0 |
||
| 312 | * @param string $value Date value |
||
| 313 | * @param string $date_format Expected date format |
||
| 314 | * @return mixed Unix timestamp representing the date. |
||
| 315 | */ |
||
| 316 | public function get_timestamp_from_value( $value, $date_format ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
| 323 | * It will work with only with the following subset ot date() options: |
||
| 324 | * |
||
| 325 | * d, j, z, m, n, y, and Y. |
||
| 326 | * |
||
| 327 | * A slight effort is made to deal with escaped characters. |
||
| 328 | * |
||
| 329 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
| 330 | * bring even more translation troubles. |
||
| 331 | * |
||
| 332 | * @since 2.2.0 |
||
| 333 | * @param string $format php date format |
||
| 334 | * @return string reformatted string |
||
| 335 | */ |
||
| 336 | 5 | public function php_to_js_dateformat( $format ) { |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
| 373 | * @since 2.2.0 |
||
| 374 | * @param $value Value to wrap/escape |
||
| 375 | * @return string Modified value |
||
| 376 | */ |
||
| 377 | 4 | public function wrap_escaped_chars( $value ) { |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Send to debug.log if WP_DEBUG is defined and true |
||
| 383 | * |
||
| 384 | * @since 2.2.0 |
||
| 385 | * |
||
| 386 | * @param string $function Function name |
||
| 387 | * @param int $line Line number |
||
| 388 | * @param mixed $msg Message to output |
||
| 389 | * @param mixed $debug Variable to print_r |
||
| 390 | */ |
||
| 391 | public function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Determine a file's extension |
||
| 399 | * @since 1.0.0 |
||
| 400 | * @param string $file File url |
||
| 401 | * @return string|false File extension or false |
||
| 402 | */ |
||
| 403 | 6 | public function get_file_ext( $file ) { |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Get the file name from a url |
||
| 410 | * @since 2.0.0 |
||
| 411 | * @param string $value File url or path |
||
| 412 | * @return string File name |
||
| 413 | */ |
||
| 414 | 5 | public function get_file_name_from_path( $value ) { |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Check if WP version is at least $version. |
||
| 421 | * @since 2.2.2 |
||
| 422 | * @param string $version WP version string to compare. |
||
| 423 | * @return bool Result of comparison check. |
||
| 424 | */ |
||
| 425 | 6 | public function wp_at_least( $version ) { |
|
| 429 | |||
| 430 | } |
||
| 431 |
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.