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 namespace Comodojo\Cache; | ||
| 28 | class Cache extends AbstractProvider { | ||
| 29 | |||
| 30 | /** | ||
| 31 | * Select the first (enabled) provider in queue, do not traverse the queue. | ||
| 32 | */ | ||
| 33 | const PICK_FIRST = 1; | ||
| 34 | |||
| 35 | /** | ||
| 36 | * Select the last (enabled) provider in queue, do not traverse the queue. | ||
| 37 | */ | ||
| 38 | const PICK_LAST = 2; | ||
| 39 | |||
| 40 | /** | ||
| 41 | * Select a random (enabled) provider in queue, do not traverse the queue. | ||
| 42 | */ | ||
| 43 | const PICK_RANDOM = 3; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Select by weight, stop at first enabled provider. | ||
| 47 | */ | ||
| 48 | const PICK_BYWEIGHT = 4; | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Ask to all (enabled) providers and match responses. | ||
| 52 | */ | ||
| 53 | const PICK_ALL = 5; | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Select the first (enabled) provider, in case of null response traverse | ||
| 57 | * the queue. | ||
| 58 | */ | ||
| 59 | const PICK_TRAVERSE = 6; | ||
| 60 | |||
| 61 | protected $selector; | ||
| 62 | |||
| 63 | protected $stack; | ||
| 64 | |||
| 65 | protected $provider; | ||
| 66 | |||
| 67 | protected $auto_set_time = false; | ||
| 68 | |||
| 69 |     public function __construct($select_mode = null, LoggerInterface $logger = null, $default_ttl = 3600, $flap_interval = 600) { | ||
| 84 | |||
| 85 |     public function getAutoSetTime() { | ||
| 90 | |||
| 91 |     public function setAutoSetTime($mode=true) { | ||
| 102 | |||
| 103 |     public function setFlapInterval($ttl) { | ||
| 115 | |||
| 116 |     public function getFlapInterval() { | ||
| 120 | |||
| 121 |     public function addProvider(ProviderInterface $provider, $weight = 0) { | ||
| 149 | |||
| 150 |     public function removeProvider($id) { | ||
| 155 | |||
| 156 |     public function getProvider($id) { | ||
| 161 | |||
| 162 |     public function getProviders($enabled=false) { | ||
| 167 | |||
| 168 |     public function getSelectedProvider() { | ||
| 173 | |||
| 174 | /* Following methods implement ProviderInterface */ | ||
| 175 | |||
| 176 | // public function getNamespace(); | ||
| 177 | |||
| 178 |     public function setNamespace($namespace = null) { | ||
| 187 | |||
| 188 | View Code Duplication |     public function setTime($time = null) { | |
| 197 | |||
| 198 | View Code Duplication |     public function setTtl($ttl = null) { | |
| 207 | |||
| 208 |     public function setLogger(LoggerInterface $logger = null) { | ||
| 217 | |||
| 218 |     public function set($name, $data, $ttl = null) { | ||
| 237 | |||
| 238 |     public function get($name) { | ||
| 261 | |||
| 262 | View Code Duplication |     public function delete($name=null) { | |
| 279 | |||
| 280 | View Code Duplication |     public function flush() { | |
| 297 | |||
| 298 |     public function status() { | ||
| 324 | |||
| 325 | // public function getCacheId(); | ||
| 326 | |||
| 327 | // public function getLogger(); | ||
| 328 | |||
| 329 | // public function setErrorState($message = null); | ||
| 330 | // public function resetErrorState(); | ||
| 331 | // public function getErrorState(); | ||
| 332 | // public function getErrorMessage(); | ||
| 333 | |||
| 334 | |||
| 335 |     private function getFromSingleProvider($selector, $name) { | ||
| 361 | |||
| 362 |     private function getFromAllProviders($name) { | ||
| 382 | |||
| 383 |     private function getTraverse($name) { | ||
| 401 | |||
| 402 | } | ||
| 403 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.