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 | public function append($key, $value) |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @override |
||
| 31 | * @inheritDoc |
||
| 32 | */ |
||
| 33 | public function bitCount($key, $start = 0, $end = 0) |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @override |
||
| 43 | * @inheritDoc |
||
| 44 | */ |
||
| 45 | public function bitField($key, $subCommand = null, ...$param) |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @override |
||
| 81 | * @inheritDoc |
||
| 82 | */ |
||
| 83 | public function bitOp($operation, $dstKey, $srcKey, ...$keys) |
||
| 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 | View Code Duplication | public function decr($key) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @override |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | public function decrBy($key, $decrement) |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @override |
||
| 132 | * @inheritDoc |
||
| 133 | */ |
||
| 134 | 1 | View Code Duplication | public function get($key) |
| 141 | |||
| 142 | /** |
||
| 143 | * @override |
||
| 144 | * @inheritDoc |
||
| 145 | */ |
||
| 146 | public function getBit($key, $offset) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @override |
||
| 157 | * @inheritDoc |
||
| 158 | */ |
||
| 159 | public function getRange($key, $start, $end) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @override |
||
| 170 | * @inheritDoc |
||
| 171 | */ |
||
| 172 | public function getSet($key, $value) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @override |
||
| 183 | * @inheritDoc |
||
| 184 | */ |
||
| 185 | View Code Duplication | public function incr($key) |
|
| 192 | |||
| 193 | /** |
||
| 194 | * @override |
||
| 195 | * @inheritDoc |
||
| 196 | */ |
||
| 197 | public function incrBy($key, $increment) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @override |
||
| 208 | * @inheritDoc |
||
| 209 | */ |
||
| 210 | public function incrByFloat($key, $increment) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * @override |
||
| 221 | * @inheritDoc |
||
| 222 | */ |
||
| 223 | 1 | public function set($key, $value, array $options = []) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * @override |
||
| 234 | * @inheritDoc |
||
| 235 | */ |
||
| 236 | public function setBit($key, $offset, $value) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @override |
||
| 247 | * @inheritDoc |
||
| 248 | */ |
||
| 249 | public function setEx($key, $seconds, $value) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @override |
||
| 259 | * @inheritDoc |
||
| 260 | */ |
||
| 261 | public function setNx($key, $value) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @override |
||
| 271 | * @inheritDoc |
||
| 272 | */ |
||
| 273 | public function setRange($key, $offset, $value) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @override |
||
| 283 | * @inheritDoc |
||
| 284 | */ |
||
| 285 | public function pSetEx($key, $milliseconds, $value) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * @override |
||
| 295 | * @inheritDoc |
||
| 296 | */ |
||
| 297 | public function mGet($key, ...$values) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @override |
||
| 309 | * @inheritDoc |
||
| 310 | */ |
||
| 311 | public function mSet(array $kvMap) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @override |
||
| 322 | * @inheritDoc |
||
| 323 | */ |
||
| 324 | public function mSetNx($kvMap) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @override |
||
| 335 | * @inheritDoc |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function strLen($key) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @override |
||
| 348 | * @inheritDoc |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function del($key,...$keys) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @override |
||
| 361 | * @inheritDoc |
||
| 362 | */ |
||
| 363 | View Code Duplication | public function dump($key) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @override |
||
| 374 | * @inheritDoc |
||
| 375 | */ |
||
| 376 | public function exists($key, ...$keys) |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @override |
||
| 388 | * @inheritDoc |
||
| 389 | */ |
||
| 390 | public function expire($key, $seconds) |
||
| 398 | |||
| 399 | /** |
||
| 400 | * @override |
||
| 401 | * @inheritDoc |
||
| 402 | */ |
||
| 403 | public function expireAt($key, $timestamp) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * @override |
||
| 414 | * @inheritDoc |
||
| 415 | */ |
||
| 416 | View Code Duplication | public function persist($key) |
|
| 423 | |||
| 424 | /** |
||
| 425 | * @override |
||
| 426 | * @inheritDoc |
||
| 427 | */ |
||
| 428 | public function pExpire($key, $milliseconds) |
||
| 436 | |||
| 437 | /** |
||
| 438 | * @override |
||
| 439 | * @inheritDoc |
||
| 440 | */ |
||
| 441 | public function pExpireAt($key, $milliseconds) |
||
| 449 | |||
| 450 | /** |
||
| 451 | * @override |
||
| 452 | * @inheritDoc |
||
| 453 | */ |
||
| 454 | public function touch($key, ...$keys) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @override |
||
| 465 | * @inheritDoc |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function ttl($key) |
|
| 475 | |||
| 476 | /** |
||
| 477 | * @override |
||
| 478 | * @inheritDoc |
||
| 479 | */ |
||
| 480 | View Code Duplication | public function type($key) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * @override |
||
| 490 | * @inheritDoc |
||
| 491 | */ |
||
| 492 | public function unLink($key, ...$keys) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @override |
||
| 503 | * @inheritDoc |
||
| 504 | */ |
||
| 505 | public function wait($numSlaves, $timeout) |
||
| 513 | |||
| 514 | /** |
||
| 515 | * @override |
||
| 516 | * @inheritDoc |
||
| 517 | */ |
||
| 518 | public function randomKey() |
||
| 525 | |||
| 526 | /** |
||
| 527 | * @override |
||
| 528 | * @inheritDoc |
||
| 529 | */ |
||
| 530 | public function rename($key, $newKey) |
||
| 537 | |||
| 538 | /** |
||
| 539 | * @override |
||
| 540 | * @inheritDoc |
||
| 541 | */ |
||
| 542 | public function renameNx($key, $newKey) |
||
| 549 | |||
| 550 | /** |
||
| 551 | * @override |
||
| 552 | * @inheritDoc |
||
| 553 | */ |
||
| 554 | public function restore($key, $ttl, $value) |
||
| 562 | |||
| 563 | /** |
||
| 564 | * @override |
||
| 565 | * @inheritDoc |
||
| 566 | */ |
||
| 567 | View Code Duplication | public function pTtl($key) |
|
| 574 | |||
| 575 | /** |
||
| 576 | * @override |
||
| 577 | * @inheritDoc |
||
| 578 | */ |
||
| 579 | public function move($key, $db) |
||
| 587 | |||
| 588 | /** |
||
| 589 | * @override |
||
| 590 | * @inheritDoc |
||
| 591 | */ |
||
| 592 | public function scan($cursor, array $options = []) |
||
| 600 | |||
| 601 | /** |
||
| 602 | * @override |
||
| 603 | * @inheritDoc |
||
| 604 | */ |
||
| 605 | public function sort($key, array $options = []) |
||
| 614 | } |
||
| 615 |
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.