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 |
||
| 4 | class autoptimizeCache { |
||
| 5 | private $filename; |
||
| 6 | private $mime; |
||
|
|
|||
| 7 | private $cachedir; |
||
| 8 | private $delayed; |
||
| 9 | |||
| 10 | public function __construct($md5,$ext='php') { |
||
| 11 | $this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
||
| 12 | $this->delayed = AUTOPTIMIZE_CACHE_DELAY; |
||
| 13 | $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
||
| 14 | if($this->nogzip == false) { |
||
| 15 | $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.php'; |
||
| 16 | } else { |
||
| 17 | if (in_array($ext, array("js","css"))) { |
||
| 18 | $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; |
||
| 19 | } else { |
||
| 20 | $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; |
||
| 21 | } |
||
| 22 | } |
||
| 23 | } |
||
| 24 | |||
| 25 | public function check() { |
||
| 26 | if(!file_exists($this->cachedir.$this->filename)) { |
||
| 27 | // No cached file, sorry |
||
| 28 | return false; |
||
| 29 | } |
||
| 30 | // Cache exists! |
||
| 31 | return true; |
||
| 32 | } |
||
| 33 | |||
| 34 | public function retrieve() { |
||
| 35 | if($this->check()) { |
||
| 36 | if($this->nogzip == false) { |
||
| 37 | return file_get_contents($this->cachedir.$this->filename.'.none'); |
||
| 38 | } else { |
||
| 39 | return file_get_contents($this->cachedir.$this->filename); |
||
| 40 | } |
||
| 41 | } |
||
| 42 | return false; |
||
| 43 | } |
||
| 44 | |||
| 45 | public function cache($code,$mime) { |
||
| 46 | if($this->nogzip == false) { |
||
| 47 | $file = ($this->delayed ? 'delayed.php' : 'default.php'); |
||
| 48 | $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'/config/'.$file); |
||
| 49 | $phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode); |
||
| 50 | file_put_contents($this->cachedir.$this->filename,$phpcode, LOCK_EX); |
||
| 51 | file_put_contents($this->cachedir.$this->filename.'.none',$code, LOCK_EX); |
||
| 52 | View Code Duplication | if(!$this->delayed) { |
|
| 53 | // Compress now! |
||
| 54 | file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE), LOCK_EX); |
||
| 55 | file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP), LOCK_EX); |
||
| 56 | } |
||
| 57 | View Code Duplication | } else { |
|
| 58 | // Write code to cache without doing anything else |
||
| 59 | file_put_contents($this->cachedir.$this->filename,$code, LOCK_EX); |
||
| 60 | if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) { |
||
| 61 | // Create an additional cached gzip file |
||
| 62 | file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($code,9,FORCE_GZIP), LOCK_EX); |
||
| 63 | } |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | public function getname() { |
||
| 68 | apply_filters('autoptimize_filter_cache_getname',AUTOPTIMIZE_CACHE_URL.$this->filename); |
||
| 69 | return $this->filename; |
||
| 70 | } |
||
| 71 | |||
| 72 | static function clearall() { |
||
| 73 | if(!autoptimizeCache::cacheavail()) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | |||
| 77 | // scan the cachedirs |
||
| 78 | View Code Duplication | foreach (array("","js","css") as $scandirName) { |
|
| 79 | $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName); |
||
| 80 | } |
||
| 81 | |||
| 82 | // clear the cachedirs |
||
| 83 | foreach ($scan as $scandirName=>$scanneddir) { |
||
| 84 | $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/"; |
||
| 85 | foreach($scanneddir as $file) { |
||
| 86 | if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) { |
||
| 87 | @unlink($thisAoCacheDir.$file); |
||
| 88 | } |
||
| 89 | } |
||
| 90 | } |
||
| 91 | |||
| 92 | @unlink(AUTOPTIMIZE_CACHE_DIR."/.htaccess"); |
||
| 93 | delete_transient("autoptimize_stats"); |
||
| 94 | |||
| 95 | // add cachepurged action |
||
| 96 | if (!function_exists('autoptimize_do_cachepurged_action')) { |
||
| 97 | function autoptimize_do_cachepurged_action() { |
||
| 100 | } |
||
| 101 | add_action("shutdown","autoptimize_do_cachepurged_action",11); |
||
| 102 | |||
| 103 | // try to purge caching plugins cache-files? |
||
| 104 | include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizePageCacheFlush.php'); |
||
| 105 | add_action("autoptimize_action_cachepurged","autoptimize_flush_pagecache",10,0); |
||
| 116 | |||
| 117 | static function stats() { |
||
| 158 | |||
| 159 | static function cacheavail() { |
||
| 247 | |||
| 248 | static function checkCacheDir($dir) { |
||
| 270 | } |
||
| 271 |
This check marks private properties in classes that are never used. Those properties can be removed.