Complex classes like AbstractCache 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 AbstractCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | abstract class AbstractCache implements CacheInterface |
||
9 | { |
||
10 | /** |
||
11 | * @var bool |
||
12 | */ |
||
13 | protected $autosave = true; |
||
14 | |||
15 | /** |
||
16 | * @var array |
||
17 | */ |
||
18 | protected $cache = []; |
||
19 | |||
20 | /** |
||
21 | * @var array |
||
22 | */ |
||
23 | protected $complete = []; |
||
24 | |||
25 | /** |
||
26 | * Destructor. |
||
27 | */ |
||
28 | 147 | public function __destruct() |
|
34 | |||
35 | /** |
||
36 | * Get the autosave setting. |
||
37 | * |
||
38 | * @return bool autosave |
||
39 | */ |
||
40 | 3 | public function getAutosave() |
|
44 | |||
45 | /** |
||
46 | * Get the autosave setting. |
||
47 | * |
||
48 | * @param bool $autosave |
||
49 | */ |
||
50 | 3 | public function setAutosave($autosave) |
|
54 | |||
55 | /** |
||
56 | * Store the contents listing. |
||
57 | * |
||
58 | * @param string $directory |
||
59 | * @param array $contents |
||
60 | * @param bool $recursive |
||
61 | * |
||
62 | * @return array contents listing |
||
63 | */ |
||
64 | 15 | public function storeContents($directory, array $contents, $recursive = false) |
|
83 | |||
84 | /** |
||
85 | * Update the metadata for an object. |
||
86 | * |
||
87 | * @param string $path object path |
||
88 | * @param array $object object metadata |
||
89 | * @param bool $autosave whether to trigger the autosave routine |
||
90 | */ |
||
91 | 48 | public function updateObject($path, array $object, $autosave = false) |
|
105 | |||
106 | /** |
||
107 | * Store object hit miss. |
||
108 | * |
||
109 | * @param string $path |
||
110 | */ |
||
111 | 6 | public function storeMiss($path) |
|
116 | |||
117 | /** |
||
118 | * Get the contents listing. |
||
119 | * |
||
120 | * @param string $dirname |
||
121 | * @param bool $recursive |
||
122 | * |
||
123 | * @return array contents listing |
||
124 | */ |
||
125 | 9 | public function listContents($dirname = '', $recursive = false) |
|
142 | |||
143 | 60 | /** |
|
144 | * {@inheritdoc} |
||
145 | 60 | */ |
|
146 | 18 | public function has($path) |
|
156 | |||
157 | 12 | /** |
|
158 | * {@inheritdoc} |
||
159 | 12 | */ |
|
160 | 6 | public function read($path) |
|
168 | |||
169 | 3 | /** |
|
170 | * {@inheritdoc} |
||
171 | 3 | */ |
|
172 | public function readStream($path) |
||
176 | |||
177 | 5 | /** |
|
178 | * {@inheritdoc} |
||
179 | 3 | */ |
|
180 | 3 | public function rename($path, $newpath) |
|
191 | |||
192 | 6 | /** |
|
193 | * {@inheritdoc} |
||
194 | 6 | */ |
|
195 | 3 | public function copy($path, $newpath) |
|
203 | |||
204 | 3 | /** |
|
205 | * {@inheritdoc} |
||
206 | 3 | */ |
|
207 | 3 | public function delete($path) |
|
211 | |||
212 | 6 | /** |
|
213 | * {@inheritdoc} |
||
214 | 6 | */ |
|
215 | 6 | public function deleteDir($dirname) |
|
227 | |||
228 | 6 | /** |
|
229 | * {@inheritdoc} |
||
230 | 6 | */ |
|
231 | 3 | public function getMimetype($path) |
|
246 | |||
247 | 3 | /** |
|
248 | * {@inheritdoc} |
||
249 | 3 | */ |
|
250 | 3 | public function getSize($path) |
|
258 | |||
259 | 3 | /** |
|
260 | * {@inheritdoc} |
||
261 | 3 | */ |
|
262 | 3 | public function getTimestamp($path) |
|
270 | |||
271 | 3 | /** |
|
272 | * {@inheritdoc} |
||
273 | 3 | */ |
|
274 | 3 | public function getVisibility($path) |
|
282 | |||
283 | 18 | /** |
|
284 | * {@inheritdoc} |
||
285 | 18 | */ |
|
286 | 15 | public function getMetadata($path) |
|
294 | |||
295 | 99 | /** |
|
296 | * {@inheritdoc} |
||
297 | 99 | */ |
|
298 | 75 | public function isComplete($dirname, $recursive) |
|
310 | |||
311 | 24 | /** |
|
312 | * {@inheritdoc} |
||
313 | 24 | */ |
|
314 | 24 | public function setComplete($dirname, $recursive) |
|
318 | |||
319 | /** |
||
320 | * Filter the contents from a listing. |
||
321 | * |
||
322 | * @param array $contents object listing |
||
323 | 39 | * |
|
324 | * @return array filtered contents |
||
325 | 39 | */ |
|
326 | 39 | public function cleanContents(array $contents) |
|
342 | 3 | ||
343 | /** |
||
344 | 3 | * {@inheritdoc} |
|
345 | 3 | */ |
|
346 | 3 | public function flush() |
|
352 | 51 | ||
353 | /** |
||
354 | 51 | * {@inheritdoc} |
|
355 | 51 | */ |
|
356 | 51 | public function autosave() |
|
362 | |||
363 | /** |
||
364 | 24 | * Retrieve serialized cache data. |
|
365 | * |
||
366 | 24 | * @return string serialized data |
|
367 | */ |
||
368 | 24 | public function getForStorage() |
|
374 | |||
375 | /** |
||
376 | 18 | * Load from serialized cache data. |
|
377 | * |
||
378 | 18 | * @param string $json |
|
379 | */ |
||
380 | 18 | public function setFromStorage($json) |
|
389 | |||
390 | /** |
||
391 | 48 | * Ensure parent directories of an object. |
|
392 | * |
||
393 | 48 | * @param string $path object path |
|
394 | */ |
||
395 | 48 | public function ensureParentDirectories($path) |
|
405 | |||
406 | /** |
||
407 | * Determines if the path is inside the directory. |
||
408 | * |
||
409 | * @param string $directory |
||
410 | 18 | * @param string $path |
|
411 | * |
||
412 | 18 | * @return bool |
|
413 | */ |
||
414 | protected function pathIsInDirectory($directory, $path) |
||
418 | } |
||
419 |
If an expression can have both
false
, andnull
as possible values. It is generally a good practice to always use strict comparison to clearly distinguish between those two values.