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 | ||
| 25 | class RedisProxy | ||
| 26 | { | ||
| 27 | const DRIVER_REDIS = 'redis'; | ||
| 28 | |||
| 29 | const DRIVER_PREDIS = 'predis'; | ||
| 30 | |||
| 31 | private $driver; | ||
| 32 | |||
| 33 | private $host; | ||
| 34 | |||
| 35 | private $port; | ||
| 36 | |||
| 37 | private $database = 0; | ||
| 38 | |||
| 39 | private $timeout; | ||
| 40 | |||
| 41 | private $supportedDrivers = [ | ||
| 42 | self::DRIVER_REDIS, | ||
| 43 | self::DRIVER_PREDIS, | ||
| 44 | ]; | ||
| 45 | |||
| 46 | private $driversOrder = []; | ||
| 47 | |||
| 48 | 112 | public function __construct($host, $port, $timeout = null) | |
| 55 | |||
| 56 | 112 | public function setDriversOrder(array $driversOrder) | |
| 69 | |||
| 70 | 108 | private function init() | |
| 75 | |||
| 76 | 108 | private function prepareDriver() | |
| 94 | |||
| 95 | 108 | private function connect($host, $port, $timeout = null) | |
| 99 | |||
| 100 | 108 | private function isConnected() | |
| 104 | |||
| 105 | 108 | public function __call($name, $arguments) | |
| 116 | |||
| 117 | /** | ||
| 118 | * @param integer $database | ||
| 119 | * @return boolean true on success | ||
| 120 | * @throws RedisProxyException on failure | ||
| 121 | */ | ||
| 122 | 108 | public function select($database) | |
| 123 |     { | ||
| 124 | 108 | $this->prepareDriver(); | |
| 125 | 108 |         if (!$this->isConnected()) { | |
| 126 | 108 | $this->connect($this->host, $this->port, $this->timeout); | |
| 127 | } | ||
| 128 | 108 |         if ($database == $this->database) { | |
| 129 | 108 | return true; | |
| 130 | } | ||
| 131 |         try { | ||
| 132 | 12 | $result = $this->driver->select($database); | |
| 133 | 2 |         } catch (Exception $e) { | |
| 134 | 2 |             throw new RedisProxyException('Invalid DB index'); | |
| 135 | } | ||
| 136 | 10 | $result = $this->transformResult($result); | |
| 137 | 10 |         if ($result === false) { | |
| 138 | 2 |             throw new RedisProxyException('Invalid DB index'); | |
| 139 | } | ||
| 140 | 8 | $this->database = $database; | |
| 141 | 8 | return $result; | |
| 142 | } | ||
| 143 | |||
| 144 | /** | ||
| 145 | * @param string|null $section | ||
| 146 | * @return array | ||
| 147 | */ | ||
| 148 | 8 | public function info($section = null) | |
| 164 | |||
| 165 | /** | ||
| 166 | * Set multiple values to multiple keys | ||
| 167 | * @param array $dictionary | ||
| 168 | * @return boolean true on success | ||
| 169 | * @throws RedisProxyException if number of arguments is wrong | ||
| 170 | */ | ||
| 171 | 12 | View Code Duplication | public function mset(...$dictionary) | 
| 182 | |||
| 183 | /** | ||
| 184 | * @param string $key | ||
| 185 | * @return string|null null if hash field is not set | ||
| 186 | */ | ||
| 187 | 16 | public function get($key) | |
| 193 | |||
| 194 | /** | ||
| 195 | * Delete a key(s) | ||
| 196 | * @param array $keys | ||
| 197 | * @return integer number of deleted keys | ||
| 198 | */ | ||
| 199 | 20 | public function del(...$keys) | |
| 204 | |||
| 205 | /** | ||
| 206 | * Delete a key(s) | ||
| 207 | * @param array $keys | ||
| 208 | * @return integer number of deleted keys | ||
| 209 | */ | ||
| 210 | 8 | public function delete(...$keys) | |
| 214 | |||
| 215 | /** | ||
| 216 | * Incrementally iterate the keys space | ||
| 217 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished | ||
| 218 | * @param string $pattern pattern for keys, use * as wild card | ||
| 219 | * @param integer $count | ||
| 220 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' | ||
| 221 | */ | ||
| 222 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) | 
| 235 | |||
| 236 | /** | ||
| 237 | * Get the value of a hash field | ||
| 238 | * @param string $key | ||
| 239 | * @param string $field | ||
| 240 | * @return string|null null if hash field is not set | ||
| 241 | */ | ||
| 242 | 16 | public function hget($key, $field) | |
| 248 | |||
| 249 | /** | ||
| 250 | * Delete one or more hash fields, returns number of deleted fields | ||
| 251 | * @param array $key | ||
| 252 | * @param array $fields | ||
| 253 | * @return integer | ||
| 254 | */ | ||
| 255 | 8 | public function hdel($key, ...$fields) | |
| 256 |     { | ||
| 257 | 8 | $this->init(); | |
| 258 | 8 |         if (is_array($fields[0])) { | |
| 259 | 4 | $fields = $fields[0]; | |
| 260 | } | ||
| 261 | 8 | return $this->driver->hdel($key, ...$fields); | |
| 262 | } | ||
| 263 | |||
| 264 | /** | ||
| 265 | * Increment the integer value of hash field by given number | ||
| 266 | * @param string $key | ||
| 267 | * @param string $field | ||
| 268 | * @param integer $increment | ||
| 269 | * @return integer | ||
| 270 | */ | ||
| 271 | 4 | public function hincrby($key, $field, $increment = 1) | |
| 276 | |||
| 277 | /** | ||
| 278 | * Increment the float value of hash field by given amount | ||
| 279 | * @param string $key | ||
| 280 | * @param string $field | ||
| 281 | * @param float $increment | ||
| 282 | * @return float | ||
| 283 | */ | ||
| 284 | 4 | public function hincrbyfloat($key, $field, $increment = 1) | |
| 289 | |||
| 290 | /** | ||
| 291 | * Set multiple values to multiple hash fields | ||
| 292 | * @param string $key | ||
| 293 | * @param array $dictionary | ||
| 294 | * @return boolean true on success | ||
| 295 | * @throws RedisProxyException if number of arguments is wrong | ||
| 296 | */ | ||
| 297 | 12 | View Code Duplication | public function hmset($key, ...$dictionary) | 
| 308 | |||
| 309 | /** | ||
| 310 | * Incrementally iterate hash fields and associated values | ||
| 311 | * @param string $key | ||
| 312 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished | ||
| 313 | * @param string $pattern pattern for fields, use * as wild card | ||
| 314 | * @param integer $count | ||
| 315 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' | ||
| 316 | */ | ||
| 317 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) | 
| 330 | |||
| 331 | /** | ||
| 332 | * Add one or more members to a set | ||
| 333 | * @param string $key | ||
| 334 | * @param array $members | ||
| 335 | * @return integer number of new members added to set | ||
| 336 | */ | ||
| 337 | 12 | public function sadd($key, ...$members) | |
| 338 |     { | ||
| 339 | 12 | $this->init(); | |
| 340 | 12 |         if (is_array($members[0])) { | |
| 341 | 8 | $members = $members[0]; | |
| 342 | } | ||
| 343 | 12 | return $this->driver->sadd($key, ...$members); | |
| 344 | } | ||
| 345 | |||
| 346 | /** | ||
| 347 | * Remove and return one or multiple random members from a set | ||
| 348 | * @param string $key | ||
| 349 | * @param integer $count number of members | ||
| 350 | * @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 | ||
| 351 | */ | ||
| 352 | 4 | public function spop($key, $count = 1) | |
| 353 |     { | ||
| 354 | 4 | $this->init(); | |
| 355 | 4 |         if ($count == 1 || $count === null) { | |
| 356 | 4 | $result = $this->driver->spop($key); | |
| 357 | 4 | return $this->convertFalseToNull($result); | |
| 358 | } | ||
| 359 | |||
| 360 | 4 | $members = []; | |
| 361 | 4 |         for ($i = 0; $i < $count; ++$i) { | |
| 362 | 4 | $member = $this->driver->spop($key); | |
| 363 | 4 |             if (!$member) { | |
| 364 | 4 | break; | |
| 365 | } | ||
| 366 | 4 | $members[] = $member; | |
| 367 | } | ||
| 368 | 4 | return empty($members) ? null : $members; | |
| 369 | } | ||
| 370 | |||
| 371 | /** | ||
| 372 | * Incrementally iterate Set elements | ||
| 373 | * @param string $key | ||
| 374 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished | ||
| 375 | * @param string $pattern pattern for member's values, use * as wild card | ||
| 376 | * @param integer $count | ||
| 377 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' | ||
| 378 | */ | ||
| 379 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) | 
| 392 | |||
| 393 | 36 | private function convertFalseToNull($result) | |
| 397 | |||
| 398 | 108 | private function transformResult($result) | |
| 399 |     { | ||
| 400 | 108 |         if ($this->driver instanceof Client && $result instanceof Status) { | |
| 401 | 54 | $result = $result->getPayload() === 'OK'; | |
| 402 | } | ||
| 403 | 108 | return $result; | |
| 404 | } | ||
| 405 | |||
| 406 | /** | ||
| 407 | * Create array from input array - odd keys are used as keys, even keys are used as values | ||
| 408 | * @param array $dictionary | ||
| 409 | * @param string $command | ||
| 410 | * @return array | ||
| 411 | * @throws RedisProxyException if number of keys is not the same as number of values | ||
| 412 | */ | ||
| 413 | 16 | private function prepareKeyValue(array $dictionary, $command) | |
| 427 | } | ||
| 428 |