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 | 37 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 111 | |||
| 112 | /** |
||
| 113 | * enable / disable the cache |
||
| 114 | * |
||
| 115 | * @param boolean $isActive |
||
| 116 | */ |
||
| 117 | 37 | public function setActive($isActive) |
|
| 121 | |||
| 122 | /** |
||
| 123 | * check if the current use is a admin || dev || server == client |
||
| 124 | * |
||
| 125 | * @return bool |
||
| 126 | */ |
||
| 127 | public function isCacheActiveForTheCurrentUser() |
||
| 155 | |||
| 156 | /** |
||
| 157 | * returns the IP address of the client |
||
| 158 | * |
||
| 159 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 160 | * proxy headers HTTP_CLIENT_IP |
||
| 161 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 162 | * use if your $_SERVER is behind a |
||
| 163 | * proxy that sets these values |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | private function getClientIp($trust_proxy_headers = false) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * check for developer |
||
| 188 | * |
||
| 189 | * @return bool |
||
| 190 | */ |
||
| 191 | private function checkForDev() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * set the default-prefix via "SERVER"-var + "SESSION"-language |
||
| 223 | */ |
||
| 224 | 37 | protected function getTheDefaultPrefix() |
|
| 232 | |||
| 233 | /** |
||
| 234 | * auto-connect to the available cache-system on the server |
||
| 235 | * |
||
| 236 | * @return iAdapter |
||
| 237 | */ |
||
| 238 | protected function autoConnectToAvailableCacheSystem() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * set cacheIsReady state |
||
| 360 | * |
||
| 361 | * @param boolean $isReady |
||
| 362 | */ |
||
| 363 | 37 | private function setCacheIsReady($isReady) |
|
| 367 | |||
| 368 | /** |
||
| 369 | * get the cacheIsReady state |
||
| 370 | * |
||
| 371 | * @return boolean |
||
| 372 | */ |
||
| 373 | 4 | public function getCacheIsReady() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * get cached-item by key |
||
| 380 | * |
||
| 381 | * @param string $key |
||
| 382 | * |
||
| 383 | * @return mixed |
||
| 384 | */ |
||
| 385 | 17 | public function getItem($key) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * calculate store-key (prefix + $rawKey) |
||
| 400 | * |
||
| 401 | * @param String $rawKey |
||
| 402 | * |
||
| 403 | * @return string |
||
| 404 | */ |
||
| 405 | 32 | private function calculateStoreKey($rawKey) |
|
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $str |
||
| 418 | * |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | 6 | private function cleanStoreKey($str) |
|
| 447 | |||
| 448 | /** |
||
| 449 | * @return string |
||
| 450 | */ |
||
| 451 | 32 | public function getPrefix() |
|
| 455 | |||
| 456 | /** |
||
| 457 | * set prefix [WARNING: do not use if you don't know what you do] |
||
| 458 | * |
||
| 459 | * @param string $prefix |
||
| 460 | */ |
||
| 461 | 37 | public function setPrefix($prefix) |
|
| 465 | |||
| 466 | /** |
||
| 467 | * set cache-item by key => value + date |
||
| 468 | * |
||
| 469 | * @param string $key |
||
| 470 | * @param mixed $value |
||
| 471 | * @param \DateTime $date |
||
| 472 | * |
||
| 473 | * @return boolean |
||
| 474 | * @throws \Exception |
||
| 475 | */ |
||
| 476 | 6 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 488 | |||
| 489 | /** |
||
| 490 | * set cache-item by key => value + ttl |
||
| 491 | * |
||
| 492 | * @param string $key |
||
| 493 | * @param mixed $value |
||
| 494 | * @param int $ttl |
||
| 495 | * |
||
| 496 | * @return bool |
||
| 497 | */ |
||
| 498 | 17 | public function setItem($key, $value, $ttl = 0) |
|
| 517 | |||
| 518 | /** |
||
| 519 | * remove cached-item |
||
| 520 | * |
||
| 521 | * @param string $key |
||
| 522 | * |
||
| 523 | * @return bool |
||
| 524 | */ |
||
| 525 | 2 | View Code Duplication | public function removeItem($key) |
| 535 | |||
| 536 | /** |
||
| 537 | * remove cache |
||
| 538 | * |
||
| 539 | * @return bool |
||
| 540 | */ |
||
| 541 | 1 | public function removeAll() |
|
| 549 | |||
| 550 | /** |
||
| 551 | * check if cached-item exists |
||
| 552 | * |
||
| 553 | * @param string $key |
||
| 554 | * |
||
| 555 | * @return boolean |
||
| 556 | */ |
||
| 557 | 6 | View Code Duplication | public function existsItem($key) |
| 567 | |||
| 568 | } |
||
| 569 |
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: