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 | public function bitCount($key, $start = 0, $end = 0) |
|
| 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) |
| 84 | { |
||
| 85 | 1 | $command = Enum::BITOP; |
|
| 86 | 1 | $args = [$operation, $dstKey, $srcKey]; |
|
| 87 | 1 | $args = array_merge($args, $keys); |
|
| 88 | |||
| 89 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 90 | } |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @override |
||
| 94 | * @inheritDoc |
||
| 95 | */ |
||
| 96 | public function bitPos($key, $bit, $start = 0, $end = 0) |
||
| 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 | 20 | public function getBit($key, $offset) |
|
| 151 | |||
| 152 | /** |
||
| 153 | * @override |
||
| 154 | * @inheritDoc |
||
| 155 | */ |
||
| 156 | 1 | public function getRange($key, $start, $end) |
|
| 164 | |||
| 165 | /** |
||
| 166 | * @override |
||
| 167 | * @inheritDoc |
||
| 168 | */ |
||
| 169 | 1 | public function getSet($key, $value) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @override |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | 1 | View Code Duplication | public function incr($key) |
| 188 | |||
| 189 | /** |
||
| 190 | * @override |
||
| 191 | * @inheritDoc |
||
| 192 | */ |
||
| 193 | 1 | public function incrBy($key, $increment) |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @override |
||
| 204 | * @inheritDoc |
||
| 205 | */ |
||
| 206 | 1 | public function incrByFloat($key, $increment) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @override |
||
| 217 | * @inheritDoc |
||
| 218 | */ |
||
| 219 | 14 | public function set($key, $value, array $options = []) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * @override |
||
| 230 | * @inheritDoc |
||
| 231 | */ |
||
| 232 | 21 | public function setBit($key, $offset, $value) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @override |
||
| 242 | * @inheritDoc |
||
| 243 | */ |
||
| 244 | 1 | public function setEx($key, $seconds, $value) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @override |
||
| 254 | * @inheritDoc |
||
| 255 | */ |
||
| 256 | public function setNx($key, $value) |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @override |
||
| 266 | * @inheritDoc |
||
| 267 | */ |
||
| 268 | 1 | public function setRange($key, $offset, $value) |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @override |
||
| 278 | * @inheritDoc |
||
| 279 | */ |
||
| 280 | 1 | public function pSetEx($key, $milliseconds, $value) |
|
| 287 | |||
| 288 | /** |
||
| 289 | * @override |
||
| 290 | * @inheritDoc |
||
| 291 | */ |
||
| 292 | 1 | View Code Duplication | public function mGet($key, ...$values) |
| 301 | |||
| 302 | /** |
||
| 303 | * @override |
||
| 304 | * @inheritDoc |
||
| 305 | */ |
||
| 306 | 2 | public function mSet(array $kvMap) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * @override |
||
| 317 | * @inheritDoc |
||
| 318 | */ |
||
| 319 | public function mSetNx($kvMap) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @override |
||
| 330 | * @inheritDoc |
||
| 331 | */ |
||
| 332 | View Code Duplication | public function strLen($key) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * @override |
||
| 343 | * @inheritDoc |
||
| 344 | */ |
||
| 345 | 1 | View Code Duplication | public function del($key,...$keys) |
| 353 | |||
| 354 | /** |
||
| 355 | * @override |
||
| 356 | * @inheritDoc |
||
| 357 | */ |
||
| 358 | View Code Duplication | public function dump($key) |
|
| 366 | |||
| 367 | /** |
||
| 368 | * @override |
||
| 369 | * @inheritDoc |
||
| 370 | */ |
||
| 371 | 1 | View Code Duplication | public function exists($key, ...$keys) |
| 380 | |||
| 381 | /** |
||
| 382 | * @override |
||
| 383 | * @inheritDoc |
||
| 384 | */ |
||
| 385 | public function expire($key, $seconds) |
||
| 393 | |||
| 394 | /** |
||
| 395 | * @override |
||
| 396 | * @inheritDoc |
||
| 397 | */ |
||
| 398 | public function expireAt($key, $timestamp) |
||
| 406 | |||
| 407 | /** |
||
| 408 | * @override |
||
| 409 | * @inheritDoc |
||
| 410 | */ |
||
| 411 | View Code Duplication | public function persist($key) |
|
| 418 | |||
| 419 | /** |
||
| 420 | * @override |
||
| 421 | * @inheritDoc |
||
| 422 | */ |
||
| 423 | public function pExpire($key, $milliseconds) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * @override |
||
| 434 | * @inheritDoc |
||
| 435 | */ |
||
| 436 | public function pExpireAt($key, $milliseconds) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * @override |
||
| 447 | * @inheritDoc |
||
| 448 | */ |
||
| 449 | View Code Duplication | public function touch($key, ...$keys) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @override |
||
| 460 | * @inheritDoc |
||
| 461 | */ |
||
| 462 | View Code Duplication | public function ttl($key) |
|
| 470 | |||
| 471 | /** |
||
| 472 | * @override |
||
| 473 | * @inheritDoc |
||
| 474 | */ |
||
| 475 | View Code Duplication | public function type($key) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @override |
||
| 485 | * @inheritDoc |
||
| 486 | */ |
||
| 487 | View Code Duplication | public function unLink($key, ...$keys) |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @override |
||
| 498 | * @inheritDoc |
||
| 499 | */ |
||
| 500 | public function wait($numSlaves, $timeout) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @override |
||
| 511 | * @inheritDoc |
||
| 512 | */ |
||
| 513 | public function randomKey() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * @override |
||
| 523 | * @inheritDoc |
||
| 524 | */ |
||
| 525 | public function rename($key, $newKey) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * @override |
||
| 535 | * @inheritDoc |
||
| 536 | */ |
||
| 537 | public function renameNx($key, $newKey) |
||
| 544 | |||
| 545 | /** |
||
| 546 | * @override |
||
| 547 | * @inheritDoc |
||
| 548 | */ |
||
| 549 | public function restore($key, $ttl, $value) |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @override |
||
| 560 | * @inheritDoc |
||
| 561 | */ |
||
| 562 | View Code Duplication | public function pTtl($key) |
|
| 569 | |||
| 570 | /** |
||
| 571 | * @override |
||
| 572 | * @inheritDoc |
||
| 573 | */ |
||
| 574 | public function move($key, $db) |
||
| 582 | |||
| 583 | /** |
||
| 584 | * @override |
||
| 585 | * @inheritDoc |
||
| 586 | */ |
||
| 587 | View Code Duplication | public function scan($cursor, array $options = []) |
|
| 595 | |||
| 596 | /** |
||
| 597 | * @override |
||
| 598 | * @inheritDoc |
||
| 599 | */ |
||
| 600 | View Code Duplication | public function sort($key, array $options = []) |
|
| 609 | } |
||
| 610 |
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.