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) |
|
| 57 | 27 | { |
|
| 58 | 112 | if (empty($driversOrder)) { |
|
| 59 | 2 | throw new RedisProxyException('You need to set at least one driver'); |
|
| 60 | } |
||
| 61 | 110 | foreach ($driversOrder as $driver) { |
|
| 62 | 110 | if (!in_array($driver, $this->supportedDrivers)) { |
|
| 63 | 56 | throw new RedisProxyException('Driver "' . $driver . '" is not supported'); |
|
| 64 | } |
||
| 65 | 54 | } |
|
| 66 | 108 | $this->driversOrder = $driversOrder; |
|
| 67 | 108 | return $this; |
|
| 68 | } |
||
| 69 | |||
| 70 | 108 | private function init() |
|
| 75 | |||
| 76 | 108 | private function prepareDriver() |
|
| 77 | { |
||
| 78 | 108 | if ($this->driver !== null) { |
|
| 79 | 108 | return; |
|
| 80 | } |
||
| 81 | |||
| 82 | 108 | foreach ($this->driversOrder as $preferredDriver) { |
|
| 83 | 108 | if ($preferredDriver === self::DRIVER_REDIS && extension_loaded('redis')) { |
|
| 84 | 54 | $this->driver = new Redis(); |
|
| 85 | 54 | return; |
|
| 86 | } |
||
| 87 | 54 | if ($preferredDriver === self::DRIVER_PREDIS && class_exists('Predis\Client')) { |
|
| 88 | 54 | $this->driver = new Client(); |
|
| 89 | 54 | return; |
|
| 90 | } |
||
| 91 | } |
||
| 92 | throw new RedisProxyException('No redis library loaded (ext-redis or predis)'); |
||
| 93 | } |
||
| 94 | |||
| 95 | 108 | private function connect($host, $port, $timeout = null) |
|
| 99 | |||
| 100 | 108 | private function isConnected() |
|
| 104 | |||
| 105 | 108 | public function __call($name, $arguments) |
|
| 106 | { |
||
| 107 | 108 | $this->init(); |
|
| 108 | 108 | $name = strtolower($name); |
|
| 109 | try { |
||
| 110 | 108 | $result = call_user_func_array([$this->driver, $name], $arguments); |
|
| 111 | 56 | } catch (Exception $e) { |
|
| 112 | 4 | throw new RedisProxyException("Error for command '$name', use getPrevious() for more info", 1484162284, $e); |
|
| 113 | } |
||
| 114 | 108 | return $this->transformResult($result); |
|
| 115 | } |
||
| 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 | 54 | } |
|
| 128 | 108 | if ($database == $this->database) { |
|
| 129 | 108 | return true; |
|
| 130 | } |
||
| 131 | try { |
||
| 132 | 12 | $result = $this->driver->select($database); |
|
| 133 | 7 | } 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) |
|
| 149 | { |
||
| 150 | 8 | $this->init(); |
|
| 151 | 8 | $section = $section ? strtolower($section) : $section; |
|
| 152 | 8 | $result = $section === null ? $this->driver->info() : $this->driver->info($section); |
|
| 153 | |||
| 154 | 8 | $databases = $section === null || $section === 'keyspace' ? $this->config('get', 'databases')['databases'] : null; |
|
| 155 | 8 | $groupedResult = InfoHelper::createInfoArray($this->driver, $result, $databases); |
|
| 156 | 8 | if ($section === null) { |
|
| 157 | 4 | return $groupedResult; |
|
| 158 | } |
||
| 159 | 8 | if (isset($groupedResult[$section])) { |
|
| 160 | 4 | return $groupedResult[$section]; |
|
| 161 | } |
||
| 162 | 4 | throw new RedisProxyException('Info section "' . $section . '" doesn\'t exist'); |
|
| 163 | } |
||
| 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 | public function mset(...$dictionary) |
|
| 172 | { |
||
| 173 | 12 | if (is_array($dictionary[0])) { |
|
| 174 | 8 | $result = $this->driver->mset(...$dictionary); |
|
| 175 | 8 | return $this->transformResult($result); |
|
| 176 | } |
||
| 177 | 8 | $dictionary = $this->prepareKeyValue($dictionary, 'mset'); |
|
| 178 | 4 | $result = $this->driver->mset($dictionary); |
|
| 179 | 4 | return $this->transformResult($result); |
|
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $key |
||
| 184 | * @return string|null null if hash field is not set |
||
| 185 | */ |
||
| 186 | 16 | public function get($key) |
|
| 187 | { |
||
| 188 | 16 | $this->init(); |
|
| 189 | 16 | $result = $this->driver->get($key); |
|
| 190 | 16 | return $this->convertFalseToNull($result); |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Delete a key(s) |
||
| 195 | * @param array ...$keys |
||
| 196 | * @return integer number of deleted keys |
||
| 197 | */ |
||
| 198 | 20 | public function del(...$keys) |
|
| 199 | { |
||
| 200 | 20 | $this->init(); |
|
| 201 | 20 | return $this->driver->del(...$keys); |
|
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Delete a key(s) |
||
| 206 | * @param array ...$keys |
||
| 207 | * @return integer number of deleted keys |
||
| 208 | */ |
||
| 209 | 8 | public function delete(...$keys) |
|
| 210 | { |
||
| 211 | 8 | return $this->del(...$keys); |
|
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Incrementally iterate the keys space |
||
| 216 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 217 | * @param string $pattern pattern for keys, use * as wild card |
||
| 218 | * @param integer $count |
||
| 219 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
| 220 | */ |
||
| 221 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
| 222 | { |
||
| 223 | 4 | if ((string)$iterator === '0') { |
|
| 224 | 4 | return null; |
|
| 225 | } |
||
| 226 | 4 | $this->init(); |
|
| 227 | 4 | if ($this->driver instanceof Client) { |
|
| 228 | 2 | $returned = $this->driver->scan($iterator, ['match' => $pattern, 'count' => $count]); |
|
| 229 | 2 | $iterator = $returned[0]; |
|
| 230 | 2 | return $returned[1]; |
|
| 231 | } |
||
| 232 | 2 | return $this->driver->scan($iterator, $pattern, $count); |
|
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the value of a hash field |
||
| 237 | * @param string $key |
||
| 238 | * @param string $field |
||
| 239 | * @return string|null null if hash field is not set |
||
| 240 | */ |
||
| 241 | 16 | public function hget($key, $field) |
|
| 242 | { |
||
| 243 | 16 | $this->init(); |
|
| 244 | 16 | $result = $this->driver->hget($key, $field); |
|
| 245 | 16 | return $this->convertFalseToNull($result); |
|
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Delete one or more hash fields, returns number of deleted fields |
||
| 250 | * @param array $key |
||
| 251 | * @param array ...$fields |
||
| 252 | * @return integer |
||
| 253 | */ |
||
| 254 | 8 | public function hdel($key, ...$fields) |
|
| 255 | { |
||
| 256 | 8 | if (is_array($fields[0])) { |
|
| 257 | 4 | $fields = $fields[0]; |
|
| 258 | 2 | } |
|
| 259 | 8 | return $this->driver->hdel($key, ...$fields); |
|
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Increment the integer value of hash field by given number |
||
| 264 | * @param string $key |
||
| 265 | * @param string $field |
||
| 266 | * @param integer $increment |
||
| 267 | * @return integer |
||
| 268 | */ |
||
| 269 | 4 | public function hincrby($key, $field, $increment = 1) |
|
| 270 | { |
||
| 271 | 4 | return $this->driver->hincrby($key, $field, (int)$increment); |
|
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * Increment the float value of hash field by given amount |
||
| 276 | * @param string $key |
||
| 277 | * @param string $field |
||
| 278 | * @param float $increment |
||
| 279 | * @return float |
||
| 280 | */ |
||
| 281 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
| 282 | { |
||
| 283 | 4 | return $this->driver->hincrbyfloat($key, $field, $increment); |
|
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Set multiple values to multiple hash fields |
||
| 288 | * @param string $key |
||
| 289 | * @param array $dictionary |
||
| 290 | * @return boolean true on success |
||
| 291 | * @throws RedisProxyException if number of arguments is wrong |
||
| 292 | */ |
||
| 293 | 12 | public function hmset($key, ...$dictionary) |
|
| 294 | { |
||
| 295 | 12 | if (is_array($dictionary[0])) { |
|
| 296 | 8 | $result = $this->driver->hmset($key, ...$dictionary); |
|
| 297 | 8 | return $this->transformResult($result); |
|
| 298 | } |
||
| 299 | 8 | $dictionary = $this->prepareKeyValue($dictionary, 'hmset'); |
|
| 300 | 4 | $result = $this->driver->hmset($key, $dictionary); |
|
| 301 | 4 | return $this->transformResult($result); |
|
| 302 | } |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Incrementally iterate hash fields and associated values |
||
| 306 | * @param string $key |
||
| 307 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 308 | * @param string $pattern pattern for fields, use * as wild card |
||
| 309 | * @param integer $count |
||
| 310 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
| 311 | */ |
||
| 312 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
| 313 | { |
||
| 314 | 4 | if ((string)$iterator === '0') { |
|
| 315 | 4 | return null; |
|
| 316 | } |
||
| 317 | 4 | $this->init(); |
|
| 318 | 4 | if ($this->driver instanceof Client) { |
|
| 319 | 2 | $returned = $this->driver->hscan($key, $iterator, ['match' => $pattern, 'count' => $count]); |
|
| 320 | 2 | $iterator = $returned[0]; |
|
| 321 | 2 | return $returned[1]; |
|
| 322 | } |
||
| 323 | 2 | return $this->driver->hscan($key, $iterator, $pattern, $count); |
|
| 324 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Add one or more members to a set |
||
| 328 | * @param string $key |
||
| 329 | * @param array ...$members |
||
| 330 | * @return integer number of new members added to set |
||
| 331 | */ |
||
| 332 | 12 | public function sadd($key, ...$members) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Remove and return one or multiple random members from a set |
||
| 342 | * @param string $key |
||
| 343 | * @param integer $count number of members |
||
| 344 | * @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 |
||
| 345 | */ |
||
| 346 | 4 | public function spop($key, $count = 1) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Incrementally iterate Set elements |
||
| 366 | * @param string $key |
||
| 367 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
| 368 | * @param string $pattern pattern for member's values, use * as wild card |
||
| 369 | * @param integer $count |
||
| 370 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
| 371 | */ |
||
| 372 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
| 373 | { |
||
| 374 | 4 | if ((string)$iterator === '0') { |
|
| 375 | 4 | return null; |
|
| 376 | } |
||
| 377 | 4 | $this->init(); |
|
| 378 | 4 | if ($this->driver instanceof Client) { |
|
| 379 | 2 | $returned = $this->driver->sscan($key, $iterator, ['match' => $pattern, 'count' => $count]); |
|
| 385 | |||
| 386 | 36 | private function convertFalseToNull($result) |
|
| 390 | |||
| 391 | 108 | private function transformResult($result) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
| 401 | * @param array $dictionary |
||
| 402 | * @param string $command |
||
| 403 | * @return array |
||
| 404 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
| 405 | */ |
||
| 406 | 16 | private function prepareKeyValue(array $dictionary, $command) |
|
| 420 | } |
||
| 421 |