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 | public function __construct(array $options=null) |
||
28 | |||
29 | /** |
||
30 | * Initialize cache directories (create them) |
||
31 | */ |
||
32 | protected function initDirectories() |
||
38 | |||
39 | /** |
||
40 | * Get the base path, and ensure they are created |
||
41 | * |
||
42 | * @param string $type The path type (key, ttl, tag) |
||
43 | * @return string |
||
44 | */ |
||
45 | protected function getBasePath($type) |
||
62 | |||
63 | /** |
||
64 | * Get the path of a cached data |
||
65 | * |
||
66 | * @param string $key The cache key |
||
67 | * @return string |
||
68 | */ |
||
69 | protected function getKeyPath($key) |
||
78 | |||
79 | /** |
||
80 | * Get the path of the expiration file for a key |
||
81 | * |
||
82 | * @param string $key The cache key |
||
83 | * @return string |
||
84 | */ |
||
85 | protected function getTtlPath($key) |
||
92 | |||
93 | /** |
||
94 | * Get the expiration data of a key |
||
95 | * |
||
96 | * @param string $key The cache key |
||
97 | * @return bool|int |
||
98 | */ |
||
99 | protected function loadExpire($key) |
||
115 | |||
116 | /** |
||
117 | * Get the path of a tag file |
||
118 | * |
||
119 | * @param string $tag The tag name |
||
120 | * @return string |
||
121 | */ |
||
122 | protected function getTagPath($tag) |
||
129 | |||
130 | /** |
||
131 | * Build and return the path of a directory |
||
132 | * |
||
133 | * @param string $path The directory path to build |
||
134 | * @return mixed |
||
135 | */ |
||
136 | protected function buildPath($path) |
||
143 | |||
144 | /** |
||
145 | * Save a tag |
||
146 | * |
||
147 | * @param string $name The tag name |
||
148 | * @param string[] $ids The list of cache keys associated to the tag |
||
149 | */ |
||
150 | protected function saveTag($name, $ids) |
||
159 | |||
160 | /** |
||
161 | * Save the expiration time of a cache |
||
162 | * |
||
163 | * @param string $key the cache key |
||
164 | * @param false|int $ttl The TTL of the cache |
||
165 | */ |
||
166 | protected function saveExpire($key, $ttl) |
||
190 | |||
191 | /** |
||
192 | * Return the list of all existing tags |
||
193 | * |
||
194 | * @return string[] |
||
195 | */ |
||
196 | protected function getAllTags() |
||
213 | |||
214 | /** |
||
215 | * Retrieves the cache content for the given key. |
||
216 | * |
||
217 | * @param string $key The cache key to retrieve. |
||
218 | * @return mixed|null Returns the cached data or null. |
||
219 | */ |
||
220 | public function loadKey($key) |
||
231 | |||
232 | /** |
||
233 | * Retrieves the cache keys for the given tag. |
||
234 | * |
||
235 | * @param string $tag The cache tag to retrieve. |
||
236 | * @return array|null Returns an array of cache keys or null. |
||
237 | */ |
||
238 | public function loadTag($tag) |
||
261 | |||
262 | /** |
||
263 | * Saves data to the cache. |
||
264 | * |
||
265 | * @param mixed $data The data to cache. |
||
266 | * @param string $key The cache id to save. |
||
267 | * @param array $tags The cache tags for this cache entry. |
||
268 | * @param int $ttl The time-to-live in seconds, if set to null the |
||
269 | * cache is valid forever. |
||
270 | * @return boolean Returns True on success or False on failure. |
||
271 | */ |
||
272 | public function save($data, $key, array $tags = null, $ttl = null) |
||
296 | |||
297 | /** |
||
298 | * Deletes the specified cache record. |
||
299 | * |
||
300 | * @param string $key The cache id to remove. |
||
301 | * @return boolean Returns True on success or False on failure. |
||
302 | */ |
||
303 | public function delete($key) |
||
329 | |||
330 | /** |
||
331 | * Removes all the cached entries associated with the given tag names. |
||
332 | * |
||
333 | * @param array $tags The array of tags to remove. |
||
334 | * @return boolean Returns True on success or False on failure. |
||
335 | */ |
||
336 | public function clean(array $tags) |
||
352 | |||
353 | /** |
||
354 | * Flush all the cached entries. |
||
355 | * |
||
356 | * @param boolean $all Wether to flush the whole database, or (preferably) |
||
357 | * the entries prefixed with prefix_key and prefix_tag. |
||
358 | * @return boolean Returns True on success or False on failure. |
||
359 | */ |
||
360 | public function flush($all = false) |
||
365 | |||
366 | /** |
||
367 | * Remove a directory |
||
368 | * |
||
369 | * @param string $dir The path of the directory to remove |
||
370 | * @return bool |
||
371 | */ |
||
372 | public function delTree($dir) { |
||
380 | |||
381 | /** |
||
382 | * Returns the time-to-live (in seconds) for the given key. |
||
383 | * |
||
384 | * @param string $key The name of the key. |
||
385 | * @return int|false Returns the number of seconds left, 0 if valid |
||
386 | * forever or False if the key is non-existant. |
||
387 | */ |
||
388 | public function getTtl($key) |
||
405 | } |