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 autoptimizeUtils 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 autoptimizeUtils, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class autoptimizeUtils |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Returns true when mbstring is available. |
||
| 14 | * |
||
| 15 | * @param bool|null $override Allows overriding the decision. |
||
| 16 | * |
||
| 17 | * @return bool |
||
| 18 | */ |
||
| 19 | public static function mbstring_available( $override = null ) |
||
| 33 | |||
| 34 | /** |
||
| 35 | * Multibyte-capable strpos() if support is available on the server. |
||
| 36 | * If not, it falls back to using \strpos(). |
||
| 37 | * |
||
| 38 | * @param string $haystack Haystack. |
||
| 39 | * @param string $needle Needle. |
||
| 40 | * @param int $offset Offset. |
||
| 41 | * @param string|null $encoding Encoding. Default null. |
||
| 42 | * |
||
| 43 | * @return int|false |
||
| 44 | */ |
||
| 45 | public static function strpos( $haystack, $needle, $offset = 0, $encoding = null ) |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Attempts to return the number of characters in the given $string if |
||
| 56 | * mbstring is available. Returns the number of bytes |
||
| 57 | * (instead of characters) as fallback. |
||
| 58 | * |
||
| 59 | * @param string $string String. |
||
| 60 | * @param string|null $encoding Encoding. |
||
| 61 | * |
||
| 62 | * @return int Number of charcters or bytes in given $string |
||
| 63 | * (characters if/when supported, bytes otherwise). |
||
| 64 | */ |
||
| 65 | public static function strlen( $string, $encoding = null ) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Our wrapper around implementations of \substr_replace() |
||
| 76 | * that attempts to not break things horribly if at all possible. |
||
| 77 | * Uses mbstring if available, before falling back to regular |
||
| 78 | * substr_replace() (which works just fine in the majority of cases). |
||
| 79 | * |
||
| 80 | * @param string $string String. |
||
| 81 | * @param string $replacement Replacement. |
||
| 82 | * @param int $start Start offset. |
||
| 83 | * @param int|null $length Length. |
||
| 84 | * @param string|null $encoding Encoding. |
||
| 85 | * |
||
| 86 | * @return string |
||
| 87 | */ |
||
| 88 | public static function substr_replace( $string, $replacement, $start, $length = null, $encoding = null ) |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Decides whether this is a "subdirectory site" or not. |
||
| 130 | * |
||
| 131 | * @param bool $override Allows overriding the decision when needed. |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | */ |
||
| 135 | public static function siteurl_not_root( $override = null ) |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Parse AUTOPTIMIZE_WP_SITE_URL into components using \parse_url(), but do |
||
| 153 | * so only once per request/lifecycle. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | */ |
||
| 157 | public static function get_ao_wp_site_url_parts() |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Modify given $cdn_url to include the site path when needed. |
||
| 170 | * |
||
| 171 | * @param string $cdn_url CDN URL to tweak. |
||
| 172 | * @param bool $force_cache_miss Force a cache miss in order to be able |
||
| 173 | * to re-run the filter. |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public static function tweak_cdn_url_if_needed( $cdn_url, $force_cache_miss = false ) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * When siteurl contans a path other than '/' and the CDN URL does not have |
||
| 208 | * a path or it's path is '/', this will modify the CDN URL's path component |
||
| 209 | * to match that of the siteurl. |
||
| 210 | * This is to support "magic" CDN urls that worked that way before v2.4... |
||
| 211 | * |
||
| 212 | * @param array $site_url_parts Site URL components array. |
||
| 213 | * @param array $cdn_url_parts CDN URL components array. |
||
| 214 | * |
||
| 215 | * @return array|false |
||
| 216 | */ |
||
| 217 | public static function maybe_replace_cdn_path( array $site_url_parts, array $cdn_url_parts ) |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Given an array or components returned from \parse_url(), assembles back |
||
| 231 | * the complete URL. |
||
| 232 | * If optional |
||
| 233 | * |
||
| 234 | * @param array $parsed_url URL components array. |
||
| 235 | * @param bool $schemeless Whether the assembled URL should be |
||
| 236 | * protocol-relative (schemeless) or not. |
||
| 237 | * |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public static function assemble_parsed_url( array $parsed_url, $schemeless = false ) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Returns true if given $url is protocol-relative. |
||
| 260 | * |
||
| 261 | * @param string $url URL to check. |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public static function is_protocol_relative( $url ) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Canonicalizes the given path regardless of it existing or not. |
||
| 278 | * |
||
| 279 | * @param string $path Path to normalize. |
||
| 280 | * |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | public static function path_canonicalize( $path ) |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Checks to see if 3rd party services are available and stores result in option |
||
| 303 | * |
||
| 304 | * @param string $return_result should we return resulting service status array (default no). |
||
| 305 | * |
||
| 306 | * @return none if $return_result is false (default), array if $return_result is true. |
||
| 307 | */ |
||
| 308 | public static function check_service_availability( $return_result = false ) |
||
| 323 | |||
| 324 | /** |
||
| 325 | * Returns true if the string is a valid regex. |
||
| 326 | * |
||
| 327 | * @param string $string String, duh. |
||
| 328 | * |
||
| 329 | * @return bool |
||
| 330 | */ |
||
| 331 | public static function str_is_valid_regex( $string ) |
||
| 339 | } |
||
| 340 |