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 | View Code Duplication | public static function mbstring_available( $override = null ) |
|
33 | |||
34 | /** |
||
35 | * Returns true when iconv is available. |
||
36 | * |
||
37 | * @param bool|null $override Allows overriding the decision. |
||
38 | * |
||
39 | * @return bool |
||
40 | */ |
||
41 | View Code Duplication | public static function iconv_available( $override = null ) |
|
55 | |||
56 | /** |
||
57 | * Multibyte-capable strpos() if support is available on the server. |
||
58 | * If not, it falls back to using \strpos(). |
||
59 | * |
||
60 | * @param string $haystack Haystack. |
||
61 | * @param string $needle Needle. |
||
62 | * @param int $offset Offset. |
||
63 | * @param string|null $encoding Encoding. Default null. |
||
64 | * |
||
65 | * @return int|false |
||
66 | */ |
||
67 | public static function strpos( $haystack, $needle, $offset = 0, $encoding = null ) |
||
77 | |||
78 | /** |
||
79 | * Attempts to return the number of characters in the given $string if |
||
80 | * mbstring or iconv is available. Returns the number of bytes |
||
81 | * (instead of characters) as fallback. |
||
82 | * |
||
83 | * @param string $string String. |
||
84 | * @param string|null $encoding Encoding. |
||
85 | * |
||
86 | * @return int Number of charcters or bytes in given $string |
||
87 | * (characters if/when supported, bytes otherwise). |
||
88 | */ |
||
89 | public static function strlen( $string, $encoding = null ) |
||
99 | |||
100 | /** |
||
101 | * Our wrapper around implementations of \substr_replace() |
||
102 | * that attempts to not break things horribly if at all possible. |
||
103 | * Uses mbstring and/or iconv if available, before falling back to regular |
||
104 | * substr_replace() (which works just fine in the majority of cases). |
||
105 | * |
||
106 | * @param string $string String. |
||
107 | * @param string $replacement Replacement. |
||
108 | * @param int $start Start offset. |
||
109 | * @param int|null $length Length. |
||
110 | * @param string|null $encoding Encoding. |
||
111 | * |
||
112 | * @return string |
||
113 | */ |
||
114 | public static function substr_replace( $string, $replacement, $start, $length = null, $encoding = null ) |
||
181 | |||
182 | /** |
||
183 | * Wrapper around iconv_substr(). |
||
184 | * |
||
185 | * @param string $s String. |
||
186 | * @param int $start Start offset. |
||
187 | * @param int|null $length Length. |
||
188 | * @param string|null $encoding Encoding. |
||
189 | * |
||
190 | * @return string |
||
191 | */ |
||
192 | protected static function iconv_substr( $s, $start, $length = null, $encoding = null ) |
||
212 | |||
213 | /** |
||
214 | * Decides whether this is a "subdirectory site" or not. |
||
215 | * |
||
216 | * @param bool $override Allows overriding the decision when needed. |
||
217 | * |
||
218 | * @return bool |
||
219 | */ |
||
220 | public static function siteurl_not_root( $override = null ) |
||
235 | |||
236 | /** |
||
237 | * Parse AUTOPTIMIZE_WP_SITE_URL into components using \parse_url(), but do |
||
238 | * so only once per request/lifecycle. |
||
239 | * |
||
240 | * @return array |
||
241 | */ |
||
242 | public static function get_ao_wp_site_url_parts() |
||
252 | |||
253 | /** |
||
254 | * Modify given $cdn_url to include the site path when needed. |
||
255 | * |
||
256 | * @param string $cdn_url CDN URL to tweak. |
||
257 | * @param bool $force_cache_miss Force a cache miss in order to be able |
||
258 | * to re-run the filter. |
||
259 | * |
||
260 | * @return string |
||
261 | */ |
||
262 | public static function tweak_cdn_url_if_needed( $cdn_url, $force_cache_miss = false ) |
||
290 | |||
291 | /** |
||
292 | * When siteurl contans a path other than '/' and the CDN URL does not have |
||
293 | * a path or it's path is '/', this will modify the CDN URL's path component |
||
294 | * to match that of the siteurl. |
||
295 | * This is to support "magic" CDN urls that worked that way before v2.4... |
||
296 | * |
||
297 | * @param array $site_url_parts Site URL components array. |
||
298 | * @param array $cdn_url_parts CDN URL components array. |
||
299 | * |
||
300 | * @return array|false |
||
301 | */ |
||
302 | public static function maybe_replace_cdn_path( array $site_url_parts, array $cdn_url_parts ) |
||
313 | |||
314 | /** |
||
315 | * Given an array or components returned from \parse_url(), assembles back |
||
316 | * the complete URL. |
||
317 | * If optional |
||
318 | * |
||
319 | * @param array $parsed_url URL components array. |
||
320 | * @param bool $schemeless Whether the assembled URL should be |
||
321 | * protocol-relative (schemeless) or not. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | public static function assemble_parsed_url( array $parsed_url, $schemeless = false ) |
||
342 | |||
343 | /** |
||
344 | * Returns true if given $url is protocol-relative. |
||
345 | * |
||
346 | * @param string $url URL to check. |
||
347 | * |
||
348 | * @return bool |
||
349 | */ |
||
350 | public static function is_protocol_relative( $url ) |
||
360 | |||
361 | /** |
||
362 | * Canonicalizes the given path regardless of it existing or not. |
||
363 | * |
||
364 | * @param string $path Path to normalize. |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public static function path_canonicalize( $path ) |
||
385 | |||
386 | /** |
||
387 | * Checks to see if 3rd party services are available and stores result in option |
||
388 | * |
||
389 | * @param string $return_result should we return resulting service status array (default no). |
||
390 | * |
||
391 | * @return none if $return_result is false (default), array if $return_result is true. |
||
392 | */ |
||
393 | public static function check_service_availability( $return_result = false ) |
||
408 | |||
409 | /** |
||
410 | * Returns true if the string is a valid regex. |
||
411 | * |
||
412 | * @param string $string String, duh. |
||
413 | * |
||
414 | * @return bool |
||
415 | */ |
||
416 | public static function str_is_valid_regex( $string ) |
||
424 | } |
||
425 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.