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 | 42 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 117 | |||
| 118 | /** |
||
| 119 | * enable / disable the cache |
||
| 120 | * |
||
| 121 | * @param boolean $isActive |
||
| 122 | */ |
||
| 123 | 42 | public function setActive($isActive) |
|
| 127 | |||
| 128 | /** |
||
| 129 | * check if the current use is a admin || dev || server == client |
||
| 130 | * |
||
| 131 | * @return bool |
||
| 132 | */ |
||
| 133 | public function isCacheActiveForTheCurrentUser() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * returns the IP address of the client |
||
| 164 | * |
||
| 165 | * @param bool $trust_proxy_headers Whether or not to trust the |
||
| 166 | * proxy headers HTTP_CLIENT_IP |
||
| 167 | * and HTTP_X_FORWARDED_FOR. ONLY |
||
| 168 | * use if your $_SERVER is behind a |
||
| 169 | * proxy that sets these values |
||
| 170 | * |
||
| 171 | * @return string |
||
| 172 | */ |
||
| 173 | private function getClientIp($trust_proxy_headers = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Check for local developer. |
||
| 194 | * |
||
| 195 | * @return bool |
||
| 196 | */ |
||
| 197 | private function checkForDev() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
||
| 229 | */ |
||
| 230 | 42 | protected function getTheDefaultPrefix() |
|
| 238 | |||
| 239 | /** |
||
| 240 | * Auto-connect to the available cache-system on the server. |
||
| 241 | * |
||
| 242 | * @return iAdapter |
||
| 243 | */ |
||
| 244 | protected function autoConnectToAvailableCacheSystem() |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Set "isReady" state. |
||
| 395 | * |
||
| 396 | * @param boolean $isReady |
||
| 397 | */ |
||
| 398 | 42 | private function setCacheIsReady($isReady) |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get the "isReady" state. |
||
| 405 | * |
||
| 406 | * @return boolean |
||
| 407 | */ |
||
| 408 | 4 | public function getCacheIsReady() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Get cached-item by key. |
||
| 415 | * |
||
| 416 | * @param string $key |
||
| 417 | * |
||
| 418 | * @return mixed |
||
| 419 | */ |
||
| 420 | 19 | public function getItem($key) |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Calculate store-key (prefix + $rawKey). |
||
| 435 | * |
||
| 436 | * @param string $rawKey |
||
| 437 | * |
||
| 438 | * @return string |
||
| 439 | */ |
||
| 440 | 33 | private function calculateStoreKey($rawKey) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
| 453 | * |
||
| 454 | * @param string $str |
||
| 455 | * |
||
| 456 | * @return string |
||
| 457 | */ |
||
| 458 | 6 | private function cleanStoreKey($str) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Get the prefix. |
||
| 487 | * |
||
| 488 | * @return string |
||
| 489 | */ |
||
| 490 | 33 | public function getPrefix() |
|
| 494 | |||
| 495 | /** |
||
| 496 | * !!! Set the prefix. !!! |
||
| 497 | * |
||
| 498 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
| 499 | * |
||
| 500 | * @param string $prefix |
||
| 501 | */ |
||
| 502 | 42 | public function setPrefix($prefix) |
|
| 506 | |||
| 507 | /** |
||
| 508 | * Set cache-item by key => value + date. |
||
| 509 | * |
||
| 510 | * @param string $key |
||
| 511 | * @param mixed $value |
||
| 512 | * @param \DateTime $date |
||
| 513 | * |
||
| 514 | * @return boolean |
||
| 515 | * @throws \Exception |
||
| 516 | */ |
||
| 517 | 6 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Set cache-item by key => value + ttl. |
||
| 532 | * |
||
| 533 | * @param string $key |
||
| 534 | * @param mixed $value |
||
| 535 | * @param int $ttl |
||
| 536 | * |
||
| 537 | * @return bool |
||
| 538 | */ |
||
| 539 | 18 | public function setItem($key, $value, $ttl = 0) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Remove a cached-item. |
||
| 561 | * |
||
| 562 | * @param string $key |
||
| 563 | * |
||
| 564 | * @return bool |
||
| 565 | */ |
||
| 566 | 2 | View Code Duplication | public function removeItem($key) |
| 576 | |||
| 577 | /** |
||
| 578 | * Remove all cached-items. |
||
| 579 | * |
||
| 580 | * @return bool |
||
| 581 | */ |
||
| 582 | 1 | public function removeAll() |
|
| 590 | |||
| 591 | /** |
||
| 592 | * Check if cached-item exists. |
||
| 593 | * |
||
| 594 | * @param string $key |
||
| 595 | * |
||
| 596 | * @return boolean |
||
| 597 | */ |
||
| 598 | 6 | View Code Duplication | public function existsItem($key) |
| 608 | |||
| 609 | /** |
||
| 610 | * Get the current adapter class-name. |
||
| 611 | * |
||
| 612 | * @return string |
||
| 613 | */ |
||
| 614 | 2 | public function getUsedAdapterClassName() |
|
| 618 | |||
| 619 | /** |
||
| 620 | * Get the current serializer class-name. |
||
| 621 | * |
||
| 622 | * @return string |
||
| 623 | */ |
||
| 624 | 2 | public function getUsedSerializerClassName() |
|
| 628 | } |
||
| 629 |
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: