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 user) |
||
| 58 | */ |
||
| 59 | 33 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * enable / disable the cache |
||
| 113 | * |
||
| 114 | * @param boolean $isActive |
||
| 115 | */ |
||
| 116 | 33 | public function setActive($isActive) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * check if the current use is a admin || dev || server == client |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | public function isCacheActiveForTheCurrentUser() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * returns the IP address of the client |
||
| 157 | * |
||
| 158 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 159 | * proxy headers HTTP_CLIENT_IP |
||
| 160 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 161 | * use if your $_SERVER is behind a |
||
| 162 | * proxy that sets these values |
||
| 163 | * |
||
| 164 | * @return string |
||
| 165 | */ |
||
| 166 | private function getClientIp($trust_proxy_headers = false) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * check for developer |
||
| 187 | * |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | private function checkForDev() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * set the default-prefix via "SERVER_NAME" + "SESSION"-language |
||
| 222 | */ |
||
| 223 | 33 | protected function getTheDefaultPrefix() |
|
| 227 | |||
| 228 | /** |
||
| 229 | * auto-connect to the available cache-system on the server |
||
| 230 | * |
||
| 231 | * @return iAdapter |
||
| 232 | */ |
||
| 233 | protected function autoConnectToAvailableCacheSystem() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * set cacheIsReady state |
||
| 354 | * |
||
| 355 | * @param boolean $isReady |
||
| 356 | */ |
||
| 357 | 33 | private function setCacheIsReady($isReady) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * get the cacheIsReady state |
||
| 364 | * |
||
| 365 | * @return boolean |
||
| 366 | */ |
||
| 367 | 4 | public function getCacheIsReady() |
|
| 371 | |||
| 372 | /** |
||
| 373 | * get cached-item by key |
||
| 374 | * |
||
| 375 | * @param string $key |
||
| 376 | * |
||
| 377 | * @return mixed |
||
| 378 | */ |
||
| 379 | 15 | public function getItem($key) |
|
| 392 | |||
| 393 | /** |
||
| 394 | * calculate store-key (prefix + $rawKey) |
||
| 395 | * |
||
| 396 | * @param String $rawKey |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | 28 | private function calculateStoreKey($rawKey) |
|
| 404 | |||
| 405 | /** |
||
| 406 | * @return mixed |
||
| 407 | */ |
||
| 408 | 28 | public function getPrefix() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * set prefix [WARNING: do not use if you don't know what you do] |
||
| 415 | * |
||
| 416 | * @param string $prefix |
||
| 417 | */ |
||
| 418 | 33 | public function setPrefix($prefix) |
|
| 422 | |||
| 423 | /** |
||
| 424 | * set cache-item by key => value + date |
||
| 425 | * |
||
| 426 | * @param string $key |
||
| 427 | * @param mixed $value |
||
| 428 | * @param \DateTime $date |
||
| 429 | * |
||
| 430 | * @return mixed|void |
||
| 431 | * @throws \Exception |
||
| 432 | */ |
||
| 433 | 6 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 445 | |||
| 446 | /** |
||
| 447 | * set cache-item by key => value + ttl |
||
| 448 | * |
||
| 449 | * @param string $key |
||
| 450 | * @param mixed $value |
||
| 451 | * @param int $ttl |
||
| 452 | * |
||
| 453 | * @return bool |
||
| 454 | */ |
||
| 455 | 15 | public function setItem($key, $value, $ttl = 0) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * remove cached-item |
||
| 478 | * |
||
| 479 | * @param string $key |
||
| 480 | * |
||
| 481 | * @return bool |
||
| 482 | */ |
||
| 483 | 1 | View Code Duplication | public function removeItem($key) |
| 493 | |||
| 494 | /** |
||
| 495 | * check if cached-item exists |
||
| 496 | * |
||
| 497 | * @param string $key |
||
| 498 | * |
||
| 499 | * @return boolean |
||
| 500 | */ |
||
| 501 | 5 | View Code Duplication | public function existsItem($key) |
| 511 | |||
| 512 | } |
||
| 513 |
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: