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 |
||
16 | class RedisQueue implements QueueInterface |
||
17 | { |
||
18 | /** |
||
19 | * @var \Redis |
||
20 | */ |
||
21 | protected $redis; |
||
22 | |||
23 | /** |
||
24 | * @var string redis key of queue |
||
25 | */ |
||
26 | protected $channel; |
||
27 | |||
28 | /** |
||
29 | * @param string $host redis server host |
||
30 | * @param int $port redis server port |
||
31 | * @param int $database redis server database num |
||
32 | * @param string $channel redis queue key |
||
33 | * @param string $prefix prefix of redis queue key |
||
34 | */ |
||
35 | 6 | public function __construct( |
|
36 | $host = '127.0.0.1', |
||
37 | $port = 6379, |
||
38 | $database = 0, |
||
39 | $channel = 'cache', |
||
40 | $prefix = 'simple-fork-' |
||
41 | ) |
||
42 | { |
||
43 | 6 | $this->redis = new \Redis(); |
|
44 | 6 | $connection_result = $this->redis->connect($host, $port); |
|
45 | 6 | if (!$connection_result) { |
|
46 | throw new \RuntimeException('can not connect to the redis server'); |
||
47 | } |
||
48 | |||
49 | 6 | View Code Duplication | if ($database != 0) { |
|
|||
50 | $select_result = $this->redis->select($database); |
||
51 | if (!$select_result) { |
||
52 | throw new \RuntimeException('can not select the database'); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | 6 | if (empty($channel)) { |
|
57 | throw new \InvalidArgumentException('channel can not be empty'); |
||
58 | } |
||
59 | |||
60 | 6 | $this->channel = $channel; |
|
61 | |||
62 | 6 | if (empty($prefix)) return; |
|
63 | |||
64 | 6 | $set_option_result = $this->redis->setOption(\Redis::OPT_PREFIX, $prefix); |
|
65 | 6 | if (!$set_option_result) { |
|
66 | throw new \RuntimeException('can not set the \Redis::OPT_PREFIX Option'); |
||
67 | } |
||
68 | 6 | } |
|
69 | |||
70 | /** |
||
71 | * put value into the queue |
||
72 | * |
||
73 | * @param $value |
||
74 | * @return bool |
||
75 | */ |
||
76 | 3 | public function put($value) |
|
85 | |||
86 | /** |
||
87 | * get value from the queue |
||
88 | * |
||
89 | * @param bool $block if block when the queue is empty |
||
90 | * @return bool|string |
||
91 | */ |
||
92 | 6 | public function get($block = false) |
|
108 | |||
109 | /** |
||
110 | * get the size of the queue |
||
111 | * |
||
112 | * @return int |
||
113 | */ |
||
114 | 6 | public function size() |
|
118 | |||
119 | /** |
||
120 | * remove the queue resource |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | public function remove() |
||
128 | |||
129 | /** |
||
130 | * close the connection |
||
131 | */ |
||
132 | 3 | public function __destruct() |
|
136 | |||
137 | /** |
||
138 | * close the connection |
||
139 | */ |
||
140 | 6 | public function close() |
|
144 | } |
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.