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 | 37 | public function __construct($adapter = null, $serializer = null, $checkForUser = true, $cacheEnabled = true, $isAdminSession = false) |
|
| 117 | |||
| 118 | 37 | /** |
|
| 119 | * enable / disable the cache |
||
| 120 | 37 | * |
|
| 121 | 37 | * @param boolean $isActive |
|
| 122 | */ |
||
| 123 | 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 | 37 | /** |
|
| 228 | 37 | * Set the default-prefix via "SERVER"-var + "SESSION"-language. |
|
| 229 | 37 | */ |
|
| 230 | 37 | 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() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Set "isReady" state. |
||
| 382 | * |
||
| 383 | * @param boolean $isReady |
||
| 384 | */ |
||
| 385 | private function setCacheIsReady($isReady) |
||
| 389 | |||
| 390 | 17 | /** |
|
| 391 | 17 | * Get the "isReady" state. |
|
| 392 | 17 | * |
|
| 393 | 17 | * @return boolean |
|
| 394 | 17 | */ |
|
| 395 | public function getCacheIsReady() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get cached-item by key. |
||
| 402 | * |
||
| 403 | * @param string $key |
||
| 404 | * |
||
| 405 | * @return mixed |
||
| 406 | */ |
||
| 407 | public function getItem($key) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Calculate store-key (prefix + $rawKey). |
||
| 422 | * |
||
| 423 | * @param string $rawKey |
||
| 424 | 6 | * |
|
| 425 | * @return string |
||
| 426 | 6 | */ |
|
| 427 | 6 | private function calculateStoreKey($rawKey) |
|
| 437 | 6 | ||
| 438 | 6 | /** |
|
| 439 | * Clean store-key (required e.g. for the "File"-Adapter). |
||
| 440 | 6 | * |
|
| 441 | 6 | * @param string $str |
|
| 442 | 6 | * |
|
| 443 | 6 | * @return string |
|
| 444 | 6 | */ |
|
| 445 | 6 | private function cleanStoreKey($str) |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Get the prefix. |
||
| 474 | * |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getPrefix() |
||
| 481 | 6 | ||
| 482 | /** |
||
| 483 | 6 | * !!! Set the prefix. !!! |
|
| 484 | 1 | * |
|
| 485 | * WARNING: Do not use if you don't know what you do. Because this will overwrite the default prefix. |
||
| 486 | * |
||
| 487 | 5 | * @param string $prefix |
|
| 488 | */ |
||
| 489 | 5 | public function setPrefix($prefix) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Set cache-item by key => value + date. |
||
| 496 | * |
||
| 497 | * @param string $key |
||
| 498 | * @param mixed $value |
||
| 499 | * @param \DateTime $date |
||
| 500 | * |
||
| 501 | 17 | * @return boolean |
|
| 502 | * @throws \Exception |
||
| 503 | */ |
||
| 504 | 17 | public function setItemToDate($key, $value, \DateTime $date) |
|
| 516 | |||
| 517 | /** |
||
| 518 | * Set cache-item by key => value + ttl. |
||
| 519 | * |
||
| 520 | * @param string $key |
||
| 521 | * @param mixed $value |
||
| 522 | * @param int $ttl |
||
| 523 | * |
||
| 524 | * @return bool |
||
| 525 | */ |
||
| 526 | public function setItem($key, $value, $ttl = 0) |
||
| 545 | |||
| 546 | 1 | /** |
|
| 547 | 1 | * Remove a cached-item. |
|
| 548 | * |
||
| 549 | * @param string $key |
||
| 550 | * |
||
| 551 | * @return bool |
||
| 552 | */ |
||
| 553 | View Code Duplication | public function removeItem($key) |
|
| 563 | 6 | ||
| 564 | /** |
||
| 565 | 6 | * Remove all cached-items. |
|
| 566 | * |
||
| 567 | * @return bool |
||
| 568 | */ |
||
| 569 | public function removeAll() |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Check if cached-item exists. |
||
| 580 | * |
||
| 581 | * @param string $key |
||
| 582 | * |
||
| 583 | * @return boolean |
||
| 584 | */ |
||
| 585 | View Code Duplication | public function existsItem($key) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * Get the current adapter class-name. |
||
| 598 | * |
||
| 599 | * @return string |
||
| 600 | */ |
||
| 601 | public function getUsedAdapterClassName() |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Get the current serializer class-name. |
||
| 608 | * |
||
| 609 | * @return string |
||
| 610 | */ |
||
| 611 | public function getUsedSerializerClassName() |
||
| 615 | } |
||
| 616 |
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: