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_EXPIRE = array(); |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | private static $STATIC_CACHE_COUNTER = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var int |
||
| 68 | */ |
||
| 69 | private $staticCacheHitCounter = 10; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * __construct |
||
| 73 | * |
||
| 74 | * @param null|iAdapter $adapter |
||
| 75 | * @param null|iSerializer $serializer |
||
| 76 | * @param boolean $checkForUser check for dev-ip or if cms-user is logged-in |
||
| 77 | * @param boolean $cacheEnabled false will disable the cache (use it e.g. for global settings) |
||
| 78 | * @param string|boolean $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this |
||
| 79 | * user) |
||
| 80 | */ |
||
| 81 | 36 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * enable / disable the cache |
||
| 140 | * |
||
| 141 | * @param boolean $isActive |
||
| 142 | */ |
||
| 143 | 36 | public function setActive($isActive) |
|
| 147 | |||
| 148 | /** |
||
| 149 | * check if the current use is a admin || dev || server == client |
||
| 150 | * |
||
| 151 | * @return bool |
||
| 152 | */ |
||
| 153 | public function isCacheActiveForTheCurrentUser() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * returns the IP address of the client |
||
| 184 | * |
||
| 185 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 186 | * proxy headers HTTP_CLIENT_IP |
||
| 187 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 188 | * use if your $_SERVER is behind a |
||
| 189 | * proxy that sets these values |
||
| 190 | * |
||
| 191 | * @return string |
||
| 192 | */ |
||
| 193 | private function getClientIp($trust_proxy_headers = false) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Check for local developer. |
||
| 214 | * |
||
| 215 | * @return bool |
||
| 216 | */ |
||
| 217 | private function checkForDev() |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
| 249 | */ |
||
| 250 | 36 | protected function getTheDefaultPrefix() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * Auto-connect to the available cache-system on the server. |
||
| 261 | * |
||
| 262 | * @return iAdapter |
||
| 263 | */ |
||
| 264 | protected function autoConnectToAvailableCacheSystem() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Set "isReady" state. |
||
| 415 | * |
||
| 416 | * @param boolean $isReady |
||
| 417 | */ |
||
| 418 | 36 | private function setCacheIsReady($isReady) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Get the "isReady" state. |
||
| 425 | * |
||
| 426 | * @return boolean |
||
| 427 | */ |
||
| 428 | 3 | public function getCacheIsReady() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Get cached-item by key. |
||
| 435 | * |
||
| 436 | * @param string $key |
||
| 437 | * @param int $forceStaticCacheHitCounter |
||
| 438 | * |
||
| 439 | * @return mixed |
||
| 440 | */ |
||
| 441 | 18 | public function getItem($key, $forceStaticCacheHitCounter = 0) |
|
| 499 | |||
| 500 | /** |
||
| 501 | * Calculate store-key (prefix + $rawKey). |
||
| 502 | * |
||
| 503 | * @param string $rawKey |
||
| 504 | * |
||
| 505 | * @return string |
||
| 506 | */ |
||
| 507 | 30 | private function calculateStoreKey($rawKey) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
| 520 | * |
||
| 521 | * @param string $str |
||
| 522 | * |
||
| 523 | * @return string |
||
| 524 | */ |
||
| 525 | 8 | private function cleanStoreKey($str) |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Get the prefix. |
||
| 554 | * |
||
| 555 | * @return string |
||
| 556 | */ |
||
| 557 | 30 | public function getPrefix() |
|
| 561 | |||
| 562 | /** |
||
| 563 | * !!! Set the prefix. !!! |
||
| 564 | * |
||
| 565 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
| 566 | * |
||
| 567 | * @param string $prefix |
||
| 568 | */ |
||
| 569 | 36 | public function setPrefix($prefix) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Get the current value, when the static cache is used. |
||
| 576 | * |
||
| 577 | * @return int |
||
| 578 | */ |
||
| 579 | public function getStaticCacheHitCounter() |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
| 586 | * |
||
| 587 | * @param int $staticCacheHitCounter |
||
| 588 | */ |
||
| 589 | public function setStaticCacheHitCounter($staticCacheHitCounter) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Set cache-item by key => value + date. |
||
| 596 | * |
||
| 597 | * @param string $key |
||
| 598 | * @param mixed $value |
||
| 599 | * @param \DateTime $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
| 600 | * |
||
| 601 | * @return boolean |
||
| 602 | * @throws \Exception |
||
| 603 | */ |
||
| 604 | 7 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Set cache-item by key => value + ttl. |
||
| 619 | * |
||
| 620 | * @param string $key |
||
| 621 | * @param mixed $value |
||
| 622 | * @param int $ttl |
||
| 623 | * |
||
| 624 | * @return bool |
||
| 625 | */ |
||
| 626 | 17 | public function setItem($key, $value, $ttl = 0) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Remove a cached-item. |
||
| 656 | * |
||
| 657 | * @param string $key |
||
| 658 | * |
||
| 659 | * @return bool |
||
| 660 | */ |
||
| 661 | 2 | public function removeItem($key) |
|
| 684 | |||
| 685 | /** |
||
| 686 | * Remove all cached-items. |
||
| 687 | * |
||
| 688 | * @return bool |
||
| 689 | */ |
||
| 690 | 1 | public function removeAll() |
|
| 705 | |||
| 706 | /** |
||
| 707 | * Check if cached-item exists. |
||
| 708 | * |
||
| 709 | * @param string $key |
||
| 710 | * |
||
| 711 | * @return boolean |
||
| 712 | */ |
||
| 713 | 5 | public function existsItem($key) |
|
| 728 | |||
| 729 | /** |
||
| 730 | * @param string $storeKey |
||
| 731 | * |
||
| 732 | * @return bool |
||
| 733 | */ |
||
| 734 | 18 | private function checkForStaticCache($storeKey) |
|
| 750 | |||
| 751 | /** |
||
| 752 | * Get the current adapter class-name. |
||
| 753 | * |
||
| 754 | * @return string |
||
| 755 | */ |
||
| 756 | 1 | public function getUsedAdapterClassName() |
|
| 760 | |||
| 761 | /** |
||
| 762 | * Get the current serializer class-name. |
||
| 763 | * |
||
| 764 | * @return string |
||
| 765 | */ |
||
| 766 | 1 | public function getUsedSerializerClassName() |
|
| 770 | } |
||
| 771 |
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: