Complex classes like TCache_Lite 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 TCache_Lite, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 46 | class TCache_Lite |
||
|
|
|||
| 47 | { |
||
| 48 | |||
| 49 | // --- Private properties --- |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Directory where to put the cache files |
||
| 53 | * (make sure to add a trailing slash) |
||
| 54 | * |
||
| 55 | * @var string $_cacheDir |
||
| 56 | */ |
||
| 57 | protected $_cacheDir = '/tmp/'; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Enable / disable caching |
||
| 61 | * |
||
| 62 | * (can be very usefull for the debug of cached scripts) |
||
| 63 | * |
||
| 64 | * @var boolean $_caching |
||
| 65 | */ |
||
| 66 | protected $_caching = true; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Cache lifetime (in seconds) |
||
| 70 | * |
||
| 71 | * @var int $_lifeTime |
||
| 72 | */ |
||
| 73 | protected $_lifeTime = 3600; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Enable / disable fileLocking |
||
| 77 | * |
||
| 78 | * (can avoid cache corruption under bad circumstances) |
||
| 79 | * |
||
| 80 | * @var boolean $_fileLocking |
||
| 81 | */ |
||
| 82 | protected $_fileLocking = true; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Timestamp of the last valid cache |
||
| 86 | * |
||
| 87 | * @var int $_refreshTime |
||
| 88 | */ |
||
| 89 | protected $_refreshTime; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * File name (with path) |
||
| 93 | * |
||
| 94 | * @var string $_file |
||
| 95 | */ |
||
| 96 | protected $_file; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Enable / disable write control (the cache is read just after writing |
||
| 100 | * to detect corrupt entries) |
||
| 101 | * |
||
| 102 | * Enable write control will lightly slow the cache writing but not the |
||
| 103 | * cache reading. Write control can detect some corrupt cache files but |
||
| 104 | * maybe it's not a perfect control |
||
| 105 | * |
||
| 106 | * @var boolean $_writeControl |
||
| 107 | */ |
||
| 108 | protected $_writeControl = true; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Enable / disable read control |
||
| 112 | * |
||
| 113 | * If enabled, a control key is embeded in cache file and this key is |
||
| 114 | * compared with the one calculated after the reading. |
||
| 115 | * |
||
| 116 | * @var boolean $_writeControl |
||
| 117 | */ |
||
| 118 | protected $_readControl = true; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Type of read control (only if read control is enabled) |
||
| 122 | * |
||
| 123 | * Available values are : |
||
| 124 | * 'md5' for a md5 hash control (best but slowest) |
||
| 125 | * 'crc32' for a crc32 hash control (lightly less safe but faster, |
||
| 126 | * better choice) |
||
| 127 | * 'strlen' for a length only test (fastest) |
||
| 128 | * |
||
| 129 | * @var boolean $_readControlType |
||
| 130 | */ |
||
| 131 | protected $_readControlType = 'crc32'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Current cache id |
||
| 135 | * |
||
| 136 | * @var string $_id |
||
| 137 | */ |
||
| 138 | protected $_id; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Current cache group |
||
| 142 | * |
||
| 143 | * @var string $_group |
||
| 144 | */ |
||
| 145 | protected $_group; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Enable / Disable "Memory Caching" |
||
| 149 | * |
||
| 150 | * NB : There is no lifetime for memory caching ! |
||
| 151 | * |
||
| 152 | * @var boolean $_memoryCaching |
||
| 153 | */ |
||
| 154 | protected $_memoryCaching = false; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Enable / Disable "Only Memory Caching" |
||
| 158 | * (be carefull, memory caching is "beta quality") |
||
| 159 | * |
||
| 160 | * @var boolean $_onlyMemoryCaching |
||
| 161 | */ |
||
| 162 | protected $_onlyMemoryCaching = false; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Memory caching array |
||
| 166 | * |
||
| 167 | * @var array $_memoryCachingArray |
||
| 168 | */ |
||
| 169 | protected $_memoryCachingArray = array(); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Memory caching counter |
||
| 173 | * |
||
| 174 | * @var int $memoryCachingCounter |
||
| 175 | */ |
||
| 176 | protected $_memoryCachingCounter = 0; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Memory caching limit |
||
| 180 | * |
||
| 181 | * @var int $memoryCachingLimit |
||
| 182 | */ |
||
| 183 | protected $_memoryCachingLimit = 1000; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * File Name protection |
||
| 187 | * |
||
| 188 | * if set to true, you can use any cache id or group name |
||
| 189 | * if set to false, it can be faster but cache ids and group names |
||
| 190 | * will be used directly in cache file names so be carefull with |
||
| 191 | * special characters... |
||
| 192 | * |
||
| 193 | * @var boolean $fileNameProtection |
||
| 194 | */ |
||
| 195 | protected $_fileNameProtection = true; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Enable / disable automatic serialization |
||
| 199 | * |
||
| 200 | * it can be used to save directly datas which aren't strings |
||
| 201 | * (but it's slower) |
||
| 202 | * |
||
| 203 | * @var boolean $_serialize |
||
| 204 | */ |
||
| 205 | protected $_automaticSerialization = false; |
||
| 206 | |||
| 207 | // --- Public methods --- |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Constructor |
||
| 211 | * |
||
| 212 | * $options is an assoc. Available options are : |
||
| 213 | * $options = array( |
||
| 214 | * 'cacheDir' => directory where to put the cache files (string), |
||
| 215 | * 'caching' => enable / disable caching (boolean), |
||
| 216 | * 'lifeTime' => cache lifetime in seconds (int), |
||
| 217 | * 'fileLocking' => enable / disable fileLocking (boolean), |
||
| 218 | * 'writeControl' => enable / disable write control (boolean), |
||
| 219 | * 'readControl' => enable / disable read control (boolean), |
||
| 220 | * 'readControlType' => type of read control 'crc32', 'md5', 'strlen', |
||
| 221 | * 'memoryCaching' => enable / disable memory caching (boolean), |
||
| 222 | * 'onlyMemoryCaching' => enable / disable only memory caching (boolean), |
||
| 223 | * 'memoryCachingLimit' => max nbr of records in memory caching (int), |
||
| 224 | * 'fileNameProtection' => enable / disable file name protection (boolean), |
||
| 225 | * 'automaticSerialization' => enable / disable serialization (boolean) |
||
| 226 | * ); |
||
| 227 | * |
||
| 228 | * @param array $options options |
||
| 229 | * @access public |
||
| 230 | */ |
||
| 231 | function __construct($options = array(null)) |
||
| 232 | { |
||
| 233 | $availableOptions = array( 'automaticSerialization', |
||
| 234 | 'fileNameProtection', |
||
| 235 | 'memoryCaching', |
||
| 236 | 'onlyMemoryCaching', |
||
| 237 | 'memoryCachingLimit', |
||
| 238 | 'cacheDir', |
||
| 239 | 'caching', |
||
| 240 | 'lifeTime', |
||
| 241 | 'fileLocking', |
||
| 242 | 'writeControl', |
||
| 243 | 'readControl', |
||
| 244 | 'readControlType'); |
||
| 245 | foreach($options as $key => $value) { |
||
| 246 | if(in_array($key, $availableOptions)) { |
||
| 247 | $property = '_'.$key; |
||
| 248 | $this->$property = $value; |
||
| 249 | } |
||
| 250 | } |
||
| 251 | $this->_refreshTime = time() - $this->_lifeTime; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Test if a cache is available and (if yes) return it |
||
| 256 | * |
||
| 257 | * @param string $id cache id |
||
| 258 | * @param string $group name of the cache group |
||
| 259 | * @param boolean $doNotTestCacheValidity if set to true, the cache |
||
| 260 | * validity won't be tested |
||
| 261 | * @return string data of the cache (or false if no cache available) |
||
| 262 | * @access public |
||
| 263 | */ |
||
| 264 | function get($id, $group = 'default', $doNotTestCacheValidity = false) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Save some data in a cache file |
||
| 307 | * |
||
| 308 | * @param string $data data to put in cache (can be another type than strings |
||
| 309 | * if automaticSerialization is on) |
||
| 310 | * @param string $id cache id |
||
| 311 | * @param string $group name of the cache group |
||
| 312 | * @return boolean true if no problem |
||
| 313 | * @access public |
||
| 314 | */ |
||
| 315 | function save($data, $id = null, $group = 'default') |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Remove a cache file |
||
| 346 | * |
||
| 347 | * @param string $id cache id |
||
| 348 | * @param string $group name of the cache group |
||
| 349 | * @return boolean true if no problem |
||
| 350 | * @access public |
||
| 351 | */ |
||
| 352 | function remove($id, $group = 'default') |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Clean the cache |
||
| 364 | * |
||
| 365 | * if no group is specified all cache files will be destroyed |
||
| 366 | * else only cache files of the specified group will be destroyed |
||
| 367 | * |
||
| 368 | * @param string $group name of the cache group |
||
| 369 | * @return boolean true if no problem |
||
| 370 | * @access public |
||
| 371 | */ |
||
| 372 | function clean($group = false) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Set a new life time |
||
| 413 | * |
||
| 414 | * @param int $newLifeTime new life time (in seconds) |
||
| 415 | * @access public |
||
| 416 | */ |
||
| 417 | function setLifeTime($newLifeTime) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * |
||
| 425 | * @access public |
||
| 426 | */ |
||
| 427 | function saveMemoryCachingState($id, $group = 'default') |
||
| 438 | |||
| 439 | /** |
||
| 440 | * |
||
| 441 | * @access public |
||
| 442 | */ |
||
| 443 | function getMemoryCachingState($id, $group = 'default', |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Return the cache last modification time |
||
| 458 | * |
||
| 459 | * BE CAREFUL : THIS METHOD IS FOR HACKING ONLY ! |
||
| 460 | * |
||
| 461 | * @return int last modification time |
||
| 462 | */ |
||
| 463 | function lastModified() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Trigger a PEAR error |
||
| 469 | * |
||
| 470 | * To improve performances, the PEAR.php file is included dynamically. |
||
| 471 | * The file is so included only when an error is triggered. So, in most |
||
| 472 | * cases, the file isn't included and perfs are much better. |
||
| 473 | * |
||
| 474 | * @param string $msg error message |
||
| 475 | * @param int $code error code |
||
| 476 | * @access public |
||
| 477 | */ |
||
| 478 | function raiseError($msg, $code) |
||
| 482 | |||
| 483 | // --- Private methods --- |
||
| 484 | |||
| 485 | /** |
||
| 486 | * |
||
| 487 | * @access private |
||
| 488 | */ |
||
| 489 | function _memoryCacheAdd($id, $data) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Make a file name (with path) |
||
| 502 | * |
||
| 503 | * @param string $id cache id |
||
| 504 | * @param string $group name of the group |
||
| 505 | * @access private |
||
| 506 | */ |
||
| 507 | function _setFileName($id, $group) |
||
| 516 | |||
| 517 | function getCacheFile() |
||
| 521 | |||
| 522 | /** |
||
| 523 | * Read the cache file and return the content |
||
| 524 | * |
||
| 525 | * @return string content of the cache file |
||
| 526 | * @access private |
||
| 527 | */ |
||
| 528 | function _read() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Write the given data in the cache file |
||
| 558 | * |
||
| 559 | * @param string $data data to put in cache |
||
| 560 | * @return boolean true if ok |
||
| 561 | * @access private |
||
| 562 | */ |
||
| 563 | function _write($data) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * Write the given data in the cache file and control it just after to avoid |
||
| 583 | * corrupted cache entries |
||
| 584 | * |
||
| 585 | * @param string $data data to put in cache |
||
| 586 | * @return boolean true if the test is ok |
||
| 587 | * @access private |
||
| 588 | */ |
||
| 589 | function _writeAndControl($data) |
||
| 595 | |||
| 596 | /** |
||
| 597 | * Make a control key with the string containing datas |
||
| 598 | * |
||
| 599 | * @param string $data data |
||
| 600 | * @param string $controlType type of control 'md5', 'crc32' or 'strlen' |
||
| 601 | * @return string control key |
||
| 602 | * @access private |
||
| 603 | */ |
||
| 604 | function _hash($data, $controlType) |
||
| 618 | |||
| 619 | } |
||
| 620 | |||
| 621 |
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider.