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 | 12 | 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 | 13 | public function clear() |
|
155 | |||
156 | 2 | public function getMultiple($keys, $default = null) |
|
170 | |||
171 | 2 | public function setMultiple($values, $ttl = null) |
|
186 | |||
187 | 1 | public function deleteMultiple($keys) |
|
198 | |||
199 | 1 | public function has($key) |
|
203 | |||
204 | 2 | public function increment($key, $step = 1) |
|
228 | |||
229 | 1 | public function decrement($key, $step = 1) |
|
233 | |||
234 | /** |
||
235 | * Clean up expired cache-files. |
||
236 | * |
||
237 | * This method is outside the scope of the PSR-16 cache concept, and is specific to |
||
238 | * this implementation, being a file-cache. |
||
239 | * |
||
240 | * In scenarios with dynamic keys (such as Session IDs) you should call this method |
||
241 | * periodically - for example from a scheduled daily cron-job. |
||
242 | * |
||
243 | * @return void |
||
244 | */ |
||
245 | 1 | public function cleanExpired() |
|
257 | |||
258 | /** |
||
259 | * For a given cache key, obtain the absolute file path |
||
260 | * |
||
261 | * @param string $key |
||
262 | * |
||
263 | * @return string absolute path to cache-file |
||
264 | * |
||
265 | * @throws InvalidArgumentException if the specified key contains a character reserved by PSR-16 |
||
266 | */ |
||
267 | 13 | protected function getPath($key) |
|
281 | |||
282 | /** |
||
283 | * @return int current timestamp |
||
284 | */ |
||
285 | 13 | protected function getTime() |
|
289 | |||
290 | /** |
||
291 | * @return Generator|string[] |
||
292 | */ |
||
293 | 13 | protected function listPaths() |
|
310 | |||
311 | /** |
||
312 | * @param string $key |
||
313 | * |
||
314 | * @throws InvalidArgumentException |
||
315 | */ |
||
316 | 13 | protected function validateKey($key) |
|
322 | } |
||
323 |
If you suppress an error, we recommend checking for the error condition explicitly: