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 CacheManager 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 CacheManager, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Cache; |
||
25 | class CacheManager { |
||
26 | |||
27 | const PICK_FIRST = 1; |
||
28 | const PICK_LAST = 2; |
||
29 | const PICK_RANDOM = 3; |
||
30 | const PICK_BYWEIGHT = 4; |
||
31 | const PICK_ALL = 5; |
||
32 | |||
33 | private $caches = array(); |
||
34 | |||
35 | private $cache_weights = array(); |
||
36 | |||
37 | private $selector = null; |
||
38 | |||
39 | private $selected_cache = null; |
||
40 | |||
41 | /** |
||
42 | * Determine the current cache scope (default: GLOBAL) |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $namespace = "GLOBAL"; |
||
47 | |||
48 | /** |
||
49 | * current time (in sec) |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $current_time = null; |
||
54 | |||
55 | /** |
||
56 | * Current instance of \Monolog\Logger |
||
57 | * |
||
58 | * @var \Monolog\Logger |
||
59 | */ |
||
60 | protected $logger = null; |
||
61 | |||
62 | /** |
||
63 | * Cache ttl |
||
64 | * |
||
65 | * @var int |
||
66 | */ |
||
67 | protected $ttl = null; |
||
68 | |||
69 | 135 | public function __construct($select_mode = null, \Monolog\Logger $logger = null) { |
|
94 | |||
95 | /** |
||
96 | * Get current time |
||
97 | * |
||
98 | * @return float |
||
99 | */ |
||
100 | 135 | final public function getTime() { |
|
105 | |||
106 | /** |
||
107 | * Get current ttl |
||
108 | * |
||
109 | * @return int |
||
110 | */ |
||
111 | 135 | final public function getTtl() { |
|
116 | |||
117 | /** |
||
118 | * Get current namespace |
||
119 | * |
||
120 | * @return int |
||
121 | */ |
||
122 | final public function getNamespace() { |
||
127 | |||
128 | /** |
||
129 | * Get current logger |
||
130 | * |
||
131 | * @return \Monolog\Logger |
||
132 | */ |
||
133 | final public function getLogger() { |
||
138 | |||
139 | public function raiseError($message, $parameters = array()) { |
||
144 | |||
145 | /** |
||
146 | * Set current time |
||
147 | * |
||
148 | * @param int $time Set current time (in msec - float) |
||
149 | * |
||
150 | * @return \Comodojo\Cache\CacheObject\CacheObject |
||
151 | * @throws \Comodojo\Exception\CacheException |
||
152 | */ |
||
153 | 135 | View Code Duplication | final public function setTime($time = null) { |
170 | |||
171 | /** |
||
172 | * Set time to live for cache |
||
173 | * |
||
174 | * @param int $ttl Time to live (in secs) |
||
175 | * |
||
176 | * @return \Comodojo\Cache\CacheObject\CacheObject |
||
177 | * @throws \Comodojo\Exception\CacheException |
||
178 | */ |
||
179 | 135 | View Code Duplication | final public function setTtl($ttl = null) { |
200 | |||
201 | /** |
||
202 | * Set namespace for cache |
||
203 | * |
||
204 | * @param string $namespace Selected namespace (64 chars limited) |
||
205 | * |
||
206 | * @return \Comodojo\Cache\CacheObject\CacheObject |
||
207 | * @throws \Comodojo\Exception\CacheException |
||
208 | */ |
||
209 | 15 | final public function setNamespace($namespace) { |
|
226 | |||
227 | /** |
||
228 | * Set the monolog instance |
||
229 | * |
||
230 | * @param \Monolog\Logger $logger |
||
231 | * |
||
232 | * @return \Comodojo\Cache\CacheObject\CacheObject |
||
233 | */ |
||
234 | final public function setLogger(\Monolog\Logger $logger) { |
||
243 | |||
244 | 135 | public function addProvider(CacheInterface $cache_provider, $weight = 0) { |
|
269 | |||
270 | public function removeProvider($cache_id) { |
||
287 | |||
288 | public function getProviders($type = null) { |
||
311 | |||
312 | 60 | public function set($name, $data, $ttl = null) { |
|
333 | |||
334 | 75 | public function get($name) { |
|
335 | |||
336 | 75 | reset($this->caches); |
|
337 | |||
338 | 75 | $this->selected_cache = null; |
|
339 | |||
340 | 75 | $result = null; |
|
341 | |||
342 | try { |
||
343 | |||
344 | 75 | switch ( $this->selector ) { |
|
345 | |||
346 | 75 | case 1: |
|
347 | |||
348 | 15 | $result = $this->getCacheByLoop($this->caches, $name); |
|
349 | |||
350 | 15 | break; |
|
351 | |||
352 | 60 | case 2: |
|
353 | |||
354 | 15 | $result = $this->getCacheByLoop(array_reverse($this->caches, true), $name); |
|
355 | |||
356 | 15 | break; |
|
357 | |||
358 | 45 | case 3: |
|
359 | |||
360 | 30 | $result = $this->getRandomCache($this->caches, $name); |
|
361 | |||
362 | 30 | break; |
|
363 | |||
364 | 15 | case 4: |
|
365 | |||
366 | 15 | $result = $this->getCacheByWeight($this->caches, $this->cache_weights, $name); |
|
367 | |||
368 | 15 | break; |
|
369 | |||
370 | case 5: |
||
371 | |||
372 | $values = array(); |
||
373 | |||
374 | foreach ( $this->caches as $cache ) { |
||
375 | |||
376 | $values[] = $cache->get($name); |
||
377 | |||
378 | } |
||
379 | |||
380 | if ( count(array_unique($values)) === 1 ) { |
||
381 | |||
382 | $result = $values[0]; |
||
383 | |||
384 | } else { |
||
385 | |||
386 | $this->raiseError("Inconsistent values in cache providers, exiting gracefully"); |
||
387 | |||
388 | $result = null; |
||
389 | |||
390 | } |
||
391 | |||
392 | break; |
||
393 | |||
394 | 75 | } |
|
395 | |||
396 | 75 | } catch (\Exception $e) { |
|
397 | |||
398 | throw $ce; |
||
399 | |||
400 | } |
||
401 | |||
402 | 75 | return $result; |
|
403 | |||
404 | } |
||
405 | |||
406 | 15 | View Code Duplication | public function delete($name = null) { |
427 | |||
428 | 15 | View Code Duplication | public function flush() { |
449 | |||
450 | 15 | View Code Duplication | public function status() { |
471 | |||
472 | public function getSelectedCache() { |
||
477 | |||
478 | 30 | private function getCacheByLoop($caches, $name) { |
|
509 | |||
510 | 30 | private function getRandomCache($caches, $name) { |
|
555 | |||
556 | 15 | private function getCacheByWeight($caches, $weights, $name) { |
|
603 | |||
604 | } |
||
605 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.