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 | ||
| 17 | class Cache implements iCache | ||
| 18 | { | ||
| 19 | |||
| 20 | /** | ||
| 21 | * @var iAdapter | ||
| 22 | */ | ||
| 23 | private $adapter; | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @var iSerializer | ||
| 27 | */ | ||
| 28 | private $serializer; | ||
| 29 | |||
| 30 | /** | ||
| 31 | * @var string | ||
| 32 | */ | ||
| 33 | private $prefix = ''; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * @var bool | ||
| 37 | */ | ||
| 38 | private $isReady = false; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * @var bool | ||
| 42 | */ | ||
| 43 | private $isActive = true; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * @var mixed no cache, if admin-session is set | ||
| 47 | */ | ||
| 48 | private $isAdminSession = false; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * __construct | ||
| 52 | * | ||
| 53 | * @param null|iAdapter $adapter | ||
| 54 | * @param null|iSerializer $serializer | ||
| 55 | * @param boolean $checkForUser check for dev-ip or if cms-user is logged-in | ||
| 56 | * @param boolean $cacheEnabled false will disable the cache (use it e.g. for global settings) | ||
| 57 | * @param string|boolean $isAdminSession set a user-id, if the user is a admin (so we can disable cache for this | ||
| 58 | * user) | ||
| 59 | */ | ||
| 60 | 39 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) | |
| 102 | |||
| 103 | /** | ||
| 104 | * enable / disable the cache | ||
| 105 | * | ||
| 106 | * @param boolean $isActive | ||
| 107 | */ | ||
| 108 | 39 | public function setActive($isActive) | |
| 112 | |||
| 113 | /** | ||
| 114 | * check if the current use is a admin || dev || server == client | ||
| 115 | * | ||
| 116 | * @return bool | ||
| 117 | */ | ||
| 118 | public function isCacheActiveForTheCurrentUser() | ||
| 146 | |||
| 147 | /** | ||
| 148 | * returns the IP address of the client | ||
| 149 | * | ||
| 150 | * @param bool $trust_proxy_headers Whether or not to trust the | ||
| 151 | * proxy headers HTTP_CLIENT_IP | ||
| 152 | * and HTTP_X_FORWARDED_FOR. ONLY | ||
| 153 | * use if your $_SERVER is behind a | ||
| 154 | * proxy that sets these values | ||
| 155 | * | ||
| 156 | * @return string | ||
| 157 | */ | ||
| 158 | private function getClientIp($trust_proxy_headers = false) | ||
| 176 | |||
| 177 | /** | ||
| 178 | * check for developer | ||
| 179 | * | ||
| 180 | * @return bool | ||
| 181 | */ | ||
| 182 | private function checkForDev() | ||
| 211 | |||
| 212 | /** | ||
| 213 | * set the default-prefix via "SERVER"-var + "SESSION"-language | ||
| 214 | */ | ||
| 215 | 39 | protected function getTheDefaultPrefix() | |
| 223 | |||
| 224 | /** | ||
| 225 | * auto-connect to the available cache-system on the server | ||
| 226 | * | ||
| 227 | * @return iAdapter | ||
| 228 | */ | ||
| 229 | protected function autoConnectToAvailableCacheSystem() | ||
| 348 | |||
| 349 | /** | ||
| 350 | * set cacheIsReady state | ||
| 351 | * | ||
| 352 | * @param boolean $isReady | ||
| 353 | */ | ||
| 354 | 39 | private function setCacheIsReady($isReady) | |
| 358 | |||
| 359 | /** | ||
| 360 | * get the cacheIsReady state | ||
| 361 | * | ||
| 362 | * @return boolean | ||
| 363 | */ | ||
| 364 | 5 | public function getCacheIsReady() | |
| 368 | |||
| 369 | /** | ||
| 370 | * get cached-item by key | ||
| 371 | * | ||
| 372 | * @param string $key | ||
| 373 | * | ||
| 374 | * @return mixed | ||
| 375 | */ | ||
| 376 | 18 | public function getItem($key) | |
| 388 | |||
| 389 | /** | ||
| 390 | * calculate store-key (prefix + $rawKey) | ||
| 391 | * | ||
| 392 | * @param String $rawKey | ||
| 393 | * | ||
| 394 | * @return string | ||
| 395 | */ | ||
| 396 | 33 | private function calculateStoreKey($rawKey) | |
| 406 | |||
| 407 | /** | ||
| 408 | * @param $str | ||
| 409 | * | ||
| 410 | * @return string | ||
| 411 | */ | ||
| 412 | 5 | private function cleanStoreKey($str) | |
| 438 | |||
| 439 | /** | ||
| 440 | * @return mixed | ||
| 441 | */ | ||
| 442 | 33 | public function getPrefix() | |
| 446 | |||
| 447 | /** | ||
| 448 | * set prefix [WARNING: do not use if you don't know what you do] | ||
| 449 | * | ||
| 450 | * @param string $prefix | ||
| 451 | */ | ||
| 452 | 39 | public function setPrefix($prefix) | |
| 456 | |||
| 457 | /** | ||
| 458 | * set cache-item by key => value + date | ||
| 459 | * | ||
| 460 | * @param string $key | ||
| 461 | * @param mixed $value | ||
| 462 | * @param \DateTime $date | ||
| 463 | * | ||
| 464 | * @return mixed|void | ||
| 465 | * @throws \Exception | ||
| 466 | */ | ||
| 467 | 7 | public function setItemToDate($key, $value, \DateTime $date) | |
| 479 | |||
| 480 | /** | ||
| 481 | * set cache-item by key => value + ttl | ||
| 482 | * | ||
| 483 | * @param string $key | ||
| 484 | * @param mixed $value | ||
| 485 | * @param int $ttl | ||
| 486 | * | ||
| 487 | * @return bool | ||
| 488 | */ | ||
| 489 | 18 | public function setItem($key, $value, $ttl = 0) | |
| 508 | |||
| 509 | /** | ||
| 510 | * remove cached-item | ||
| 511 | * | ||
| 512 | * @param string $key | ||
| 513 | * | ||
| 514 | * @return bool | ||
| 515 | */ | ||
| 516 | 1 | View Code Duplication | public function removeItem($key) | 
| 526 | |||
| 527 | /** | ||
| 528 | * check if cached-item exists | ||
| 529 | * | ||
| 530 | * @param string $key | ||
| 531 | * | ||
| 532 | * @return boolean | ||
| 533 | */ | ||
| 534 | 6 | View Code Duplication | public function existsItem($key) | 
| 544 | |||
| 545 | } | ||
| 546 | 
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: