Complex classes like FileCache 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 FileCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class FileCache implements CacheInterface |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var string control characters for keys, reserved by PSR-16 |
||
| 25 | */ |
||
| 26 | const PSR16_RESERVED = '/\{|\}|\(|\)|\/|\\\\|\@|\:/u'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | private $cache_path; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | private $default_ttl; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | private $dir_mode; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | private $file_mode; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @param string $cache_path absolute root path of cache-file folder |
||
| 50 | * @param int $default_ttl default time-to-live (in seconds) |
||
| 51 | * @param int $dir_mode permission mode for created dirs |
||
| 52 | * @param int $file_mode permission mode for created files |
||
| 53 | */ |
||
| 54 | 14 | public function __construct($cache_path, $default_ttl, $dir_mode = 0775, $file_mode = 0664) |
|
| 76 | |||
| 77 | 12 | public function get($key, $default = null) |
|
| 111 | |||
| 112 | 13 | public function set($key, $value, $ttl = null) |
|
| 151 | |||
| 152 | 2 | public function delete($key) |
|
| 156 | |||
| 157 | 14 | public function clear() |
|
| 171 | |||
| 172 | 2 | public function getMultiple($keys, $default = null) |
|
| 186 | |||
| 187 | 2 | public function setMultiple($values, $ttl = null) |
|
| 202 | |||
| 203 | 1 | public function deleteMultiple($keys) |
|
| 214 | |||
| 215 | 1 | public function has($key) |
|
| 219 | |||
| 220 | 2 | public function increment($key, $step = 1) |
|
| 244 | |||
| 245 | 1 | public function decrement($key, $step = 1) |
|
| 249 | |||
| 250 | /** |
||
| 251 | * Clean up expired cache-files. |
||
| 252 | * |
||
| 253 | * This method is outside the scope of the PSR-16 cache concept, and is specific to |
||
| 254 | * this implementation, being a file-cache. |
||
| 255 | * |
||
| 256 | * In scenarios with dynamic keys (such as Session IDs) you should call this method |
||
| 257 | * periodically - for example from a scheduled daily cron-job. |
||
| 258 | * |
||
| 259 | * @return void |
||
| 260 | */ |
||
| 261 | 1 | public function cleanExpired() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * For a given cache key, obtain the absolute file path |
||
| 276 | * |
||
| 277 | * @param string $key |
||
| 278 | * |
||
| 279 | * @return string absolute path to cache-file |
||
| 280 | * |
||
| 281 | * @throws InvalidArgumentException if the specified key contains a character reserved by PSR-16 |
||
| 282 | */ |
||
| 283 | 14 | protected function getPath($key) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return int current timestamp |
||
| 300 | */ |
||
| 301 | 14 | protected function getTime() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @return Generator|string[] |
||
| 308 | */ |
||
| 309 | 14 | protected function listPaths() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $key |
||
| 329 | * |
||
| 330 | * @throws InvalidArgumentException |
||
| 331 | */ |
||
| 332 | 14 | protected function validateKey($key) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Recursively create directories and apply permission mask |
||
| 341 | * |
||
| 342 | * @param string $path absolute directory path |
||
| 343 | */ |
||
| 344 | 14 | private function mkdir($path) |
|
| 355 | } |
||
| 356 |
If you suppress an error, we recommend checking for the error condition explicitly: