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 | /** |
||
24 | * @var iAdapter |
||
25 | */ |
||
26 | protected $adapter; |
||
27 | |||
28 | /** |
||
29 | * @var iSerializer |
||
30 | */ |
||
31 | protected $serializer; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $prefix = ''; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $isReady = false; |
||
42 | |||
43 | /** |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $isActive = true; |
||
47 | |||
48 | /** |
||
49 | * @var mixed no cache, if admin-session is set |
||
50 | */ |
||
51 | protected $isAdminSession = false; |
||
52 | |||
53 | /** |
||
54 | * @var array |
||
55 | */ |
||
56 | protected static $STATIC_CACHE = array(); |
||
57 | |||
58 | /** |
||
59 | * @var array |
||
60 | */ |
||
61 | protected static $STATIC_CACHE_EXPIRE = array(); |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected static $STATIC_CACHE_COUNTER = array(); |
||
67 | |||
68 | /** |
||
69 | * @var int |
||
70 | */ |
||
71 | protected $staticCacheHitCounter = 10; |
||
72 | |||
73 | /** |
||
74 | * __construct |
||
75 | * |
||
76 | * @param null|iAdapter $adapter |
||
77 | * @param null|iSerializer $serializer |
||
78 | * @param boolean $checkForUser check for dev-ip or if cms-user is logged-in |
||
79 | * @param boolean $cacheEnabled false will disable the cache (use it e.g. for global settings) |
||
80 | * @param string|boolean $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this |
||
81 | * user) |
||
82 | */ |
||
83 | 51 | public function __construct(iAdapter $adapter = null, iSerializer $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
139 | |||
140 | /** |
||
141 | * enable / disable the cache |
||
142 | * |
||
143 | * @param boolean $isActive |
||
144 | */ |
||
145 | 51 | public function setActive($isActive) |
|
149 | |||
150 | /** |
||
151 | * check if the current use is a admin || dev || server == client |
||
152 | * |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isCacheActiveForTheCurrentUser() |
||
183 | |||
184 | /** |
||
185 | * returns the IP address of the client |
||
186 | * |
||
187 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
188 | * proxy headers HTTP_CLIENT_IP |
||
189 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
190 | * use if your $_SERVER is behind a |
||
191 | * proxy that sets these values |
||
192 | * |
||
193 | * @return string |
||
194 | */ |
||
195 | protected function getClientIp($trust_proxy_headers = false) |
||
213 | |||
214 | /** |
||
215 | * Check for local developer. |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | protected function checkForDev() |
||
248 | |||
249 | /** |
||
250 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
251 | */ |
||
252 | 51 | protected function getTheDefaultPrefix() |
|
260 | |||
261 | /** |
||
262 | * Auto-connect to the available cache-system on the server. |
||
263 | * |
||
264 | * @return iAdapter |
||
265 | */ |
||
266 | protected function autoConnectToAvailableCacheSystem() |
||
414 | |||
415 | /** |
||
416 | * Set "isReady" state. |
||
417 | * |
||
418 | * @param boolean $isReady |
||
419 | */ |
||
420 | 51 | protected function setCacheIsReady($isReady) |
|
424 | |||
425 | /** |
||
426 | * Get the "isReady" state. |
||
427 | * |
||
428 | * @return boolean |
||
429 | */ |
||
430 | 4 | public function getCacheIsReady() |
|
434 | |||
435 | /** |
||
436 | * Get cached-item by key. |
||
437 | * |
||
438 | * @param string $key |
||
439 | * @param int $forceStaticCacheHitCounter |
||
440 | * |
||
441 | * @return mixed |
||
442 | */ |
||
443 | 22 | public function getItem($key, $forceStaticCacheHitCounter = 0) |
|
501 | |||
502 | /** |
||
503 | * Calculate store-key (prefix + $rawKey). |
||
504 | * |
||
505 | * @param string $rawKey |
||
506 | * |
||
507 | * @return string |
||
508 | */ |
||
509 | 42 | protected function calculateStoreKey($rawKey) |
|
519 | |||
520 | /** |
||
521 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
522 | * |
||
523 | * @param string $str |
||
524 | * |
||
525 | * @return string |
||
526 | */ |
||
527 | 8 | protected function cleanStoreKey($str) |
|
553 | |||
554 | /** |
||
555 | * Get the prefix. |
||
556 | * |
||
557 | * @return string |
||
558 | */ |
||
559 | 42 | public function getPrefix() |
|
563 | |||
564 | /** |
||
565 | * !!! Set the prefix. !!! |
||
566 | * |
||
567 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
568 | * |
||
569 | * @param string $prefix |
||
570 | */ |
||
571 | 51 | public function setPrefix($prefix) |
|
575 | |||
576 | /** |
||
577 | * Get the current value, when the static cache is used. |
||
578 | * |
||
579 | * @return int |
||
580 | */ |
||
581 | public function getStaticCacheHitCounter() |
||
585 | |||
586 | /** |
||
587 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
588 | * |
||
589 | * @param int $staticCacheHitCounter |
||
590 | */ |
||
591 | public function setStaticCacheHitCounter($staticCacheHitCounter) |
||
595 | |||
596 | /** |
||
597 | * Set cache-item by key => value + date. |
||
598 | * |
||
599 | * @param string $key |
||
600 | * @param mixed $value |
||
601 | * @param \DateTime $date <p>If the date is in the past, we will remove the existing cache-item.</p> |
||
602 | * |
||
603 | * @return boolean |
||
604 | * @throws \Exception |
||
605 | */ |
||
606 | 8 | public function setItemToDate($key, $value, \DateTime $date) |
|
618 | |||
619 | /** |
||
620 | * Set cache-item by key => value + ttl. |
||
621 | * |
||
622 | * @param string $key |
||
623 | * @param mixed $value |
||
624 | * @param null|int|\DateInterval $ttl |
||
625 | * |
||
626 | * @return bool |
||
627 | */ |
||
628 | 23 | public function setItem($key, $value, $ttl = 0) |
|
661 | |||
662 | /** |
||
663 | * Remove a cached-item. |
||
664 | * |
||
665 | * @param string $key |
||
666 | * |
||
667 | * @return bool |
||
668 | */ |
||
669 | 3 | public function removeItem($key) |
|
692 | |||
693 | /** |
||
694 | * Remove all cached-items. |
||
695 | * |
||
696 | * @return bool |
||
697 | */ |
||
698 | 1 | public function removeAll() |
|
713 | |||
714 | /** |
||
715 | * Check if cached-item exists. |
||
716 | * |
||
717 | * @param string $key |
||
718 | * |
||
719 | * @return boolean |
||
720 | */ |
||
721 | 10 | public function existsItem($key) |
|
736 | |||
737 | /** |
||
738 | * @param string $storeKey |
||
739 | * |
||
740 | * @return bool |
||
741 | */ |
||
742 | 26 | protected function checkForStaticCache($storeKey) |
|
758 | |||
759 | /** |
||
760 | * Get the current adapter class-name. |
||
761 | * |
||
762 | * @return string |
||
763 | */ |
||
764 | 2 | public function getUsedAdapterClassName() |
|
768 | |||
769 | /** |
||
770 | * Get the current serializer class-name. |
||
771 | * |
||
772 | * @return string |
||
773 | */ |
||
774 | 2 | public function getUsedSerializerClassName() |
|
778 | } |
||
779 |
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: