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 |
||
| 24 | class RedisProxy |
||
| 25 | { |
||
| 26 | const DRIVER_REDIS = 'redis'; |
||
| 27 | |||
| 28 | const DRIVER_PREDIS = 'predis'; |
||
| 29 | |||
| 30 | private $driver; |
||
| 31 | |||
| 32 | private $host; |
||
| 33 | |||
| 34 | private $port; |
||
| 35 | |||
| 36 | private $database = 0; |
||
| 37 | |||
| 38 | private $selectedDatabase = 0; |
||
| 39 | |||
| 40 | private $timeout; |
||
| 41 | |||
| 42 | private $supportedDrivers = [ |
||
| 43 | self::DRIVER_REDIS, |
||
| 44 | self::DRIVER_PREDIS, |
||
| 45 | ]; |
||
| 46 | |||
| 47 | private $driversOrder = []; |
||
| 48 | 112 | ||
| 49 | public function __construct($host, $port, $database = 0, $timeout = null) |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Set driver priorities - default is 1. redis, 2. predis |
||
| 60 | * @param array $driversOrder |
||
| 61 | * @return RedisProxy |
||
| 62 | * @throws RedisProxyException if some driver is not supported |
||
| 63 | 112 | */ |
|
| 64 | public function setDriversOrder(array $driversOrder) |
||
| 74 | 110 | ||
| 75 | private function init() |
||
| 80 | 110 | ||
| 81 | private function prepareDriver() |
||
| 99 | 108 | ||
| 100 | /** |
||
| 101 | 108 | * @return string|null |
|
| 102 | */ |
||
| 103 | public function actualDriver() |
||
| 113 | |||
| 114 | 108 | private function connect($host, $port, $timeout = null) |
|
| 118 | 108 | ||
| 119 | private function isConnected() |
||
| 123 | |||
| 124 | public function __call($name, $arguments) |
||
| 135 | |||
| 136 | 12 | /** |
|
| 137 | 7 | * @param integer $database |
|
| 138 | 2 | * @return boolean true on success |
|
| 139 | * @throws RedisProxyException on failure |
||
| 140 | 10 | */ |
|
| 141 | 10 | public function select($database) |
|
| 163 | 8 | ||
| 164 | 4 | /** |
|
| 165 | * @param string|null $section |
||
| 166 | 4 | * @return array |
|
| 167 | */ |
||
| 168 | public function info($section = null) |
||
| 184 | 4 | ||
| 185 | /** |
||
| 186 | * @param string $key |
||
| 187 | * @return string|null null if hash field is not set |
||
| 188 | */ |
||
| 189 | public function get($key) |
||
| 195 | 16 | ||
| 196 | /** |
||
| 197 | * Delete a key(s) |
||
| 198 | * @param array $keys |
||
| 199 | * @return integer number of deleted keys |
||
| 200 | */ |
||
| 201 | public function del(...$keys) |
||
| 206 | 20 | ||
| 207 | /** |
||
| 208 | * Delete a key(s) |
||
| 209 | * @param array $keys |
||
| 210 | * @return integer number of deleted keys |
||
| 211 | */ |
||
| 212 | public function delete(...$keys) |
||
| 216 | 8 | ||
| 217 | /** |
||
| 218 | * Set multiple values to multiple keys |
||
| 219 | * @param array $dictionary |
||
| 220 | * @return boolean true on success |
||
| 221 | * @throws RedisProxyException if number of arguments is wrong |
||
| 222 | */ |
||
| 223 | View Code Duplication | public function mset(...$dictionary) |
|
| 234 | 2 | ||
| 235 | 2 | /** |
|
| 236 | * Multi get |
||
| 237 | 2 | * @param array $keys |
|
| 238 | * @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 |
||
| 239 | */ |
||
| 240 | View Code Duplication | public function mget(...$keys) |
|
| 254 | |||
| 255 | /** |
||
| 256 | * Incrementally iterate the keys space |
||
| 257 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 258 | * @param string $pattern pattern for keys, use * as wild card |
||
| 259 | 8 | * @param integer $count |
|
| 260 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
| 261 | 8 | */ |
|
| 262 | 8 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
| 275 | 4 | ||
| 276 | /** |
||
| 277 | 4 | * Get the value of a hash field |
|
| 278 | 4 | * @param string $key |
|
| 279 | * @param string $field |
||
| 280 | * @return string|null null if hash field is not set |
||
| 281 | */ |
||
| 282 | public function hget($key, $field) |
||
| 288 | 4 | ||
| 289 | /** |
||
| 290 | 4 | * Delete one or more hash fields, returns number of deleted fields |
|
| 291 | 4 | * @param array $key |
|
| 292 | * @param array $fields |
||
| 293 | * @return integer |
||
| 294 | */ |
||
| 295 | public function hdel($key, ...$fields) |
||
| 303 | 12 | ||
| 304 | 12 | /** |
|
| 305 | 8 | * Increment the integer value of hash field by given number |
|
| 306 | 8 | * @param string $key |
|
| 307 | * @param string $field |
||
| 308 | 8 | * @param integer $increment |
|
| 309 | 4 | * @return integer |
|
| 310 | 4 | */ |
|
| 311 | public function hincrby($key, $field, $increment = 1) |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Increment the float value of hash field by given amount |
||
| 319 | * @param string $key |
||
| 320 | * @param string $field |
||
| 321 | 4 | * @param float $increment |
|
| 322 | * @return float |
||
| 323 | 4 | */ |
|
| 324 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
| 329 | 2 | ||
| 330 | 2 | /** |
|
| 331 | * Set multiple values to multiple hash fields |
||
| 332 | 2 | * @param string $key |
|
| 333 | * @param array $dictionary |
||
| 334 | * @return boolean true on success |
||
| 335 | * @throws RedisProxyException if number of arguments is wrong |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function hmset($key, ...$dictionary) |
|
| 348 | |||
| 349 | /** |
||
| 350 | * Multi hash get |
||
| 351 | * @param string $key |
||
| 352 | * @param array $fields |
||
| 353 | * @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 |
||
| 354 | */ |
||
| 355 | View Code Duplication | public function hmget($key, ...$fields) |
|
| 369 | |||
| 370 | 4 | /** |
|
| 371 | 2 | * Incrementally iterate hash fields and associated values |
|
| 372 | 4 | * @param string $key |
|
| 373 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 374 | * @param string $pattern pattern for fields, use * as wild card |
||
| 375 | * @param integer $count |
||
| 376 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
| 377 | */ |
||
| 378 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
|
| 391 | 2 | ||
| 392 | 2 | /** |
|
| 393 | * Add one or more members to a set |
||
| 394 | 2 | * @param string $key |
|
| 395 | * @param array $members |
||
| 396 | * @return integer number of new members added to set |
||
| 397 | 36 | */ |
|
| 398 | public function sadd($key, ...$members) |
||
| 406 | 27 | ||
| 407 | 108 | /** |
|
| 408 | * Remove and return one or multiple random members from a set |
||
| 409 | * @param string $key |
||
| 410 | * @param integer $count number of members |
||
| 411 | * @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 |
||
| 412 | */ |
||
| 413 | public function spop($key, $count = 1) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Incrementally iterate Set elements |
||
| 434 | * @param string $key |
||
| 435 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 436 | * @param string $pattern pattern for member's values, use * as wild card |
||
| 437 | * @param integer $count |
||
| 438 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
| 439 | */ |
||
| 440 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
|
| 453 | |||
| 454 | private function convertFalseToNull($result) |
||
| 458 | |||
| 459 | private function transformResult($result) |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
| 469 | * @param array $dictionary |
||
| 470 | * @param string $command |
||
| 471 | * @return array |
||
| 472 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
| 473 | */ |
||
| 474 | private function prepareKeyValue(array $dictionary, $command) |
||
| 488 | } |
||
| 489 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.