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 RedisCache 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 RedisCache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | class RedisCache extends BaseEventEmitter implements CacheInterface |
||
19 | { |
||
20 | use LoopAwareTrait; |
||
21 | |||
22 | /** |
||
23 | * @var mixed[] |
||
24 | */ |
||
25 | protected $config; |
||
26 | |||
27 | /** |
||
28 | * @var RedisInterface |
||
29 | */ |
||
30 | protected $redis; |
||
31 | |||
32 | /** |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $selected; |
||
36 | |||
37 | /** |
||
38 | * @param LoopInterface $loop |
||
39 | * @param mixed[] $config |
||
40 | */ |
||
41 | public function __construct(LoopInterface $loop, $config = []) |
||
50 | |||
51 | /** |
||
52 | * |
||
53 | */ |
||
54 | public function __destruct() |
||
59 | |||
60 | /** |
||
61 | * @override |
||
62 | * @inheritDoc |
||
63 | */ |
||
64 | 11 | public function isStarted() |
|
68 | |||
69 | /** |
||
70 | * @override |
||
71 | * @inheritDoc |
||
72 | */ |
||
73 | public function start() |
||
77 | |||
78 | /** |
||
79 | * @override |
||
80 | * @inheritDoc |
||
81 | */ |
||
82 | public function stop() |
||
86 | |||
87 | /** |
||
88 | * @override |
||
89 | * @inheritDoc |
||
90 | */ |
||
91 | public function end() |
||
95 | |||
96 | /** |
||
97 | * @override |
||
98 | * @inheritDoc |
||
99 | */ |
||
100 | public function isPaused() |
||
104 | |||
105 | /** |
||
106 | * @override |
||
107 | * @inheritDoc |
||
108 | */ |
||
109 | public function pause() |
||
113 | |||
114 | /** |
||
115 | * @override |
||
116 | * @inheritDoc |
||
117 | */ |
||
118 | public function resume() |
||
122 | |||
123 | /** |
||
124 | * @override |
||
125 | * @inheritDoc |
||
126 | */ |
||
127 | 1 | public function set($key, $val, $ttl = 0.0) |
|
150 | |||
151 | /** |
||
152 | * @override |
||
153 | * @inheritDoc |
||
154 | */ |
||
155 | 1 | View Code Duplication | public function get($key) |
165 | |||
166 | /** |
||
167 | * @override |
||
168 | * @inheritDoc |
||
169 | */ |
||
170 | 1 | View Code Duplication | public function remove($key) |
180 | |||
181 | /** |
||
182 | * @override |
||
183 | * @inheritDoc |
||
184 | */ |
||
185 | 1 | View Code Duplication | public function exists($key) |
195 | |||
196 | /** |
||
197 | * @override |
||
198 | * @inheritDoc |
||
199 | */ |
||
200 | 1 | public function setTtl($key, $ttl) |
|
218 | |||
219 | /** |
||
220 | * @override |
||
221 | * @inheritDoc |
||
222 | */ |
||
223 | 1 | View Code Duplication | public function getTtl($key) |
233 | |||
234 | /** |
||
235 | * @override |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | 1 | View Code Duplication | public function removeTtl($key) |
248 | |||
249 | /** |
||
250 | * @override |
||
251 | * @inheritDoc |
||
252 | */ |
||
253 | 1 | View Code Duplication | public function existsTtl($key) |
263 | |||
264 | /** |
||
265 | * @override |
||
266 | * @inheritDoc |
||
267 | */ |
||
268 | 1 | View Code Duplication | public function getKeys() |
279 | |||
280 | /** |
||
281 | * @override |
||
282 | * @inheritDoc |
||
283 | */ |
||
284 | 1 | public function getStats() |
|
300 | |||
301 | /** |
||
302 | * @override |
||
303 | * @inheritDoc |
||
304 | */ |
||
305 | 1 | public function flush() |
|
313 | |||
314 | /** |
||
315 | * Create configuration. |
||
316 | * |
||
317 | * @return mixed[] |
||
318 | */ |
||
319 | protected function createConfig($config = []) |
||
323 | |||
324 | /** |
||
325 | * Create Redis client. |
||
326 | * |
||
327 | * @return RedisInterface |
||
328 | */ |
||
329 | protected function createRedis() |
||
333 | |||
334 | /** |
||
335 | * Parse and return endpoint. |
||
336 | * |
||
337 | * @param string $endpoint |
||
338 | * @return string |
||
339 | */ |
||
340 | protected function createEndpoint($endpoint) |
||
348 | |||
349 | /** |
||
350 | * Attach events. |
||
351 | */ |
||
352 | private function attachEvents() |
||
364 | } |
||
365 |
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.