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:
1 | <?php namespace Comodojo\Cache\Components; |
||
24 | class StackManager { |
||
25 | |||
26 | private $stack = array(); |
||
27 | |||
28 | private $weights = array(); |
||
29 | |||
30 | private $failures = array(); |
||
31 | |||
32 | private $flap_interval = 600; |
||
33 | |||
34 | public function __construct($flap_interval) { |
||
39 | |||
40 | public function getFlapInterval() { |
||
45 | |||
46 | public function setFlapInterval($ttl) { |
||
53 | |||
54 | 1 | public function add(ProviderInterface $provider, $weight = 0) { |
|
67 | |||
68 | 1 | View Code Duplication | public function remove($id) { |
|
|||
69 | |||
70 | 1 | if ( array_key_exists($id, $this->stack) && array_key_exists($id, $this->weights) ) { |
|
71 | |||
72 | 1 | unset($this->stack[$id]); |
|
73 | |||
74 | 1 | unset($this->weights[$id]); |
|
75 | |||
76 | 1 | if ( array_key_exists($id, $this->failures) ) unset($this->failures[$id]); |
|
77 | |||
78 | 1 | } else { |
|
79 | |||
80 | throw new CacheException("Provider not registered"); |
||
81 | |||
82 | } |
||
83 | |||
84 | 1 | } |
|
85 | |||
86 | public function get($id) { |
||
97 | |||
98 | public function disable($id) { |
||
99 | |||
100 | if ( array_key_exists($id, $this->stack) && array_key_exists($id, $this->weights) ) { |
||
101 | |||
102 | $this->stack[$id]->disable(); |
||
103 | |||
104 | $this->failures[$id] = time(); |
||
105 | |||
106 | } else { |
||
107 | |||
108 | throw new CacheException("Provider not registered"); |
||
109 | |||
110 | } |
||
111 | |||
112 | } |
||
113 | |||
114 | View Code Duplication | public function enable($id) { |
|
115 | |||
116 | if ( array_key_exists($id, $this->stack) && array_key_exists($id, $this->weights) ) { |
||
117 | |||
118 | $this->stack[$id]->enable(); |
||
119 | |||
120 | if ( array_key_exists($id, $this->failures) ) unset($this->failures[$id]); |
||
121 | |||
122 | } else { |
||
123 | |||
124 | throw new CacheException("Provider not registered"); |
||
125 | |||
126 | } |
||
127 | |||
128 | } |
||
129 | |||
130 | 53 | public function getAll($enabled=true) { |
|
135 | |||
136 | 7 | public function getRandom() { |
|
141 | |||
142 | 7 | View Code Duplication | public function getFirst() { |
153 | |||
154 | 7 | View Code Duplication | public function getLast() { |
165 | |||
166 | 7 | public function getByWeight() { |
|
179 | |||
180 | 47 | private function checkCacheFlap($cache_id, $cache) { |
|
181 | |||
182 | 47 | if ( $cache->isEnabled() === false && array_key_exists($cache_id, $this->failures) ) { |
|
192 | |||
193 | 47 | private function getEnabled() { |
|
205 | |||
206 | } |
||
207 |
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.