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) |
| 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 | 1 | public function bitField($key, $subCommand, ...$param) |
|
| 46 | { |
||
| 47 | 1 | $command = Enum::BITFIELD; |
|
| 48 | 1 | $subCommand = strtoupper($subCommand); |
|
| 49 | //TODO: control flow improvement |
||
| 50 | switch ($subCommand) { |
||
| 51 | 1 | View Code Duplication | case 'GET' : { |
| 52 | 1 | @list ($type, $offset) = $param; |
|
| 53 | 1 | $args = [$key, $subCommand, $type, $offset]; |
|
| 54 | 1 | 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 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @override |
||
| 82 | * @inheritDoc |
||
| 83 | */ |
||
| 84 | 1 | public function bitOp($operation, $dstKey, $srcKey, ...$keys) |
|
| 85 | { |
||
| 86 | 1 | $command = Enum::BITOP; |
|
| 87 | 1 | $args = [$operation, $dstKey, $srcKey]; |
|
| 88 | 1 | $args = array_merge($args, $keys); |
|
| 89 | |||
| 90 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 91 | } |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @override |
||
| 95 | * @inheritDoc |
||
| 96 | */ |
||
| 97 | 1 | View Code Duplication | public function bitPos($key, $bit, $start = 0, $end = -1) |
| 98 | { |
||
| 99 | 1 | $command = Enum::BITPOS; |
|
| 100 | 1 | $args = [$key, $bit, $start, $end]; |
|
| 101 | |||
| 102 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @override |
||
| 107 | * @inheritDoc |
||
| 108 | */ |
||
| 109 | 1 | View Code Duplication | public function decr($key) |
| 110 | { |
||
| 111 | 1 | $command = Enum::DECR; |
|
| 112 | 1 | $args = [$key]; |
|
| 113 | |||
| 114 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @override |
||
| 119 | * @inheritDoc |
||
| 120 | */ |
||
| 121 | 1 | public function decrBy($key, $decrement) |
|
| 122 | { |
||
| 123 | 1 | $command = Enum::DECRBY; |
|
| 124 | 1 | $args = [$key, $decrement]; |
|
| 125 | |||
| 126 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @override |
||
| 131 | * @inheritDoc |
||
| 132 | */ |
||
| 133 | 5 | View Code Duplication | public function get($key) |
| 134 | { |
||
| 135 | 5 | $command = Enum::GET; |
|
| 136 | 5 | $args = [$key]; |
|
| 137 | |||
| 138 | 5 | return $this->dispatch(Builder::build($command, $args)); |
|
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @override |
||
| 143 | * @inheritDoc |
||
| 144 | */ |
||
| 145 | 2 | public function getBit($key, $offset) |
|
| 146 | { |
||
| 147 | 2 | $command = Enum::GETBIT; |
|
| 148 | 2 | $args = [$key, $offset]; |
|
| 149 | |||
| 150 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * @override |
||
| 155 | * @inheritDoc |
||
| 156 | */ |
||
| 157 | 1 | public function getRange($key, $start, $end) |
|
| 158 | { |
||
| 159 | 1 | $command = Enum::GETRANGE; |
|
| 160 | 1 | $args = [$key, $start, $end]; |
|
| 161 | |||
| 162 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 163 | } |
||
| 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) |
|
| 194 | { |
||
| 195 | 1 | $command = Enum::INCRBY; |
|
| 196 | 1 | $args = [$key, $increment]; |
|
| 197 | |||
| 198 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 199 | } |
||
| 200 | |||
| 201 | /** |
||
| 202 | * @override |
||
| 203 | * @inheritDoc |
||
| 204 | */ |
||
| 205 | 1 | public function incrByFloat($key, $increment) |
|
| 206 | { |
||
| 207 | 1 | $command = Enum::INCRBYFLOAT; |
|
| 208 | 1 | $args = [$key, $increment]; |
|
| 209 | |||
| 210 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @override |
||
| 215 | * @inheritDoc |
||
| 216 | */ |
||
| 217 | 29 | public function set($key, $value, array $options = []) |
|
| 218 | { |
||
| 219 | 29 | $command = Enum::SET; |
|
| 220 | 29 | array_unshift($options, $key, $value); |
|
| 221 | 29 | $args = $options; |
|
| 222 | |||
| 223 | 29 | return $this->dispatch(Builder::build($command, $args)); |
|
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @override |
||
| 228 | * @inheritDoc |
||
| 229 | */ |
||
| 230 | 2 | public function setBit($key, $offset, $value) |
|
| 231 | { |
||
| 232 | 2 | $command = Enum::SETBIT; |
|
| 233 | 2 | $args = [$key, $offset, $value]; |
|
| 234 | |||
| 235 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @override |
||
| 240 | * @inheritDoc |
||
| 241 | */ |
||
| 242 | 3 | public function setEx($key, $seconds, $value) |
|
| 243 | { |
||
| 244 | 3 | $command = Enum::SETEX; |
|
| 245 | 3 | $args = [$key, $seconds, $value]; |
|
| 246 | |||
| 247 | 3 | return $this->dispatch(Builder::build($command, $args)); |
|
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @override |
||
| 252 | * @inheritDoc |
||
| 253 | */ |
||
| 254 | 1 | View Code Duplication | public function setNx($key, $value) |
| 255 | { |
||
| 256 | 1 | $command = Enum::SETNX; |
|
| 257 | 1 | $args = [$key, $value]; |
|
| 258 | |||
| 259 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @override |
||
| 264 | * @inheritDoc |
||
| 265 | */ |
||
| 266 | 1 | public function setRange($key, $offset, $value) |
|
| 267 | { |
||
| 268 | 1 | $command = Enum::SETRANGE; |
|
| 269 | 1 | $args = [$key, $offset, $value]; |
|
| 270 | |||
| 271 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @override |
||
| 276 | * @inheritDoc |
||
| 277 | */ |
||
| 278 | 1 | public function pSetEx($key, $milliseconds, $value) |
|
| 279 | { |
||
| 280 | 1 | $command = Enum::PSETEX; |
|
| 281 | 1 | $args = [$key, $milliseconds, $value]; |
|
| 282 | |||
| 283 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @override |
||
| 288 | * @inheritDoc |
||
| 289 | */ |
||
| 290 | 3 | public function mGet($key, ...$keys) |
|
| 291 | { |
||
| 292 | 3 | $command = Enum::MGET; |
|
| 293 | 3 | $args = [$key]; |
|
| 294 | 3 | $args = array_merge($args, $keys); |
|
| 295 | |||
| 296 | 3 | return $this->dispatch(Builder::build($command, $args)); |
|
| 297 | } |
||
| 298 | |||
| 299 | /** |
||
| 300 | * @override |
||
| 301 | * @inheritDoc |
||
| 302 | */ |
||
| 303 | 2 | View Code Duplication | public function mSet(array $kvMap) |
| 304 | { |
||
| 305 | //TODO: change the param $kvMap to ...$kv,cauz map not allow duplicate key |
||
| 306 | 2 | $command = Enum::MSET; |
|
| 307 | 2 | $args = []; |
|
| 308 | 2 | if (!empty($kvMap)) { |
|
| 309 | 2 | foreach ($kvMap as $key => $val) { |
|
| 310 | 2 | $args[] = $key; |
|
| 311 | 2 | $args[] = $val; |
|
| 312 | } |
||
| 313 | } |
||
| 314 | |||
| 315 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @override |
||
| 320 | * @inheritDoc |
||
| 321 | */ |
||
| 322 | 1 | View Code Duplication | public function mSetNx($kvMap) |
| 323 | { |
||
| 324 | 1 | $command = Enum::MSETNX; |
|
| 325 | 1 | $args = []; |
|
| 326 | 1 | if (!empty($kvMap)) { |
|
| 327 | 1 | foreach ($kvMap as $key => $val) { |
|
| 328 | 1 | $args[] = $key; |
|
| 329 | 1 | $args[] = $val; |
|
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 334 | } |
||
| 335 | |||
| 336 | /** |
||
| 337 | * @override |
||
| 338 | * @inheritDoc |
||
| 339 | */ |
||
| 340 | 1 | View Code Duplication | public function strLen($key) |
| 341 | { |
||
| 342 | 1 | $command = Enum::STRLEN; |
|
| 343 | 1 | $args = [$key]; |
|
| 344 | |||
| 345 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 346 | } |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @override |
||
| 350 | * @inheritDoc |
||
| 351 | */ |
||
| 352 | 2 | View Code Duplication | public function del($key,...$keys) |
| 353 | { |
||
| 354 | 2 | $command = Enum::DEL; |
|
| 355 | 2 | $keys[] = $key; |
|
| 356 | 2 | $args = $keys; |
|
| 357 | |||
| 358 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 359 | } |
||
| 360 | |||
| 361 | /** |
||
| 362 | * @override |
||
| 363 | * @inheritDoc |
||
| 364 | */ |
||
| 365 | 2 | View Code Duplication | public function dump($key) |
| 366 | { |
||
| 367 | 2 | $command = Enum::DUMP; |
|
| 368 | 2 | $args = [$key]; |
|
| 369 | |||
| 370 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * @override |
||
| 375 | * @inheritDoc |
||
| 376 | */ |
||
| 377 | 1 | public function exists($key, ...$keys) |
|
| 378 | { |
||
| 379 | 1 | $command = Enum::EXISTS; |
|
| 380 | 1 | $args = [$key]; |
|
| 381 | 1 | $args = array_merge($args, $keys); |
|
| 382 | |||
| 383 | 1 | 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 | 1 | public function expireAt($key, $timestamp) |
|
| 403 | { |
||
| 404 | 1 | $command = Enum::EXPIREAT; |
|
| 405 | 1 | $args = [$key, $timestamp]; |
|
| 406 | |||
| 407 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 408 | } |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @override |
||
| 412 | * @inheritDoc |
||
| 413 | */ |
||
| 414 | 1 | View Code Duplication | public function persist($key) |
| 415 | { |
||
| 416 | 1 | $command = Enum::PERSIST; |
|
| 417 | 1 | $args = [$key]; |
|
| 418 | |||
| 419 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 420 | } |
||
| 421 | |||
| 422 | /** |
||
| 423 | * @override |
||
| 424 | * @inheritDoc |
||
| 425 | */ |
||
| 426 | 1 | public function pExpire($key, $milliseconds) |
|
| 427 | { |
||
| 428 | 1 | $command = Enum::PEXPIRE; |
|
| 429 | 1 | $args = [$key, $milliseconds]; |
|
| 430 | |||
| 431 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @override |
||
| 436 | * @inheritDoc |
||
| 437 | */ |
||
| 438 | 1 | View Code Duplication | public function pExpireAt($key, $milTimestamp) |
| 439 | { |
||
| 440 | 1 | $command = Enum::PEXPIREAT; |
|
| 441 | 1 | $args = [$key, $milTimestamp]; |
|
| 442 | |||
| 443 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 444 | } |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @override |
||
| 448 | * @inheritDoc |
||
| 449 | */ |
||
| 450 | 1 | public function touch($key, ...$keys) |
|
| 451 | { |
||
| 452 | 1 | $command = Enum::TOUCH; |
|
| 453 | 1 | $args = [$key]; |
|
| 454 | 1 | $args = array_merge($args, $keys); |
|
| 455 | |||
| 456 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 457 | } |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @override |
||
| 461 | * @inheritDoc |
||
| 462 | */ |
||
| 463 | 2 | View Code Duplication | public function ttl($key) |
| 464 | { |
||
| 465 | 2 | $command = Enum::TTL; |
|
| 466 | 2 | $args = [$key]; |
|
| 467 | |||
| 468 | 2 | return $this->dispatch(Builder::build($command, $args)); |
|
| 469 | } |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @override |
||
| 473 | * @inheritDoc |
||
| 474 | */ |
||
| 475 | 1 | View Code Duplication | public function type($key) |
| 476 | { |
||
| 477 | 1 | $command = Enum::TYPE; |
|
| 478 | 1 | $args = [$key]; |
|
| 479 | |||
| 480 | 1 | return $this->dispatch(Builder::build($command, $args)); |
|
| 481 | } |
||
| 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) |
||
| 501 | { |
||
| 502 | $command = Enum::WAIT; |
||
| 503 | $args = [$numSlaves, $timeout]; |
||
| 504 | |||
| 505 | return $this->dispatch(Builder::build($command, $args)); |
||
| 506 | } |
||
| 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) |
||
| 572 | { |
||
| 573 | $command = Enum::MOVE; |
||
| 574 | $args = [$key, $db]; |
||
| 575 | |||
| 576 | return $this->dispatch(Builder::build($command, $args)); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * @override |
||
| 581 | * @inheritDoc |
||
| 582 | */ |
||
| 583 | public function scan($cursor, array $options = []) |
||
| 584 | { |
||
| 585 | $command = Enum::SCAN; |
||
| 586 | $args = [$cursor]; |
||
| 587 | $args = array_merge($args, $options); |
||
| 588 | |||
| 589 | return $this->dispatch(Builder::build($command, $args)); |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * @override |
||
| 594 | * @inheritDoc |
||
| 595 | */ |
||
| 596 | public function sort($key, array $options = []) |
||
| 597 | { |
||
| 598 | $command = Enum::SORT; |
||
| 599 | $args = [$key]; |
||
| 600 | $args = array_merge($args, $options); |
||
| 601 | |||
| 604 | |||
| 605 | /** |
||
| 606 | * @override |
||
| 607 | * @inheritDoc |
||
| 608 | */ |
||
| 609 | 1 | public function keys($key = '*') |
|
| 616 | } |
||
| 617 |
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.