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 | 118 | public function __construct( |
|
197 | |||
198 | /** |
||
199 | * @param array $array |
||
200 | * |
||
201 | * @return void |
||
202 | */ |
||
203 | public function setUnserializeOptions(array $array = []) |
||
207 | |||
208 | /** |
||
209 | * Auto-connect to the available cache-system on the server. |
||
210 | * |
||
211 | * @param CacheAdapterAutoManager $cacheAdapterManagerForAutoConnect <p>Overwrite some Adapters for the |
||
212 | * auto-connect-function.</p> |
||
213 | * @param bool $cacheAdapterManagerForAutoConnectOverwrite <p>true === Use only Adapters from |
||
214 | * your |
||
215 | * "CacheAdapterManager".</p> |
||
216 | * |
||
217 | * @return iAdapter |
||
218 | */ |
||
219 | 27 | protected function autoConnectToAvailableCacheSystem( |
|
268 | |||
269 | /** |
||
270 | * Calculate store-key (prefix + $rawKey). |
||
271 | * |
||
272 | * @param string $rawKey |
||
273 | * |
||
274 | * @return string |
||
275 | */ |
||
276 | 98 | protected function calculateStoreKey(string $rawKey): string |
|
286 | |||
287 | /** |
||
288 | * Check for local developer. |
||
289 | * |
||
290 | * @return bool |
||
291 | */ |
||
292 | protected function checkForDev(): bool |
||
321 | |||
322 | /** |
||
323 | * @param string $storeKey |
||
324 | * |
||
325 | * @return bool |
||
326 | */ |
||
327 | 66 | protected function checkForStaticCache(string $storeKey): bool |
|
337 | |||
338 | /** |
||
339 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
340 | * |
||
341 | * @param string $str |
||
342 | * |
||
343 | * @return string |
||
344 | */ |
||
345 | 56 | protected function cleanStoreKey(string $str): string |
|
349 | |||
350 | /** |
||
351 | * Check if cached-item exists. |
||
352 | * |
||
353 | * @param string $key |
||
354 | * |
||
355 | * @return bool |
||
356 | */ |
||
357 | 24 | public function existsItem(string $key): bool |
|
372 | |||
373 | /** |
||
374 | * Get cached-item by key. |
||
375 | * |
||
376 | * @param string $key |
||
377 | * @param int $forceStaticCacheHitCounter |
||
378 | * |
||
379 | * @return mixed |
||
380 | */ |
||
381 | 55 | public function getItem(string $key, int $forceStaticCacheHitCounter = 0) |
|
436 | |||
437 | /** |
||
438 | * Remove all cached-items. |
||
439 | * |
||
440 | * @return bool |
||
441 | */ |
||
442 | 5 | public function removeAll(): bool |
|
457 | |||
458 | /** |
||
459 | * Remove a cached-item. |
||
460 | * |
||
461 | * @param string $key |
||
462 | * |
||
463 | * @return bool |
||
464 | */ |
||
465 | 10 | public function removeItem(string $key): bool |
|
488 | |||
489 | /** |
||
490 | * Set cache-item by key => value + ttl. |
||
491 | * |
||
492 | * @param string $key |
||
493 | * @param mixed $value |
||
494 | * @param \DateInterval|int|null $ttl |
||
495 | * |
||
496 | * @throws \InvalidArgumentException |
||
497 | * |
||
498 | * @return bool |
||
499 | */ |
||
500 | 56 | public function setItem(string $key, $value, $ttl = 0): bool |
|
532 | |||
533 | /** |
||
534 | * Set cache-item by key => value + date. |
||
535 | * |
||
536 | * @param string $key |
||
537 | * @param mixed $value |
||
538 | * @param \DateTimeInterface $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
539 | * |
||
540 | * @throws InvalidArgumentException |
||
541 | * <p>If the $date is in the past.</p> |
||
542 | * |
||
543 | * @return bool |
||
544 | */ |
||
545 | 26 | public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool |
|
555 | |||
556 | /** |
||
557 | * Get the "isReady" state. |
||
558 | * |
||
559 | * @return bool |
||
560 | */ |
||
561 | 8 | public function getCacheIsReady(): bool |
|
565 | |||
566 | /** |
||
567 | * returns the IP address of the client |
||
568 | * |
||
569 | * @param bool $trust_proxy_headers <p> |
||
570 | * Whether or not to trust the |
||
571 | * proxy headers HTTP_CLIENT_IP |
||
572 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
573 | * use if your $_SERVER is behind a |
||
574 | * proxy that sets these values |
||
575 | * </p> |
||
576 | * |
||
577 | * @return string |
||
578 | */ |
||
579 | protected function getClientIp(bool $trust_proxy_headers = false): string |
||
597 | |||
598 | /** |
||
599 | * Get the prefix. |
||
600 | * |
||
601 | * @return string |
||
602 | */ |
||
603 | 98 | public function getPrefix(): string |
|
607 | |||
608 | /** |
||
609 | * Get the current value, when the static cache is used. |
||
610 | * |
||
611 | * @return int |
||
612 | */ |
||
613 | public function getStaticCacheHitCounter(): int |
||
617 | |||
618 | /** |
||
619 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
620 | * |
||
621 | * @return string |
||
622 | */ |
||
623 | 118 | protected function getTheDefaultPrefix(): string |
|
633 | |||
634 | /** |
||
635 | * Get the current adapter class-name. |
||
636 | * |
||
637 | * @return string |
||
638 | * |
||
639 | * @psalm-return class-string|string |
||
640 | */ |
||
641 | 4 | public function getUsedAdapterClassName(): string |
|
649 | |||
650 | /** |
||
651 | * Get the current serializer class-name. |
||
652 | * |
||
653 | * @return string |
||
654 | * |
||
655 | * @psalm-return class-string|string |
||
656 | */ |
||
657 | 4 | public function getUsedSerializerClassName(): string |
|
665 | |||
666 | /** |
||
667 | * check if the current use is a admin || dev || server == client |
||
668 | * |
||
669 | * @return bool |
||
670 | */ |
||
671 | public function isCacheActiveForTheCurrentUser(): bool |
||
714 | |||
715 | /** |
||
716 | * enable / disable the cache |
||
717 | * |
||
718 | * @param bool $isActive |
||
719 | * |
||
720 | * @return void |
||
721 | */ |
||
722 | public function setActive(bool $isActive) |
||
726 | |||
727 | /** |
||
728 | * Set "isReady" state. |
||
729 | * |
||
730 | * @param bool $isReady |
||
731 | * |
||
732 | * @return void |
||
733 | */ |
||
734 | 118 | protected function setCacheIsReady(bool $isReady) |
|
738 | |||
739 | /** |
||
740 | * !!! Set the prefix. !!! |
||
741 | * |
||
742 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
743 | * |
||
744 | * @param string $prefix |
||
745 | * |
||
746 | * @return void |
||
747 | */ |
||
748 | 118 | public function setPrefix(string $prefix) |
|
752 | |||
753 | /** |
||
754 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
755 | * |
||
756 | * @param int $staticCacheHitCounter |
||
757 | * |
||
758 | * @return void |
||
759 | */ |
||
760 | public function setStaticCacheHitCounter(int $staticCacheHitCounter) |
||
764 | } |
||
765 |