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 Redis 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 Redis, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | class Redis extends Common |
||
22 | { |
||
23 | const DEFAULT_PORT = 6379; |
||
24 | const DEFAULT_TIMEOUT = 0.0; |
||
25 | const DEFAULT_WEIGHT = 1; |
||
26 | |||
27 | protected $connectionsOptions = array(); |
||
28 | protected $connections = array(); |
||
29 | |||
30 | protected $backendsWeights = array(); |
||
31 | protected $nbBackends = 0; |
||
32 | |||
33 | protected $hashring = array(); |
||
34 | protected $nbHashrings = 0; |
||
35 | |||
36 | protected $nativeExpires = false; |
||
37 | |||
38 | protected $replicas = 256; |
||
39 | protected $slicesCount = 0; |
||
40 | protected $slicesHalf = 0; |
||
41 | protected $slicesDiv = 0; |
||
42 | |||
43 | protected $localCache = array(); |
||
44 | protected $localCacheCount = 0; |
||
45 | protected $localCacheSize = 256; |
||
46 | |||
47 | protected $hashringIsInitialized = false; |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | * |
||
52 | * Additional options: |
||
53 | * "local_cache_size" => the size of local cache |
||
54 | * "native_expires" => use or not native expiration time |
||
55 | * "servers" => array with connections parameters |
||
56 | * array( |
||
57 | * array('host' => '127.0.0.1', 'port' => 6379, 'timeout' => 0.0, 'weight' => 2), |
||
58 | * array('host' => '127.0.0.1', 'port' => 6380, 'timeout' => 0.0, 'weight' => 1), |
||
59 | * array('host' => '127.0.0.1', 'port' => 6381, 'timeout' => 0.0, 'weight' => 1), |
||
60 | * ) |
||
61 | * |
||
62 | * @codeCoverageIgnore |
||
63 | * @param array $options |
||
64 | * @throws \Endeveit\Cache\Exception |
||
65 | */ |
||
66 | public function __construct(array $options = array()) |
||
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | * |
||
101 | * @param string $id |
||
102 | * @param integer $value |
||
103 | * @return integer |
||
104 | */ |
||
105 | public function increment($id, $value = 1) |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | * |
||
115 | * @param string $id |
||
116 | * @param integer $value |
||
117 | * @return integer |
||
118 | */ |
||
119 | public function decrement($id, $value = 1) |
||
125 | |||
126 | /** |
||
127 | * Adds new connection to connections pool. |
||
128 | * |
||
129 | * @param string $host |
||
130 | * @param integer $port |
||
131 | * @param float $timeout |
||
132 | * @param integer $weight |
||
133 | * @throws \Endeveit\Cache\Exception |
||
134 | */ |
||
135 | protected function addConnection($host, $port, $timeout, $weight) |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | * |
||
153 | * @param string $id |
||
154 | * @return mixed|false |
||
155 | */ |
||
156 | View Code Duplication | protected function doLoad($id) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | * |
||
174 | * @param array $identifiers |
||
175 | * @return array |
||
176 | */ |
||
177 | protected function doLoadMany(array $identifiers) |
||
216 | |||
217 | /** |
||
218 | * {@inheritdoc} |
||
219 | * |
||
220 | * @param string $id |
||
221 | * @return mixed|false |
||
222 | */ |
||
223 | protected function doLoadRaw($id) |
||
229 | |||
230 | /** |
||
231 | * {@inheritdoc} |
||
232 | * |
||
233 | * @param mixed $data |
||
234 | * @param string $id |
||
235 | * @param array $tags |
||
236 | * @return boolean |
||
237 | */ |
||
238 | protected function doSave($data, $id, array $tags = array()) |
||
255 | |||
256 | /** |
||
257 | * {@inheritdoc} |
||
258 | * |
||
259 | * @param mixed $data |
||
260 | * @param string $id |
||
261 | * @param integer|boolean $lifetime |
||
262 | * @return boolean |
||
263 | */ |
||
264 | protected function doSaveScalar($data, $id, $lifetime = false) |
||
275 | |||
276 | /** |
||
277 | * {@inheritdoc} |
||
278 | * |
||
279 | * @param array $tags |
||
280 | * @return boolean |
||
281 | */ |
||
282 | protected function doRemoveByTags(array $tags) |
||
300 | |||
301 | /** |
||
302 | * {@inheritdoc} |
||
303 | * |
||
304 | * @return boolean |
||
305 | */ |
||
306 | protected function doFlush() |
||
314 | |||
315 | /** |
||
316 | * Returns connection by key name. |
||
317 | * |
||
318 | * @param string $id |
||
319 | * @return \Redis |
||
320 | * @throws \RuntimeException |
||
321 | * @throws \Endeveit\Cache\Exception |
||
322 | */ |
||
323 | private function getConnection($id) |
||
402 | |||
403 | /** |
||
404 | * Returns \Redis object by key value. |
||
405 | * |
||
406 | * @param integer $key |
||
407 | * @return \Redis |
||
408 | */ |
||
409 | private function getRedisObject($key) |
||
422 | |||
423 | /** |
||
424 | * Initialization of hashring. |
||
425 | */ |
||
426 | private function initializeHashring() |
||
467 | |||
468 | /** |
||
469 | * Cleans up the local cache. |
||
470 | */ |
||
471 | private function cleanBackendsCache() |
||
476 | } |
||
477 |
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.