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 | /** |
||
57 | * Set driver priorities - default is 1. redis, 2. predis |
||
58 | * @param array $driversOrder |
||
59 | * @return RedisProxy |
||
60 | * @throws RedisProxyException if some driver is not supported |
||
61 | */ |
||
62 | 112 | public function setDriversOrder(array $driversOrder) |
|
72 | |||
73 | 110 | private function init() |
|
78 | |||
79 | 110 | private function prepareDriver() |
|
97 | |||
98 | 108 | private function connect($host, $port, $timeout = null) |
|
102 | |||
103 | 108 | private function isConnected() |
|
107 | |||
108 | 110 | public function __call($name, $arguments) |
|
119 | |||
120 | /** |
||
121 | * @param integer $database |
||
122 | * @return boolean true on success |
||
123 | * @throws RedisProxyException on failure |
||
124 | */ |
||
125 | 108 | public function select($database) |
|
146 | |||
147 | /** |
||
148 | * @param string|null $section |
||
149 | * @return array |
||
150 | */ |
||
151 | 8 | public function info($section = null) |
|
167 | |||
168 | /** |
||
169 | * Set multiple values to multiple keys |
||
170 | * @param array $dictionary |
||
171 | * @return boolean true on success |
||
172 | * @throws RedisProxyException if number of arguments is wrong |
||
173 | */ |
||
174 | 12 | View Code Duplication | public function mset(...$dictionary) |
185 | |||
186 | /** |
||
187 | * @param string $key |
||
188 | * @return string|null null if hash field is not set |
||
189 | */ |
||
190 | 16 | public function get($key) |
|
196 | |||
197 | /** |
||
198 | * Delete a key(s) |
||
199 | * @param array $keys |
||
200 | * @return integer number of deleted keys |
||
201 | */ |
||
202 | 20 | public function del(...$keys) |
|
207 | |||
208 | /** |
||
209 | * Delete a key(s) |
||
210 | * @param array $keys |
||
211 | * @return integer number of deleted keys |
||
212 | */ |
||
213 | 8 | public function delete(...$keys) |
|
217 | |||
218 | /** |
||
219 | * Incrementally iterate the keys space |
||
220 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
221 | * @param string $pattern pattern for keys, use * as wild card |
||
222 | * @param integer $count |
||
223 | * @return array|boolean|null list of found keys, returns null if $iterator is 0 or '0' |
||
224 | */ |
||
225 | 4 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
238 | |||
239 | /** |
||
240 | * Get the value of a hash field |
||
241 | * @param string $key |
||
242 | * @param string $field |
||
243 | * @return string|null null if hash field is not set |
||
244 | */ |
||
245 | 16 | public function hget($key, $field) |
|
251 | |||
252 | /** |
||
253 | * Delete one or more hash fields, returns number of deleted fields |
||
254 | * @param array $key |
||
255 | * @param array $fields |
||
256 | * @return integer |
||
257 | */ |
||
258 | 8 | public function hdel($key, ...$fields) |
|
266 | |||
267 | /** |
||
268 | * Increment the integer value of hash field by given number |
||
269 | * @param string $key |
||
270 | * @param string $field |
||
271 | * @param integer $increment |
||
272 | * @return integer |
||
273 | */ |
||
274 | 4 | public function hincrby($key, $field, $increment = 1) |
|
279 | |||
280 | /** |
||
281 | * Increment the float value of hash field by given amount |
||
282 | * @param string $key |
||
283 | * @param string $field |
||
284 | * @param float $increment |
||
285 | * @return float |
||
286 | */ |
||
287 | 4 | public function hincrbyfloat($key, $field, $increment = 1) |
|
292 | |||
293 | /** |
||
294 | * Set multiple values to multiple hash fields |
||
295 | * @param string $key |
||
296 | * @param array $dictionary |
||
297 | * @return boolean true on success |
||
298 | * @throws RedisProxyException if number of arguments is wrong |
||
299 | */ |
||
300 | 12 | View Code Duplication | public function hmset($key, ...$dictionary) |
311 | |||
312 | /** |
||
313 | * Incrementally iterate hash fields and associated values |
||
314 | * @param string $key |
||
315 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
316 | * @param string $pattern pattern for fields, use * as wild card |
||
317 | * @param integer $count |
||
318 | * @return array|boolean|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
319 | */ |
||
320 | 4 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
333 | |||
334 | /** |
||
335 | * Add one or more members to a set |
||
336 | * @param string $key |
||
337 | * @param array $members |
||
338 | * @return integer number of new members added to set |
||
339 | */ |
||
340 | 12 | public function sadd($key, ...$members) |
|
348 | |||
349 | /** |
||
350 | * Remove and return one or multiple random members from a set |
||
351 | * @param string $key |
||
352 | * @param integer $count number of members |
||
353 | * @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 |
||
354 | */ |
||
355 | 4 | public function spop($key, $count = 1) |
|
373 | |||
374 | /** |
||
375 | * Incrementally iterate Set elements |
||
376 | * @param string $key |
||
377 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
378 | * @param string $pattern pattern for member's values, use * as wild card |
||
379 | * @param integer $count |
||
380 | * @return array|boolean|null list of found members, returns null if $iterator is 0 or '0' |
||
381 | */ |
||
382 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
395 | |||
396 | 36 | private function convertFalseToNull($result) |
|
400 | |||
401 | 108 | private function transformResult($result) |
|
408 | |||
409 | /** |
||
410 | * Create array from input array - odd keys are used as keys, even keys are used as values |
||
411 | * @param array $dictionary |
||
412 | * @param string $command |
||
413 | * @return array |
||
414 | * @throws RedisProxyException if number of keys is not the same as number of values |
||
415 | */ |
||
416 | 16 | private function prepareKeyValue(array $dictionary, $command) |
|
430 | } |
||
431 |