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 RedisProxy 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 RedisProxy, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class RedisProxy |
||
| 32 | { |
||
| 33 | use SortedSetBehavior; |
||
| 34 | |||
| 35 | const DRIVER_REDIS = 'redis'; |
||
| 36 | |||
| 37 | const DRIVER_PREDIS = 'predis'; |
||
| 38 | |||
| 39 | const TYPE_STRING = 'string'; |
||
| 40 | |||
| 41 | const TYPE_SET = 'set'; |
||
| 42 | |||
| 43 | const TYPE_HASH = 'hash'; |
||
| 44 | |||
| 45 | const TYPE_LIST = 'list'; |
||
| 46 | |||
| 47 | const TYPE_SORTED_SET = 'sorted_set'; |
||
| 48 | |||
| 49 | private $driver; |
||
| 50 | |||
| 51 | private $host; |
||
| 52 | |||
| 53 | private $port; |
||
| 54 | |||
| 55 | private $database = 0; |
||
| 56 | |||
| 57 | private $selectedDatabase = 0; |
||
| 58 | |||
| 59 | private $timeout; |
||
| 60 | |||
| 61 | private $supportedDrivers = [ |
||
| 62 | self::DRIVER_REDIS, |
||
| 63 | self::DRIVER_PREDIS, |
||
| 64 | ]; |
||
| 65 | |||
| 66 | private $driversOrder = []; |
||
| 67 | |||
| 68 | private $redisTypeMap = [ |
||
| 69 | self::DRIVER_REDIS => [ |
||
| 70 | Redis::REDIS_STRING => self::TYPE_STRING, |
||
| 71 | Redis::REDIS_SET => self::TYPE_SET, |
||
| 72 | Redis::REDIS_HASH => self::TYPE_HASH, |
||
| 73 | Redis::REDIS_LIST => self::TYPE_LIST, |
||
| 74 | Redis::REDIS_ZSET => self::TYPE_SORTED_SET, |
||
| 75 | ], |
||
| 76 | self::DRIVER_PREDIS => [ |
||
| 77 | 'string' => self::TYPE_STRING, |
||
| 78 | 'set' => self::TYPE_SET, |
||
| 79 | 'hash' => self::TYPE_HASH, |
||
| 80 | 'list' => self::TYPE_LIST, |
||
| 81 | 'zset' => self::TYPE_SORTED_SET, |
||
| 82 | ], |
||
| 83 | ]; |
||
| 84 | |||
| 85 | 260 | public function __construct($host, $port, $database = 0, $timeout = null) |
|
| 93 | |||
| 94 | /** |
||
| 95 | * Set driver priorities - default is 1. redis, 2. predis |
||
| 96 | * @param array $driversOrder |
||
| 97 | * @return RedisProxy |
||
| 98 | * @throws RedisProxyException if some driver is not supported |
||
| 99 | */ |
||
| 100 | 260 | public function setDriversOrder(array $driversOrder) |
|
| 110 | |||
| 111 | 258 | private function init() |
|
| 116 | |||
| 117 | 258 | private function prepareDriver() |
|
| 135 | |||
| 136 | /** |
||
| 137 | * @return string|null |
||
| 138 | */ |
||
| 139 | 258 | public function actualDriver() |
|
| 149 | |||
| 150 | 256 | private function connect($host, $port, $timeout = null) |
|
| 154 | |||
| 155 | 256 | private function isConnected() |
|
| 159 | |||
| 160 | 258 | public function __call($name, $arguments) |
|
| 171 | |||
| 172 | /** |
||
| 173 | * @param int $database |
||
| 174 | * @return boolean true on success |
||
| 175 | * @throws RedisProxyException on failure |
||
| 176 | */ |
||
| 177 | 256 | public function select($database) |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param string|null $section |
||
| 202 | * @return array |
||
| 203 | */ |
||
| 204 | 8 | public function info($section = null) |
|
| 220 | |||
| 221 | /** |
||
| 222 | * Determine if a key exists |
||
| 223 | * @param string $key |
||
| 224 | * @return boolean |
||
| 225 | */ |
||
| 226 | 4 | public function exists($key) |
|
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $key |
||
| 235 | * @return string|null |
||
| 236 | */ |
||
| 237 | 4 | public function type($key) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Get the value of a key |
||
| 247 | * @param string $key |
||
| 248 | * @return string|null null if key not set |
||
| 249 | */ |
||
| 250 | 88 | public function get($key) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Set the string value of a key and return its old value |
||
| 259 | * @param string $key |
||
| 260 | * @param string $value |
||
| 261 | * @return string|null null if key was not set before |
||
| 262 | */ |
||
| 263 | 4 | public function getset($key, $value) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Set a key's time to live in seconds |
||
| 272 | * @param string $key |
||
| 273 | * @param int $seconds |
||
| 274 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 275 | */ |
||
| 276 | 12 | public function expire($key, $seconds) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Set a key's time to live in milliseconds |
||
| 285 | * @param string $key |
||
| 286 | * @param int $miliseconds |
||
| 287 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 288 | */ |
||
| 289 | 8 | public function pexpire($key, $miliseconds) |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Set the expiration for a key as a UNIX timestamp |
||
| 298 | * @param string $key |
||
| 299 | * @param int $timestamp |
||
| 300 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 301 | */ |
||
| 302 | 4 | public function expireat($key, $timestamp) |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Set the expiration for a key as a UNIX timestamp specified in milliseconds |
||
| 311 | * @param string $key |
||
| 312 | * @param int $milisecondsTimestamp |
||
| 313 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 314 | */ |
||
| 315 | 4 | public function pexpireat($key, $milisecondsTimestamp) |
|
| 321 | |||
| 322 | /** |
||
| 323 | * Set the value and expiration in milliseconds of a key |
||
| 324 | * @param string $key |
||
| 325 | * @param int $miliseconds |
||
| 326 | * @param string $value |
||
| 327 | * @return boolean |
||
| 328 | */ |
||
| 329 | 4 | public function psetex($key, $miliseconds, $value) |
|
| 338 | |||
| 339 | /** |
||
| 340 | * Remove the expiration from a key |
||
| 341 | * @param string $key |
||
| 342 | * @return boolean |
||
| 343 | */ |
||
| 344 | 4 | public function persist($key) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Set the value of a key, only if the key does not exist |
||
| 353 | * @param string $key |
||
| 354 | * @param string $value |
||
| 355 | * @return boolean true if the key was set, false if the key was not set |
||
| 356 | */ |
||
| 357 | 4 | public function setnx($key, $value) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Delete a key(s) |
||
| 366 | * @param array $keys |
||
| 367 | * @return int number of deleted keys |
||
| 368 | */ |
||
| 369 | 32 | public function del(...$keys) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Delete a key(s) |
||
| 378 | * @param array $keys |
||
| 379 | * @return int number of deleted keys |
||
| 380 | */ |
||
| 381 | 12 | public function delete(...$keys) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Increment the integer value of a key by one |
||
| 388 | * @param string $key |
||
| 389 | * @return integer |
||
| 390 | */ |
||
| 391 | 4 | public function incr($key) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Increment the integer value of a key by the given amount |
||
| 399 | * @param string $key |
||
| 400 | * @param integer $increment |
||
| 401 | * @return integer |
||
| 402 | */ |
||
| 403 | 4 | public function incrby($key, $increment = 1) |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Increment the float value of a key by the given amount |
||
| 411 | * @param string $key |
||
| 412 | * @param float $increment |
||
| 413 | * @return float |
||
| 414 | */ |
||
| 415 | 8 | public function incrbyfloat($key, $increment = 1) |
|
| 420 | |||
| 421 | /** |
||
| 422 | * Decrement the integer value of a key by one |
||
| 423 | * @param string $key |
||
| 424 | * @return integer |
||
| 425 | */ |
||
| 426 | 4 | public function decr($key) |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Decrement the integer value of a key by the given number |
||
| 434 | * @param string $key |
||
| 435 | * @param integer $decrement |
||
| 436 | * @return integer |
||
| 437 | */ |
||
| 438 | 4 | public function decrby($key, $decrement = 1) |
|
| 443 | |||
| 444 | /** |
||
| 445 | * Decrement the float value of a key by the given amount |
||
| 446 | * @param string $key |
||
| 447 | * @param float $decrement |
||
| 448 | * @return float |
||
| 449 | */ |
||
| 450 | 4 | public function decrbyfloat($key, $decrement = 1) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Return a serialized version of the value stored at the specified key |
||
| 457 | * @param string $key |
||
| 458 | * @return string|null serialized value, null if key doesn't exist |
||
| 459 | */ |
||
| 460 | 4 | public function dump($key) |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Set multiple values to multiple keys |
||
| 469 | * @param array $dictionary |
||
| 470 | * @return boolean true on success |
||
| 471 | * @throws RedisProxyException if number of arguments is wrong |
||
| 472 | */ |
||
| 473 | 12 | View Code Duplication | public function mset(...$dictionary) |
| 484 | |||
| 485 | /** |
||
| 486 | * Multi get |
||
| 487 | * @param array $keys |
||
| 488 | * @return array Returns the values for all specified keys. For every key that does not hold a string value or does not exist, null is returned |
||
| 489 | */ |
||
| 490 | 4 | public function mget(...$keys) |
|
| 500 | |||
| 501 | /** |
||
| 502 | * Incrementally iterate the keys space |
||
| 503 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 504 | * @param string $pattern pattern for keys, use * as wild card |
||
| 505 | * @param int $count |
||
| 506 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
| 507 | */ |
||
| 508 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
| 521 | |||
| 522 | /** |
||
| 523 | * Get the value of a hash field |
||
| 524 | * @param string $key |
||
| 525 | * @param string $field |
||
| 526 | * @return string|null null if hash field is not set |
||
| 527 | */ |
||
| 528 | 16 | public function hget($key, $field) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Delete one or more hash fields, returns number of deleted fields |
||
| 537 | * @param array $key |
||
| 538 | * @param array $fields |
||
| 539 | * @return int |
||
| 540 | */ |
||
| 541 | 8 | public function hdel($key, ...$fields) |
|
| 547 | |||
| 548 | /** |
||
| 549 | * Increment the integer value of hash field by given number |
||
| 550 | * @param string $key |
||
| 551 | * @param string $field |
||
| 552 | * @param int $increment |
||
| 553 | * @return int |
||
| 554 | */ |
||
| 555 | 4 | public function hincrby($key, $field, $increment = 1) |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Increment the float value of hash field by given amount |
||
| 563 | * @param string $key |
||
| 564 | * @param string $field |
||
| 565 | * @param float $increment |
||
| 566 | * @return float |
||
| 567 | */ |
||
| 568 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Set multiple values to multiple hash fields |
||
| 576 | * @param string $key |
||
| 577 | * @param array $dictionary |
||
| 578 | * @return boolean true on success |
||
| 579 | * @throws RedisProxyException if number of arguments is wrong |
||
| 580 | */ |
||
| 581 | 12 | View Code Duplication | public function hmset($key, ...$dictionary) |
| 592 | |||
| 593 | /** |
||
| 594 | * Multi hash get |
||
| 595 | * @param string $key |
||
| 596 | * @param array $fields |
||
| 597 | * @return array Returns the values for all specified fields. For every field that does not hold a string value or does not exist, null is returned |
||
| 598 | */ |
||
| 599 | 4 | public function hmget($key, ...$fields) |
|
| 609 | |||
| 610 | /** |
||
| 611 | * Incrementally iterate hash fields and associated values |
||
| 612 | * @param string $key |
||
| 613 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 614 | * @param string $pattern pattern for fields, use * as wild card |
||
| 615 | * @param int $count |
||
| 616 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
| 617 | */ |
||
| 618 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
| 631 | |||
| 632 | /** |
||
| 633 | * Add one or more members to a set |
||
| 634 | * @param string $key |
||
| 635 | * @param array $members |
||
| 636 | * @return int number of new members added to set |
||
| 637 | */ |
||
| 638 | 16 | public function sadd($key, ...$members) |
|
| 644 | |||
| 645 | /** |
||
| 646 | * Remove and return one or multiple random members from a set |
||
| 647 | * @param string $key |
||
| 648 | * @param int $count number of members |
||
| 649 | * @return mixed string if $count is null or 1 and $key exists, array if $count > 1 and $key exists, null if $key doesn't exist |
||
| 650 | */ |
||
| 651 | 4 | public function spop($key, $count = 1) |
|
| 669 | |||
| 670 | /** |
||
| 671 | * Incrementally iterate Set elements |
||
| 672 | * @param string $key |
||
| 673 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 674 | * @param string $pattern pattern for member's values, use * as wild card |
||
| 675 | * @param int $count |
||
| 676 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
| 677 | */ |
||
| 678 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
| 691 | |||
| 692 | /** |
||
| 693 | * Prepend one or multiple values to a list |
||
| 694 | * @param string $key |
||
| 695 | * @param array $elements |
||
| 696 | * @return int the length of the list after the push operations |
||
| 697 | */ |
||
| 698 | 28 | public function lpush($key, ...$elements) |
|
| 704 | |||
| 705 | /** |
||
| 706 | * Append one or multiple values to a list |
||
| 707 | * @param string $key |
||
| 708 | * @param array $elements |
||
| 709 | * @return int the length of the list after the push operations |
||
| 710 | */ |
||
| 711 | 12 | public function rpush($key, ...$elements) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Remove and get the first element in a list |
||
| 720 | * @param string $key |
||
| 721 | * @return string|null |
||
| 722 | */ |
||
| 723 | 4 | public function lpop($key) |
|
| 729 | |||
| 730 | /** |
||
| 731 | * Remove and get the last element in a list |
||
| 732 | * @param string $key |
||
| 733 | * @return string|null |
||
| 734 | */ |
||
| 735 | 4 | public function rpop($key) |
|
| 741 | |||
| 742 | /** |
||
| 743 | * Get an element from a list by its index |
||
| 744 | * @param string $key |
||
| 745 | * @param int $index zero-based, so 0 means the first element, 1 the second element and so on. -1 means the last element, -2 means the penultimate and so forth |
||
| 746 | * @return string|null |
||
| 747 | */ |
||
| 748 | 12 | public function lindex($key, $index) |
|
| 754 | |||
| 755 | /** |
||
| 756 | * Returns null instead of false for Redis driver |
||
| 757 | * @param mixed $result |
||
| 758 | * @return mixed |
||
| 759 | */ |
||
| 760 | 128 | private function convertFalseToNull($result) |
|
| 764 | |||
| 765 | /** |
||
| 766 | * Transforms Predis result Payload to boolean |
||
| 767 | * @param mixed $result |
||
| 768 | * @return mixed |
||
| 769 | */ |
||
| 770 | 256 | private function transformResult($result) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
| 780 | * @param array $dictionary |
||
| 781 | * @param string $command |
||
| 782 | * @return array |
||
| 783 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
| 784 | */ |
||
| 785 | 16 | private function prepareKeyValue(array $dictionary, $command) |
|
| 799 | |||
| 800 | 88 | private function prepareArguments($command, ...$params) |
|
| 810 | } |
||
| 811 |