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 | 3 | public function zCount($key, $min, $max) |
|
| 24 | |||
| 25 | 21 | public function zAdd($zkey, $score, $value) |
|
| 29 | |||
| 30 | 1 | public function hGetAll($key) |
|
| 34 | |||
| 35 | 7 | public function hIncrBy($key, $hashKey, $value) |
|
| 36 | { |
||
| 37 | 7 | return $this->predis->hIncrBy($key, $hashKey, $value); |
|
| 38 | } |
||
| 39 | |||
| 40 | 21 | View Code Duplication | public function set($key, $value) |
|
|
|||
| 41 | { |
||
| 42 | /** @var Status $result */ |
||
| 43 | 21 | $result = $this->predis->set($key, $value); |
|
| 44 | 21 | if ('OK' == $result->getPayload()) { |
|
| 45 | 21 | return true; |
|
| 46 | } |
||
| 47 | |||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | 15 | public function get($key) |
|
| 52 | { |
||
| 53 | 15 | $this->predis->multi(); |
|
| 54 | 15 | $this->predis->exists($key); |
|
| 55 | 15 | $this->predis->get($key); |
|
| 56 | 15 | list($exists, $result) = $this->predis->exec(); |
|
| 57 | 15 | if (!$exists) { |
|
| 58 | 1 | return false; |
|
| 59 | } |
||
| 60 | |||
| 61 | 15 | return $result; |
|
| 62 | } |
||
| 63 | |||
| 64 | 1 | View Code Duplication | public function setEx($key, $seconds, $value) |
| 65 | { |
||
| 66 | 1 | $result = $this->predis->setex($key, $seconds, $value); |
|
| 67 | 1 | if ('OK' == $result->getPayload()) { |
|
| 68 | 1 | return true; |
|
| 69 | } |
||
| 70 | |||
| 71 | return false; |
||
| 72 | } |
||
| 73 | |||
| 74 | 15 | public function lRem($lKey, $count, $value) |
|
| 78 | |||
| 79 | 21 | public function lPush($lKey, array $values) |
|
| 83 | |||
| 84 | 3 | public function lRange($lKey, $start, $stop) |
|
| 88 | |||
| 89 | 15 | public function del(array $keys) |
|
| 93 | |||
| 94 | 5 | public function zRem($zkey, $value) |
|
| 98 | |||
| 99 | 3 | protected function getOptions($pattern = '', $count = 0) |
|
| 100 | { |
||
| 101 | 3 | $options = []; |
|
| 102 | 3 | if ('' !== $pattern) { |
|
| 111 | |||
| 112 | 3 | View Code Duplication | public function zScan($key, &$cursor, $pattern = '', $count = 0) |
| 119 | |||
| 120 | 3 | public function mGet(array $keys) |
|
| 124 | |||
| 125 | 3 | protected function getResults(&$results, &$cursor) |
|
| 136 | |||
| 137 | 3 | protected function setCursor(&$cursor) |
|
| 143 | |||
| 144 | 3 | View Code Duplication | public function hScan($key, &$cursor, $pattern = '', $count = 0) |
| 151 | |||
| 152 | 15 | public function zPop($key) |
|
| 176 | |||
| 177 | 15 | public function zPopByMaxScore($key, $max) |
|
| 201 | } |
||
| 202 |
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.