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 | 107 | 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 | 90 | 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 | 59 | 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 | 48 | 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 | 23 | 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 | 49 | public function getItem(string $key, int $forceStaticCacheHitCounter = 0) |
|
377 | { |
||
378 | 49 | if (!$this->adapter instanceof iAdapter) { |
|
379 | return null; |
||
380 | } |
||
381 | |||
382 | 49 | $storeKey = $this->calculateStoreKey($key); |
|
383 | |||
384 | // check if we already using static-cache |
||
385 | 49 | $useStaticCache = true; |
|
386 | 49 | if ($this->adapter instanceof AdapterArray) { |
|
387 | 12 | $useStaticCache = false; |
|
388 | } |
||
389 | |||
390 | 49 | if (!isset(self::$STATIC_CACHE_COUNTER[$storeKey])) { |
|
391 | 29 | self::$STATIC_CACHE_COUNTER[$storeKey] = 0; |
|
392 | } |
||
393 | |||
394 | // get from static-cache |
||
395 | if ( |
||
396 | 49 | $useStaticCache |
|
397 | && |
||
398 | 49 | $this->checkForStaticCache($storeKey) |
|
399 | ) { |
||
400 | 8 | return self::$STATIC_CACHE[$storeKey]; |
|
401 | } |
||
402 | |||
403 | 47 | $serialized = $this->adapter->get($storeKey); |
|
404 | 47 | $value = $serialized && $this->serializer ? $this->serializer->unserialize($serialized) : null; |
|
405 | |||
406 | 47 | self::$STATIC_CACHE_COUNTER[$storeKey]++; |
|
407 | |||
408 | // save into static-cache if needed |
||
409 | if ( |
||
410 | 47 | $useStaticCache |
|
411 | && |
||
412 | ( |
||
413 | ( |
||
414 | 35 | $forceStaticCacheHitCounter !== 0 |
|
415 | && |
||
416 | 1 | self::$STATIC_CACHE_COUNTER[$storeKey] >= $forceStaticCacheHitCounter |
|
417 | ) |
||
418 | || |
||
419 | ( |
||
420 | 35 | $this->staticCacheHitCounter !== 0 |
|
421 | && |
||
422 | 47 | self::$STATIC_CACHE_COUNTER[$storeKey] >= $this->staticCacheHitCounter |
|
423 | ) |
||
424 | ) |
||
425 | ) { |
||
426 | 10 | self::$STATIC_CACHE[$storeKey] = $value; |
|
427 | } |
||
428 | |||
429 | 47 | return $value; |
|
430 | } |
||
431 | |||
432 | /** |
||
433 | * Remove all cached-items. |
||
434 | * |
||
435 | * @return bool |
||
436 | */ |
||
437 | 4 | public function removeAll(): bool |
|
452 | |||
453 | /** |
||
454 | * Remove a cached-item. |
||
455 | * |
||
456 | * @param string $key |
||
457 | * |
||
458 | * @return bool |
||
459 | */ |
||
460 | 9 | 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 | 50 | 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 | 23 | public function setItemToDate(string $key, $value, \DateTimeInterface $date): bool |
|
551 | |||
552 | /** |
||
553 | * Get the "isReady" state. |
||
554 | * |
||
555 | * @return bool |
||
556 | */ |
||
557 | 7 | 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 | 90 | 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 | 107 | protected function getTheDefaultPrefix(): string |
|
627 | |||
628 | /** |
||
629 | * Get the current adapter class-name. |
||
630 | * |
||
631 | * @return string |
||
632 | */ |
||
633 | 3 | public function getUsedAdapterClassName(): string |
|
642 | |||
643 | /** |
||
644 | * Get the current serializer class-name. |
||
645 | * |
||
646 | * @return string |
||
647 | */ |
||
648 | 3 | public function getUsedSerializerClassName(): string |
|
657 | |||
658 | /** |
||
659 | * check if the current use is a admin || dev || server == client |
||
660 | * |
||
661 | * @return bool |
||
662 | */ |
||
663 | public function isCacheActiveForTheCurrentUser(): bool |
||
705 | |||
706 | /** |
||
707 | * enable / disable the cache |
||
708 | * |
||
709 | * @param bool $isActive |
||
710 | */ |
||
711 | public function setActive(bool $isActive) |
||
715 | |||
716 | /** |
||
717 | * Set "isReady" state. |
||
718 | * |
||
719 | * @param bool $isReady |
||
720 | */ |
||
721 | 107 | protected function setCacheIsReady(bool $isReady) |
|
725 | |||
726 | /** |
||
727 | * !!! Set the prefix. !!! |
||
728 | * |
||
729 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
730 | * |
||
731 | * @param string $prefix |
||
732 | */ |
||
733 | 107 | public function setPrefix(string $prefix) |
|
737 | |||
738 | /** |
||
739 | * Set the static-hit-counter: Who often do we hit the cache, before we use static cache? |
||
740 | * |
||
741 | * @param int $staticCacheHitCounter |
||
742 | */ |
||
743 | public function setStaticCacheHitCounter(int $staticCacheHitCounter) |
||
747 | } |
||
748 |