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