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 url which is used to load local resources. |
||
| 17 | * @var string |
||
| 18 | * @since 2.0.0 |
||
| 19 | */ |
||
| 20 | protected $url = ''; |
||
| 21 | |||
| 22 | 1 | /** |
|
| 23 | 1 | * Utility method that attempts to get an attachment's ID by it's url |
|
| 24 | * @since 1.0.0 |
||
| 25 | 1 | * @param string $img_url Attachment url |
|
| 26 | * @return int|false Attachment ID or false |
||
| 27 | 1 | */ |
|
| 28 | 1 | public function image_id_from_url( $img_url ) { |
|
| 70 | |||
| 71 | /** |
||
| 72 | * Utility method that returns time string offset by timezone |
||
| 73 | * @since 1.0.0 |
||
| 74 | * @param string $tzstring Time string |
||
| 75 | * @return string Offset time string |
||
| 76 | */ |
||
| 77 | 2 | public function timezone_offset( $tzstring ) { |
|
| 97 | |||
| 98 | /** |
||
| 99 | * Utility method that returns a timezone string representing the default timezone for the site. |
||
| 100 | 5 | * |
|
| 101 | 5 | * Roughly copied from WordPress, as get_option('timezone_string') will return |
|
| 102 | * an empty string if no value has been set on the options page. |
||
| 103 | * A timezone string is required by the wp_timezone_choice() used by the |
||
| 104 | * select_timezone field. |
||
| 105 | 5 | * |
|
| 106 | 5 | * @since 1.0.0 |
|
| 107 | 5 | * @return string Timezone string |
|
| 108 | */ |
||
| 109 | public function timezone_string() { |
||
| 130 | 2 | ||
| 131 | 2 | /** |
|
| 132 | 1 | * Returns a timestamp, first checking if value already is a timestamp. |
|
| 133 | * @since 2.0.0 |
||
| 134 | * @param string|int $string Possible timestamp string |
||
| 135 | 1 | * @return int Time stamp |
|
| 136 | */ |
||
| 137 | public function make_valid_time_stamp( $string ) { |
||
| 146 | 1 | ||
| 147 | /** |
||
| 148 | * Determine if a value is a valid timestamp |
||
| 149 | * @since 2.0.0 |
||
| 150 | * @param mixed $timestamp Value to check |
||
| 151 | * @return boolean Whether value is a valid timestamp |
||
| 152 | */ |
||
| 153 | public function is_valid_time_stamp( $timestamp ) { |
||
| 158 | |||
| 159 | /** |
||
| 160 | 1 | * Checks if a value is 'empty'. Still accepts 0. |
|
| 161 | * @since 2.0.0 |
||
| 162 | * @param mixed $value Value to check |
||
| 163 | * @return bool True or false |
||
| 164 | */ |
||
| 165 | public function isempty( $value ) { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Insert a single array item inside another array at a set position |
||
| 171 | * @since 2.0.2 |
||
| 172 | * @param array &$array Array to modify. Is passed by reference, and no return is needed. |
||
| 173 | * @param array $new New array to insert |
||
| 174 | * @param int $position Position in the main array to insert the new array |
||
| 175 | */ |
||
| 176 | public function array_insert( &$array, $new, $position ) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Defines the url which is used to load local resources. |
||
| 184 | * This may need to be filtered for local Window installations. |
||
| 185 | * If resources do not load, please check the wiki for details. |
||
| 186 | * @since 1.0.1 |
||
| 187 | * @return string URL to CMB2 resources |
||
| 188 | */ |
||
| 189 | public function url( $path = '' ) { |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Converts a system path to a URL |
||
| 208 | * @since 2.2.2 |
||
| 209 | * @param string $dir Directory path to convert. |
||
| 210 | * @return string Converted URL. |
||
| 211 | */ |
||
| 212 | public static function get_url_from_dir( $dir ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * `wp_normalize_path` wrapper for back-compat. Normalize a filesystem path. |
||
| 248 | * |
||
| 249 | * On windows systems, replaces backslashes with forward slashes |
||
| 250 | * and forces upper-case drive letters. |
||
| 251 | * Allows for two leading slashes for Windows network shares, but |
||
| 252 | * ensures that all other duplicate slashes are reduced to a single. |
||
| 253 | * |
||
| 254 | * @since 2.2.0 |
||
| 255 | * |
||
| 256 | * @param string $path Path to normalize. |
||
| 257 | * @return string Normalized path. |
||
| 258 | */ |
||
| 259 | protected static function normalize_path( $path ) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Get timestamp from text date |
||
| 276 | * @since 2.2.0 |
||
| 277 | * @param string $value Date value |
||
| 278 | * @param string $date_format Expected date format |
||
| 279 | * @return mixed Unix timestamp representing the date. |
||
| 280 | */ |
||
| 281 | public function get_timestamp_from_value( $value, $date_format ) { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Takes a php date() format string and returns a string formatted to suit for the date/time pickers |
||
| 288 | * It will work with only with the following subset ot date() options: |
||
| 289 | * |
||
| 290 | * d, j, z, m, n, y, and Y. |
||
| 291 | * |
||
| 292 | * A slight effort is made to deal with escaped characters. |
||
| 293 | * |
||
| 294 | * Other options are ignored, because they would either bring compatibility problems between PHP and JS, or |
||
| 295 | * bring even more translation troubles. |
||
| 296 | * |
||
| 297 | * @since 2.2.0 |
||
| 298 | * @param string $format php date format |
||
| 299 | * @return string reformatted string |
||
| 300 | */ |
||
| 301 | public function php_to_js_dateformat( $format ) { |
||
| 335 | |||
| 336 | /** |
||
| 337 | * Helper function for CMB_Utils->php_to_js_dateformat, because php 5.2 was retarded. |
||
| 338 | * @since 2.2.0 |
||
| 339 | * @param $value Value to wrap/escape |
||
| 340 | * @return string Modified value |
||
| 341 | */ |
||
| 342 | public function wrap_escaped_chars( $value ) { |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Send to debug.log if WP_DEBUG is defined and true |
||
| 348 | * |
||
| 349 | * @since 2.2.0 |
||
| 350 | * |
||
| 351 | * @param string $function Function name |
||
| 352 | * @param int $line Line number |
||
| 353 | * @param mixed $msg Message to output |
||
| 354 | * @param mixed $debug Variable to print_r |
||
| 355 | */ |
||
| 356 | public function log_if_debug( $function, $line, $msg, $debug = null ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Determine a file's extension |
||
| 364 | * @since 1.0.0 |
||
| 365 | * @param string $file File url |
||
| 366 | * @return string|false File extension or false |
||
| 367 | */ |
||
| 368 | public function get_file_ext( $file ) { |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get the file name from a url |
||
| 375 | * @since 2.0.0 |
||
| 376 | * @param string $value File url or path |
||
| 377 | * @return string File name |
||
| 378 | */ |
||
| 379 | public function get_file_name_from_path( $value ) { |
||
| 383 | } |
||
| 384 |
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.