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 | trait ApiListTrait |
||
10 | { |
||
11 | /** |
||
12 | * @param Request $request |
||
13 | * @return mixed |
||
14 | */ |
||
15 | abstract function dispatch(Request $request); |
||
16 | |||
17 | /** |
||
18 | * @override |
||
19 | * @inheritDoc |
||
20 | */ |
||
21 | View Code Duplication | public function blPop(array $keys, $timeout) |
|
43 | |||
44 | /** |
||
45 | * @override |
||
46 | * @inheritDoc |
||
47 | */ |
||
48 | View Code Duplication | public function brPop(array $keys, $timeout) |
|
70 | |||
71 | /** |
||
72 | * @override |
||
73 | * @inheritDoc |
||
74 | */ |
||
75 | public function brPopLPush($src, $dst, $timeout) |
||
83 | |||
84 | /** |
||
85 | * @override |
||
86 | * @inheritDoc |
||
87 | */ |
||
88 | public function lIndex($key, $index) |
||
96 | |||
97 | /** |
||
98 | * @override |
||
99 | * @inheritDoc |
||
100 | */ |
||
101 | public function lInsert($key, $action, $pivot, $value) |
||
109 | |||
110 | /** |
||
111 | * @override |
||
112 | * @inheritDoc |
||
113 | */ |
||
114 | View Code Duplication | public function lLen($key) |
|
122 | |||
123 | /** |
||
124 | * @override |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | View Code Duplication | public function lPop($key) |
|
135 | |||
136 | /** |
||
137 | * @override |
||
138 | * @inheritDoc |
||
139 | */ |
||
140 | public function lPush($key,...$values) |
||
147 | |||
148 | public function lPushX($key, $value) |
||
155 | |||
156 | /** |
||
157 | * @override |
||
158 | * @inheritDoc |
||
159 | */ |
||
160 | View Code Duplication | public function lRange($key, $start = 0, $stop = -1) |
|
161 | { |
||
162 | $command = Enum::LRANGE; |
||
163 | $args = [$key, $start, $stop]; |
||
164 | |||
165 | return $this->dispatch(Builder::build($command, $args)); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * @override |
||
170 | * @inheritDoc |
||
171 | */ |
||
172 | public function lRem($key, $count, $value) |
||
180 | |||
181 | /** |
||
182 | * @override |
||
183 | * @inheritDoc |
||
184 | */ |
||
185 | public function lSet($key, $index, $value) |
||
193 | |||
194 | /** |
||
195 | * @override |
||
196 | * @inheritDoc |
||
197 | */ |
||
198 | public function lTrim($key, $start, $stop) |
||
206 | |||
207 | /** |
||
208 | * @override |
||
209 | * @inheritDoc |
||
210 | */ |
||
211 | View Code Duplication | public function rPop($key) |
|
218 | |||
219 | /** |
||
220 | * @override |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | View Code Duplication | public function rPopLPush($src, $dst) |
|
231 | |||
232 | /** |
||
233 | * @override |
||
234 | * @inheritDoc |
||
235 | */ |
||
236 | public function rPush($key, ...$values) |
||
244 | |||
245 | /** |
||
246 | * @override |
||
247 | * @inheritDoc |
||
248 | */ |
||
249 | public function rPushX($key, $value) |
||
256 | } |
||
257 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.