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 | 16 | View Code Duplication | public function zAdd($key, array $options = [], ...$scoreMembers) |
28 | |||
29 | /** |
||
30 | * @override |
||
31 | * @inheritDoc |
||
32 | */ |
||
33 | 1 | View Code Duplication | public function zCard($key) |
40 | |||
41 | /** |
||
42 | * @override |
||
43 | * @inheritDoc |
||
44 | */ |
||
45 | 1 | View Code Duplication | public function zCount($key, $min, $max) |
52 | |||
53 | /** |
||
54 | * @override |
||
55 | * @inheritDoc |
||
56 | */ |
||
57 | 1 | public function zIncrBy($key, $increment, $member) |
|
64 | |||
65 | /** |
||
66 | * @override |
||
67 | * @inheritDoc |
||
68 | */ |
||
69 | public function zInterStore($dst, $numKeys) |
||
77 | |||
78 | /** |
||
79 | * @override |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | 1 | View Code Duplication | public function zLexCount($key, $min, $max) |
89 | |||
90 | /** |
||
91 | * @override |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | 1 | public function zRange($key, $star = 0, $stop = -1, $withScores = false) |
|
112 | |||
113 | /** |
||
114 | * @override |
||
115 | * @inheritDoc |
||
116 | */ |
||
117 | 1 | public function zRangeByLex($key, $min, $max, array $options = []) |
|
125 | |||
126 | /** |
||
127 | * @override |
||
128 | * @inheritDoc |
||
129 | */ |
||
130 | 1 | public function zRevRangeByLex($key, $max, $min, array $options = []) |
|
138 | |||
139 | /** |
||
140 | * @override |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | 1 | View Code Duplication | public function zRangeByScore($key, $min, $max, $withScores = false, $offset = 0, $count = 0) |
171 | |||
172 | /** |
||
173 | * @override |
||
174 | * @inheritDoc |
||
175 | */ |
||
176 | 1 | public function zRank($key, $member) |
|
183 | |||
184 | /** |
||
185 | * @override |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | 1 | public function zRem($key, ...$members) |
|
196 | |||
197 | /** |
||
198 | * @override |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | 1 | public function zRemRangeByLex($key, $min, $max, array $options = []) |
|
209 | |||
210 | /** |
||
211 | * @override |
||
212 | * @inheritDoc |
||
213 | */ |
||
214 | 1 | public function zRemRangeByRank($key, $start, $stop) |
|
221 | |||
222 | /** |
||
223 | * @override |
||
224 | * @inheritDoc |
||
225 | */ |
||
226 | 1 | public function zRemRangeByScore($key, $min, $max, array $options = []) |
|
234 | |||
235 | /** |
||
236 | * @override |
||
237 | * @inheritDoc |
||
238 | */ |
||
239 | 1 | public function zRevRange($key, $start, $stop, $withScores = false) |
|
267 | |||
268 | /** |
||
269 | * @override |
||
270 | * @inheritDoc |
||
271 | */ |
||
272 | 1 | View Code Duplication | public function zRevRangeByScore($key, $max, $min, $withScores = false, $offset = 0, $count = 0) |
300 | |||
301 | /** |
||
302 | * @override |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | 1 | public function zRevRank($key, $member) |
|
312 | |||
313 | /** |
||
314 | * @override |
||
315 | * @inheritDoc |
||
316 | */ |
||
317 | public function zScore($key, $member) |
||
324 | |||
325 | /** |
||
326 | * @override |
||
327 | * @inheritDoc |
||
328 | */ |
||
329 | public function zScan($key, $cursor, array $options = []) |
||
338 | |||
339 | /** |
||
340 | * @inheritDoc |
||
341 | */ |
||
342 | public function zUnionScore($dst, $numKeys) |
||
350 | } |
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.