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 MemcachedCache 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 MemcachedCache, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Comodojo\Cache; |
||
26 | class MemcachedCache extends CacheObject { |
||
27 | |||
28 | /** |
||
29 | * Internal memcached handler |
||
30 | * |
||
31 | * @var \Memcached |
||
32 | */ |
||
33 | private $instance = null; |
||
34 | |||
35 | /** |
||
36 | * Class constructor |
||
37 | * |
||
38 | * @param string $server Server address (or IP) |
||
39 | * @param string $port (optional) Server port |
||
40 | * @param string $weight (optional) Server weight |
||
41 | * @param string $persistent_id (optional) Persistent id |
||
42 | * @param \Monolog\Logger $logger Logger instance |
||
43 | * |
||
44 | * @throws \Comodojo\Exception\CacheException |
||
45 | */ |
||
46 | 186 | public function __construct( $server, $port=11211, $weight=0, $persistent_id=null, \Monolog\Logger $logger=null ) { |
|
98 | |||
99 | /** |
||
100 | * Set cache element |
||
101 | * |
||
102 | * This method will throw only logical exceptions. |
||
103 | * In case of failures, it will return a boolean false. |
||
104 | * |
||
105 | * @param string $name Name for cache element |
||
106 | * @param mixed $data Data to cache |
||
107 | * @param int $ttl Time to live |
||
108 | * |
||
109 | * @return bool |
||
110 | * @throws \Comodojo\Exception\CacheException |
||
111 | */ |
||
112 | 72 | public function set($name, $data, $ttl=null) { |
|
113 | |||
114 | 72 | if ( empty($name) ) throw new CacheException("Name of object cannot be empty"); |
|
115 | |||
116 | 72 | if ( is_null($data) ) throw new CacheException("Object content cannot be null"); |
|
117 | |||
118 | 72 | if ( !$this->isEnabled() ) return false; |
|
119 | |||
120 | 72 | $this->resetErrorState(); |
|
121 | |||
122 | try { |
||
123 | |||
124 | 72 | $this->setTtl($ttl); |
|
125 | |||
126 | 72 | $namespace = $this->getNamespaceKey(); |
|
127 | |||
128 | 72 | if ( $namespace === false ) $namespace = $this->setNamespaceKey(); |
|
129 | |||
130 | 72 | if ( $namespace === false ) { |
|
131 | |||
132 | $this->raiseError("Error writing cache (Memcached), exiting gracefully", array( |
||
133 | "RESULTCODE" => $this->instance->getResultCode(), |
||
134 | "RESULTMESSAGE" => $this->instance->getResultMessage() |
||
135 | )); |
||
136 | |||
137 | $this->setErrorState(); |
||
138 | |||
139 | $return = false; |
||
140 | |||
141 | } else { |
||
142 | |||
143 | 72 | $shadowName = $namespace."-".md5($name); |
|
144 | |||
145 | 72 | $shadowTtl = $this->getTime() + $this->ttl; |
|
146 | |||
147 | 72 | $shadowData = serialize($data); |
|
148 | |||
149 | 72 | $return = $this->instance->set($shadowName, $shadowData, $shadowTtl); |
|
150 | |||
151 | 72 | View Code Duplication | if ( $return === false ) { |
|
|||
152 | |||
153 | $this->raiseError("Error writing cache (Memcached), exiting gracefully", array( |
||
154 | "RESULTCODE" => $this->instance->getResultCode(), |
||
155 | "RESULTMESSAGE" => $this->instance->getResultMessage() |
||
156 | )); |
||
157 | |||
158 | $this->setErrorState(); |
||
159 | |||
160 | } |
||
161 | |||
162 | } |
||
163 | |||
164 | 72 | } catch (CacheException $ce) { |
|
165 | |||
166 | throw $ce; |
||
167 | |||
168 | } |
||
169 | |||
170 | 72 | return $return; |
|
171 | |||
172 | } |
||
173 | |||
174 | /** |
||
175 | * Get cache element |
||
176 | * |
||
177 | * This method will throw only logical exceptions. |
||
178 | * In case of failures, it will return a null value. |
||
179 | * In case of cache not found, it will return a null value. |
||
180 | * |
||
181 | * @param string $name Name for cache element |
||
182 | * |
||
183 | * @return mixed |
||
184 | * @throws \Comodojo\Exception\CacheException |
||
185 | */ |
||
186 | 46 | public function get($name) { |
|
222 | |||
223 | /** |
||
224 | * Delete cache object (or entire namespace if $name is null) |
||
225 | * |
||
226 | * This method will throw only logical exceptions. |
||
227 | * In case of failures, it will return a boolean false. |
||
228 | * |
||
229 | * @param string $name Name for cache element |
||
230 | * |
||
231 | * @return bool |
||
232 | * @throws \Comodojo\Exception\CacheException |
||
233 | */ |
||
234 | 18 | public function delete($name=null) { |
|
269 | |||
270 | /** |
||
271 | * Clean cache objects in all namespaces |
||
272 | * |
||
273 | * This method will throw only logical exceptions. |
||
274 | * |
||
275 | * @return bool |
||
276 | * @throws \Comodojo\Exception\CacheException |
||
277 | */ |
||
278 | 18 | public function flush() { |
|
302 | |||
303 | /** |
||
304 | * Get cache status |
||
305 | * |
||
306 | * @return array |
||
307 | */ |
||
308 | 18 | public function status() { |
|
309 | |||
310 | 18 | View Code Duplication | if ( !$this->isEnabled() ) return array( |
311 | "provider" => "memcached", |
||
312 | "enabled" => false, |
||
313 | "objects" => null, |
||
314 | "options" => array() |
||
315 | ); |
||
316 | |||
317 | 18 | $stats = $this->instance->getStats(); |
|
318 | |||
319 | 18 | $objects = 0; |
|
320 | |||
321 | 18 | foreach ($stats as $key => $value) { |
|
322 | |||
323 | 18 | $objects = max($objects, $value['curr_items']); |
|
324 | |||
325 | 18 | } |
|
326 | |||
327 | return array( |
||
328 | 18 | "provider" => "memcached", |
|
329 | 18 | "enabled" => $this->isEnabled(), |
|
330 | 18 | "objects" => intval($objects), |
|
331 | "options" => $stats |
||
332 | 18 | ); |
|
333 | |||
334 | } |
||
335 | |||
336 | /** |
||
337 | * Get the current memcached instance |
||
338 | * |
||
339 | * @return \Memcached |
||
340 | */ |
||
341 | final public function getInstance() { |
||
346 | |||
347 | /** |
||
348 | * Set key for namespace |
||
349 | * |
||
350 | * @return mixed |
||
351 | */ |
||
352 | 36 | View Code Duplication | private function setNamespaceKey() { |
361 | |||
362 | /** |
||
363 | * Get key for namespace |
||
364 | * |
||
365 | * @return string |
||
366 | */ |
||
367 | 119 | private function getNamespaceKey() { |
|
372 | |||
373 | /** |
||
374 | * Add a Memcached server to stack |
||
375 | * |
||
376 | * @param string $server Server address (or IP) |
||
377 | * @param string $port Server port |
||
378 | * @param string $weight Server weight |
||
379 | */ |
||
380 | 186 | private function addServer($server, $port, $weight) { |
|
406 | |||
407 | /** |
||
408 | * Check Memcached availability |
||
409 | * |
||
410 | * @return bool |
||
411 | */ |
||
412 | 186 | private static function getMemcachedStatus() { |
|
417 | |||
418 | } |
||
419 |
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.