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 | 64 | public function __construct($host, $port, $timeout = null) |
|
55 | |||
56 | 64 | public function setDriversOrder(array $driversOrder) |
|
69 | |||
70 | 60 | private function init() |
|
75 | |||
76 | 60 | private function prepareDriver() |
|
94 | |||
95 | 60 | private function connect($host, $port, $timeout = null) |
|
99 | |||
100 | 60 | private function isConnected() |
|
104 | |||
105 | 60 | public function __call($name, $arguments) |
|
111 | |||
112 | /** |
||
113 | * @param integer $database |
||
114 | * @return boolean true on success |
||
115 | * @throws RedisProxyException on failure |
||
116 | */ |
||
117 | 60 | public function select($database) |
|
138 | |||
139 | /** |
||
140 | * @param string|null $section |
||
141 | * @return array |
||
142 | */ |
||
143 | 8 | public function info($section = null) |
|
159 | |||
160 | /** |
||
161 | * @param string $key |
||
162 | * @return string|null null if hash field is not set |
||
163 | */ |
||
164 | 12 | public function get($key) |
|
170 | |||
171 | /** |
||
172 | * Delete a key(s) |
||
173 | * @param array ...$keys |
||
174 | * @return integer number of deleted keys |
||
175 | */ |
||
176 | 12 | public function del(...$keys) |
|
181 | |||
182 | /** |
||
183 | * Delete a key(s) |
||
184 | * @param array ...$keys |
||
185 | * @return integer number of deleted keys |
||
186 | */ |
||
187 | 8 | public function delete(...$keys) |
|
191 | |||
192 | /** |
||
193 | * Incrementally iterate the keys space |
||
194 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
195 | * @param string $pattern pattern for keys, use * as wild card |
||
196 | * @param integer $count |
||
197 | * @return array|null list of found keys, returns null if $iterator is 0 or '0' |
||
198 | */ |
||
199 | View Code Duplication | public function scan(&$iterator, $pattern = null, $count = null) |
|
212 | |||
213 | /** |
||
214 | * Get the value of a hash field |
||
215 | * @param string $key |
||
216 | * @param string $field |
||
217 | * @return string|null null if hash field is not set |
||
218 | */ |
||
219 | 4 | public function hget($key, $field) |
|
225 | |||
226 | /** |
||
227 | * Delete one or more hash fields, returns number of deleted fields |
||
228 | * @param array ...$key |
||
229 | * @param array $fields |
||
230 | * @return integer |
||
231 | */ |
||
232 | 8 | public function hdel($key, ...$fields) |
|
241 | |||
242 | /** |
||
243 | * Incrementally iterate hash fields and associated values |
||
244 | * @param string $key |
||
245 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
246 | * @param string $pattern pattern for fields, use * as wild card |
||
247 | * @param integer $count |
||
248 | * @return array|null list of found fields with associated values, returns null if $iterator is 0 or '0' |
||
249 | */ |
||
250 | View Code Duplication | public function hscan($key, &$iterator, $pattern = null, $count = null) |
|
263 | |||
264 | /** |
||
265 | * Add one or more members to a set |
||
266 | * @param string $key |
||
267 | * @param array ...$members |
||
268 | * @return integer number of new members added to set |
||
269 | */ |
||
270 | 12 | public function sadd($key, ...$members) |
|
277 | |||
278 | /** |
||
279 | * Remove and return one or multiple random members from a set |
||
280 | * @param string $key |
||
281 | * @param integer $count number of members |
||
282 | * @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 |
||
283 | */ |
||
284 | 4 | public function spop($key, $count = 1) |
|
301 | |||
302 | /** |
||
303 | * Incrementally iterate Set elements |
||
304 | * @param string $key |
||
305 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
306 | * @param string $pattern pattern for member's values, use * as wild card |
||
307 | * @param integer $count |
||
308 | * @return array|null list of found members, returns null if $iterator is 0 or '0' |
||
309 | */ |
||
310 | 4 | View Code Duplication | public function sscan($key, &$iterator, $pattern = null, $count = null) |
323 | |||
324 | /** |
||
325 | * Incrementally iterate sorted sets elements and associated scores |
||
326 | * @param string $key |
||
327 | * @param mixed $iterator iterator / cursor, use $iterator = null for start scanning, when $iterator is changed to 0 or '0', scanning is finished |
||
328 | * @param string $pattern pattern for element's values, use * as wild card |
||
329 | * @param integer $count |
||
330 | * @return array|null list of found elements, returns null if $iterator is 0 or '0' |
||
331 | */ |
||
332 | View Code Duplication | public function zscan($key, &$iterator, $pattern = null, $count = null) |
|
345 | |||
346 | 20 | private function convertFalseToNull($result) |
|
350 | |||
351 | 60 | private function transformResult($result) |
|
358 | } |
||
359 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.