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 | 119 | public function __construct( |
|
201 | |||
202 | /** |
||
203 | * @param array $array |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | public function setUnserializeOptions(array $array = []) |
||
211 | |||
212 | /** |
||
213 | * Auto-connect to the available cache-system on the server. |
||
214 | * |
||
215 | * @param CacheAdapterAutoManager $cacheAdapterManagerForAutoConnect <p>Overwrite some Adapters for the |
||
216 | * auto-connect-function.</p> |
||
217 | * @param bool $cacheAdapterManagerForAutoConnectOverwrite <p>true === Use only Adapters from |
||
218 | * your |
||
219 | * "CacheAdapterManager".</p> |
||
220 | * |
||
221 | * @return iAdapter |
||
222 | */ |
||
223 | 27 | protected function autoConnectToAvailableCacheSystem( |
|
272 | |||
273 | /** |
||
274 | * Calculate store-key (prefix + $rawKey). |
||
275 | * |
||
276 | * @param string $rawKey |
||
277 | * |
||
278 | * @return string |
||
279 | */ |
||
280 | 99 | protected function calculateStoreKey(string $rawKey): string |
|
290 | |||
291 | /** |
||
292 | * Check for local developer. |
||
293 | * |
||
294 | * @return bool |
||
295 | */ |
||
296 | protected function checkForDev(): bool |
||
325 | |||
326 | /** |
||
327 | * @param string $storeKey |
||
328 | * |
||
329 | * @return bool |
||
330 | */ |
||
331 | 67 | protected function checkForStaticCache(string $storeKey): bool |
|
341 | |||
342 | /** |
||
343 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
344 | * |
||
345 | * @param string $str |
||
346 | * |
||
347 | * @return string |
||
348 | */ |
||
349 | 57 | protected function cleanStoreKey(string $str): string |
|
353 | |||
354 | /** |
||
355 | * Check if cached-item exists. |
||
356 | * |
||
357 | * @param string $key |
||
358 | * |
||
359 | * @return bool |
||
360 | */ |
||
361 | 24 | public function existsItem(string $key): bool |
|
376 | |||
377 | /** |
||
378 | * Get cached-item by key. |
||
379 | * |
||
380 | * @param string $key |
||
381 | * @param int $forceStaticCacheHitCounter |
||
382 | * |
||
383 | * @return mixed |
||
384 | */ |
||
385 | 56 | public function getItem(string $key, int $forceStaticCacheHitCounter = 0) |
|
444 | |||
445 | /** |
||
446 | * Remove all cached-items. |
||
447 | * |
||
448 | * @return bool |
||
449 | */ |
||
450 | 5 | public function removeAll(): bool |
|
465 | |||
466 | /** |
||
467 | * Remove a cached-item. |
||
468 | * |
||
469 | * @param string $key |
||
470 | * |
||
471 | * @return bool |
||
472 | */ |
||
473 | 10 | public function removeItem(string $key): bool |
|
496 | |||
497 | /** |
||
498 | * Set cache-item by key => value + ttl. |
||
499 | * |
||
500 | * @param string $key |
||
501 | * @param mixed $value |
||
502 | * @param \DateInterval|int|null $ttl |
||
503 | * |
||
504 | * @throws \InvalidArgumentException |
||
505 | * |
||
506 | * @return bool |
||
507 | */ |
||
508 | 57 | public function setItem(string $key, $value, $ttl = 0): bool |
|
540 | |||
541 | /** |
||
542 | * Set cache-item by key => value + date. |
||
543 | * |
||
544 | * @param string $key |
||
545 | * @param mixed $value |
||
546 | * @param \DateTimeInterface $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
547 | * |
||
548 | * @throws InvalidArgumentException |
||
549 | * <p>If the $date is in the past.</p> |
||
550 | * |
||
551 | * @return bool |
||
552 | */ |
||
553 | 26 | public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool |
|
563 | |||
564 | /** |
||
565 | * Get the "isReady" state. |
||
566 | * |
||
567 | * @return bool |
||
568 | */ |
||
569 | 8 | public function getCacheIsReady(): bool |
|
573 | |||
574 | /** |
||
575 | * returns the IP address of the client |
||
576 | * |
||
577 | * @param bool $trust_proxy_headers <p> |
||
578 | * Whether or not to trust the |
||
579 | * proxy headers HTTP_CLIENT_IP |
||
580 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
581 | * use if your $_SERVER is behind a |
||
582 | * proxy that sets these values |
||
583 | * </p> |
||
584 | * |
||
585 | * @return string |
||
586 | */ |
||
587 | protected function getClientIp(bool $trust_proxy_headers = false): string |
||
605 | |||
606 | /** |
||
607 | * Get the prefix. |
||
608 | * |
||
609 | * @return string |
||
610 | */ |
||
611 | 99 | public function getPrefix(): string |
|
615 | |||
616 | /** |
||
617 | * Get the current value, when the static cache is used. |
||
618 | * |
||
619 | * @return int |
||
620 | */ |
||
621 | public function getStaticCacheHitCounter(): int |
||
625 | |||
626 | /** |
||
627 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
628 | * |
||
629 | * @return string |
||
630 | */ |
||
631 | 119 | protected function getTheDefaultPrefix(): string |
|
641 | |||
642 | /** |
||
643 | * Get the current adapter class-name. |
||
644 | * |
||
645 | * @return string |
||
646 | * |
||
647 | * @psalm-return class-string|string |
||
648 | */ |
||
649 | 4 | public function getUsedAdapterClassName(): string |
|
657 | |||
658 | /** |
||
659 | * Get the current serializer class-name. |
||
660 | * |
||
661 | * @return string |
||
662 | * |
||
663 | * @psalm-return class-string|string |
||
664 | */ |
||
665 | 4 | public function getUsedSerializerClassName(): string |
|
673 | |||
674 | /** |
||
675 | * check if the current use is a admin || dev || server == client |
||
676 | * |
||
677 | * @return bool |
||
678 | */ |
||
679 | public function isCacheActiveForTheCurrentUser(): bool |
||
722 | |||
723 | /** |
||
724 | * enable / disable the cache |
||
725 | * |
||
726 | * @param bool $isActive |
||
727 | * |
||
728 | * @return void |
||
729 | */ |
||
730 | public function setActive(bool $isActive) |
||
734 | |||
735 | /** |
||
736 | * Set "isReady" state. |
||
737 | * |
||
738 | * @param bool $isReady |
||
739 | * |
||
740 | * @return void |
||
741 | */ |
||
742 | 119 | protected function setCacheIsReady(bool $isReady) |
|
746 | |||
747 | /** |
||
748 | * !!! Set the prefix. !!! |
||
749 | * |
||
750 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
751 | * |
||
752 | * @param string $prefix |
||
753 | * |
||
754 | * @return void |
||
755 | */ |
||
756 | 119 | public function setPrefix(string $prefix) |
|
760 | |||
761 | /** |
||
762 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
763 | * |
||
764 | * @param int $staticCacheHitCounter |
||
765 | * |
||
766 | * @return void |
||
767 | */ |
||
768 | public function setStaticCacheHitCounter(int $staticCacheHitCounter) |
||
772 | } |
||
773 |