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) |
|
65 | { |
||
66 | 15 | $directories = [$directory]; |
|
67 | |||
68 | 15 | foreach ($contents as $object) { |
|
69 | 15 | $this->updateObject($object['path'], $object); |
|
70 | 15 | $object = $this->cache[$object['path']]; |
|
71 | |||
72 | 15 | if ($recursive && $this->pathIsInDirectory($directory, $object['path'])) { |
|
73 | 9 | $directories[] = $object['dirname']; |
|
74 | 9 | } |
|
75 | 15 | } |
|
76 | |||
77 | 15 | foreach (array_unique($directories) as $directory) { |
|
78 | 15 | $this->setComplete($directory, $recursive); |
|
79 | 15 | } |
|
80 | |||
81 | 15 | $this->autosave(); |
|
82 | 15 | } |
|
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) |
|
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | 60 | public function has($path) |
|
153 | |||
154 | /** |
||
155 | * {@inheritdoc} |
||
156 | */ |
||
157 | 12 | public function read($path) |
|
165 | |||
166 | /** |
||
167 | * {@inheritdoc} |
||
168 | */ |
||
169 | 3 | public function readStream($path) |
|
173 | |||
174 | /** |
||
175 | * {@inheritdoc} |
||
176 | */ |
||
177 | 3 | public function rename($path, $newpath) |
|
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | */ |
||
192 | 6 | public function copy($path, $newpath) |
|
200 | |||
201 | /** |
||
202 | * {@inheritdoc} |
||
203 | */ |
||
204 | 3 | public function delete($path) |
|
208 | |||
209 | /** |
||
210 | * {@inheritdoc} |
||
211 | */ |
||
212 | 6 | public function deleteDir($dirname) |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | */ |
||
228 | 6 | public function getMimetype($path) |
|
243 | |||
244 | /** |
||
245 | * {@inheritdoc} |
||
246 | */ |
||
247 | 3 | public function getSize($path) |
|
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | */ |
||
259 | 3 | public function getTimestamp($path) |
|
267 | |||
268 | /** |
||
269 | * {@inheritdoc} |
||
270 | */ |
||
271 | 3 | public function getVisibility($path) |
|
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | 18 | public function getMetadata($path) |
|
291 | |||
292 | /** |
||
293 | * {@inheritdoc} |
||
294 | */ |
||
295 | 99 | public function isComplete($dirname, $recursive) |
|
307 | |||
308 | /** |
||
309 | * {@inheritdoc} |
||
310 | */ |
||
311 | 24 | public function setComplete($dirname, $recursive) |
|
315 | |||
316 | /** |
||
317 | * Filter the contents from a listing. |
||
318 | * |
||
319 | * @param array $contents object listing |
||
320 | * |
||
321 | * @return array filtered contents |
||
322 | */ |
||
323 | 39 | public function cleanContents(array $contents) |
|
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | 3 | public function flush() |
|
348 | |||
349 | /** |
||
350 | * {@inheritdoc} |
||
351 | */ |
||
352 | 51 | public function autosave() |
|
358 | |||
359 | /** |
||
360 | * Retrieve serialized cache data. |
||
361 | * |
||
362 | * @return string serialized data |
||
363 | */ |
||
364 | 24 | public function getForStorage() |
|
370 | |||
371 | /** |
||
372 | * Load from serialized cache data. |
||
373 | * |
||
374 | * @param string $json |
||
375 | */ |
||
376 | 18 | public function setFromStorage($json) |
|
385 | |||
386 | /** |
||
387 | * Ensure parent directories of an object. |
||
388 | * |
||
389 | * @param string $path object path |
||
390 | */ |
||
391 | 48 | public function ensureParentDirectories($path) |
|
401 | |||
402 | /** |
||
403 | * Determines if the path is inside the directory. |
||
404 | * |
||
405 | * @param string $directory |
||
406 | * @param string $path |
||
407 | * |
||
408 | * @return bool |
||
409 | */ |
||
410 | 18 | protected function pathIsInDirectory($directory, $path) |
|
414 | } |
||
415 |
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.