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 | 13 | public function __construct($cache_path, $default_ttl) |
|
66 | |||
67 | public function get($key, $default = null) |
||
101 | |||
102 | public function set($key, $value, $ttl = null) |
||
136 | |||
137 | public function delete($key) |
||
141 | |||
142 | public function clear() |
||
150 | |||
151 | public function getMultiple($keys) |
||
161 | |||
162 | public function setMultiple($items, $ttl = null) |
||
172 | |||
173 | public function deleteMultiple($keys) |
||
179 | |||
180 | public function has($key) |
||
184 | |||
185 | public function increment($key, $step = 1) |
||
209 | |||
210 | public function decrement($key, $step = 1) |
||
214 | |||
215 | /** |
||
216 | * Clean up expired cache-files. |
||
217 | * |
||
218 | * This method is outside the scope of the PSR-16 cache concept, and is specific to |
||
219 | * this implementation, being a file-cache. |
||
220 | * |
||
221 | * In scenarios with dynamic keys (such as Session IDs) you should call this method |
||
222 | * periodically - for example from a scheduled daily cron-job. |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | public function cleanExpired() |
||
238 | |||
239 | /** |
||
240 | * For a given cache key, obtain the absolute file path |
||
241 | * |
||
242 | * @param string $key |
||
243 | * |
||
244 | * @return string absolute path to cache-file |
||
245 | * |
||
246 | * @throws InvalidArgumentException if the specified key contains a character reserved by PSR-16 |
||
247 | */ |
||
248 | protected function getPath($key) |
||
264 | |||
265 | /** |
||
266 | * @return int current timestamp |
||
267 | */ |
||
268 | 13 | protected function getTime() |
|
272 | |||
273 | /** |
||
274 | * @return Generator|string[] |
||
275 | */ |
||
276 | protected function listPaths() |
||
293 | } |
||
294 |
If you suppress an error, we recommend checking for the error condition explicitly: