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 Cache 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 Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class Cache |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var \Memcache |
||
| 21 | */ |
||
| 22 | protected $memcache = null; |
||
| 23 | |||
| 24 | const JSON = 1; |
||
| 25 | const TEXT = 2; |
||
| 26 | const GZIP = 3; |
||
| 27 | const JSONGZ = 4; |
||
| 28 | const MEMCACHE = 5; |
||
| 29 | |||
| 30 | const CACHE_SESSION_VAR = '__CACHE__'; |
||
| 31 | |||
| 32 | use SingletonTrait; |
||
| 33 | |||
| 34 | 3 | public function __construct() |
|
| 35 | { |
||
| 36 | 3 | $this->setLoaded(true); |
|
| 37 | 3 | } |
|
| 38 | |||
| 39 | /** |
||
| 40 | * @return bool |
||
| 41 | */ |
||
| 42 | 3 | public static function canUseMemcache() |
|
| 43 | { |
||
| 44 | 3 | return Config::getParam('psfs.memcache', false) && !Config::getParam('debug') && class_exists('Memcached'); |
|
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param string $data |
||
| 49 | * @param string $path |
||
| 50 | * @throws GeneratorException |
||
| 51 | * @throws ConfigException |
||
| 52 | */ |
||
| 53 | 8 | View Code Duplication | private function saveTextToFile($data, $path) |
| 54 | { |
||
| 55 | 8 | GeneratorHelper::createDir(dirname($path)); |
|
| 56 | 8 | if (false === FileHelper::writeFile($path, $data)) { |
|
| 57 | 1 | throw new ConfigException(_('No se tienen los permisos suficientes para escribir en el fichero ') . $path); |
|
| 58 | } |
||
| 59 | 7 | } |
|
| 60 | |||
| 61 | /** |
||
| 62 | * @param string $path |
||
| 63 | * @param int $transform |
||
| 64 | * @param boolean $absolute |
||
| 65 | * @return mixed |
||
| 66 | */ |
||
| 67 | 9 | public function getDataFromFile($path, $transform = Cache::TEXT, $absolute = false) |
|
| 68 | { |
||
| 69 | 9 | Logger::log('Gathering data from cache', LOG_DEBUG, ['path' => $path]); |
|
| 70 | 9 | $data = null; |
|
| 71 | 9 | $absolutePath = $absolute ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
| 72 | 9 | if (file_exists($absolutePath)) { |
|
| 73 | 6 | $data = FileHelper::readFile($absolutePath); |
|
| 74 | } |
||
| 75 | 9 | return self::extractDataWithFormat($data, $transform); |
|
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $path |
||
| 80 | * @param int $expires |
||
| 81 | * @param boolean $absolute |
||
| 82 | * @return bool |
||
| 83 | */ |
||
| 84 | 1 | private function hasExpiredCache($path, $expires = 300, $absolute = false) |
|
| 85 | { |
||
| 86 | 1 | Logger::log('Checking expiration', LOG_DEBUG, ['path' => $path]); |
|
| 87 | 1 | $absolutePath = ($absolute) ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
| 88 | 1 | $lasModificationDate = filemtime($absolutePath); |
|
| 89 | 1 | return ($lasModificationDate + $expires <= time()); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $data |
||
|
|
|||
| 94 | * @param int $transform |
||
| 95 | * @return mixed |
||
| 96 | */ |
||
| 97 | 9 | View Code Duplication | public static function extractDataWithFormat($data = null, $transform = Cache::TEXT) |
| 98 | { |
||
| 99 | 9 | Logger::log('Extracting data from cache'); |
|
| 100 | switch ($transform) { |
||
| 101 | 9 | case self::JSON: |
|
| 102 | 9 | $data = json_decode($data, true); |
|
| 103 | 9 | break; |
|
| 104 | 5 | case self::JSONGZ: |
|
| 105 | 5 | $data = self::extractDataWithFormat($data, self::GZIP); |
|
| 106 | 5 | $data = self::extractDataWithFormat($data, self::JSON); |
|
| 107 | 5 | break; |
|
| 108 | 5 | case self::GZIP: |
|
| 109 | 5 | if (null !== $data && function_exists('gzuncompress')) { |
|
| 110 | 2 | $data = @gzuncompress($data ?: ''); |
|
| 111 | } |
||
| 112 | 5 | break; |
|
| 113 | } |
||
| 114 | 9 | return $data; |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string $data |
||
| 119 | * @param int $transform |
||
| 120 | * @return string |
||
| 121 | */ |
||
| 122 | 8 | View Code Duplication | public static function transformData($data, $transform = Cache::TEXT) |
| 141 | |||
| 142 | /** |
||
| 143 | * @param string $path |
||
| 144 | * @param mixed $data |
||
| 145 | * @param int $transform |
||
| 146 | * @param bool $absolute |
||
| 147 | * @throws GeneratorException |
||
| 148 | * @throws ConfigException |
||
| 149 | */ |
||
| 150 | 8 | public function storeData($path, $data, $transform = Cache::TEXT, $absolute = false) |
|
| 151 | { |
||
| 152 | 8 | Logger::log('Store data in cache', LOG_DEBUG, ['path' => $path]); |
|
| 153 | 8 | $data = self::transformData($data, $transform); |
|
| 154 | 8 | $absolutePath = $absolute ? $path : CACHE_DIR . DIRECTORY_SEPARATOR . $path; |
|
| 155 | 8 | $this->saveTextToFile($data, $absolutePath); |
|
| 156 | 7 | } |
|
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $path |
||
| 160 | * @param int $expires |
||
| 161 | * @param null $function |
||
| 162 | * @param int $transform |
||
| 163 | * @return mixed|null |
||
| 164 | * @throws GeneratorException |
||
| 165 | * @throws ConfigException |
||
| 166 | */ |
||
| 167 | 1 | public function readFromCache($path, $expires = 300, $function = null, $transform = Cache::TEXT) |
|
| 168 | { |
||
| 169 | 1 | $data = null; |
|
| 170 | 1 | Logger::log('Reading data from cache', LOG_DEBUG, ['path' => $path]); |
|
| 171 | 1 | if (file_exists(CACHE_DIR . DIRECTORY_SEPARATOR . $path)) { |
|
| 172 | 1 | if (is_callable($function) && $this->hasExpiredCache($path, $expires)) { |
|
| 173 | 1 | $data = $function(); |
|
| 174 | 1 | $this->storeData($path, $data, $transform, false, $expires); |
|
| 175 | } else { |
||
| 176 | 1 | $data = $this->getDataFromFile($path, $transform); |
|
| 177 | } |
||
| 178 | } |
||
| 179 | 1 | return $data; |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | 1 | private static function checkAdminSite() |
|
| 186 | { |
||
| 187 | 1 | return Security::getInstance()->canAccessRestrictedAdmin(); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @return integer|boolean |
||
| 192 | */ |
||
| 193 | 1 | public static function needCache() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | 2 | public function getRequestCacheHash() |
|
| 211 | { |
||
| 212 | 2 | $hashPath = null; |
|
| 213 | 2 | $filename = null; |
|
| 214 | 2 | $action = Security::getInstance()->getSessionKey(self::CACHE_SESSION_VAR); |
|
| 215 | 2 | Logger::log('Gathering cache hash for request', LOG_DEBUG, $action); |
|
| 225 | |||
| 226 | 1 | public function flushCache() { |
|
| 239 | } |
||
| 240 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.