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 ApiKeyValTrait 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 ApiKeyValTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait ApiKeyValTrait |
||
10 | { |
||
11 | /** |
||
12 | * @param Request $request |
||
13 | * @return mixed |
||
14 | */ |
||
15 | abstract function dispatch(Request $request); |
||
16 | |||
17 | /** |
||
18 | * @override |
||
19 | * @inheritDoc |
||
20 | */ |
||
21 | 1 | View Code Duplication | public function append($key, $value) |
22 | { |
||
23 | 1 | $command = Enum::APPEND; |
|
24 | 1 | $args = [$key, $value]; |
|
25 | |||
26 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
27 | } |
||
28 | |||
29 | /** |
||
30 | * @override |
||
31 | * @inheritDoc |
||
32 | */ |
||
33 | 1 | View Code Duplication | public function bitCount($key, $start = 0, $end = -1) |
34 | { |
||
35 | 1 | $command = Enum::BITCOUNT; |
|
36 | 1 | $args = [$key, $start, $end]; |
|
37 | |||
38 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * @override |
||
43 | * @inheritDoc |
||
44 | */ |
||
45 | public function bitField($key, $subCommand = null, ...$param) |
||
78 | |||
79 | /** |
||
80 | * @override |
||
81 | * @inheritDoc |
||
82 | */ |
||
83 | 1 | View Code Duplication | public function bitOp($operation, $dstKey, $srcKey, ...$keys) |
91 | |||
92 | /** |
||
93 | * @override |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | 1 | View Code Duplication | public function bitPos($key, $bit, $start = 0, $end = -1) |
97 | { |
||
98 | 1 | $command = Enum::BITPOS; |
|
99 | 1 | $args = [$key, $bit, $start, $end]; |
|
100 | |||
101 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * @override |
||
106 | * @inheritDoc |
||
107 | */ |
||
108 | 1 | View Code Duplication | public function decr($key) |
115 | |||
116 | /** |
||
117 | * @override |
||
118 | * @inheritDoc |
||
119 | */ |
||
120 | 1 | public function decrBy($key, $decrement) |
|
127 | |||
128 | /** |
||
129 | * @override |
||
130 | * @inheritDoc |
||
131 | */ |
||
132 | 3 | View Code Duplication | public function get($key) |
139 | |||
140 | /** |
||
141 | * @override |
||
142 | * @inheritDoc |
||
143 | */ |
||
144 | 2 | public function getBit($key, $offset) |
|
151 | |||
152 | /** |
||
153 | * @override |
||
154 | * @inheritDoc |
||
155 | */ |
||
156 | 1 | public function getRange($key, $start, $end) |
|
163 | |||
164 | /** |
||
165 | * @override |
||
166 | * @inheritDoc |
||
167 | */ |
||
168 | 1 | public function getSet($key, $value) |
|
175 | |||
176 | /** |
||
177 | * @override |
||
178 | * @inheritDoc |
||
179 | */ |
||
180 | 1 | View Code Duplication | public function incr($key) |
187 | |||
188 | /** |
||
189 | * @override |
||
190 | * @inheritDoc |
||
191 | */ |
||
192 | 1 | public function incrBy($key, $increment) |
|
199 | |||
200 | /** |
||
201 | * @override |
||
202 | * @inheritDoc |
||
203 | */ |
||
204 | 1 | public function incrByFloat($key, $increment) |
|
211 | |||
212 | /** |
||
213 | * @override |
||
214 | * @inheritDoc |
||
215 | */ |
||
216 | 25 | public function set($key, $value, array $options = []) |
|
224 | |||
225 | /** |
||
226 | * @override |
||
227 | * @inheritDoc |
||
228 | */ |
||
229 | 2 | public function setBit($key, $offset, $value) |
|
236 | |||
237 | /** |
||
238 | * @override |
||
239 | * @inheritDoc |
||
240 | */ |
||
241 | 3 | public function setEx($key, $seconds, $value) |
|
248 | |||
249 | /** |
||
250 | * @override |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | 1 | public function setNx($key, $value) |
|
260 | |||
261 | /** |
||
262 | * @override |
||
263 | * @inheritDoc |
||
264 | */ |
||
265 | 1 | public function setRange($key, $offset, $value) |
|
272 | |||
273 | /** |
||
274 | * @override |
||
275 | * @inheritDoc |
||
276 | */ |
||
277 | 1 | public function pSetEx($key, $milliseconds, $value) |
|
284 | |||
285 | /** |
||
286 | * @override |
||
287 | * @inheritDoc |
||
288 | */ |
||
289 | 3 | View Code Duplication | public function mGet($key, ...$keys) |
297 | |||
298 | /** |
||
299 | * @override |
||
300 | * @inheritDoc |
||
301 | */ |
||
302 | 2 | View Code Duplication | public function mSet(array $kvMap) |
315 | |||
316 | /** |
||
317 | * @override |
||
318 | * @inheritDoc |
||
319 | */ |
||
320 | 1 | View Code Duplication | public function mSetNx($kvMap) |
333 | |||
334 | /** |
||
335 | * @override |
||
336 | * @inheritDoc |
||
337 | */ |
||
338 | 1 | View Code Duplication | public function strLen($key) |
345 | |||
346 | /** |
||
347 | * @override |
||
348 | * @inheritDoc |
||
349 | */ |
||
350 | 2 | View Code Duplication | public function del($key,...$keys) |
358 | |||
359 | /** |
||
360 | * @override |
||
361 | * @inheritDoc |
||
362 | */ |
||
363 | 2 | View Code Duplication | public function dump($key) |
371 | |||
372 | /** |
||
373 | * @override |
||
374 | * @inheritDoc |
||
375 | */ |
||
376 | 1 | View Code Duplication | public function exists($key, ...$keys) |
384 | |||
385 | /** |
||
386 | * @override |
||
387 | * @inheritDoc |
||
388 | */ |
||
389 | public function expire($key, $seconds) |
||
396 | |||
397 | /** |
||
398 | * @override |
||
399 | * @inheritDoc |
||
400 | */ |
||
401 | 1 | public function expireAt($key, $timestamp) |
|
408 | |||
409 | /** |
||
410 | * @override |
||
411 | * @inheritDoc |
||
412 | */ |
||
413 | 1 | View Code Duplication | public function persist($key) |
420 | |||
421 | /** |
||
422 | * @override |
||
423 | * @inheritDoc |
||
424 | */ |
||
425 | 1 | public function pExpire($key, $milliseconds) |
|
432 | |||
433 | /** |
||
434 | * @override |
||
435 | * @inheritDoc |
||
436 | */ |
||
437 | 1 | View Code Duplication | public function pExpireAt($key, $milTimestamp) |
444 | |||
445 | /** |
||
446 | * @override |
||
447 | * @inheritDoc |
||
448 | */ |
||
449 | 1 | View Code Duplication | public function touch($key, ...$keys) |
457 | |||
458 | /** |
||
459 | * @override |
||
460 | * @inheritDoc |
||
461 | */ |
||
462 | 2 | View Code Duplication | public function ttl($key) |
469 | |||
470 | /** |
||
471 | * @override |
||
472 | * @inheritDoc |
||
473 | */ |
||
474 | 1 | View Code Duplication | public function type($key) |
481 | |||
482 | /** |
||
483 | * @override |
||
484 | * @inheritDoc |
||
485 | */ |
||
486 | View Code Duplication | public function unLink($key, ...$keys) |
|
494 | |||
495 | /** |
||
496 | * @override |
||
497 | * @inheritDoc |
||
498 | */ |
||
499 | public function wait($numSlaves, $timeout) |
||
507 | |||
508 | /** |
||
509 | * @override |
||
510 | * @inheritDoc |
||
511 | */ |
||
512 | 1 | public function randomKey() |
|
518 | |||
519 | /** |
||
520 | * @override |
||
521 | * @inheritDoc |
||
522 | */ |
||
523 | 1 | public function rename($key, $newKey) |
|
530 | |||
531 | /** |
||
532 | * @override |
||
533 | * @inheritDoc |
||
534 | */ |
||
535 | 1 | public function renameNx($key, $newKey) |
|
542 | |||
543 | /** |
||
544 | * @override |
||
545 | * @inheritDoc |
||
546 | */ |
||
547 | 1 | public function restore($key, $ttl, $value) |
|
554 | |||
555 | /** |
||
556 | * @override |
||
557 | * @inheritDoc |
||
558 | */ |
||
559 | 3 | View Code Duplication | public function pTtl($key) |
566 | |||
567 | /** |
||
568 | * @override |
||
569 | * @inheritDoc |
||
570 | */ |
||
571 | public function move($key, $db) |
||
579 | |||
580 | /** |
||
581 | * @override |
||
582 | * @inheritDoc |
||
583 | */ |
||
584 | View Code Duplication | public function scan($cursor, array $options = []) |
|
592 | |||
593 | /** |
||
594 | * @override |
||
595 | * @inheritDoc |
||
596 | */ |
||
597 | View Code Duplication | public function sort($key, array $options = []) |
|
606 | } |
||
607 |
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.