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) |
||
76 | |||
77 | /** |
||
78 | * @override |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | 1 | View Code Duplication | public function zLexCount($key, $min, $max) |
88 | |||
89 | /** |
||
90 | * @override |
||
91 | * @inheritDoc |
||
92 | */ |
||
93 | 1 | public function zRange($key, $star = 0, $stop = -1, $withScores = false) |
|
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | 1 | public function zRangeByLex($key, $min, $max, array $options = []) |
|
124 | |||
125 | /** |
||
126 | * @override |
||
127 | * @inheritDoc |
||
128 | */ |
||
129 | 1 | public function zRevRangeByLex($key, $max, $min, array $options = []) |
|
137 | |||
138 | /** |
||
139 | * @override |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | 1 | View Code Duplication | public function zRangeByScore($key, $min, $max, $withScores = false, $offset = 0, $count = 0) |
170 | |||
171 | /** |
||
172 | * @override |
||
173 | * @inheritDoc |
||
174 | */ |
||
175 | 1 | public function zRank($key, $member) |
|
182 | |||
183 | /** |
||
184 | * @override |
||
185 | * @inheritDoc |
||
186 | */ |
||
187 | 1 | public function zRem($key, ...$members) |
|
195 | |||
196 | /** |
||
197 | * @override |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | 1 | public function zRemRangeByLex($key, $min, $max, array $options = []) |
|
208 | |||
209 | /** |
||
210 | * @override |
||
211 | * @inheritDoc |
||
212 | */ |
||
213 | 1 | public function zRemRangeByRank($key, $start, $stop) |
|
220 | |||
221 | /** |
||
222 | * @override |
||
223 | * @inheritDoc |
||
224 | */ |
||
225 | 1 | public function zRemRangeByScore($key, $min, $max, array $options = []) |
|
233 | |||
234 | /** |
||
235 | * @override |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | 1 | public function zRevRange($key, $start, $stop, $withScores = false) |
|
266 | |||
267 | /** |
||
268 | * @override |
||
269 | * @inheritDoc |
||
270 | */ |
||
271 | 1 | View Code Duplication | public function zRevRangeByScore($key, $max, $min, $withScores = false, $offset = 0, $count = 0) |
299 | |||
300 | /** |
||
301 | * @override |
||
302 | * @inheritDoc |
||
303 | */ |
||
304 | 1 | public function zRevRank($key, $member) |
|
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.