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 |
||
| 20 | class Cache implements iCache |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var array |
||
| 24 | */ |
||
| 25 | protected static $STATIC_CACHE = []; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected static $STATIC_CACHE_EXPIRE = []; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | protected static $STATIC_CACHE_COUNTER = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var iAdapter|null |
||
| 39 | */ |
||
| 40 | protected $adapter; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var iSerializer|null |
||
| 44 | */ |
||
| 45 | protected $serializer; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $unserialize_options = ['allowed_classes' => true]; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | protected $prefix = ''; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var bool |
||
| 59 | */ |
||
| 60 | protected $isReady = false; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var bool |
||
| 64 | */ |
||
| 65 | protected $isActive = true; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | protected $useCheckForDev; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $useCheckForAdminSession; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | protected $useCheckForServerIpIsClientIp; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | protected $disableCacheGetParameter; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var bool |
||
| 89 | */ |
||
| 90 | protected $isAdminSession; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var int |
||
| 94 | */ |
||
| 95 | protected $staticCacheHitCounter = 10; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * __construct |
||
| 99 | * |
||
| 100 | * @param iAdapter|null $adapter |
||
| 101 | * @param iSerializer|null $serializer |
||
| 102 | * @param bool $checkForUsage <p>check for admin-session && check |
||
| 103 | * for server-ip == client-ip |
||
| 104 | * && check for dev</p> |
||
| 105 | * @param bool $cacheEnabled <p>false === disable the cache (use |
||
| 106 | * it |
||
| 107 | * e.g. for global settings)</p> |
||
| 108 | * @param bool $isAdminSession <p>true === disable cache for this |
||
| 109 | * user |
||
| 110 | * (use it e.g. for admin user settings) |
||
| 111 | * @param bool $useCheckForAdminSession <p>use $isAdminSession flag or |
||
| 112 | * not</p> |
||
| 113 | * @param bool $useCheckForDev <p>use checkForDev() or not</p> |
||
| 114 | * @param bool $useCheckForServerIpIsClientIp <p>use check for server-ip == |
||
| 115 | * client-ip or not</p> |
||
| 116 | * @param string $disableCacheGetParameter <p>set the _GET parameter for |
||
| 117 | * disabling the cache, disable this |
||
| 118 | * check via empty string</p> |
||
| 119 | * @param CacheAdapterAutoManager $cacheAdapterManagerForAutoConnect <p>Overwrite some Adapters for the |
||
| 120 | * auto-connect-function.</p> |
||
| 121 | * @param bool $cacheAdapterManagerForAutoConnectOverwrite <p>true === Use only Adapters from |
||
| 122 | * your |
||
| 123 | * "CacheAdapterManager".</p> |
||
| 124 | */ |
||
| 125 | 89 | public function __construct( |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param array $array |
||
| 197 | */ |
||
| 198 | public function setUnserializeOptions(array $array = []) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Auto-connect to the available cache-system on the server. |
||
| 205 | * |
||
| 206 | * @param CacheAdapterAutoManager $cacheAdapterManagerForAutoConnect <p>Overwrite some Adapters for the |
||
| 207 | * auto-connect-function.</p> |
||
| 208 | * @param bool $cacheAdapterManagerForAutoConnectOverwrite <p>true === Use only Adapters from |
||
| 209 | * your |
||
| 210 | * "CacheAdapterManager".</p> |
||
| 211 | * |
||
| 212 | * @return iAdapter |
||
| 213 | */ |
||
| 214 | 27 | protected function autoConnectToAvailableCacheSystem( |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Calculate store-key (prefix + $rawKey). |
||
| 266 | * |
||
| 267 | * @param string $rawKey |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | 73 | protected function calculateStoreKey(string $rawKey): string |
|
| 281 | |||
| 282 | /** |
||
| 283 | * Check for local developer. |
||
| 284 | * |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | protected function checkForDev(): bool |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $storeKey |
||
| 319 | * |
||
| 320 | * @return bool |
||
| 321 | */ |
||
| 322 | 44 | protected function checkForStaticCache(string $storeKey): bool |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
| 335 | * |
||
| 336 | * @param string $str |
||
| 337 | * |
||
| 338 | * @return string |
||
| 339 | */ |
||
| 340 | 44 | protected function cleanStoreKey(string $str): string |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Check if cached-item exists. |
||
| 347 | * |
||
| 348 | * @param string $key |
||
| 349 | * |
||
| 350 | * @return bool |
||
| 351 | */ |
||
| 352 | 12 | public function existsItem(string $key): bool |
|
| 367 | |||
| 368 | /** |
||
| 369 | * Get cached-item by key. |
||
| 370 | * |
||
| 371 | * @param string $key |
||
| 372 | * @param int $forceStaticCacheHitCounter |
||
| 373 | * |
||
| 374 | * @return mixed |
||
| 375 | */ |
||
| 376 | 38 | public function getItem(string $key, int $forceStaticCacheHitCounter = 0) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Remove all cached-items. |
||
| 434 | * |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | 1 | public function removeAll(): bool |
|
| 452 | |||
| 453 | /** |
||
| 454 | * Remove a cached-item. |
||
| 455 | * |
||
| 456 | * @param string $key |
||
| 457 | * |
||
| 458 | * @return bool |
||
| 459 | */ |
||
| 460 | 6 | public function removeItem(string $key): bool |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Set cache-item by key => value + ttl. |
||
| 486 | * |
||
| 487 | * @param string $key |
||
| 488 | * @param mixed $value |
||
| 489 | * @param \DateInterval|int|null $ttl |
||
| 490 | * |
||
| 491 | * @throws \InvalidArgumentException |
||
| 492 | * |
||
| 493 | * @return bool |
||
| 494 | */ |
||
| 495 | 41 | public function setItem(string $key, $value, $ttl = 0): bool |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Set cache-item by key => value + date. |
||
| 531 | * |
||
| 532 | * @param string $key |
||
| 533 | * @param mixed $value |
||
| 534 | * @param \DateTimeInterface $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
| 535 | * |
||
| 536 | * @throws InvalidArgumentException |
||
| 537 | * <p>If the $date is in the past.</p> |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | 19 | public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool |
|
| 551 | |||
| 552 | /** |
||
| 553 | * Get the "isReady" state. |
||
| 554 | * |
||
| 555 | * @return bool |
||
| 556 | */ |
||
| 557 | 6 | public function getCacheIsReady(): bool |
|
| 561 | |||
| 562 | /** |
||
| 563 | * returns the IP address of the client |
||
| 564 | * |
||
| 565 | * @param bool $trust_proxy_headers <p> |
||
| 566 | * Whether or not to trust the |
||
| 567 | * proxy headers HTTP_CLIENT_IP |
||
| 568 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 569 | * use if your $_SERVER is behind a |
||
| 570 | * proxy that sets these values |
||
| 571 | * </p> |
||
| 572 | * |
||
| 573 | * @return string |
||
| 574 | */ |
||
| 575 | protected function getClientIp(bool $trust_proxy_headers = false): string |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Get the prefix. |
||
| 596 | * |
||
| 597 | * @return string |
||
| 598 | */ |
||
| 599 | 73 | public function getPrefix(): string |
|
| 603 | |||
| 604 | /** |
||
| 605 | * Get the current value, when the static cache is used. |
||
| 606 | * |
||
| 607 | * @return int |
||
| 608 | */ |
||
| 609 | public function getStaticCacheHitCounter(): int |
||
| 613 | |||
| 614 | /** |
||
| 615 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
| 616 | */ |
||
| 617 | 89 | protected function getTheDefaultPrefix(): string |
|
| 625 | |||
| 626 | /** |
||
| 627 | * Get the current adapter class-name. |
||
| 628 | * |
||
| 629 | * @return string |
||
| 630 | */ |
||
| 631 | 3 | public function getUsedAdapterClassName(): string |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Get the current serializer class-name. |
||
| 643 | * |
||
| 644 | * @return string |
||
| 645 | */ |
||
| 646 | 3 | public function getUsedSerializerClassName(): string |
|
| 655 | |||
| 656 | /** |
||
| 657 | * check if the current use is a admin || dev || server == client |
||
| 658 | * |
||
| 659 | * @return bool |
||
| 660 | */ |
||
| 661 | public function isCacheActiveForTheCurrentUser(): bool |
||
| 703 | |||
| 704 | /** |
||
| 705 | * enable / disable the cache |
||
| 706 | * |
||
| 707 | * @param bool $isActive |
||
| 708 | */ |
||
| 709 | public function setActive(bool $isActive) |
||
| 713 | |||
| 714 | /** |
||
| 715 | * Set "isReady" state. |
||
| 716 | * |
||
| 717 | * @param bool $isReady |
||
| 718 | */ |
||
| 719 | 89 | protected function setCacheIsReady(bool $isReady) |
|
| 723 | |||
| 724 | /** |
||
| 725 | * !!! Set the prefix. !!! |
||
| 726 | * |
||
| 727 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
| 728 | * |
||
| 729 | * @param string $prefix |
||
| 730 | */ |
||
| 731 | 89 | public function setPrefix(string $prefix) |
|
| 735 | |||
| 736 | /** |
||
| 737 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
| 738 | * |
||
| 739 | * @param int $staticCacheHitCounter |
||
| 740 | */ |
||
| 741 | public function setStaticCacheHitCounter(int $staticCacheHitCounter) |
||
| 745 | } |
||
| 746 |