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 RedisProxy 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 RedisProxy, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class RedisProxy |
||
24 | { |
||
25 | const DRIVER_REDIS = 'redis'; |
||
26 | |||
27 | const DRIVER_PREDIS = 'predis'; |
||
28 | |||
29 | private $driver; |
||
30 | |||
31 | private $host; |
||
32 | |||
33 | private $port; |
||
34 | |||
35 | private $database = 0; |
||
36 | |||
37 | private $timeout; |
||
38 | |||
39 | private $supportedDrivers = [ |
||
40 | self::DRIVER_REDIS, |
||
41 | self::DRIVER_PREDIS, |
||
42 | ]; |
||
43 | |||
44 | private $driversOrder = []; |
||
45 | |||
46 | 112 | public function __construct($host, $port, $timeout = null) |
|
53 | |||
54 | 112 | public function setDriversOrder(array $driversOrder) |
|
67 | |||
68 | 108 | private function init() |
|
73 | |||
74 | 108 | private function prepareDriver() |
|
92 | |||
93 | 108 | private function connect($host, $port, $timeout = null) |
|
97 | |||
98 | 108 | private function isConnected() |
|
102 | |||
103 | 108 | public function __call($name, $arguments) |
|
114 | |||
115 | /** |
||
116 | * @param integer $database |
||
117 | * @return boolean true on success |
||
118 | * @throws RedisProxyException on failure |
||
119 | */ |
||
120 | 108 | public function select($database) |
|
141 | |||
142 | /** |
||
143 | * @param string|null $section |
||
144 | * @return array |
||
145 | */ |
||
146 | 8 | public function info($section = null) |
|
162 | |||
163 | /** |
||
164 | * Set multiple values to multiple keys |
||
165 | * @param array $dictionary |
||
166 | * @return boolean true on success |
||
167 | * @throws RedisProxyException if number of arguments is wrong |
||
168 | */ |
||
169 | 12 | View Code Duplication | public function mset(...$dictionary) |
179 | |||
180 | /** |
||
181 | * @param string $key |
||
182 | * @return string|null null if hash field is not set |
||
183 | */ |
||
184 | 16 | public function get($key) |
|
190 | |||
191 | /** |
||
192 | * Delete a key(s) |
||
193 | * @param array ...$keys |
||
194 | * @return integer number of deleted keys |
||
195 | */ |
||
196 | 20 | public function del(...$keys) |
|
201 | |||
202 | /** |
||
203 | * Delete a key(s) |
||
204 | * @param array ...$keys |
||
205 | * @return integer number of deleted keys |
||
206 | */ |
||
207 | 8 | public function delete(...$keys) |
|
211 | |||
212 | /** |
||
213 | * Incrementally iterate the keys space |
||
214 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
215 | * @param string $pattern pattern for keys, use * as wild card |
||
216 | * @param integer $count |
||
217 | * @return array|null list of found keys, returns null if $iterator is 0 or '0' |
||
218 | */ |
||
219 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
233 | |||
234 | /** |
||
235 | * Get the value of a hash field |
||
236 | * @param string $key |
||
237 | * @param string $field |
||
238 | * @return string|null null if hash field is not set |
||
239 | */ |
||
240 | 16 | public function hget($key, $field) |
|
246 | |||
247 | /** |
||
248 | * Delete one or more hash fields, returns number of deleted fields |
||
249 | * @param array $key |
||
250 | * @param array ...$fields |
||
251 | * @return integer |
||
252 | */ |
||
253 | 8 | public function hdel($key, ...$fields) |
|
260 | |||
261 | /** |
||
262 | * Increment the integer value of hash field by given number |
||
263 | * @param string $key |
||
264 | * @param string $field |
||
265 | * @param integer $increment |
||
266 | * @return integer |
||
267 | */ |
||
268 | 4 | public function hincrby($key, $field, $increment = 1) |
|
272 | |||
273 | /** |
||
274 | * Increment the float value of hash field by given amount |
||
275 | * @param string $key |
||
276 | * @param string $field |
||
277 | * @param float $increment |
||
278 | * @return float |
||
279 | */ |
||
280 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
284 | |||
285 | /** |
||
286 | * Set multiple values to multiple hash fields |
||
287 | * @param string $key |
||
288 | * @param array $dictionary |
||
289 | * @return boolean true on success |
||
290 | * @throws RedisProxyException if number of arguments is wrong |
||
291 | */ |
||
292 | 12 | View Code Duplication | public function hmset($key, ...$dictionary) |
302 | |||
303 | /** |
||
304 | * Incrementally iterate hash fields and associated values |
||
305 | * @param string $key |
||
306 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
307 | * @param string $pattern pattern for fields, use * as wild card |
||
308 | * @param integer $count |
||
309 | * @return array|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
310 | */ |
||
311 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
325 | |||
326 | /** |
||
327 | * Add one or more members to a set |
||
328 | * @param string $key |
||
329 | * @param array ...$members |
||
330 | * @return integer number of new members added to set |
||
331 | */ |
||
332 | 12 | public function sadd($key, ...$members) |
|
339 | |||
340 | /** |
||
341 | * Remove and return one or multiple random members from a set |
||
342 | * @param string $key |
||
343 | * @param integer $count number of members |
||
344 | * @return mixed string if $count is null or 1 and $key exists, array if $count > 1 and $key exists, null if $key doesn't exist |
||
345 | */ |
||
346 | 4 | public function spop($key, $count = 1) |
|
363 | |||
364 | /** |
||
365 | * Incrementally iterate Set elements |
||
366 | * @param string $key |
||
367 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
368 | * @param string $pattern pattern for member's values, use * as wild card |
||
369 | * @param integer $count |
||
370 | * @return array|null list of found members, returns null if $iterator is 0 or '0' |
||
371 | */ |
||
372 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
386 | |||
387 | /** |
||
388 | * Incrementally iterate sorted sets elements and associated scores |
||
389 | * @param string $key |
||
390 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
391 | * @param string $pattern pattern for element's values, use * as wild card |
||
392 | * @param integer $count |
||
393 | * @return array|null list of found elements, returns null if $iterator is 0 or '0' |
||
394 | */ |
||
395 | View Code Duplication | public function zscan($key, &$iterator, $pattern = null, $count = null) |
|
409 | |||
410 | 36 | private function convertFalseToNull($result) |
|
414 | |||
415 | 108 | private function transformResult($result) |
|
422 | |||
423 | /** |
||
424 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
425 | * @param array $dictionary |
||
426 | * @param string $command |
||
427 | * @return array |
||
428 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
429 | */ |
||
430 | 16 | private function prepareKeyValue(array $dictionary, $command) |
|
444 | } |
||
445 |
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.