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 | View Code Duplication | public function append($key, $value) |
|
| 22 | { |
||
| 23 | $command = Enum::APPEND; |
||
| 24 | $args = [$key, $value]; |
||
| 25 | |||
| 26 | return $this->dispatch(Builder::build($command, $args)); |
||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @override |
||
| 31 | * @inheritDoc |
||
| 32 | */ |
||
| 33 | View Code Duplication | public function bitCount($key, $start = 0, $end = -1) |
|
| 34 | { |
||
| 35 | $command = Enum::BITCOUNT; |
||
| 36 | $args = [$key, $start, $end]; |
||
| 37 | |||
| 38 | return $this->dispatch(Builder::build($command, $args)); |
||
| 39 | } |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @override |
||
| 43 | * @inheritDoc |
||
| 44 | */ |
||
| 45 | public function bitField($key, $subCommand, ...$param) |
||
| 46 | { |
||
| 47 | $command = Enum::BITFIELD; |
||
| 48 | $subCommand = strtoupper($subCommand); |
||
| 49 | //TODO: control flow improvement |
||
| 50 | switch ($subCommand) { |
||
| 51 | View Code Duplication | case 'GET' : { |
|
| 52 | @list ($type, $offset) = $param; |
||
| 53 | $args = [$key, $subCommand, $type, $offset]; |
||
| 54 | break; |
||
| 55 | } |
||
| 56 | View Code Duplication | case 'SET' : { |
|
| 57 | @list ($type, $offset, $value) = $param; |
||
| 58 | $args = [$key, $subCommand, $type, $offset, $value]; |
||
| 59 | break; |
||
| 60 | } |
||
| 61 | case 'INCRBY' : { |
||
| 62 | @list ($type, $offset, $increment) = $param; |
||
| 63 | $args = [$key, $type, $offset, $increment]; |
||
| 64 | break; |
||
| 65 | } |
||
| 66 | case 'OVERFLOW' : { |
||
| 67 | @list ($behavior) = $param; |
||
| 68 | $args = [$key, $subCommand, $behavior]; |
||
| 69 | break; |
||
| 70 | } |
||
| 71 | default : { |
||
| 72 | $args = []; |
||
| 73 | break; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this->dispatch(Builder::build($command, $args)); |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @override |
||
| 82 | * @inheritDoc |
||
| 83 | */ |
||
| 84 | public function bitOp($operation, $dstKey, $srcKey, ...$keys) |
||
| 85 | { |
||
| 86 | $command = Enum::BITOP; |
||
| 87 | $args = [$operation, $dstKey, $srcKey]; |
||
| 88 | $args = array_merge($args, $keys); |
||
| 89 | |||
| 90 | return $this->dispatch(Builder::build($command, $args)); |
||
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @override |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function bitPos($key, $bit, $start = 0, $end = -1) |
|
| 98 | { |
||
| 99 | $command = Enum::BITPOS; |
||
| 100 | $args = [$key, $bit, $start, $end]; |
||
| 101 | |||
| 102 | return $this->dispatch(Builder::build($command, $args)); |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @override |
||
| 107 | * @inheritDoc |
||
| 108 | */ |
||
| 109 | View Code Duplication | public function decr($key) |
|
| 110 | { |
||
| 111 | $command = Enum::DECR; |
||
| 112 | $args = [$key]; |
||
| 113 | |||
| 114 | return $this->dispatch(Builder::build($command, $args)); |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @override |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | public function decrBy($key, $decrement) |
||
| 122 | { |
||
| 123 | $command = Enum::DECRBY; |
||
| 124 | $args = [$key, $decrement]; |
||
| 125 | |||
| 126 | return $this->dispatch(Builder::build($command, $args)); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @override |
||
| 131 | * @inheritDoc |
||
| 132 | */ |
||
| 133 | View Code Duplication | public function get($key) |
|
| 134 | { |
||
| 135 | $command = Enum::GET; |
||
| 136 | $args = [$key]; |
||
| 137 | |||
| 138 | return $this->dispatch(Builder::build($command, $args)); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @override |
||
| 143 | * @inheritDoc |
||
| 144 | */ |
||
| 145 | public function getBit($key, $offset) |
||
| 146 | { |
||
| 147 | $command = Enum::GETBIT; |
||
| 148 | $args = [$key, $offset]; |
||
| 149 | |||
| 150 | return $this->dispatch(Builder::build($command, $args)); |
||
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @override |
||
| 155 | * @inheritDoc |
||
| 156 | */ |
||
| 157 | public function getRange($key, $start, $end) |
||
| 158 | { |
||
| 159 | $command = Enum::GETRANGE; |
||
| 160 | $args = [$key, $start, $end]; |
||
| 161 | |||
| 162 | return $this->dispatch(Builder::build($command, $args)); |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @override |
||
| 167 | * @inheritDoc |
||
| 168 | */ |
||
| 169 | public function getSet($key, $value) |
||
| 170 | { |
||
| 171 | $command = Enum::GETSET; |
||
| 172 | $args = [$key, $value]; |
||
| 173 | |||
| 174 | return $this->dispatch(Builder::build($command, $args)); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @override |
||
| 179 | * @inheritDoc |
||
| 180 | */ |
||
| 181 | View Code Duplication | public function incr($key) |
|
| 182 | { |
||
| 183 | $command = Enum::INCR; |
||
| 184 | $args = [$key]; |
||
| 185 | |||
| 186 | return $this->dispatch(Builder::build($command, $args)); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @override |
||
| 191 | * @inheritDoc |
||
| 192 | */ |
||
| 193 | public function incrBy($key, $increment) |
||
| 194 | { |
||
| 195 | $command = Enum::INCRBY; |
||
| 196 | $args = [$key, $increment]; |
||
| 197 | |||
| 198 | return $this->dispatch(Builder::build($command, $args)); |
||
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @override |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | public function incrByFloat($key, $increment) |
||
| 206 | { |
||
| 207 | $command = Enum::INCRBYFLOAT; |
||
| 208 | $args = [$key, $increment]; |
||
| 209 | |||
| 210 | return $this->dispatch(Builder::build($command, $args)); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @override |
||
| 215 | * @inheritDoc |
||
| 216 | */ |
||
| 217 | public function set($key, $value, array $options = []) |
||
| 218 | { |
||
| 219 | $command = Enum::SET; |
||
| 220 | array_unshift($options, $key, $value); |
||
| 221 | $args = $options; |
||
| 222 | |||
| 223 | return $this->dispatch(Builder::build($command, $args)); |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @override |
||
| 228 | * @inheritDoc |
||
| 229 | */ |
||
| 230 | public function setBit($key, $offset, $value) |
||
| 231 | { |
||
| 232 | $command = Enum::SETBIT; |
||
| 233 | $args = [$key, $offset, $value]; |
||
| 234 | |||
| 235 | return $this->dispatch(Builder::build($command, $args)); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @override |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | public function setEx($key, $seconds, $value) |
||
| 243 | { |
||
| 244 | $command = Enum::SETEX; |
||
| 245 | $args = [$key, $seconds, $value]; |
||
| 246 | |||
| 247 | return $this->dispatch(Builder::build($command, $args)); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @override |
||
| 252 | * @inheritDoc |
||
| 253 | */ |
||
| 254 | public function setNx($key, $value) |
||
| 255 | { |
||
| 256 | $command = Enum::SETNX; |
||
| 257 | $args = [$key, $value]; |
||
| 258 | |||
| 259 | return $this->dispatch(Builder::build($command, $args)); |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @override |
||
| 264 | * @inheritDoc |
||
| 265 | */ |
||
| 266 | public function setRange($key, $offset, $value) |
||
| 267 | { |
||
| 268 | $command = Enum::SETRANGE; |
||
| 269 | $args = [$key, $offset, $value]; |
||
| 270 | |||
| 271 | return $this->dispatch(Builder::build($command, $args)); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @override |
||
| 276 | * @inheritDoc |
||
| 277 | */ |
||
| 278 | public function pSetEx($key, $milliseconds, $value) |
||
| 279 | { |
||
| 280 | $command = Enum::PSETEX; |
||
| 281 | $args = [$key, $milliseconds, $value]; |
||
| 282 | |||
| 283 | return $this->dispatch(Builder::build($command, $args)); |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @override |
||
| 288 | * @inheritDoc |
||
| 289 | */ |
||
| 290 | public function mGet($key, ...$keys) |
||
| 291 | { |
||
| 292 | $command = Enum::MGET; |
||
| 293 | $args = [$key]; |
||
| 294 | $args = array_merge($args, $keys); |
||
| 295 | |||
| 296 | return $this->dispatch(Builder::build($command, $args)); |
||
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @override |
||
| 301 | * @inheritDoc |
||
| 302 | */ |
||
| 303 | View Code Duplication | public function mSet(array $kvMap) |
|
| 304 | { |
||
| 305 | $command = Enum::MSET; |
||
| 306 | $args = []; |
||
| 307 | if (!empty($kvMap)) { |
||
| 308 | foreach ($kvMap as $key => $val) { |
||
| 309 | $args[] = $key; |
||
| 310 | $args[] = $val; |
||
| 311 | } |
||
| 312 | } |
||
| 313 | |||
| 314 | return $this->dispatch(Builder::build($command, $args)); |
||
| 315 | } |
||
| 316 | |||
| 317 | /** |
||
| 318 | * @override |
||
| 319 | * @inheritDoc |
||
| 320 | */ |
||
| 321 | View Code Duplication | public function mSetNx($kvMap) |
|
| 322 | { |
||
| 323 | $command = Enum::MSETNX; |
||
| 324 | $args = []; |
||
| 325 | if (!empty($kvMap)) { |
||
| 326 | foreach ($kvMap as $key => $val) { |
||
| 327 | $args[] = $key; |
||
| 328 | $args[] = $val; |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | return $this->dispatch(Builder::build($command, $args)); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * @override |
||
| 337 | * @inheritDoc |
||
| 338 | */ |
||
| 339 | View Code Duplication | public function strLen($key) |
|
| 340 | { |
||
| 341 | $command = Enum::STRLEN; |
||
| 342 | $args = [$key]; |
||
| 343 | |||
| 344 | return $this->dispatch(Builder::build($command, $args)); |
||
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @override |
||
| 349 | * @inheritDoc |
||
| 350 | */ |
||
| 351 | View Code Duplication | public function del($key,...$keys) |
|
| 352 | { |
||
| 353 | $command = Enum::DEL; |
||
| 354 | $keys[] = $key; |
||
| 355 | $args = $keys; |
||
| 356 | |||
| 357 | return $this->dispatch(Builder::build($command, $args)); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * @override |
||
| 362 | * @inheritDoc |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function dump($key) |
|
| 365 | { |
||
| 366 | // TODO: Implement dump() method. |
||
| 367 | $command = Enum::DUMP; |
||
| 368 | $args = [$key]; |
||
| 369 | |||
| 370 | return $this->dispatch(Builder::build($command, $args)); |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @override |
||
| 375 | * @inheritDoc |
||
| 376 | */ |
||
| 377 | public function exists($key, ...$keys) |
||
| 378 | { |
||
| 379 | $command = Enum::EXISTS; |
||
| 380 | $args = [$key]; |
||
| 381 | $args = array_merge($args, $keys); |
||
| 382 | |||
| 383 | return $this->dispatch(Builder::build($command, $args)); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @override |
||
| 388 | * @inheritDoc |
||
| 389 | */ |
||
| 390 | public function expire($key, $seconds) |
||
| 391 | { |
||
| 392 | $command = Enum::EXPIRE; |
||
| 393 | $args = [$key, $seconds]; |
||
| 394 | |||
| 395 | return $this->dispatch(Builder::build($command, $args)); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * @override |
||
| 400 | * @inheritDoc |
||
| 401 | */ |
||
| 402 | public function expireAt($key, $timestamp) |
||
| 403 | { |
||
| 404 | $command = Enum::EXPIREAT; |
||
| 405 | $args = [$key, $timestamp]; |
||
| 406 | |||
| 407 | return $this->dispatch(Builder::build($command, $args)); |
||
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @override |
||
| 412 | * @inheritDoc |
||
| 413 | */ |
||
| 414 | View Code Duplication | public function persist($key) |
|
| 415 | { |
||
| 416 | $command = Enum::PERSIST; |
||
| 417 | $args = [$key]; |
||
| 418 | |||
| 419 | return $this->dispatch(Builder::build($command, $args)); |
||
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @override |
||
| 424 | * @inheritDoc |
||
| 425 | */ |
||
| 426 | public function pExpire($key, $milliseconds) |
||
| 427 | { |
||
| 428 | $command = Enum::PEXPIRE; |
||
| 429 | $args = [$key, $milliseconds]; |
||
| 430 | |||
| 431 | return $this->dispatch(Builder::build($command, $args)); |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @override |
||
| 436 | * @inheritDoc |
||
| 437 | */ |
||
| 438 | View Code Duplication | public function pExpireAt($key, $milTimestamp) |
|
| 439 | { |
||
| 440 | $command = Enum::PEXPIREAT; |
||
| 441 | $args = [$key, $milTimestamp]; |
||
| 442 | |||
| 443 | return $this->dispatch(Builder::build($command, $args)); |
||
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @override |
||
| 448 | * @inheritDoc |
||
| 449 | */ |
||
| 450 | public function touch($key, ...$keys) |
||
| 451 | { |
||
| 452 | $command = Enum::TOUCH; |
||
| 453 | $args = [$key]; |
||
| 454 | $args = array_merge($args, $keys); |
||
| 455 | |||
| 456 | return $this->dispatch(Builder::build($command, $args)); |
||
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @override |
||
| 461 | * @inheritDoc |
||
| 462 | */ |
||
| 463 | View Code Duplication | public function ttl($key) |
|
| 464 | { |
||
| 465 | $command = Enum::TTL; |
||
| 466 | $args = [$key]; |
||
| 467 | |||
| 468 | return $this->dispatch(Builder::build($command, $args)); |
||
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @override |
||
| 473 | * @inheritDoc |
||
| 474 | */ |
||
| 475 | View Code Duplication | public function type($key) |
|
| 482 | |||
| 483 | /** |
||
| 484 | * @override |
||
| 485 | * @inheritDoc |
||
| 486 | */ |
||
| 487 | public function unLink($key, ...$keys) |
||
| 488 | { |
||
| 489 | $command = Enum::UNLINK; |
||
| 490 | $args = [$key]; |
||
| 491 | $args = array_merge($args, $keys); |
||
| 492 | |||
| 493 | return $this->dispatch(Builder::build($command, $args)); |
||
| 494 | } |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @override |
||
| 498 | * @inheritDoc |
||
| 499 | */ |
||
| 500 | public function wait($numSlaves, $timeout) |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @override |
||
| 511 | * @inheritDoc |
||
| 512 | */ |
||
| 513 | public function randomKey() |
||
| 514 | { |
||
| 515 | $command = Enum::RANDOMKEY; |
||
| 516 | |||
| 517 | return $this->dispatch(Builder::build($command)); |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @override |
||
| 522 | * @inheritDoc |
||
| 523 | */ |
||
| 524 | public function rename($key, $newKey) |
||
| 525 | { |
||
| 526 | $command = Enum::RENAME; |
||
| 527 | $args = [$key, $newKey]; |
||
| 528 | |||
| 529 | return $this->dispatch(Builder::build($command, $args)); |
||
| 530 | } |
||
| 531 | |||
| 532 | /** |
||
| 533 | * @override |
||
| 534 | * @inheritDoc |
||
| 535 | */ |
||
| 536 | public function renameNx($key, $newKey) |
||
| 537 | { |
||
| 538 | $command = Enum::RENAMENX; |
||
| 539 | $args = [$key, $newKey]; |
||
| 540 | |||
| 541 | return $this->dispatch(Builder::build($command, $args)); |
||
| 542 | } |
||
| 543 | |||
| 544 | /** |
||
| 545 | * @override |
||
| 546 | * @inheritDoc |
||
| 547 | */ |
||
| 548 | public function restore($key, $ttl, $value) |
||
| 549 | { |
||
| 550 | $command = Enum::RESTORE; |
||
| 551 | $args = [$key, $ttl, $value]; |
||
| 552 | |||
| 553 | return $this->dispatch(Builder::build($command, $args)); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * @override |
||
| 558 | * @inheritDoc |
||
| 559 | */ |
||
| 560 | View Code Duplication | public function pTtl($key) |
|
| 561 | { |
||
| 562 | $command = Enum::PTTL; |
||
| 563 | $args = [$key]; |
||
| 564 | |||
| 565 | return $this->dispatch(Builder::build($command, $args)); |
||
| 566 | } |
||
| 567 | |||
| 568 | /** |
||
| 569 | * @override |
||
| 570 | * @inheritDoc |
||
| 571 | */ |
||
| 572 | public function move($key, $db) |
||
| 580 | |||
| 581 | /** |
||
| 582 | * @override |
||
| 583 | * @inheritDoc |
||
| 584 | */ |
||
| 585 | public function scan($cursor, array $options = []) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * @override |
||
| 596 | * @inheritDoc |
||
| 597 | */ |
||
| 598 | public function sort($key, array $options = []) |
||
| 607 | } |
||
| 608 |
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.