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 |
||
| 28 | class RedisProxy |
||
| 29 | { |
||
| 30 | use SortedSetBehavior; |
||
| 31 | |||
| 32 | use ListBehavior; |
||
| 33 | |||
| 34 | const DRIVER_REDIS = 'redis'; |
||
| 35 | |||
| 36 | const DRIVER_PREDIS = 'predis'; |
||
| 37 | |||
| 38 | const TYPE_STRING = 'string'; |
||
| 39 | |||
| 40 | const TYPE_SET = 'set'; |
||
| 41 | |||
| 42 | const TYPE_HASH = 'hash'; |
||
| 43 | |||
| 44 | const TYPE_LIST = 'list'; |
||
| 45 | |||
| 46 | const TYPE_SORTED_SET = 'sorted_set'; |
||
| 47 | |||
| 48 | private $driver; |
||
| 49 | |||
| 50 | private $host; |
||
| 51 | |||
| 52 | private $port; |
||
| 53 | |||
| 54 | private $database = 0; |
||
| 55 | |||
| 56 | private $selectedDatabase = 0; |
||
| 57 | |||
| 58 | private $timeout; |
||
| 59 | |||
| 60 | private $supportedDrivers = [ |
||
| 61 | self::DRIVER_REDIS, |
||
| 62 | self::DRIVER_PREDIS, |
||
| 63 | ]; |
||
| 64 | |||
| 65 | private $driversOrder = []; |
||
| 66 | |||
| 67 | private $redisTypeMap = [ |
||
| 68 | self::DRIVER_REDIS => [ |
||
| 69 | Redis::REDIS_STRING => self::TYPE_STRING, |
||
| 70 | Redis::REDIS_SET => self::TYPE_SET, |
||
| 71 | Redis::REDIS_HASH => self::TYPE_HASH, |
||
| 72 | Redis::REDIS_LIST => self::TYPE_LIST, |
||
| 73 | Redis::REDIS_ZSET => self::TYPE_SORTED_SET, |
||
| 74 | ], |
||
| 75 | self::DRIVER_PREDIS => [ |
||
| 76 | 'string' => self::TYPE_STRING, |
||
| 77 | 'set' => self::TYPE_SET, |
||
| 78 | 'hash' => self::TYPE_HASH, |
||
| 79 | 'list' => self::TYPE_LIST, |
||
| 80 | 'zset' => self::TYPE_SORTED_SET, |
||
| 81 | ], |
||
| 82 | ]; |
||
| 83 | |||
| 84 | public function __construct($host, $port, $database = 0, $timeout = null) |
||
| 92 | 260 | ||
| 93 | /** |
||
| 94 | * Set driver priorities - default is 1. redis, 2. predis |
||
| 95 | * @param array $driversOrder |
||
| 96 | * @return RedisProxy |
||
| 97 | * @throws RedisProxyException if some driver is not supported |
||
| 98 | */ |
||
| 99 | public function setDriversOrder(array $driversOrder) |
||
| 109 | |||
| 110 | protected function init() |
||
| 115 | 256 | ||
| 116 | private function prepareDriver() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @return string|null |
||
| 137 | */ |
||
| 138 | public function actualDriver() |
||
| 148 | |||
| 149 | private function connect($host, $port, $timeout = null) |
||
| 153 | |||
| 154 | private function isConnected() |
||
| 158 | |||
| 159 | public function __call($name, $arguments) |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @param int $database |
||
| 173 | * @return boolean true on success |
||
| 174 | * @throws RedisProxyException on failure |
||
| 175 | */ |
||
| 176 | public function select($database) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string|null $section |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | public function info($section = null) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Determine if a key exists |
||
| 222 | * @param string $key |
||
| 223 | * @return boolean |
||
| 224 | */ |
||
| 225 | public function exists($key) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param string $key |
||
| 234 | * @return string|null |
||
| 235 | */ |
||
| 236 | public function type($key) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the value of a key |
||
| 246 | * @param string $key |
||
| 247 | * @return string|null null if key not set |
||
| 248 | */ |
||
| 249 | public function get($key) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Set the string value of a key and return its old value |
||
| 258 | * @param string $key |
||
| 259 | * @param string $value |
||
| 260 | * @return string|null null if key was not set before |
||
| 261 | */ |
||
| 262 | public function getset($key, $value) |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Set a key's time to live in seconds |
||
| 271 | * @param string $key |
||
| 272 | * @param int $seconds |
||
| 273 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 274 | */ |
||
| 275 | public function expire($key, $seconds) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Set a key's time to live in milliseconds |
||
| 284 | * @param string $key |
||
| 285 | * @param int $miliseconds |
||
| 286 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 287 | */ |
||
| 288 | public function pexpire($key, $miliseconds) |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set the expiration for a key as a UNIX timestamp |
||
| 297 | * @param string $key |
||
| 298 | * @param int $timestamp |
||
| 299 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 300 | */ |
||
| 301 | public function expireat($key, $timestamp) |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Set the expiration for a key as a UNIX timestamp specified in milliseconds |
||
| 310 | * @param string $key |
||
| 311 | * @param int $milisecondsTimestamp |
||
| 312 | * @return boolean true if the timeout was set, false if key does not exist or the timeout could not be set |
||
| 313 | */ |
||
| 314 | public function pexpireat($key, $milisecondsTimestamp) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Set the value and expiration in milliseconds of a key |
||
| 323 | * @param string $key |
||
| 324 | * @param int $miliseconds |
||
| 325 | * @param string $value |
||
| 326 | * @return boolean |
||
| 327 | */ |
||
| 328 | public function psetex($key, $miliseconds, $value) |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Remove the expiration from a key |
||
| 340 | * @param string $key |
||
| 341 | * @return boolean |
||
| 342 | */ |
||
| 343 | public function persist($key) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Set the value of a key, only if the key does not exist |
||
| 352 | * @param string $key |
||
| 353 | * @param string $value |
||
| 354 | * @return boolean true if the key was set, false if the key was not set |
||
| 355 | */ |
||
| 356 | public function setnx($key, $value) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Delete a key(s) |
||
| 365 | * @param array $keys |
||
| 366 | * @return int number of deleted keys |
||
| 367 | */ |
||
| 368 | public function del(...$keys) |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Delete a key(s) |
||
| 377 | * @param array $keys |
||
| 378 | * @return int number of deleted keys |
||
| 379 | */ |
||
| 380 | public function delete(...$keys) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Increment the integer value of a key by one |
||
| 387 | * @param string $key |
||
| 388 | * @return integer |
||
| 389 | */ |
||
| 390 | public function incr($key) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Increment the integer value of a key by the given amount |
||
| 398 | * @param string $key |
||
| 399 | * @param integer $increment |
||
| 400 | * @return integer |
||
| 401 | */ |
||
| 402 | public function incrby($key, $increment = 1) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Increment the float value of a key by the given amount |
||
| 410 | * @param string $key |
||
| 411 | * @param float $increment |
||
| 412 | * @return float |
||
| 413 | */ |
||
| 414 | public function incrbyfloat($key, $increment = 1) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Decrement the integer value of a key by one |
||
| 422 | * @param string $key |
||
| 423 | * @return integer |
||
| 424 | */ |
||
| 425 | public function decr($key) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Decrement the integer value of a key by the given number |
||
| 433 | * @param string $key |
||
| 434 | * @param integer $decrement |
||
| 435 | * @return integer |
||
| 436 | */ |
||
| 437 | public function decrby($key, $decrement = 1) |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Decrement the float value of a key by the given amount |
||
| 445 | * @param string $key |
||
| 446 | * @param float $decrement |
||
| 447 | * @return float |
||
| 448 | */ |
||
| 449 | public function decrbyfloat($key, $decrement = 1) |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return a serialized version of the value stored at the specified key |
||
| 456 | * @param string $key |
||
| 457 | * @return string|null serialized value, null if key doesn't exist |
||
| 458 | */ |
||
| 459 | public function dump($key) |
||
| 465 | |||
| 466 | /** |
||
| 467 | * Set multiple values to multiple keys |
||
| 468 | * @param array $dictionary |
||
| 469 | * @return boolean true on success |
||
| 470 | * @throws RedisProxyException if number of arguments is wrong |
||
| 471 | */ |
||
| 472 | View Code Duplication | public function mset(...$dictionary) |
|
| 483 | |||
| 484 | /** |
||
| 485 | * Multi get |
||
| 486 | * @param array $keys |
||
| 487 | * @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 |
||
| 488 | */ |
||
| 489 | public function mget(...$keys) |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Incrementally iterate the keys space |
||
| 502 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 503 | * @param string $pattern pattern for keys, use * as wild card |
||
| 504 | * @param int $count |
||
| 505 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
| 506 | */ |
||
| 507 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Get the value of a hash field |
||
| 523 | * @param string $key |
||
| 524 | * @param string $field |
||
| 525 | * @return string|null null if hash field is not set |
||
| 526 | */ |
||
| 527 | public function hget($key, $field) |
||
| 533 | |||
| 534 | /** |
||
| 535 | * Delete one or more hash fields, returns number of deleted fields |
||
| 536 | * @param array $key |
||
| 537 | * @param array $fields |
||
| 538 | * @return int |
||
| 539 | */ |
||
| 540 | public function hdel($key, ...$fields) |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Increment the integer value of hash field by given number |
||
| 549 | * @param string $key |
||
| 550 | * @param string $field |
||
| 551 | * @param int $increment |
||
| 552 | * @return int |
||
| 553 | */ |
||
| 554 | public function hincrby($key, $field, $increment = 1) |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Increment the float value of hash field by given amount |
||
| 562 | * @param string $key |
||
| 563 | * @param string $field |
||
| 564 | * @param float $increment |
||
| 565 | * @return float |
||
| 566 | */ |
||
| 567 | public function hincrbyfloat($key, $field, $increment = 1) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Set multiple values to multiple hash fields |
||
| 575 | * @param string $key |
||
| 576 | * @param array $dictionary |
||
| 577 | * @return boolean true on success |
||
| 578 | * @throws RedisProxyException if number of arguments is wrong |
||
| 579 | */ |
||
| 580 | View Code Duplication | public function hmset($key, ...$dictionary) |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Multi hash get |
||
| 594 | * @param string $key |
||
| 595 | * @param array $fields |
||
| 596 | * @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 |
||
| 597 | */ |
||
| 598 | public function hmget($key, ...$fields) |
||
| 608 | |||
| 609 | /** |
||
| 610 | * Incrementally iterate hash fields and associated values |
||
| 611 | * @param string $key |
||
| 612 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 613 | * @param string $pattern pattern for fields, use * as wild card |
||
| 614 | * @param int $count |
||
| 615 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
| 616 | */ |
||
| 617 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Add one or more members to a set |
||
| 633 | * @param string $key |
||
| 634 | * @param array $members |
||
| 635 | * @return int number of new members added to set |
||
| 636 | */ |
||
| 637 | public function sadd($key, ...$members) |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Remove and return one or multiple random members from a set |
||
| 646 | * @param string $key |
||
| 647 | * @param int $count number of members |
||
| 648 | * @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 |
||
| 649 | */ |
||
| 650 | public function spop($key, $count = 1) |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Incrementally iterate Set elements |
||
| 671 | * @param string $key |
||
| 672 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 673 | * @param string $pattern pattern for member's values, use * as wild card |
||
| 674 | * @param int $count |
||
| 675 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
| 676 | */ |
||
| 677 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Returns null instead of false for Redis driver |
||
| 693 | * @param mixed $result |
||
| 694 | * @return mixed |
||
| 695 | */ |
||
| 696 | private function convertFalseToNull($result) |
||
| 700 | 28 | ||
| 701 | 28 | /** |
|
| 702 | 28 | * Transforms Predis result Payload to boolean |
|
| 703 | * @param mixed $result |
||
| 704 | * @return mixed |
||
| 705 | */ |
||
| 706 | private function transformResult($result) |
||
| 713 | 12 | ||
| 714 | 12 | /** |
|
| 715 | 12 | * Create array from input array - odd keys are used as keys, even keys are used as values |
|
| 716 | * @param array $dictionary |
||
| 717 | * @param string $command |
||
| 718 | * @return array |
||
| 719 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
| 720 | */ |
||
| 721 | private function prepareKeyValue(array $dictionary, $command) |
||
| 735 | 4 | ||
| 736 | private function prepareArguments($command, ...$params) |
||
| 746 | } |
||
| 747 |