Complex classes like Cache 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 Cache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class Cache implements iCache |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var iAdapter |
||
| 23 | */ |
||
| 24 | private $adapter; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var iSerializer |
||
| 28 | */ |
||
| 29 | private $serializer; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $prefix = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private $isReady = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $isActive = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var mixed no cache, if admin-session is set |
||
| 48 | */ |
||
| 49 | private $isAdminSession = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var array |
||
| 53 | */ |
||
| 54 | private static $STATIC_CACHE = array(); |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var array |
||
| 58 | */ |
||
| 59 | private static $STATIC_CACHE_COUNTER = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * __construct |
||
| 63 | * |
||
| 64 | * @param null|iAdapter $adapter |
||
| 65 | * @param null|iSerializer $serializer |
||
| 66 | * @param boolean $checkForUser check for dev-ip or if cms-user is logged-in |
||
| 67 | * @param boolean $cacheEnabled false will disable the cache (use it e.g. for global settings) |
||
| 68 | * @param string|boolean $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this |
||
| 69 | * user) |
||
| 70 | */ |
||
| 71 | 42 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * enable / disable the cache |
||
| 130 | * |
||
| 131 | * @param boolean $isActive |
||
| 132 | */ |
||
| 133 | 42 | public function setActive($isActive) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * check if the current use is a admin || dev || server == client |
||
| 140 | * |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | public function isCacheActiveForTheCurrentUser() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * returns the IP address of the client |
||
| 174 | * |
||
| 175 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 176 | * proxy headers HTTP_CLIENT_IP |
||
| 177 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 178 | * use if your $_SERVER is behind a |
||
| 179 | * proxy that sets these values |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | private function getClientIp($trust_proxy_headers = false) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Check for local developer. |
||
| 204 | * |
||
| 205 | * @return bool |
||
| 206 | */ |
||
| 207 | private function checkForDev() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
| 239 | */ |
||
| 240 | 42 | protected function getTheDefaultPrefix() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Auto-connect to the available cache-system on the server. |
||
| 251 | * |
||
| 252 | * @return iAdapter |
||
| 253 | */ |
||
| 254 | protected function autoConnectToAvailableCacheSystem() |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set "isReady" state. |
||
| 405 | * |
||
| 406 | * @param boolean $isReady |
||
| 407 | */ |
||
| 408 | 42 | private function setCacheIsReady($isReady) |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get the "isReady" state. |
||
| 415 | * |
||
| 416 | * @return boolean |
||
| 417 | */ |
||
| 418 | 4 | public function getCacheIsReady() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Get cached-item by key. |
||
| 425 | * |
||
| 426 | * @param string $key |
||
| 427 | * @param int $staticCacheHitCounter WARNING: This static cache has no TTL, it will be cleaned on the next request |
||
| 428 | * and it will use more memory as e.g. memcache. |
||
| 429 | * |
||
| 430 | * @return mixed |
||
| 431 | */ |
||
| 432 | 19 | public function getItem($key, $staticCacheHitCounter = 0) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Calculate store-key (prefix + $rawKey). |
||
| 481 | * |
||
| 482 | * @param string $rawKey |
||
| 483 | * |
||
| 484 | * @return string |
||
| 485 | */ |
||
| 486 | 33 | private function calculateStoreKey($rawKey) |
|
| 496 | |||
| 497 | /** |
||
| 498 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
| 499 | * |
||
| 500 | * @param string $str |
||
| 501 | * |
||
| 502 | * @return string |
||
| 503 | */ |
||
| 504 | 6 | private function cleanStoreKey($str) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Get the prefix. |
||
| 533 | * |
||
| 534 | * @return string |
||
| 535 | */ |
||
| 536 | 33 | public function getPrefix() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * !!! Set the prefix. !!! |
||
| 543 | * |
||
| 544 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
| 545 | * |
||
| 546 | * @param string $prefix |
||
| 547 | */ |
||
| 548 | 42 | public function setPrefix($prefix) |
|
| 552 | |||
| 553 | /** |
||
| 554 | * Set cache-item by key => value + date. |
||
| 555 | * |
||
| 556 | * @param string $key |
||
| 557 | * @param mixed $value |
||
| 558 | * @param \DateTime $date |
||
| 559 | * |
||
| 560 | * @return boolean |
||
| 561 | * @throws \Exception |
||
| 562 | */ |
||
| 563 | 6 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 575 | |||
| 576 | /** |
||
| 577 | * Set cache-item by key => value + ttl. |
||
| 578 | * |
||
| 579 | * @param string $key |
||
| 580 | * @param mixed $value |
||
| 581 | * @param int $ttl |
||
| 582 | * |
||
| 583 | * @return bool |
||
| 584 | */ |
||
| 585 | 18 | public function setItem($key, $value, $ttl = 0) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Remove a cached-item. |
||
| 607 | * |
||
| 608 | * @param string $key |
||
| 609 | * |
||
| 610 | * @return bool |
||
| 611 | */ |
||
| 612 | 2 | public function removeItem($key) |
|
| 622 | |||
| 623 | /** |
||
| 624 | * Remove all cached-items. |
||
| 625 | * |
||
| 626 | * @return bool |
||
| 627 | */ |
||
| 628 | 1 | public function removeAll() |
|
| 636 | |||
| 637 | /** |
||
| 638 | * Check if cached-item exists. |
||
| 639 | * |
||
| 640 | * @param string $key |
||
| 641 | * |
||
| 642 | * @return boolean |
||
| 643 | */ |
||
| 644 | 6 | public function existsItem($key) |
|
| 666 | |||
| 667 | /** |
||
| 668 | * Get the current adapter class-name. |
||
| 669 | * |
||
| 670 | * @return string |
||
| 671 | */ |
||
| 672 | 2 | public function getUsedAdapterClassName() |
|
| 676 | |||
| 677 | /** |
||
| 678 | * Get the current serializer class-name. |
||
| 679 | * |
||
| 680 | * @return string |
||
| 681 | */ |
||
| 682 | 2 | public function getUsedSerializerClassName() |
|
| 686 | } |
||
| 687 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: