Complex classes like Directory 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 Directory, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Directory extends AbstractCache |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * Constructor. |
||
| 17 | * |
||
| 18 | * @param array $options Array of options. |
||
| 19 | */ |
||
| 20 | 289 | public function __construct(array $options=null) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Initialize cache directories (create them) |
||
| 32 | 289 | */ |
|
| 33 | protected function initDirectories() |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Get the base path, and ensure they are created |
||
| 42 | * |
||
| 43 | * @param string $type The path type (key, ttl, tag) |
||
| 44 | * @return string |
||
| 45 | 289 | */ |
|
| 46 | protected function getBasePath($type) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Get the file data. |
||
| 66 | * If enable, lock file to preserve atomicity |
||
| 67 | * |
||
| 68 | * @param string $path The file path |
||
| 69 | 272 | * @return string |
|
| 70 | */ |
||
| 71 | 272 | protected function readFile($path) |
|
| 85 | 238 | ||
| 86 | /** |
||
| 87 | 238 | * Get the path of a cached data |
|
| 88 | 238 | * |
|
| 89 | * @param string $key The cache key |
||
| 90 | 238 | * @return string |
|
| 91 | */ |
||
| 92 | protected function getKeyPath($key) |
||
| 101 | 51 | ||
| 102 | /** |
||
| 103 | 51 | * Get the path of the expiration file for a key |
|
| 104 | 17 | * |
|
| 105 | * @param string $key The cache key |
||
| 106 | * @return string |
||
| 107 | 51 | */ |
|
| 108 | protected function getTtlPath($key) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Get the expiration data of a key |
||
| 118 | * |
||
| 119 | * @param string $key The cache key |
||
| 120 | * @return bool|int |
||
| 121 | */ |
||
| 122 | 136 | protected function loadExpire($key) |
|
| 138 | 289 | ||
| 139 | 289 | /** |
|
| 140 | 289 | * Get the path of a tag file |
|
| 141 | 289 | * |
|
| 142 | * @param string $tag The tag name |
||
| 143 | * @return string |
||
| 144 | */ |
||
| 145 | protected function getTagPath($tag) |
||
| 152 | 119 | ||
| 153 | 119 | /** |
|
| 154 | * Build and return the path of a directory |
||
| 155 | 119 | * |
|
| 156 | 119 | * @param string $path The directory path to build |
|
| 157 | 119 | * @return mixed |
|
| 158 | 119 | */ |
|
| 159 | protected function buildPath($path) |
||
| 166 | 238 | ||
| 167 | /** |
||
| 168 | 238 | * Save a tag |
|
| 169 | * |
||
| 170 | 238 | * @param string $name The tag name |
|
| 171 | 238 | * @param string[] $ids The list of cache keys associated to the tag |
|
| 172 | */ |
||
| 173 | 238 | protected function saveTag($name, $ids) |
|
| 182 | 204 | ||
| 183 | /** |
||
| 184 | 17 | * Save the expiration time of a cache |
|
| 185 | 51 | * |
|
| 186 | * @param string $key the cache key |
||
| 187 | * @param false|int $ttl The TTL of the cache |
||
| 188 | 51 | */ |
|
| 189 | 51 | protected function saveExpire($key, $ttl) |
|
| 213 | |||
| 214 | /** |
||
| 215 | * Return the list of all existing tags |
||
| 216 | * |
||
| 217 | * @return string[] |
||
| 218 | */ |
||
| 219 | protected function getAllTags() |
||
| 236 | |||
| 237 | /** |
||
| 238 | 136 | * Retrieves the cache content for the given key. |
|
| 239 | * |
||
| 240 | 136 | * @param string $key The cache key to retrieve. |
|
| 241 | 34 | * @return mixed|null Returns the cached data or null. |
|
| 242 | */ |
||
| 243 | public function loadKey($key) |
||
| 254 | 17 | ||
| 255 | /** |
||
| 256 | * Retrieves the cache keys for the given tag. |
||
| 257 | 85 | * |
|
| 258 | * @param string $tag The cache tag to retrieve. |
||
| 259 | 85 | * @return array|null Returns an array of cache keys or null. |
|
| 260 | */ |
||
| 261 | public function loadTag($tag) |
||
| 284 | 119 | ||
| 285 | 119 | /** |
|
| 286 | 119 | * Saves data to the cache. |
|
| 287 | * |
||
| 288 | 238 | * @param mixed $data The data to cache. |
|
| 289 | 51 | * @param string $key The cache id to save. |
|
| 290 | 51 | * @param array $tags The cache tags for this cache entry. |
|
| 291 | 221 | * @param int $ttl The time-to-live in seconds, if set to null the |
|
| 292 | * cache is valid forever. |
||
| 293 | * @return boolean Returns True on success or False on failure. |
||
| 294 | 238 | */ |
|
| 295 | public function save($data, $key, array $tags = null, $ttl = null) |
||
| 319 | 34 | ||
| 320 | 34 | /** |
|
| 321 | 34 | * Deletes the specified cache record. |
|
| 322 | 34 | * |
|
| 323 | 68 | * @param string $key The cache id to remove. |
|
| 324 | * @return boolean Returns True on success or False on failure. |
||
| 325 | 68 | */ |
|
| 326 | public function delete($key) |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Removes all the cached entries associated with the given tag names. |
||
| 355 | * |
||
| 356 | * @param array $tags The array of tags to remove. |
||
| 357 | * @return boolean Returns True on success or False on failure. |
||
| 358 | */ |
||
| 359 | public function clean(array $tags) |
||
| 375 | 289 | ||
| 376 | 289 | /** |
|
| 377 | 289 | * Flush all the cached entries. |
|
| 378 | 289 | * |
|
| 379 | * @param boolean $all Wether to flush the whole database, or (preferably) |
||
| 380 | * the entries prefixed with prefix_key and prefix_tag. |
||
| 381 | * @return boolean Returns True on success or False on failure. |
||
| 382 | */ |
||
| 383 | public function flush($all = false) |
||
| 388 | 51 | ||
| 389 | /** |
||
| 390 | 51 | * Remove a directory |
|
| 391 | * |
||
| 392 | 51 | * @param string $dir The path of the directory to remove |
|
| 393 | * @return bool |
||
| 394 | 51 | */ |
|
| 395 | 34 | public function delTree($dir) { |
|
| 403 | 34 | ||
| 404 | /** |
||
| 405 | * Returns the time-to-live (in seconds) for the given key. |
||
| 406 | * |
||
| 407 | * @param string $key The name of the key. |
||
| 408 | * @return int|false Returns the number of seconds left, 0 if valid |
||
| 409 | * forever or False if the key is non-existant. |
||
| 410 | */ |
||
| 411 | public function getTtl($key) |
||
| 428 | } |