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 |
||
22 | class Cache implements iCache |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var iAdapter |
||
27 | */ |
||
28 | protected $adapter; |
||
29 | |||
30 | /** |
||
31 | * @var iSerializer |
||
32 | */ |
||
33 | protected $serializer; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $prefix = ''; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | protected $isReady = false; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $isActive = true; |
||
49 | |||
50 | /** |
||
51 | * @var mixed no cache, if admin-session is set |
||
52 | */ |
||
53 | protected $isAdminSession = false; |
||
54 | |||
55 | /** |
||
56 | * @var array |
||
57 | */ |
||
58 | protected static $STATIC_CACHE = []; |
||
59 | |||
60 | /** |
||
61 | * @var array |
||
62 | */ |
||
63 | protected static $STATIC_CACHE_EXPIRE = []; |
||
64 | |||
65 | /** |
||
66 | * @var array |
||
67 | */ |
||
68 | protected static $STATIC_CACHE_COUNTER = []; |
||
69 | |||
70 | /** |
||
71 | * @var int |
||
72 | */ |
||
73 | protected $staticCacheHitCounter = 10; |
||
74 | |||
75 | /** |
||
76 | * __construct |
||
77 | * |
||
78 | * @param null|iAdapter $adapter |
||
79 | * @param null|iSerializer $serializer |
||
80 | * @param bool $checkForUser check for dev-ip or if cms-user is logged-in |
||
81 | * @param bool $cacheEnabled false will disable the cache (use it e.g. for global settings) |
||
82 | * @param string|bool $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this |
||
83 | * user) |
||
84 | */ |
||
85 | 43 | public function __construct(iAdapter $adapter = null, iSerializer $serializer = null, bool $checkForUser = true, bool $cacheEnabled = true, $isAdminSession = false) |
|
141 | |||
142 | /** |
||
143 | * enable / disable the cache |
||
144 | * |
||
145 | * @param bool $isActive |
||
146 | */ |
||
147 | 43 | public function setActive(bool $isActive) |
|
151 | |||
152 | /** |
||
153 | * check if the current use is a admin || dev || server == client |
||
154 | * |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function isCacheActiveForTheCurrentUser(): bool |
||
185 | |||
186 | /** |
||
187 | * returns the IP address of the client |
||
188 | * |
||
189 | * @param bool $trust_proxy_headers <p> |
||
190 | * Whether or not to trust the |
||
191 | * proxy headers HTTP_CLIENT_IP |
||
192 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
193 | * use if your $_SERVER is behind a |
||
194 | * proxy that sets these values |
||
195 | * </p> |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | protected function getClientIp(bool $trust_proxy_headers = false): string |
||
217 | |||
218 | /** |
||
219 | * Check for local developer. |
||
220 | * |
||
221 | * @return bool |
||
222 | */ |
||
223 | protected function checkForDev(): bool |
||
252 | |||
253 | /** |
||
254 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
255 | */ |
||
256 | 43 | protected function getTheDefaultPrefix() |
|
264 | |||
265 | /** |
||
266 | * Auto-connect to the available cache-system on the server. |
||
267 | * |
||
268 | * @return iAdapter |
||
269 | */ |
||
270 | protected function autoConnectToAvailableCacheSystem(): iAdapter |
||
420 | |||
421 | /** |
||
422 | * Set "isReady" state. |
||
423 | * |
||
424 | * @param bool $isReady |
||
425 | */ |
||
426 | 43 | protected function setCacheIsReady(bool $isReady) |
|
430 | |||
431 | /** |
||
432 | * Get the "isReady" state. |
||
433 | * |
||
434 | * @return bool |
||
435 | */ |
||
436 | 3 | public function getCacheIsReady(): bool |
|
440 | |||
441 | /** |
||
442 | * Get cached-item by key. |
||
443 | * |
||
444 | * @param string $key |
||
445 | * @param int $forceStaticCacheHitCounter |
||
446 | * |
||
447 | * @return mixed |
||
448 | */ |
||
449 | 19 | public function getItem(string $key, int $forceStaticCacheHitCounter = 0) |
|
504 | |||
505 | /** |
||
506 | * Calculate store-key (prefix + $rawKey). |
||
507 | * |
||
508 | * @param string $rawKey |
||
509 | * |
||
510 | * @return string |
||
511 | */ |
||
512 | 37 | protected function calculateStoreKey(string $rawKey): string |
|
522 | |||
523 | /** |
||
524 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
525 | * |
||
526 | * @param string $str |
||
527 | * |
||
528 | * @return string |
||
529 | */ |
||
530 | 8 | protected function cleanStoreKey(string $str): string |
|
556 | |||
557 | /** |
||
558 | * Get the prefix. |
||
559 | * |
||
560 | * @return string |
||
561 | */ |
||
562 | 37 | public function getPrefix(): string |
|
566 | |||
567 | /** |
||
568 | * !!! Set the prefix. !!! |
||
569 | * |
||
570 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
571 | * |
||
572 | * @param string $prefix |
||
573 | */ |
||
574 | 43 | public function setPrefix(string $prefix) |
|
578 | |||
579 | /** |
||
580 | * Get the current value, when the static cache is used. |
||
581 | * |
||
582 | * @return int |
||
583 | */ |
||
584 | public function getStaticCacheHitCounter(): int |
||
588 | |||
589 | /** |
||
590 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
591 | * |
||
592 | * @param int $staticCacheHitCounter |
||
593 | */ |
||
594 | public function setStaticCacheHitCounter(int $staticCacheHitCounter) |
||
598 | |||
599 | /** |
||
600 | * Set cache-item by key => value + date. |
||
601 | * |
||
602 | * @param string $key |
||
603 | * @param mixed $value |
||
604 | * @param \DateTime $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
605 | * |
||
606 | * @return bool |
||
607 | * @throws \Exception |
||
608 | */ |
||
609 | 7 | public function setItemToDate(string $key, $value, \DateTime $date): bool |
|
621 | |||
622 | /** |
||
623 | * Set cache-item by key => value + ttl. |
||
624 | * |
||
625 | * @param string $key |
||
626 | * @param mixed $value |
||
627 | * @param null|int|\DateInterval $ttl |
||
628 | * |
||
629 | * @return bool |
||
630 | */ |
||
631 | 20 | public function setItem(string $key, $value, $ttl = 0): bool |
|
665 | |||
666 | /** |
||
667 | * Remove a cached-item. |
||
668 | * |
||
669 | * @param string $key |
||
670 | * |
||
671 | * @return bool |
||
672 | */ |
||
673 | 3 | public function removeItem(string $key): bool |
|
696 | |||
697 | /** |
||
698 | * Remove all cached-items. |
||
699 | * |
||
700 | * @return bool |
||
701 | */ |
||
702 | 1 | public function removeAll(): bool |
|
717 | |||
718 | /** |
||
719 | * Check if cached-item exists. |
||
720 | * |
||
721 | * @param string $key |
||
722 | * |
||
723 | * @return bool |
||
724 | */ |
||
725 | 9 | public function existsItem(string $key): bool |
|
740 | |||
741 | /** |
||
742 | * @param string $storeKey |
||
743 | * |
||
744 | * @return bool |
||
745 | */ |
||
746 | 22 | protected function checkForStaticCache(string $storeKey): bool |
|
756 | |||
757 | /** |
||
758 | * Get the current adapter class-name. |
||
759 | * |
||
760 | * @return string |
||
761 | */ |
||
762 | 1 | public function getUsedAdapterClassName(): string |
|
766 | |||
767 | /** |
||
768 | * Get the current serializer class-name. |
||
769 | * |
||
770 | * @return string |
||
771 | */ |
||
772 | 1 | public function getUsedSerializerClassName(): string |
|
776 | } |
||
777 |