Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 18 | class Cache implements iCache |
||
| 19 | { |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var iAdapter |
||
| 23 | */ |
||
| 24 | private $adapter; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var iSerializer |
||
| 28 | */ |
||
| 29 | private $serializer; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $prefix = ''; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | private $isReady = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | private $isActive = true; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var mixed no cache, if admin-session is set |
||
| 48 | */ |
||
| 49 | private $isAdminSession = false; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * __construct |
||
| 53 | * |
||
| 54 | * @param null|iAdapter $adapter |
||
| 55 | * @param null|iSerializer $serializer |
||
| 56 | * @param boolean $checkForUser check for dev-ip or if cms-user is logged-in |
||
| 57 | * @param boolean $cacheEnabled false will disable the cache (use it e.g. for global settings) |
||
| 58 | * @param string|boolean $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this |
||
| 59 | * user) |
||
| 60 | */ |
||
| 61 | 43 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * enable / disable the cache |
||
| 115 | * |
||
| 116 | * @param boolean $isActive |
||
| 117 | */ |
||
| 118 | 43 | public function setActive($isActive) |
|
| 122 | |||
| 123 | /** |
||
| 124 | * check if the current use is a admin || dev || server == client |
||
| 125 | * |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | public function isCacheActiveForTheCurrentUser() |
||
| 156 | |||
| 157 | /** |
||
| 158 | * returns the IP address of the client |
||
| 159 | * |
||
| 160 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 161 | * proxy headers HTTP_CLIENT_IP |
||
| 162 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 163 | * use if your $_SERVER is behind a |
||
| 164 | * proxy that sets these values |
||
| 165 | * |
||
| 166 | * @return string |
||
| 167 | */ |
||
| 168 | private function getClientIp($trust_proxy_headers = false) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * check for developer |
||
| 189 | * |
||
| 190 | * @return bool |
||
| 191 | */ |
||
| 192 | private function checkForDev() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * set the default-prefix via "SERVER"-var + "SESSION"-language |
||
| 224 | */ |
||
| 225 | 43 | protected function getTheDefaultPrefix() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * auto-connect to the available cache-system on the server |
||
| 236 | * |
||
| 237 | * @return iAdapter |
||
| 238 | */ |
||
| 239 | protected function autoConnectToAvailableCacheSystem() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * set cacheIsReady state |
||
| 363 | * |
||
| 364 | * @param boolean $isReady |
||
| 365 | */ |
||
| 366 | 43 | private function setCacheIsReady($isReady) |
|
| 370 | |||
| 371 | /** |
||
| 372 | * get the cacheIsReady state |
||
| 373 | * |
||
| 374 | * @return boolean |
||
| 375 | */ |
||
| 376 | 5 | public function getCacheIsReady() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * get cached-item by key |
||
| 383 | * |
||
| 384 | * @param string $key |
||
| 385 | * |
||
| 386 | * @return mixed |
||
| 387 | */ |
||
| 388 | 20 | public function getItem($key) |
|
| 400 | |||
| 401 | /** |
||
| 402 | * calculate store-key (prefix + $rawKey) |
||
| 403 | * |
||
| 404 | * @param String $rawKey |
||
| 405 | * |
||
| 406 | * @return string |
||
| 407 | */ |
||
| 408 | 37 | private function calculateStoreKey($rawKey) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @param string $str |
||
| 421 | * |
||
| 422 | * @return string |
||
| 423 | */ |
||
| 424 | 6 | private function cleanStoreKey($str) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * @return string |
||
| 453 | */ |
||
| 454 | 37 | public function getPrefix() |
|
| 458 | |||
| 459 | /** |
||
| 460 | * set prefix [WARNING: do not use if you don't know what you do] |
||
| 461 | * |
||
| 462 | * @param string $prefix |
||
| 463 | */ |
||
| 464 | 43 | public function setPrefix($prefix) |
|
| 468 | |||
| 469 | /** |
||
| 470 | * set cache-item by key => value + date |
||
| 471 | * |
||
| 472 | * @param string $key |
||
| 473 | * @param mixed $value |
||
| 474 | * @param \DateTime $date |
||
| 475 | * |
||
| 476 | * @return boolean |
||
| 477 | * @throws \Exception |
||
| 478 | */ |
||
| 479 | 7 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 491 | |||
| 492 | /** |
||
| 493 | * set cache-item by key => value + ttl |
||
| 494 | * |
||
| 495 | * @param string $key |
||
| 496 | * @param mixed $value |
||
| 497 | * @param int $ttl |
||
| 498 | * |
||
| 499 | * @return bool |
||
| 500 | */ |
||
| 501 | 20 | public function setItem($key, $value, $ttl = 0) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * remove cached-item |
||
| 523 | * |
||
| 524 | * @param string $key |
||
| 525 | * |
||
| 526 | * @return bool |
||
| 527 | */ |
||
| 528 | 2 | View Code Duplication | public function removeItem($key) |
| 538 | |||
| 539 | /** |
||
| 540 | * remove cache |
||
| 541 | * |
||
| 542 | * @return bool |
||
| 543 | */ |
||
| 544 | 1 | public function removeAll() |
|
| 552 | |||
| 553 | /** |
||
| 554 | * check if cached-item exists |
||
| 555 | * |
||
| 556 | * @param string $key |
||
| 557 | * |
||
| 558 | * @return boolean |
||
| 559 | */ |
||
| 560 | 7 | View Code Duplication | public function existsItem($key) |
| 570 | |||
| 571 | } |
||
| 572 |
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: