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 ApiSetSortedTrait 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 ApiSetSortedTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait ApiSetSortedTrait |
||
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 zAdd($key, array $options = [], ...$scoreMembers) |
|
22 | { |
||
23 | $command = Enum::ZADD; |
||
24 | $args = array_merge([$key], $options, $scoreMembers); |
||
25 | |||
26 | return $this->dispatch(Builder::build($command, $args)); |
||
27 | } |
||
28 | |||
29 | /** |
||
30 | * @override |
||
31 | * @inheritDoc |
||
32 | */ |
||
33 | View Code Duplication | public function zCard($key) |
|
34 | { |
||
35 | $command = Enum::ZCARD; |
||
36 | $args = [$key]; |
||
37 | |||
38 | return $this->dispatch(Builder::build($command, $args)); |
||
39 | } |
||
40 | |||
41 | /** |
||
42 | * @override |
||
43 | * @inheritDoc |
||
44 | */ |
||
45 | View Code Duplication | public function zCount($key, $min, $max) |
|
46 | { |
||
47 | $command = Enum::ZCOUNT; |
||
48 | $args = [$key, $min, $max]; |
||
49 | |||
50 | return $this->dispatch(Builder::build($command, $args)); |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @override |
||
55 | * @inheritDoc |
||
56 | */ |
||
57 | public function zIncrBy($key, $increment, $member) |
||
58 | { |
||
59 | $command = Enum::ZINCRBY; |
||
60 | $args = [$key, $increment, $member]; |
||
61 | |||
62 | return $this->dispatch(Builder::build($command, $args)); |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @override |
||
67 | * @inheritDoc |
||
68 | */ |
||
69 | public function zInterStore($dst, $numKeys) |
||
76 | |||
77 | /** |
||
78 | * @override |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | View Code Duplication | public function zLexCount($key, $min, $max) |
|
82 | { |
||
83 | $command = Enum::ZLEXCOUNT; |
||
84 | $args = [$key, $min, $max]; |
||
85 | |||
86 | return $this->dispatch(Builder::build($command, $args)); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @override |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | public function zRange($key, $star = 0, $stop = -1, $withScores = false) |
||
94 | { |
||
95 | $command = Enum::ZRANGE; |
||
96 | $args = [$key, $star, $stop]; |
||
97 | if ($withScores) { |
||
98 | $args[] = 'WITHSCORES'; |
||
99 | return $this->dispatch(Builder::build($command, $args))->then(function ($value) { |
||
100 | $len = count($value); |
||
101 | $ret = []; |
||
102 | for ($i=0; $i<$len; $i+=2) { |
||
103 | $ret[$value[$i]] = $value[$i+1]; |
||
104 | } |
||
105 | return $ret; |
||
106 | }); |
||
107 | } |
||
108 | |||
109 | return $this->dispatch(Builder::build($command, $args)); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | public function zRangeByLex($key, $min, $max, array $options = []) |
||
117 | { |
||
118 | $command = Enum::ZRANGEBYLEX; |
||
119 | $args = [$key, $min, $max]; |
||
120 | $args = array_merge($args, $options); |
||
121 | |||
122 | return $this->dispatch(Builder::build($command, $args)); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * @override |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | public function zRevRangeByLex($key, $max, $min, array $options = []) |
||
130 | { |
||
131 | $command = Enum::ZREVRANGEBYLEX; |
||
132 | $args = [$key, $max,$min]; |
||
133 | $args = array_merge($args,$options); |
||
134 | |||
135 | return $this->dispatch(Builder::build($command, $args)); |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @override |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | View Code Duplication | public function zRangeByScore($key, $min, $max, $withScores = false, $offset = 0, $count = 0) |
|
143 | { |
||
144 | $command = Enum::ZRANGEBYSCORE; |
||
145 | $args = [$key, $min, $max]; |
||
146 | if ($withScores === true) { |
||
147 | $args[] = 'WITHSCORES'; |
||
148 | } |
||
149 | if ($offset != 0 || $count != 0) { |
||
150 | $args[] = 'LIMIT'; |
||
151 | $args[] = $offset; |
||
152 | $args[] = $count; |
||
153 | } |
||
154 | $promise = $this->dispatch(Builder::build($command, $args)); |
||
155 | |||
156 | return $withScores ? $promise->then(function ($value) { |
||
157 | $len = is_array($value) ? count($value) : 0; |
||
158 | if ($len > 0) { |
||
159 | $ret = []; |
||
160 | for ($i=0; $i<$len; $i+=2) { |
||
161 | $ret[$value[$i]] = $value[$i+1]; |
||
162 | } |
||
163 | |||
164 | return $ret; |
||
165 | } |
||
166 | |||
167 | return $value; |
||
168 | } ) : $promise; |
||
169 | } |
||
170 | |||
171 | /** |
||
172 | * @override |
||
173 | * @inheritDoc |
||
174 | */ |
||
175 | public function zRank($key, $member) |
||
176 | { |
||
177 | $command = Enum::ZRANK; |
||
178 | $args = [$key,$member]; |
||
179 | |||
180 | return $this->dispatch(Builder::build($command, $args)); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * @override |
||
185 | * @inheritDoc |
||
186 | */ |
||
187 | public function zRem($key, ...$members) |
||
188 | { |
||
189 | $command = Enum::ZREM; |
||
190 | $args = [$key]; |
||
191 | $args = array_merge($args, $members); |
||
192 | |||
193 | return $this->dispatch(Builder::build($command, $args)); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @override |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | public function zRemRangeByLex($key, $min, $max, array $options = []) |
||
201 | { |
||
202 | $command = Enum::ZREMRANGEBYLEX; |
||
203 | $args = [$key, $min, $max]; |
||
204 | $args = array_merge($args, $options); |
||
205 | |||
206 | return $this->dispatch(Builder::build($command, $args)); |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @override |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | public function zRemRangeByRank($key, $start, $stop) |
||
214 | { |
||
215 | $command = Enum::ZREMRANGEBYRANK; |
||
216 | $args = [$key, $start,$stop]; |
||
217 | |||
218 | return $this->dispatch(Builder::build($command, $args)); |
||
219 | } |
||
220 | |||
221 | /** |
||
222 | * @override |
||
223 | * @inheritDoc |
||
224 | */ |
||
225 | public function zRemRangeByScore($key, $min, $max, array $options = []) |
||
226 | { |
||
227 | $command = Enum::ZREMRANGEBYSCORE; |
||
228 | $args = [$key, $min, $max]; |
||
229 | $args = array_merge($args, $options); |
||
230 | |||
231 | return $this->dispatch(Builder::build($command, $args)); |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * @override |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | public function zRevRange($key, $start, $stop, $withScores = false) |
||
239 | { |
||
240 | $command = Enum::ZREVRANGE; |
||
241 | $args = [$key, $start, $stop]; |
||
242 | |||
243 | if ($withScores === true) { |
||
244 | $args[] = 'WITHSCORES'; |
||
245 | |||
246 | return $this->dispatch(Builder::build($command, $args)) |
||
247 | ->then(function ($value) { |
||
248 | $len = is_array($value) ? count($value) : 0; |
||
249 | if ($len > 0) { |
||
250 | $ret = []; |
||
251 | for ($i=0; $i<$len; $i+=2) { |
||
252 | $member = $value[$i]; |
||
253 | $score = $value[$i+1]; |
||
254 | $ret[$member] = $score; |
||
255 | } |
||
256 | |||
257 | return $ret; |
||
258 | } |
||
259 | |||
260 | return $value; |
||
261 | }); |
||
262 | } |
||
263 | |||
264 | return $promise = $this->dispatch(Builder::build($command, $args)); |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @override |
||
269 | * @inheritDoc |
||
270 | */ |
||
271 | View Code Duplication | public function zRevRangeByScore($key, $max, $min, $withScores = false, $offset = 0, $count = 0) |
|
272 | { |
||
273 | $command = Enum::ZREVRANGEBYSCORE; |
||
274 | $args = [$key, $max, $min]; |
||
275 | if ($withScores === true) { |
||
276 | $args[] = 'WITHSCORES'; |
||
277 | } |
||
278 | if ($offset != 0 || $count != 0) { |
||
279 | $args[] = 'LIMIT'; |
||
280 | $args[] = $offset; |
||
281 | $args[] = $count; |
||
282 | } |
||
283 | $promise = $this->dispatch(Builder::build($command, $args)); |
||
284 | |||
285 | return $withScores ? $promise->then(function ($value) { |
||
286 | $len = is_array($value) ? count($value) : 0; |
||
287 | if ($len > 0) { |
||
288 | $ret = []; |
||
289 | for ($i=0; $i<$len; $i+=2) { |
||
290 | $ret[$value[$i]] = $value[$i+1]; |
||
291 | } |
||
292 | |||
293 | return $ret; |
||
294 | } |
||
295 | |||
296 | return $value; |
||
297 | } ) : $promise; |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @override |
||
302 | * @inheritDoc |
||
303 | */ |
||
304 | public function zRevRank($key, $member) |
||
305 | { |
||
306 | $command = Enum::ZREVRANK; |
||
307 | $args = [$key,$member]; |
||
308 | |||
309 | return $this->dispatch(Builder::build($command, $args)); |
||
310 | } |
||
311 | |||
312 | /** |
||
313 | * @override |
||
314 | * @inheritDoc |
||
315 | */ |
||
316 | public function zScore($key, $member) |
||
323 | |||
324 | /** |
||
325 | * @override |
||
326 | * @inheritDoc |
||
327 | */ |
||
328 | public function zScan($key, $cursor, array $options = []) |
||
336 | |||
337 | /** |
||
338 | * @inheritDoc |
||
339 | */ |
||
340 | public function zUnionScore($dst, $numKeys) |
||
347 | } |
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.