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 |
||
| 9 | abstract class AbstractCache implements CacheInterface |
||
| 10 | { |
||
| 11 | /** |
||
| 12 | * @var bool |
||
| 13 | */ |
||
| 14 | protected $autosave = true; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * @var array |
||
| 18 | */ |
||
| 19 | protected $cache = []; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var array |
||
| 23 | */ |
||
| 24 | protected $complete = []; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var Config |
||
| 28 | 147 | */ |
|
| 29 | protected $config; |
||
| 30 | 147 | ||
| 31 | 6 | /** |
|
| 32 | 6 | * Destructor. |
|
| 33 | 147 | */ |
|
| 34 | public function __destruct() |
||
| 40 | 3 | ||
| 41 | /** |
||
| 42 | 3 | * Get the config object or null. |
|
| 43 | * |
||
| 44 | * @return Config|null config |
||
| 45 | */ |
||
| 46 | public function getConfig() |
||
| 50 | 3 | ||
| 51 | /** |
||
| 52 | 3 | * Set the config. |
|
| 53 | 3 | * |
|
| 54 | * @param Config|array|null $config |
||
| 55 | */ |
||
| 56 | public function setConfig($config) |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Get the autosave setting. |
||
| 63 | * |
||
| 64 | 15 | * @return bool autosave |
|
| 65 | */ |
||
| 66 | 15 | public function getAutosave() |
|
| 70 | 15 | ||
| 71 | /** |
||
| 72 | 15 | * Get the autosave setting. |
|
| 73 | 9 | * |
|
| 74 | 9 | * @param bool $autosave |
|
| 75 | 15 | */ |
|
| 76 | public function setAutosave($autosave) |
||
| 80 | |||
| 81 | 15 | /** |
|
| 82 | 15 | * Store the contents listing. |
|
| 83 | * |
||
| 84 | * @param string $directory |
||
| 85 | * @param array $contents |
||
| 86 | * @param bool $recursive |
||
| 87 | * |
||
| 88 | * @return array contents listing |
||
| 89 | */ |
||
| 90 | public function storeContents($directory, array $contents, $recursive = false) |
||
| 110 | |||
| 111 | 6 | /** |
|
| 112 | * Update the metadata for an object. |
||
| 113 | 6 | * |
|
| 114 | 6 | * @param string $path object path |
|
| 115 | 6 | * @param array $object object metadata |
|
| 116 | * @param bool $autosave whether to trigger the autosave routine |
||
| 117 | */ |
||
| 118 | public function updateObject($path, array $object, $autosave = false) |
||
| 134 | 3 | ||
| 135 | 9 | /** |
|
| 136 | * Store object hit miss. |
||
| 137 | 9 | * |
|
| 138 | * @param string $path |
||
| 139 | */ |
||
| 140 | public function storeMiss($path) |
||
| 145 | 60 | ||
| 146 | 18 | /** |
|
| 147 | * Get the contents listing. |
||
| 148 | * |
||
| 149 | 54 | * @param string $dirname |
|
| 150 | 3 | * @param bool $recursive |
|
| 151 | * |
||
| 152 | 51 | * @return array contents listing |
|
| 153 | */ |
||
| 154 | public function listContents($dirname = '', $recursive = false) |
||
| 173 | |||
| 174 | /** |
||
| 175 | * {@inheritdoc} |
||
| 176 | */ |
||
| 177 | 5 | public function has($path) |
|
| 189 | |||
| 190 | /** |
||
| 191 | * {@inheritdoc} |
||
| 192 | 6 | */ |
|
| 193 | public function read($path) |
||
| 203 | |||
| 204 | 3 | /** |
|
| 205 | * {@inheritdoc} |
||
| 206 | 3 | */ |
|
| 207 | 3 | public function readStream($path) |
|
| 211 | |||
| 212 | 6 | /** |
|
| 213 | * {@inheritdoc} |
||
| 214 | 6 | */ |
|
| 215 | 6 | public function rename($path, $newpath) |
|
| 232 | |||
| 233 | /** |
||
| 234 | 6 | * {@inheritdoc} |
|
| 235 | 3 | */ |
|
| 236 | public function copy($path, $newpath) |
||
| 247 | 3 | ||
| 248 | /** |
||
| 249 | 3 | * {@inheritdoc} |
|
| 250 | 3 | */ |
|
| 251 | public function delete($path) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * {@inheritdoc} |
||
| 258 | */ |
||
| 259 | 3 | public function deleteDir($dirname) |
|
| 275 | |||
| 276 | /** |
||
| 277 | 3 | * {@inheritdoc} |
|
| 278 | */ |
||
| 279 | public function getMimetype($path) |
||
| 295 | 99 | ||
| 296 | /** |
||
| 297 | 99 | * {@inheritdoc} |
|
| 298 | 75 | */ |
|
| 299 | public function getSize($path) |
||
| 309 | |||
| 310 | /** |
||
| 311 | 24 | * {@inheritdoc} |
|
| 312 | */ |
||
| 313 | 24 | public function getTimestamp($path) |
|
| 323 | 39 | ||
| 324 | /** |
||
| 325 | 39 | * {@inheritdoc} |
|
| 326 | 39 | */ |
|
| 327 | 39 | public function getVisibility($path) |
|
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritdoc} |
||
| 340 | */ |
||
| 341 | public function getMetadata($path) |
||
| 351 | |||
| 352 | 51 | /** |
|
| 353 | * {@inheritdoc} |
||
| 354 | 51 | */ |
|
| 355 | 51 | public function isComplete($dirname, $recursive) |
|
| 369 | |||
| 370 | /** |
||
| 371 | * {@inheritdoc} |
||
| 372 | */ |
||
| 373 | public function setComplete($dirname, $recursive) |
||
| 377 | |||
| 378 | 18 | /** |
|
| 379 | * Filter the contents from a listing. |
||
| 380 | 18 | * |
|
| 381 | 18 | * @param array $contents object listing |
|
| 382 | 18 | * |
|
| 383 | 18 | * @return array filtered contents |
|
| 384 | 18 | */ |
|
| 385 | public function cleanContents(array $contents) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * {@inheritdoc} |
||
| 404 | */ |
||
| 405 | public function flush() |
||
| 411 | |||
| 412 | 18 | /** |
|
| 413 | * {@inheritdoc} |
||
| 414 | */ |
||
| 415 | public function autosave() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * Retrieve serialized cache data. |
||
| 424 | * |
||
| 425 | * @return string serialized data |
||
| 426 | */ |
||
| 427 | public function getForStorage() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Load from serialized cache data. |
||
| 436 | * |
||
| 437 | * @param string $json |
||
| 438 | */ |
||
| 439 | public function setFromStorage($json) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Ensure parent directories of an object. |
||
| 451 | * |
||
| 452 | * @param string $path object path |
||
| 453 | */ |
||
| 454 | public function ensureParentDirectories($path) |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Determines if the path is inside the directory. |
||
| 467 | * |
||
| 468 | * @param string $directory |
||
| 469 | * @param string $path |
||
| 470 | * |
||
| 471 | * @return bool |
||
| 472 | */ |
||
| 473 | protected function pathIsInDirectory($directory, $path) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Return the path string checking case_sensitive config value |
||
| 480 | * |
||
| 481 | * @param string $path |
||
| 482 | * |
||
| 483 | * @return string |
||
| 484 | */ |
||
| 485 | protected function case($path) |
||
| 493 | } |
||
| 494 |
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.