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' ) |
||
| 42 | { |
||
| 43 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
||
| 44 | $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
||
| 45 | if ( ! $this->nogzip ) { |
||
| 46 | $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.php'; |
||
| 47 | } else { |
||
| 48 | if ( in_array( $ext, array( 'js', 'css' ) ) ) { |
||
| 49 | $this->filename = $ext . '/' . AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.' . $ext; |
||
| 50 | } else { |
||
| 51 | $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX . $md5 . '.' . $ext; |
||
| 52 | } |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Returns true if the cached file exists on disk. |
||
| 58 | * |
||
| 59 | * @return bool |
||
| 60 | */ |
||
| 61 | public function check() |
||
| 62 | { |
||
| 63 | return file_exists( $this->cachedir . $this->filename ); |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns cache contents if they exist, false otherwise. |
||
| 68 | * |
||
| 69 | * @return string|false |
||
| 70 | */ |
||
| 71 | public function retrieve() |
||
| 72 | { |
||
| 73 | if ( $this->check() ) { |
||
| 74 | if ( false == $this->nogzip ) { |
||
|
|
|||
| 75 | return file_get_contents( $this->cachedir . $this->filename . '.none' ); |
||
| 76 | } else { |
||
| 77 | return file_get_contents( $this->cachedir . $this->filename ); |
||
| 78 | } |
||
| 79 | } |
||
| 80 | return false; |
||
| 81 | } |
||
| 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 ) |
||
| 92 | { |
||
| 93 | if ( false === $this->nogzip ) { |
||
| 94 | // We handle gzipping ourselves. |
||
| 95 | $file = 'default.php'; |
||
| 96 | $phpcode = file_get_contents( AUTOPTIMIZE_PLUGIN_DIR . 'config/' . $file ); |
||
| 97 | $phpcode = str_replace( array( '%%CONTENT%%', 'exit;' ), array( $mime, '' ), $phpcode ); |
||
| 98 | |||
| 99 | file_put_contents( $this->cachedir . $this->filename, $phpcode ); |
||
| 100 | file_put_contents( $this->cachedir . $this->filename . '.none', $data ); |
||
| 101 | } else { |
||
| 102 | // Write code to cache without doing anything else. |
||
| 103 | file_put_contents( $this->cachedir . $this->filename, $data ); |
||
| 104 | if ( apply_filters( 'autoptimize_filter_cache_create_static_gzip', false ) ) { |
||
| 105 | // Create an additional cached gzip file. |
||
| 106 | file_put_contents( $this->cachedir . $this->filename . '.gz', gzencode( $data, 9, FORCE_GZIP ) ); |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Get cache filename. |
||
| 113 | * |
||
| 114 | * @return string |
||
| 115 | */ |
||
| 116 | public function getname() |
||
| 117 | { |
||
| 118 | // NOTE: This could've maybe been a do_action() instead, however, |
||
| 119 | // that ship has sailed. |
||
| 120 | // The original idea here was to provide 3rd party code a hook so that |
||
| 121 | // it can "listen" to all the complete autoptimized-urls that the page |
||
| 122 | // will emit... Or something to that effect I think? |
||
| 123 | apply_filters( 'autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL . $this->filename ); |
||
| 124 | |||
| 125 | return $this->filename; |
||
| 126 | } |
||
| 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 ) |
||
| 137 | { |
||
| 138 | if ( '.' !== $file && '..' !== $file && |
||
| 139 | false !== strpos( $file, AUTOPTIMIZE_CACHEFILE_PREFIX ) && |
||
| 140 | is_file( $dir . $file ) ) { |
||
| 141 | |||
| 142 | // It's a valid file! |
||
| 143 | return true; |
||
| 144 | } |
||
| 145 | |||
| 146 | // Everything else is considered invalid! |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Clears contents of AUTOPTIMIZE_CACHE_DIR. |
||
| 152 | * |
||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | protected static function clear_cache_classic() |
||
| 156 | { |
||
| 157 | $contents = self::get_cache_contents(); |
||
| 158 | foreach ( $contents as $name => $files ) { |
||
| 159 | $dir = rtrim( AUTOPTIMIZE_CACHE_DIR . $name, '/' ) . '/'; |
||
| 160 | foreach ( $files as $file ) { |
||
| 161 | if ( self::is_valid_cache_file( $dir, $file ) ) { |
||
| 162 | @unlink( $dir . $file ); // @codingStandardsIgnoreLine |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | @unlink( AUTOPTIMIZE_CACHE_DIR . '/.htaccess' ); // @codingStandardsIgnoreLine |
||
| 168 | } |
||
| 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 ) |
||
| 179 | { |
||
| 180 | $files = self::get_dir_contents( $pathname ); |
||
| 181 | foreach ( $files as $file ) { |
||
| 182 | $path = $pathname . '/' . $file; |
||
| 183 | if ( is_dir( $path ) ) { |
||
| 184 | self::rmdir( $path ); |
||
| 185 | } else { |
||
| 186 | unlink( $path ); |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | return rmdir( $pathname ); |
||
| 191 | } |
||
| 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() |
||
| 201 | { |
||
| 202 | $ok = false; |
||
| 203 | $dir = self::get_pathname_base(); |
||
| 204 | $new_name = self::get_unique_name(); |
||
| 205 | |||
| 206 | // Makes sure the new pathname is on the same level... |
||
| 207 | $new_pathname = dirname( $dir ) . '/' . $new_name; |
||
| 208 | $renamed = @rename( $dir, $new_pathname ); // @codingStandardsIgnoreLine |
||
| 209 | |||
| 210 | // When renamed, re-create the default cache directory back so it's |
||
| 211 | // available again... |
||
| 212 | if ( $renamed ) { |
||
| 213 | $ok = self::cacheavail(); |
||
| 214 | } |
||
| 215 | |||
| 216 | return $ok; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Returns true when advanced cache clearing is enabled. |
||
| 221 | * |
||
| 222 | * @return bool |
||
| 223 | */ |
||
| 224 | public static function advanced_cache_clear_enabled() |
||
| 225 | { |
||
| 226 | return apply_filters( 'autoptimize_filter_cache_clear_advanced', false ); |
||
| 227 | } |
||
| 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() |
||
| 235 | { |
||
| 236 | $prefix = self::get_advanced_cache_clear_prefix(); |
||
| 237 | $new_name = uniqid( $prefix, true ); |
||
| 238 | |||
| 239 | return $new_name; |
||
| 240 | } |
||
| 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() |
||
| 248 | { |
||
| 249 | $pathname = self::get_pathname_base(); |
||
| 250 | $basename = basename( $pathname ); |
||
| 251 | $prefix = $basename . '-'; |
||
| 252 | |||
| 253 | return $prefix; |
||
| 254 | } |
||
| 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 ) |
||
| 265 | { |
||
| 266 | return array_slice( scandir( $pathname ), 2 ); |
||
| 267 | } |
||
| 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 | foreach ( $files as $file ) { |
||
| 286 | $path = $parent . '/' . $file; |
||
| 287 | $prefixed = ( false !== strpos( $path, $prefix ) ); |
||
| 288 | // Removing only our own (prefixed) directories... |
||
| 289 | if ( is_dir( $path ) && $prefixed ) { |
||
| 290 | $ok = self::rmdir( $path ); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | return $ok; |
||
| 295 | } |
||
| 296 | |||
| 297 | /** |
||
| 298 | * Returns the cache directory pathname used. |
||
| 299 | * Done as a function so we canSlightly different |
||
| 300 | * if multisite is used and `autoptimize_separate_blog_caches` filter |
||
| 301 | * is used. |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | public static function get_pathname() |
||
| 306 | { |
||
| 307 | $pathname = self::get_pathname_base(); |
||
| 308 | |||
| 309 | if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
||
| 310 | $blog_id = get_current_blog_id(); |
||
| 311 | $pathname .= $blog_id . '/'; |
||
| 312 | } |
||
| 313 | |||
| 314 | return $pathname; |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the base path of our cache directory. |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | */ |
||
| 322 | protected static function get_pathname_base() |
||
| 323 | { |
||
| 324 | $pathname = WP_CONTENT_DIR . AUTOPTIMIZE_CACHE_CHILD_DIR; |
||
| 325 | |||
| 326 | return $pathname; |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Deletes everything from the cache directories. |
||
| 331 | * |
||
| 332 | * @param bool $propagate Whether to trigger additional actions when cache is purged. |
||
| 333 | * |
||
| 334 | * @return bool |
||
| 335 | */ |
||
| 336 | public static function clearall( $propagate = true ) |
||
| 337 | { |
||
| 338 | if ( ! self::cacheavail() ) { |
||
| 339 | return false; |
||
| 340 | } |
||
| 341 | |||
| 342 | // TODO/FIXME: If cache is big, switch to advanced/new cache clearing automatically? |
||
| 343 | if ( self::advanced_cache_clear_enabled() ) { |
||
| 344 | self::clear_cache_via_rename(); |
||
| 345 | } else { |
||
| 346 | self::clear_cache_classic(); |
||
| 347 | } |
||
| 348 | |||
| 349 | // Remove the transient so it gets regenerated... |
||
| 350 | delete_transient( 'autoptimize_stats' ); |
||
| 351 | |||
| 352 | // Cache was just purged, clear page cache and allow others to hook into our purging... |
||
| 353 | if ( true === $propagate ) { |
||
| 354 | if ( ! function_exists( 'autoptimize_do_cachepurged_action' ) ) { |
||
| 355 | function autoptimize_do_cachepurged_action() { |
||
| 358 | } |
||
| 359 | add_action( 'shutdown', 'autoptimize_do_cachepurged_action', 11 ); |
||
| 360 | add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCache', 'flushPageCache' ), 10, 0 ); |
||
| 361 | } |
||
| 362 | |||
| 363 | // Warm cache (part of speedupper)! |
||
| 364 | if ( apply_filters( 'autoptimize_filter_speedupper', true ) ) { |
||
| 365 | $url = site_url() . '/?ao_speedup_cachebuster=' . rand( 1, 100000 ); |
||
| 366 | $cache = @wp_remote_get( $url ); // @codingStandardsIgnoreLine |
||
| 367 | unset( $cache ); |
||
| 368 | } |
||
| 369 | |||
| 370 | return true; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Wrapper for clearall but with false param |
||
| 375 | * to ensure the event is not propagated to others |
||
| 376 | * through our own hooks (to avoid infinite loops). |
||
| 377 | * |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | public static function clearall_actionless() |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the contents of our cache dirs. |
||
| 387 | * |
||
| 388 | * @return array |
||
| 389 | */ |
||
| 390 | protected static function get_cache_contents() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Returns stats about cached contents. |
||
| 403 | * |
||
| 404 | * @return array |
||
| 405 | */ |
||
| 406 | public static function stats() |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Performs a scan of cache directory contents and returns an array |
||
| 432 | * with 3 values: count, size, timestamp. |
||
| 433 | * count = total number of found files |
||
| 434 | * size = total filesize (in bytes) of found files |
||
| 435 | * timestamp = unix timestamp when the scan was last performed/finished. |
||
| 436 | * |
||
| 437 | * @return array |
||
| 438 | */ |
||
| 439 | protected static function stats_scan() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Ensures the cache directory exists, is writeable and contains the |
||
| 475 | * required .htaccess files. |
||
| 476 | * Returns false in case it fails to ensure any of those things. |
||
| 477 | * |
||
| 478 | * @return bool |
||
| 479 | */ |
||
| 480 | public static function cacheavail() |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Ensures the specified `$dir` exists and is writeable. |
||
| 566 | * Returns false if that's not the case. |
||
| 567 | * |
||
| 568 | * @param string $dir Directory to check/create. |
||
| 569 | * |
||
| 570 | * @return bool |
||
| 571 | */ |
||
| 572 | protected static function check_cache_dir( $dir ) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Flushes as many page cache plugin's caches as possible. |
||
| 598 | * |
||
| 599 | * @return void |
||
| 600 | */ |
||
| 601 | // @codingStandardsIgnoreStart |
||
| 602 | public static function flushPageCache() |
||
| 660 | // @codingStandardsIgnoreEnd |
||
| 661 | } |
||
| 662 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.