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 |
||
9 | class Predis implements RedisInterface |
||
10 | { |
||
11 | protected $predis; |
||
12 | protected $maxRetries; |
||
13 | |||
14 | 1 | public function __construct(Client $predis, $maxRetries = 5) |
|
19 | |||
20 | 5 | public function zCount($key, $min, $max) |
|
24 | |||
25 | 19 | public function zAdd($zkey, $score, $value) |
|
29 | |||
30 | 19 | View Code Duplication | public function set($key, $value) |
40 | |||
41 | 13 | public function get($key) |
|
42 | { |
||
43 | 13 | $this->predis->multi(); |
|
44 | 13 | $this->predis->exists($key); |
|
45 | 13 | $this->predis->get($key); |
|
46 | 13 | list($exists, $result) = $this->predis->exec(); |
|
47 | 13 | if (!$exists) { |
|
48 | 1 | return false; |
|
49 | } |
||
50 | |||
51 | 13 | return $result; |
|
52 | } |
||
53 | |||
54 | 1 | View Code Duplication | public function setEx($key, $seconds, $value) |
55 | { |
||
56 | 1 | $result = $this->predis->setex($key, $seconds, $value); |
|
57 | 1 | if ('OK' == $result->getPayload()) { |
|
58 | 1 | return true; |
|
59 | } |
||
60 | |||
61 | return false; |
||
62 | } |
||
63 | |||
64 | 15 | public function lRem($lKey, $count, $value) |
|
68 | |||
69 | 19 | public function lPush($lKey, array $values) |
|
73 | |||
74 | 3 | public function lRange($lKey, $start, $stop) |
|
78 | |||
79 | 15 | public function del(array $keys) |
|
83 | |||
84 | 5 | public function zRem($zkey, $value) |
|
88 | |||
89 | 15 | public function zPop($key) |
|
113 | |||
114 | 15 | public function zPopByMaxScore($key, $max) |
|
115 | { |
||
138 | } |
||
139 |
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.