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 autoptimizeCache 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 autoptimizeCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class autoptimizeCache |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Cache filename. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | */ |
||
| 17 | private $filename; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Cache directory path (with a trailing slash). |
||
| 21 | * |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | private $cachedir; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Whether gzipping is done by the web server or us. |
||
| 28 | * True => we don't gzip, the web server does it. |
||
| 29 | * False => we do it ourselves. |
||
| 30 | * |
||
| 31 | * @var bool |
||
| 32 | */ |
||
| 33 | private $nogzip; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Ctor. |
||
| 37 | * |
||
| 38 | * @param string $md5 Hash. |
||
| 39 | * @param string $ext Extension. |
||
| 40 | */ |
||
| 41 | public function __construct( $md5, $ext = 'php' ) |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns true if the cached file exists on disk. |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function check() |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns cache contents if they exist, false otherwise. |
||
| 68 | * |
||
| 69 | * @return string|false |
||
| 70 | */ |
||
| 71 | public function retrieve() |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Stores given $data in cache. |
||
| 85 | * |
||
| 86 | * @param string $data Data to cache. |
||
| 87 | * @param string $mime Mimetype. |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function cache( $data, $mime ) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get cache filename. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getname() |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Returns true if given `$file` is considered a valid Autoptimize cache file, |
||
| 130 | * false otherwise. |
||
| 131 | * |
||
| 132 | * @param string $dir Directory name (with a trailing slash). |
||
| 133 | * @param string $file Filename. |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | protected static function is_valid_cache_file( $dir, $file ) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Clears contents of AUTOPTIMIZE_CACHE_DIR. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | protected static function clear_cache_classic() |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Recursively deletes the specified pathname (file/directory) if possible. |
||
| 172 | * Returns true on success, false otherwise. |
||
| 173 | * |
||
| 174 | * @param string $pathname Pathname to remove. |
||
| 175 | * |
||
| 176 | * @return bool |
||
| 177 | */ |
||
| 178 | protected static function rmdir( $pathname ) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Clears contents of AUTOPTIMIZE_CACHE_DIR by renaming the current |
||
| 195 | * cache directory into a new one with a unique name and then |
||
| 196 | * re-creating the default (empty) cache directory. |
||
| 197 | * |
||
| 198 | * @return bool Returns true when everything is done successfully, false otherwise. |
||
| 199 | */ |
||
| 200 | protected static function clear_cache_via_rename() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns true when advanced cache clearing is enabled. |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public static function advanced_cache_clear_enabled() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Returns a (hopefully) unique new cache folder name for renaming purposes. |
||
| 231 | * |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | protected static function get_unique_name() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Get cache prefix name used in advanced cache clearing mode. |
||
| 244 | * |
||
| 245 | * @return string |
||
| 246 | */ |
||
| 247 | protected static function get_advanced_cache_clear_prefix() |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns an array of file and directory names found within |
||
| 258 | * the given $pathname without '.' and '..' elements. |
||
| 259 | * |
||
| 260 | * @param string $pathname Pathname. |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | protected static function get_dir_contents( $pathname ) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Wipes directories which were created as part of the fast cache clearing |
||
| 271 | * routine (which renames the current cache directory into a new one with |
||
| 272 | * a custom-prefixed unique name). |
||
| 273 | * |
||
| 274 | * @return bool |
||
| 275 | */ |
||
| 276 | public static function delete_advanced_cache_clear_artifacts() |
||
| 277 | { |
||
| 278 | $dir = self::get_pathname_base(); |
||
| 279 | $prefix = self::get_advanced_cache_clear_prefix(); |
||
| 280 | $parent = dirname( $dir ); |
||
| 281 | $ok = false; |
||
| 282 | |||
| 283 | // Returns the list of files without '.' and '..' elements. |
||
| 284 | $files = self::get_dir_contents( $parent ); |
||
| 285 | if ( is_array( $files ) && ! empty( $files ) ) { |
||
| 286 | foreach ( $files as $file ) { |
||
| 287 | $path = $parent . '/' . $file; |
||
| 288 | $prefixed = ( false !== strpos( $path, $prefix ) ); |
||
| 289 | // Removing only our own (prefixed) directories... |
||
| 290 | if ( is_dir( $path ) && $prefixed ) { |
||
| 291 | $ok = self::rmdir( $path ); |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | return $ok; |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Returns the cache directory pathname used. |
||
| 301 | * Done as a function so we canSlightly different |
||
| 302 | * if multisite is used and `autoptimize_separate_blog_caches` filter |
||
| 303 | * is used. |
||
| 304 | * |
||
| 305 | * @return string |
||
| 306 | */ |
||
| 307 | public static function get_pathname() |
||
| 308 | { |
||
| 309 | $pathname = self::get_pathname_base(); |
||
| 310 | |||
| 311 | View Code Duplication | if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
|
| 312 | $blog_id = get_current_blog_id(); |
||
| 313 | $pathname .= $blog_id . '/'; |
||
| 314 | } |
||
| 315 | |||
| 316 | return $pathname; |
||
| 317 | } |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Returns the base path of our cache directory. |
||
| 321 | * |
||
| 322 | * @return string |
||
| 323 | */ |
||
| 324 | protected static function get_pathname_base() |
||
| 325 | { |
||
| 326 | $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; |
||
| 327 | |||
| 328 | return $pathname; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Deletes everything from the cache directories. |
||
| 333 | * |
||
| 334 | * @param bool $propagate Whether to trigger additional actions when cache is purged. |
||
| 335 | * |
||
| 336 | * @return bool |
||
| 337 | */ |
||
| 338 | public static function clearall( $propagate = true ) |
||
| 339 | { |
||
| 340 | if ( ! self::cacheavail() ) { |
||
| 341 | return false; |
||
| 342 | } |
||
| 343 | |||
| 344 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
||
| 345 | if ( self::advanced_cache_clear_enabled() ) { |
||
| 346 | self::clear_cache_via_rename(); |
||
| 347 | } else { |
||
| 348 | self::clear_cache_classic(); |
||
| 349 | } |
||
| 350 | |||
| 351 | // Remove the transient so it gets regenerated... |
||
| 352 | delete_transient( 'autoptimize_stats' ); |
||
| 353 | |||
| 354 | // Cache was just purged, clear page cache and allow others to hook into our purging... |
||
| 355 | if ( true === $propagate ) { |
||
| 356 | if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { |
||
| 357 | function autoptimize_do_cachepurged_action() { |
||
| 360 | } |
||
| 361 | add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); |
||
| 362 | add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); |
||
| 363 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Wrapper for clearall but with false param |
||
| 377 | * to ensure the event is not propagated to others |
||
| 378 | * through our own hooks (to avoid infinite loops). |
||
| 379 | * |
||
| 380 | * @return bool |
||
| 381 | */ |
||
| 382 | public static function clearall_actionless() |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Returns the contents of our cache dirs. |
||
| 389 | * |
||
| 390 | * @return array |
||
| 391 | */ |
||
| 392 | protected static function get_cache_contents() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Returns stats about cached contents. |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public static function stats() |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Performs a scan of cache directory contents and returns an array |
||
| 434 | * with 3 values: count, size, timestamp. |
||
| 435 | * count = total number of found files |
||
| 436 | * size = total filesize (in bytes) of found files |
||
| 437 | * timestamp = unix timestamp when the scan was last performed/finished. |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | protected static function stats_scan() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Ensures the cache directory exists, is writeable and contains the |
||
| 477 | * required .htaccess files. |
||
| 478 | * Returns false in case it fails to ensure any of those things. |
||
| 479 | * |
||
| 480 | * @return bool |
||
| 481 | */ |
||
| 482 | public static function cacheavail() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Ensures the specified `$dir` exists and is writeable. |
||
| 568 | * Returns false if that's not the case. |
||
| 569 | * |
||
| 570 | * @param string $dir Directory to check/create. |
||
| 571 | * |
||
| 572 | * @return bool |
||
| 573 | */ |
||
| 574 | protected static function check_cache_dir( $dir ) |
||
| 597 | |||
| 598 | /** |
||
| 599 | * Flushes as many page cache plugin's caches as possible. |
||
| 600 | * |
||
| 601 | * @return void |
||
| 602 | */ |
||
| 603 | // @codingStandardsIgnoreStart |
||
| 604 | public static function flushPageCache() |
||
| 662 | // @codingStandardsIgnoreEnd |
||
| 663 | } |
||
| 664 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.