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 | { |
||
116 | |||
117 | /** |
||
118 | * @param integer $database |
||
119 | * @return boolean true on success |
||
120 | * @throws RedisProxyException on failure |
||
121 | */ |
||
122 | 108 | public function select($database) |
|
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) |
|
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) |
|
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) |
|
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) |
|
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 |