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 Manager 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 Manager, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\SimpleCache; |
||
32 | class Manager extends AbstractProvider implements SimpleCacheManagerInterface { |
||
33 | |||
34 | use NamespaceTrait, GenericManagerTrait { |
||
35 | GenericManagerTrait::setNamespace insteadof NamespaceTrait; |
||
36 | } |
||
37 | |||
38 | const DEFAULT_PICK_MODE = 1; |
||
39 | |||
40 | protected $pick_mode; |
||
41 | |||
42 | protected $stack; |
||
43 | |||
44 | protected $align_cache; |
||
45 | |||
46 | protected $vacuum; |
||
47 | |||
48 | protected $selected; |
||
49 | |||
50 | 34 | View Code Duplication | public function __construct( |
73 | |||
74 | 33 | public function addProvider(EnhancedSimpleCacheInterface $provider, $weight = 0) { |
|
79 | |||
80 | 23 | public function get($key, $default = null) { |
|
85 | |||
86 | 29 | public function set($key, $value, $ttl = null) { |
|
87 | |||
88 | 29 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->set($key, $value, $ttl); |
|
89 | |||
90 | 28 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
91 | 1 | return $this->selectProvider()->set($key, $value, $ttl); |
|
92 | } |
||
93 | |||
94 | 27 | $result = []; |
|
95 | |||
96 | 27 | foreach ( $this->stack as $provider ) { |
|
97 | |||
98 | 27 | $pro = $provider[0]; |
|
99 | |||
100 | 27 | $provider_result = $pro->set($key, $value, $ttl); |
|
101 | |||
102 | 26 | $this->logger->debug("Saving item $key into provider ". |
|
103 | 26 | $pro->getId().": ".($provider_result ? "OK" : "NOK - ".$pro->getStateMessage())); |
|
104 | |||
105 | 26 | $result[] = $provider_result; |
|
106 | |||
107 | 26 | } |
|
108 | |||
109 | 26 | return !in_array(false, $result); |
|
110 | |||
111 | } |
||
112 | |||
113 | 3 | View Code Duplication | public function delete($key) { |
114 | |||
115 | 3 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->delete($key); |
|
116 | |||
117 | 2 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
118 | return $this->selectProvider()->delete($key); |
||
119 | } |
||
120 | |||
121 | 2 | $result = []; |
|
122 | |||
123 | 2 | foreach ( $this->stack as $provider ) { |
|
124 | |||
125 | 2 | $result[] = $provider[0]->delete($key); |
|
126 | |||
127 | 2 | } |
|
128 | |||
129 | 2 | return !in_array(false, $result); |
|
130 | |||
131 | } |
||
132 | |||
133 | 2 | public function getMultiple($keys, $default = null) { |
|
138 | |||
139 | 2 | public function setMultiple($values, $ttl = null) { |
|
140 | |||
141 | 2 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->setMultiple($values, $ttl); |
|
142 | |||
143 | 1 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
144 | return $this->selectProvider()->setMultiple($values, $ttl); |
||
145 | } |
||
146 | |||
147 | 1 | $result = []; |
|
148 | |||
149 | 1 | foreach ( $this->stack as $provider ) { |
|
150 | |||
151 | 1 | $result[] = $provider[0]->setMultiple($values, $ttl); |
|
152 | |||
153 | 1 | } |
|
154 | |||
155 | 1 | return !in_array(false, $result); |
|
156 | |||
157 | } |
||
158 | |||
159 | 2 | View Code Duplication | public function deleteMultiple($keys) { |
160 | |||
161 | 2 | if ( iterator_count($this->stack) == 0 ) return $this->vacuum->deleteMultiple($keys); |
|
162 | |||
163 | 1 | if ( $this->align_cache === false && $this->pick_mode < 5 ) { |
|
164 | return $this->selectProvider()->deleteMultiple($keys); |
||
165 | } |
||
166 | |||
167 | 1 | $result = []; |
|
168 | |||
169 | 1 | foreach ( $this->stack as $provider ) { |
|
170 | |||
171 | 1 | $result[] = $provider[0]->deleteMultiple($keys); |
|
172 | |||
173 | 1 | } |
|
174 | |||
175 | 1 | return !in_array(false, $result); |
|
176 | |||
177 | } |
||
178 | |||
179 | 18 | public function has($key) { |
|
184 | |||
185 | 1 | View Code Duplication | public static function createFromConfiguration(Configuration $configuration, LoggerInterface $logger) { |
186 | |||
187 | 1 | list($enable, $manager_configuration, $providers) = ConfigurationParser::parse($configuration, $logger); |
|
188 | |||
189 | $manager = new Manager(...$manager_configuration); |
||
190 | |||
191 | if ( $enable ) { |
||
192 | foreach ( $providers as $name => $provider ) { |
||
193 | $instance = $provider->instance; |
||
194 | $weight = $provider->weight; |
||
195 | $id = $instance->getId(); |
||
196 | $logger->debug("Adding provider $name ($id) to cache manager (w $weight)"); |
||
197 | $manager->addProvider($instance, $weight); |
||
198 | } |
||
199 | } |
||
200 | |||
201 | return $manager; |
||
202 | |||
203 | } |
||
204 | |||
205 | 29 | protected function selectFrom($mode, $key, $default = null) { |
|
206 | |||
207 | 29 | View Code Duplication | if ( $this->pick_mode < 5 ) { |
208 | |||
209 | 27 | $result = $this->fromSingleProvider($mode, $key, $default); |
|
210 | |||
211 | 29 | } else if ( $this->pick_mode == 5 ) { |
|
212 | |||
213 | 1 | $result = $this->fromAllProviders($mode, $key, $default); |
|
214 | |||
215 | 1 | } else { |
|
216 | |||
217 | 1 | $result = $this->traverse($mode, $key, $default); |
|
218 | |||
219 | } |
||
220 | |||
221 | 29 | return $result; |
|
222 | |||
223 | } |
||
224 | |||
225 | 27 | protected function fromSingleProvider($mode, $key, $default) { |
|
248 | |||
249 | 1 | protected function fromAllProviders($mode, $key, $default) { |
|
250 | |||
251 | 1 | $result = []; |
|
252 | |||
253 | 1 | if ( $mode == 'GET' ) { |
|
254 | |||
303 | |||
304 | 1 | protected function traverse($mode, $key, $default) { |
|
379 | |||
380 | } |
||
381 |
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.