Complex classes like Files 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 Files, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class Files extends AbstractCache |
||
23 | { |
||
24 | /** |
||
25 | * Constructor. |
||
26 | * |
||
27 | * @param array $options Array of options. |
||
28 | */ |
||
29 | 306 | public function __construct(array $options = array()) |
|
40 | |||
41 | /** |
||
42 | * Retrieves the cache content for the given key. |
||
43 | * |
||
44 | * @param string $key The cache key to retrieve. |
||
45 | * @return mixed|null Returns the cached data or null. |
||
46 | */ |
||
47 | 153 | public function loadKey($key) |
|
78 | |||
79 | 119 | /** |
|
80 | * Retrieves the cache keys for the given tag. |
||
81 | 119 | * |
|
82 | 17 | * @param string $tag The cache tag to retrieve. |
|
83 | * @return array|null Returns an array of cache keys or null. |
||
84 | */ |
||
85 | 102 | public function loadTag($tag) |
|
111 | |||
112 | /** |
||
113 | * Get the file data. |
||
114 | 187 | * If enable, lock file to preserve atomicity |
|
115 | * |
||
116 | 187 | * @param string $path The file path |
|
117 | 187 | * @param int $line The line to read. If -1 read the whole file |
|
118 | 187 | * @return string |
|
119 | 187 | */ |
|
120 | protected function readFile($path, $line = -1) |
||
143 | |||
144 | /** |
||
145 | * Saves data to the cache. |
||
146 | * |
||
147 | * @param mixed $data The data to cache. |
||
148 | 221 | * @param string $key The cache id to save. |
|
149 | * @param array $tags The cache tags for this cache entry. |
||
150 | 221 | * @param int $ttl The time-to-live in seconds, if set to null the |
|
151 | 221 | * cache is valid forever. |
|
152 | * @return boolean Returns True on success or False on failure. |
||
153 | 221 | */ |
|
154 | 221 | public function save($data, $key, array $tags = null, $ttl = null) |
|
176 | |||
177 | 68 | /** |
|
178 | * Deletes the specified cache record. |
||
179 | 68 | * |
|
180 | 68 | * @param string $key The cache id to remove. |
|
181 | 68 | * @return boolean Returns True on success or False on failure. |
|
182 | 34 | */ |
|
183 | public function delete($key) |
||
193 | |||
194 | 17 | /** |
|
195 | * Removes all the cached entries associated with the given tag names. |
||
196 | 17 | * |
|
197 | 17 | * @param array $tags The array of tags to remove. |
|
198 | 17 | * @return boolean Returns True on success or False on failure. |
|
199 | 17 | */ |
|
200 | 17 | public function clean(array $tags) |
|
221 | |||
222 | 272 | /** |
|
223 | 272 | * Flush all the cached entries. |
|
224 | 272 | * |
|
225 | 272 | * @param boolean $all Wether to flush the whole database, or (preferably) |
|
226 | * the entries prefixed with prefix_key and prefix_tag. |
||
227 | 204 | * @return boolean Returns True on success or False on failure. |
|
228 | 204 | */ |
|
229 | 204 | public function flush($all = false) |
|
230 | { |
||
231 | 204 | $files = scandir($this->getOption('directory')); |
|
232 | 204 | foreach ($files as $file) { |
|
233 | 204 | if ('.' === substr($file, 0, 1)) { |
|
234 | 272 | continue; |
|
235 | 272 | } |
|
236 | $path = $this->getOption('directory') . DIRECTORY_SEPARATOR . $file; |
||
237 | $fullKey = base64_decode($file); |
||
238 | $key = $this->removePrefixKey($fullKey); |
||
239 | |||
240 | if ($all || (!$all && ($key !== $fullKey || '' === $this->options['prefix_key']))) { |
||
241 | unlink($path); |
||
242 | } |
||
243 | } |
||
244 | 34 | } |
|
245 | |||
246 | 34 | /** |
|
247 | 34 | * Returns the time-to-live (in seconds) for the given key. |
|
248 | 34 | * |
|
249 | 34 | * @param string $key The name of the key. |
|
250 | * @return int|false Returns the number of seconds left, 0 if valid |
||
251 | * forever or False if the key is non-existant. |
||
252 | 34 | */ |
|
253 | public function getTtl($key) |
||
269 | } |
||
270 |