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:
| 1 | <?php |
||
| 16 | class CacheFile extends Cache { |
||
| 17 | |||
| 18 | |||
| 19 | /** |
||
| 20 | * Constructor |
||
| 21 | * |
||
| 22 | * @param array $ar_cfg |
||
| 23 | */ |
||
| 24 | public function __construct ($ar_cfg = array()) { |
||
| 29 | |||
| 30 | |||
| 31 | /** |
||
| 32 | * Check if cache is ready for use. |
||
| 33 | * |
||
| 34 | * @return boolean |
||
| 35 | */ |
||
| 36 | public function ChkCfg () { |
||
| 61 | |||
| 62 | |||
| 63 | /** |
||
| 64 | * Check config/cache store dir valid and writable |
||
| 65 | * If error, return error msg, else return empty str. |
||
| 66 | * |
||
| 67 | * @param string $dir |
||
| 68 | * @return string |
||
| 69 | */ |
||
| 70 | public function ChkCfgFileDir ($dir) { |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Check cache rule exist and valid |
||
| 91 | * If error, return error msg, else return empty str. |
||
| 92 | * |
||
| 93 | * @param string $rule |
||
| 94 | * @return string |
||
| 95 | */ |
||
| 96 | View Code Duplication | public function ChkCfgFileRule($rule) { |
|
| 105 | |||
| 106 | |||
| 107 | /** |
||
| 108 | * Delete cache data |
||
| 109 | * @param string $key |
||
| 110 | * @return $this |
||
| 111 | */ |
||
| 112 | public function Del ($key) { |
||
| 121 | |||
| 122 | |||
| 123 | /** |
||
| 124 | * Is cache data expire ? |
||
| 125 | * |
||
| 126 | * File cache does not keep lifetime in cache, |
||
| 127 | * so it need a lifetime from outside, |
||
| 128 | * or use default lifetime config. |
||
| 129 | * |
||
| 130 | * @param string $key |
||
| 131 | * @param int $lifetime Cache lifetime, in second. |
||
| 132 | * @return boolean True means it IS expired. |
||
| 133 | */ |
||
| 134 | public function Expire ($key, $lifetime = NULL) { |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Compute path of a key's data file |
||
| 155 | * |
||
| 156 | * @param string $key |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | View Code Duplication | public function FilePath ($key) { |
|
| 174 | |||
| 175 | |||
| 176 | /** |
||
| 177 | * Compute name of a key's data file |
||
| 178 | * |
||
| 179 | * @param string $key |
||
| 180 | * @return string |
||
| 181 | */ |
||
| 182 | protected function FilePathFilename($key) { |
||
| 185 | |||
| 186 | |||
| 187 | /** |
||
| 188 | * Compute path of a key by a single rule section |
||
| 189 | * |
||
| 190 | * @param string $rule |
||
| 191 | * @param string $key |
||
| 192 | * @return string |
||
| 193 | * @see $sCacheRule |
||
| 194 | */ |
||
| 195 | View Code Duplication | protected function FilePathSec($rule, $key) { |
|
| 227 | |||
| 228 | |||
| 229 | /** |
||
| 230 | * Read cache and return value |
||
| 231 | * |
||
| 232 | * File cache should check lifetime when get, |
||
| 233 | * return NULL when fail. |
||
| 234 | * |
||
| 235 | * @param string $key |
||
| 236 | * @param int $lifetime Cache lifetime, 0/no check, NULL/cfg |
||
| 237 | * @return mixed |
||
| 238 | */ |
||
| 239 | public function Get ($key, $lifetime = NULL) { |
||
| 258 | |||
| 259 | |||
| 260 | /** |
||
| 261 | * Init treatment |
||
| 262 | * |
||
| 263 | * @param array $ar_cfg |
||
| 264 | * @return object |
||
| 265 | */ |
||
| 266 | public function Init () { |
||
| 273 | |||
| 274 | |||
| 275 | /** |
||
| 276 | * Write data to cache |
||
| 277 | * |
||
| 278 | * Lifetime check when get. |
||
| 279 | * |
||
| 280 | * @param string $key |
||
| 281 | * @param mixed $val |
||
| 282 | * @param int $lifetime |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | View Code Duplication | public function Set ($key, $val, $lifetime = NULL) { |
|
| 299 | |||
| 300 | |||
| 301 | /** |
||
| 302 | * Set default config |
||
| 303 | * |
||
| 304 | * @return this |
||
| 305 | */ |
||
| 306 | View Code Duplication | protected function SetCfgDefault () { |
|
| 333 | |||
| 334 | |||
| 335 | } // end of class CacheFile |
||
| 336 | ?> |
||
| 337 |