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 |
||
26 | class RedisProxy |
||
27 | { |
||
28 | const DRIVER_REDIS = 'redis'; |
||
29 | |||
30 | const DRIVER_PREDIS = 'predis'; |
||
31 | |||
32 | const TYPE_STRING = 'string'; |
||
33 | |||
34 | const TYPE_SET = 'set'; |
||
35 | |||
36 | const TYPE_HASH = 'hash'; |
||
37 | |||
38 | const TYPE_LIST = 'list'; |
||
39 | |||
40 | const TYPE_SORTED_SET = 'sorted_set'; |
||
41 | |||
42 | private $driver; |
||
43 | |||
44 | private $host; |
||
45 | |||
46 | private $port; |
||
47 | |||
48 | private $database = 0; |
||
49 | |||
50 | private $selectedDatabase = 0; |
||
51 | |||
52 | private $timeout; |
||
53 | |||
54 | private $supportedDrivers = [ |
||
55 | self::DRIVER_REDIS, |
||
56 | self::DRIVER_PREDIS, |
||
57 | ]; |
||
58 | |||
59 | private $driversOrder = []; |
||
60 | |||
61 | 172 | public function __construct($host, $port, $database = 0, $timeout = null) |
|
69 | |||
70 | /** |
||
71 | * Set driver priorities - default is 1. redis, 2. predis |
||
72 | * @param array $driversOrder |
||
73 | * @return RedisProxy |
||
74 | * @throws RedisProxyException if some driver is not supported |
||
75 | */ |
||
76 | 172 | public function setDriversOrder(array $driversOrder) |
|
86 | |||
87 | 170 | private function init() |
|
92 | |||
93 | 170 | private function prepareDriver() |
|
111 | |||
112 | /** |
||
113 | * @return string|null |
||
114 | */ |
||
115 | 170 | public function actualDriver() |
|
125 | |||
126 | 168 | private function connect($host, $port, $timeout = null) |
|
130 | |||
131 | 168 | private function isConnected() |
|
135 | |||
136 | 170 | public function __call($name, $arguments) |
|
147 | |||
148 | /** |
||
149 | * @param int $database |
||
150 | * @return boolean true on success |
||
151 | * @throws RedisProxyException on failure |
||
152 | */ |
||
153 | 168 | public function select($database) |
|
175 | |||
176 | /** |
||
177 | * @param string $key |
||
178 | * @return string|null |
||
179 | */ |
||
180 | 4 | public function type($key) |
|
212 | |||
213 | /** |
||
214 | * @param string|null $section |
||
215 | * @return array |
||
216 | */ |
||
217 | 8 | public function info($section = null) |
|
233 | |||
234 | /** |
||
235 | * @param string $key |
||
236 | * @return string|null null if hash field is not set |
||
237 | */ |
||
238 | 16 | public function get($key) |
|
244 | |||
245 | /** |
||
246 | * Delete a key(s) |
||
247 | * @param array $keys |
||
248 | * @return int number of deleted keys |
||
249 | */ |
||
250 | 28 | public function del(...$keys) |
|
256 | |||
257 | /** |
||
258 | * Delete a key(s) |
||
259 | * @param array $keys |
||
260 | * @return int number of deleted keys |
||
261 | */ |
||
262 | 12 | public function delete(...$keys) |
|
266 | |||
267 | /** |
||
268 | * Set multiple values to multiple keys |
||
269 | * @param array $dictionary |
||
270 | * @return boolean true on success |
||
271 | * @throws RedisProxyException if number of arguments is wrong |
||
272 | */ |
||
273 | 12 | View Code Duplication | public function mset(...$dictionary) |
284 | |||
285 | /** |
||
286 | * Multi get |
||
287 | * @param array $keys |
||
288 | * @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 |
||
289 | */ |
||
290 | 4 | public function mget(...$keys) |
|
300 | |||
301 | /** |
||
302 | * Incrementally iterate the keys space |
||
303 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
304 | * @param string $pattern pattern for keys, use * as wild card |
||
305 | * @param int $count |
||
306 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
307 | */ |
||
308 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
321 | |||
322 | /** |
||
323 | * Get the value of a hash field |
||
324 | * @param string $key |
||
325 | * @param string $field |
||
326 | * @return string|null null if hash field is not set |
||
327 | */ |
||
328 | 16 | public function hget($key, $field) |
|
334 | |||
335 | /** |
||
336 | * Delete one or more hash fields, returns number of deleted fields |
||
337 | * @param array $key |
||
338 | * @param array $fields |
||
339 | * @return int |
||
340 | */ |
||
341 | 8 | public function hdel($key, ...$fields) |
|
347 | |||
348 | /** |
||
349 | * Increment the integer value of hash field by given number |
||
350 | * @param string $key |
||
351 | * @param string $field |
||
352 | * @param int $increment |
||
353 | * @return int |
||
354 | */ |
||
355 | 4 | public function hincrby($key, $field, $increment = 1) |
|
360 | |||
361 | /** |
||
362 | * Increment the float value of hash field by given amount |
||
363 | * @param string $key |
||
364 | * @param string $field |
||
365 | * @param float $increment |
||
366 | * @return float |
||
367 | */ |
||
368 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
373 | |||
374 | /** |
||
375 | * Set multiple values to multiple hash fields |
||
376 | * @param string $key |
||
377 | * @param array $dictionary |
||
378 | * @return boolean true on success |
||
379 | * @throws RedisProxyException if number of arguments is wrong |
||
380 | */ |
||
381 | 12 | View Code Duplication | public function hmset($key, ...$dictionary) |
392 | |||
393 | /** |
||
394 | * Multi hash get |
||
395 | * @param string $key |
||
396 | * @param array $fields |
||
397 | * @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 |
||
398 | */ |
||
399 | 4 | public function hmget($key, ...$fields) |
|
409 | |||
410 | /** |
||
411 | * Incrementally iterate hash fields and associated values |
||
412 | * @param string $key |
||
413 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
414 | * @param string $pattern pattern for fields, use * as wild card |
||
415 | * @param int $count |
||
416 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
417 | */ |
||
418 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
431 | |||
432 | /** |
||
433 | * Add one or more members to a set |
||
434 | * @param string $key |
||
435 | * @param array $members |
||
436 | * @return int number of new members added to set |
||
437 | */ |
||
438 | 16 | public function sadd($key, ...$members) |
|
444 | |||
445 | /** |
||
446 | * Remove and return one or multiple random members from a set |
||
447 | * @param string $key |
||
448 | * @param int $count number of members |
||
449 | * @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 |
||
450 | */ |
||
451 | 4 | public function spop($key, $count = 1) |
|
469 | |||
470 | /** |
||
471 | * Incrementally iterate Set elements |
||
472 | * @param string $key |
||
473 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
474 | * @param string $pattern pattern for member's values, use * as wild card |
||
475 | * @param int $count |
||
476 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
477 | */ |
||
478 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
491 | |||
492 | /** |
||
493 | * Prepend one or multiple values to a list |
||
494 | * @param string $key |
||
495 | * @param array $elements |
||
496 | * @return int the length of the list after the push operations |
||
497 | */ |
||
498 | 28 | public function lpush($key, ...$elements) |
|
504 | |||
505 | /** |
||
506 | * Append one or multiple values to a list |
||
507 | * @param string $key |
||
508 | * @param array $elements |
||
509 | * @return int the length of the list after the push operations |
||
510 | */ |
||
511 | 12 | public function rpush($key, ...$elements) |
|
517 | |||
518 | /** |
||
519 | * Remove and get the first element in a list |
||
520 | * @param string $key |
||
521 | * @return string|null |
||
522 | */ |
||
523 | 4 | public function lpop($key) |
|
529 | |||
530 | /** |
||
531 | * Remove and get the last element in a list |
||
532 | * @param string $key |
||
533 | * @return string|null |
||
534 | */ |
||
535 | 4 | public function rpop($key) |
|
541 | |||
542 | /** |
||
543 | * Get an element from a list by its index |
||
544 | * @param string $key |
||
545 | * @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 |
||
546 | * @return string|null |
||
547 | */ |
||
548 | 12 | public function lindex($key, $index) |
|
554 | |||
555 | /** |
||
556 | * Returns null instead of false for Redis driver |
||
557 | * @param mixed $result |
||
558 | * @return mixed |
||
559 | */ |
||
560 | 56 | private function convertFalseToNull($result) |
|
564 | |||
565 | /** |
||
566 | * Transforms Predis result Payload to boolean |
||
567 | * @param mixed $result |
||
568 | * @return mixed |
||
569 | */ |
||
570 | 168 | private function transformResult($result) |
|
577 | |||
578 | /** |
||
579 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
580 | * @param array $dictionary |
||
581 | * @param string $command |
||
582 | * @return array |
||
583 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
584 | */ |
||
585 | 16 | private function prepareKeyValue(array $dictionary, $command) |
|
599 | |||
600 | 84 | private function prepareArguments($command, ...$params) |
|
610 | } |
||
611 |