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 |
||
| 22 | class FileCache implements CacheInterface, CounterInterface |
||
| 23 | { |
||
| 24 | // TODO garbage collection |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string control characters for keys, reserved by PSR-16 |
||
| 28 | */ |
||
| 29 | const PSR16_RESERVED = '/\{|\}|\(|\)|\/|\\\\|\@|\:/u'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $cache_path; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | private $default_ttl; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param string $cache_path absolute root path of cache-file folder |
||
| 43 | * @param int $default_ttl default time-to-live (in seconds) |
||
| 44 | * |
||
| 45 | * @throws InvalidArgumentException if the specified cache-path does not exist (or is not writable) |
||
| 46 | */ |
||
| 47 | public function __construct($cache_path, $default_ttl) |
||
| 62 | |||
| 63 | public function get($key, $default = null) |
||
| 97 | |||
| 98 | public function set($key, $value, $ttl = null) |
||
| 132 | |||
| 133 | public function delete($key) |
||
| 137 | |||
| 138 | public function clear() |
||
| 146 | |||
| 147 | public function getMultiple($keys) |
||
| 157 | |||
| 158 | public function setMultiple($items, $ttl = null) |
||
| 168 | |||
| 169 | public function deleteMultiple($keys) |
||
| 175 | |||
| 176 | public function has($key) |
||
| 180 | |||
| 181 | public function increment($key, $step = 1) |
||
| 205 | |||
| 206 | public function decrement($key, $step = 1) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Clean up expired cache-files. |
||
| 213 | * |
||
| 214 | * This method is outside the scope of the PSR-16 cache concept, and is specific to |
||
| 215 | * this implementation, being a file-cache. |
||
| 216 | * |
||
| 217 | * In scenarios with dynamic keys (such as Session IDs) you should call this method |
||
| 218 | * periodically - for example from a scheduled daily cron-job. |
||
| 219 | * |
||
| 220 | * @return void |
||
| 221 | */ |
||
| 222 | public function cleanExpired() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * For a given cache key, obtain the absolute file path |
||
| 237 | * |
||
| 238 | * @param string $key |
||
| 239 | * |
||
| 240 | * @return string absolute path to cache-file |
||
| 241 | * |
||
| 242 | * @throws InvalidArgumentException if the specified key contains a character reserved by PSR-16 |
||
| 243 | */ |
||
| 244 | protected function getPath($key) |
||
| 260 | |||
| 261 | /** |
||
| 262 | * @return int current timestamp |
||
| 263 | */ |
||
| 264 | protected function getTime() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @return Generator|string[] |
||
| 271 | */ |
||
| 272 | protected function listPaths() |
||
| 289 | } |
||
| 290 |
If you suppress an error, we recommend checking for the error condition explicitly: