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) { |
||
|
|
|||
| 70 | |||
| 71 | $this->selector = filter_var($select_mode, FILTER_VALIDATE_INT, array( |
||
| 72 | 'options' => array( |
||
| 73 | 'min_range' => 1, |
||
| 74 | 'max_range' => 6, |
||
| 75 | 'default' => 1 |
||
| 76 | ) |
||
| 77 | )); |
||
| 78 | |||
| 79 | $this->stack = new StackManager($flap_interval); |
||
| 80 | |||
| 81 | parent::__construct($logger); |
||
| 82 | |||
| 83 | } |
||
| 84 | |||
| 85 | public function getAutoSetTime() { |
||
| 86 | |||
| 87 | return $this->auto_set_time; |
||
| 88 | |||
| 89 | } |
||
| 90 | |||
| 91 | public function setAutoSetTime($mode=true) { |
||
| 92 | |||
| 93 | $this->auto_set_time = filter_var($mode, FILTER_VALIDATE_BOOLEAN, array( |
||
| 94 | 'options' => array( |
||
| 95 | 'default' => true |
||
| 96 | ) |
||
| 97 | )); |
||
| 98 | |||
| 99 | return $this; |
||
| 100 | |||
| 101 | } |
||
| 102 | |||
| 103 | public function setFlapInterval($ttl) { |
||
| 104 | |||
| 105 | $ttl = filter_var($ttl, FILTER_VALIDATE_INT, array( |
||
| 106 | 'options' => array( |
||
| 107 | 'min_range' => 1, |
||
| 108 | 'default' => 700 |
||
| 109 | ) |
||
| 110 | )); |
||
| 111 | |||
| 112 | return $this->stack->setFlapInterval($ttl); |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | public function getFlapInterval() { |
||
| 117 | |||
| 118 | return $this->stack->getFlapInterval(); |
||
| 119 | } |
||
| 120 | |||
| 121 | 1 | public function addProvider(ProviderInterface $provider, $weight = 0) { |
|
| 122 | |||
| 123 | try { |
||
| 124 | |||
| 125 | 1 | if ( $provider->isEnabled() === false ) { |
|
| 126 | |||
| 127 | $id = $provider->getCacheId(); |
||
| 128 | $type = get_class($provider); |
||
| 129 | |||
| 130 | $this->logger->warning("Adding provider $type [$id] as disabled: it will never serve cache requests"); |
||
| 131 | |||
| 132 | } |
||
| 133 | |||
| 134 | 1 | $provider->setTime($this->getTime()) |
|
| 135 | 1 | ->setTtl($this->getTtl()) |
|
| 136 | 1 | ->setLogger($this->getLogger()); |
|
| 137 | |||
| 138 | 1 | $id = $this->stack->add($provider, $weight); |
|
| 139 | |||
| 140 | 1 | } catch (CacheException $ce) { |
|
| 141 | |||
| 142 | throw $ce; |
||
| 143 | |||
| 144 | } |
||
| 145 | |||
| 146 | 1 | return $id; |
|
| 147 | |||
| 148 | } |
||
| 149 | |||
| 150 | 1 | public function removeProvider($id) { |
|
| 155 | |||
| 156 | public function getProvider($id) { |
||
| 157 | |||
| 158 | return $this->stack->get($id); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | 3 | public function getProviders($enabled=false) { |
|
| 167 | |||
| 168 | 4 | public function getSelectedProvider() { |
|
| 173 | |||
| 174 | /* Following methods implement ProviderInterface */ |
||
| 175 | |||
| 176 | // public function getNamespace(); |
||
| 177 | |||
| 178 | 12 | public function setNamespace($namespace = null) { |
|
| 179 | |||
| 180 | 12 | parent::setNamespace($namespace); |
|
| 187 | |||
| 188 | 47 | 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 | 29 | public function set($name, $data, $ttl = null) { |
|
| 237 | |||
| 238 | 41 | public function get($name) { |
|
| 261 | |||
| 262 | 10 | View Code Duplication | public function delete($name=null) { |
| 279 | |||
| 280 | 6 | View Code Duplication | public function flush() { |
| 297 | |||
| 298 | 6 | 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 | 28 | private function getFromSingleProvider($selector, $name) { |
|
| 361 | |||
| 362 | 6 | private function getFromAllProviders($name) { |
|
| 382 | |||
| 383 | 7 | 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.