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 | // TODO garbage collection |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var string control characters for keys, reserved by PSR-16 |
||
| 27 | */ |
||
| 28 | const PSR16_RESERVED = '/\{|\}|\(|\)|\/|\\\\|\@|\:/u'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | private $cache_path; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var int |
||
| 37 | */ |
||
| 38 | private $default_ttl; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $cache_path absolute root path of cache-file folder |
||
| 42 | * @param int $default_ttl default time-to-live (in seconds) |
||
| 43 | * |
||
| 44 | * @throws InvalidArgumentException if the specified cache-path does not exist (or is not writable) |
||
| 45 | */ |
||
| 46 | 13 | public function __construct($cache_path, $default_ttl) |
|
| 65 | |||
| 66 | 11 | public function get($key, $default = null) |
|
| 100 | |||
| 101 | 12 | public function set($key, $value, $ttl = null) |
|
| 135 | |||
| 136 | 2 | public function delete($key) |
|
| 140 | |||
| 141 | 12 | public function clear() |
|
| 149 | |||
| 150 | 1 | public function getMultiple($keys, $default = null) |
|
| 160 | |||
| 161 | 2 | public function setMultiple($items, $ttl = null) |
|
| 171 | |||
| 172 | 1 | public function deleteMultiple($keys) |
|
| 178 | |||
| 179 | 1 | public function has($key) |
|
| 183 | |||
| 184 | 2 | public function increment($key, $step = 1) |
|
| 208 | |||
| 209 | 1 | public function decrement($key, $step = 1) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Clean up expired cache-files. |
||
| 216 | * |
||
| 217 | * This method is outside the scope of the PSR-16 cache concept, and is specific to |
||
| 218 | * this implementation, being a file-cache. |
||
| 219 | * |
||
| 220 | * In scenarios with dynamic keys (such as Session IDs) you should call this method |
||
| 221 | * periodically - for example from a scheduled daily cron-job. |
||
| 222 | * |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | 1 | public function cleanExpired() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * For a given cache key, obtain the absolute file path |
||
| 240 | * |
||
| 241 | * @param string $key |
||
| 242 | * |
||
| 243 | * @return string absolute path to cache-file |
||
| 244 | * |
||
| 245 | * @throws InvalidArgumentException if the specified key contains a character reserved by PSR-16 |
||
| 246 | */ |
||
| 247 | 13 | protected function getPath($key) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * @return int current timestamp |
||
| 266 | */ |
||
| 267 | 13 | protected function getTime() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return Generator|string[] |
||
| 274 | */ |
||
| 275 | 12 | protected function listPaths() |
|
| 292 | } |
||
| 293 |
If you suppress an error, we recommend checking for the error condition explicitly: