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 |
||
| 22 | class Directory extends AbstractCache |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * Constructor. |
||
| 26 | * |
||
| 27 | * @param array $options Array of options. |
||
| 28 | */ |
||
| 29 | 323 | public function __construct(array $options = array()) |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Initialize cache directories (create them) |
||
| 41 | */ |
||
| 42 | 323 | protected function initDirectories() |
|
| 48 | |||
| 49 | /** |
||
| 50 | * Get the base path, and ensure they are created |
||
| 51 | * |
||
| 52 | * @param string $type The path type (key, ttl, tag) |
||
| 53 | * @return string |
||
| 54 | */ |
||
| 55 | 323 | protected function getBasePath($type) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Get the file data. |
||
| 75 | * If enable, lock file to preserve atomicity |
||
| 76 | * |
||
| 77 | * @param string $path The file path |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | 119 | protected function readFile($path) |
|
| 94 | |||
| 95 | /** |
||
| 96 | * Get the path of a cached data |
||
| 97 | * |
||
| 98 | * @param string $key The cache key |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 272 | protected function getKeyPath($key) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * Get the path of the expiration file for a key |
||
| 113 | * |
||
| 114 | * @param string $key The cache key |
||
| 115 | * @return string |
||
| 116 | */ |
||
| 117 | 238 | protected function getTtlPath($key) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Get the expiration data of a key |
||
| 127 | * |
||
| 128 | * @param string $key The cache key |
||
| 129 | * @return bool|int |
||
| 130 | */ |
||
| 131 | 51 | protected function loadExpire($key) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Get the path of a tag file |
||
| 150 | * |
||
| 151 | * @param string $tag The tag name |
||
| 152 | * @return string |
||
| 153 | */ |
||
| 154 | 136 | protected function getTagPath($tag) |
|
| 161 | |||
| 162 | /** |
||
| 163 | * Build and return the path of a directory |
||
| 164 | * |
||
| 165 | * @param string $path The directory path to build |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | 323 | protected function buildPath($path) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Save a tag |
||
| 178 | * |
||
| 179 | * @param string $name The tag name |
||
| 180 | * @param string[] $ids The list of cache keys associated to the tag |
||
| 181 | */ |
||
| 182 | 119 | protected function saveTag($name, $ids) |
|
| 191 | |||
| 192 | /** |
||
| 193 | * Save the expiration time of a cache |
||
| 194 | * |
||
| 195 | * @param string $key the cache key |
||
| 196 | * @param false|int $ttl The TTL of the cache |
||
| 197 | */ |
||
| 198 | 238 | protected function saveExpire($key, $ttl) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Return the list of all existing tags |
||
| 225 | * |
||
| 226 | * @return string[] |
||
| 227 | */ |
||
| 228 | 68 | protected function getAllTags() |
|
| 245 | |||
| 246 | /** |
||
| 247 | * Retrieves the cache content for the given key. |
||
| 248 | * |
||
| 249 | * @param string $key The cache key to retrieve. |
||
| 250 | * @return mixed|null Returns the cached data or null. |
||
| 251 | */ |
||
| 252 | 136 | public function loadKey($key) |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Retrieves the cache keys for the given tag. |
||
| 266 | * |
||
| 267 | * @param string $tag The cache tag to retrieve. |
||
| 268 | * @return array|null Returns an array of cache keys or null. |
||
| 269 | */ |
||
| 270 | 136 | public function loadTag($tag) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Saves data to the cache. |
||
| 296 | * |
||
| 297 | * @param mixed $data The data to cache. |
||
| 298 | * @param string $key The cache id to save. |
||
| 299 | * @param array $tags The cache tags for this cache entry. |
||
| 300 | * @param int $ttl The time-to-live in seconds, if set to null the |
||
| 301 | * cache is valid forever. |
||
| 302 | * @return boolean Returns True on success or False on failure. |
||
| 303 | */ |
||
| 304 | 238 | public function save($data, $key, array $tags = null, $ttl = null) |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Deletes the specified cache record. |
||
| 331 | * |
||
| 332 | * @param string $key The cache id to remove. |
||
| 333 | * @return boolean Returns True on success or False on failure. |
||
| 334 | */ |
||
| 335 | 85 | public function delete($key) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * Removes all the cached entries associated with the given tag names. |
||
| 364 | * |
||
| 365 | * @param array $tags The array of tags to remove. |
||
| 366 | * @return boolean Returns True on success or False on failure. |
||
| 367 | */ |
||
| 368 | 17 | public function clean(array $tags) |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Flush all the cached entries. |
||
| 390 | * |
||
| 391 | * @param boolean $all Wether to flush the whole database, or (preferably) |
||
| 392 | 289 | * the entries prefixed with prefix_key and prefix_tag. |
|
| 393 | * @return boolean Returns True on success or False on failure. |
||
| 394 | 289 | */ |
|
| 395 | 289 | public function flush($all = false) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Remove a directory |
||
| 403 | * |
||
| 404 | 289 | * @param string $dir The path of the directory to remove |
|
| 405 | 289 | * @return bool |
|
| 406 | 289 | */ |
|
| 407 | 289 | public function delTree($dir) { |
|
| 415 | |||
| 416 | /** |
||
| 417 | * Returns the time-to-live (in seconds) for the given key. |
||
| 418 | * |
||
| 419 | * @param string $key The name of the key. |
||
| 420 | 51 | * @return int|false Returns the number of seconds left, 0 if valid |
|
| 421 | * forever or False if the key is non-existant. |
||
| 422 | 51 | */ |
|
| 423 | public function getTtl($key) |
||
| 440 | } |
||
| 441 |