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 Cache 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 Cache, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Cache extends BaseEventEmitter implements CacheInterface |
||
17 | { |
||
18 | use LoopAwareTrait; |
||
19 | |||
20 | /** |
||
21 | * @var TimerInterface |
||
22 | */ |
||
23 | protected $loopTimer; |
||
24 | |||
25 | /** |
||
26 | * @var mixed[] |
||
27 | */ |
||
28 | protected $config; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | protected $open; |
||
34 | |||
35 | /** |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $paused; |
||
39 | |||
40 | /** |
||
41 | * @var mixed[] |
||
42 | */ |
||
43 | protected $storage; |
||
44 | |||
45 | /** |
||
46 | * @var TimerInterface[] |
||
47 | */ |
||
48 | protected $timers; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $timersCounter; |
||
54 | |||
55 | /** |
||
56 | * @var mixed[] |
||
57 | */ |
||
58 | protected $stats; |
||
59 | |||
60 | /** |
||
61 | * @param LoopInterface $loop |
||
62 | * @param mixed[] $config |
||
63 | */ |
||
64 | 29 | public function __construct(LoopInterface $loop, $config = []) |
|
76 | |||
77 | /** |
||
78 | * |
||
79 | */ |
||
80 | 13 | public function __destruct() |
|
85 | |||
86 | /** |
||
87 | * @override |
||
88 | * @inheritDoc |
||
89 | */ |
||
90 | public function isStarted() |
||
94 | |||
95 | /** |
||
96 | * @override |
||
97 | * @inheritDoc |
||
98 | */ |
||
99 | 16 | public function start() |
|
111 | |||
112 | /** |
||
113 | * @override |
||
114 | * @inheritDoc |
||
115 | */ |
||
116 | 29 | public function stop() |
|
128 | |||
129 | /** |
||
130 | * @override |
||
131 | * @inheritDoc |
||
132 | */ |
||
133 | 2 | public function end() |
|
137 | |||
138 | /** |
||
139 | * @override |
||
140 | * @inheritDoc |
||
141 | */ |
||
142 | public function isPaused() |
||
146 | |||
147 | /** |
||
148 | * @override |
||
149 | * @inheritDoc |
||
150 | */ |
||
151 | 16 | public function pause() |
|
160 | |||
161 | /** |
||
162 | * @override |
||
163 | * @inheritDoc |
||
164 | */ |
||
165 | 16 | public function resume() |
|
173 | |||
174 | /** |
||
175 | * @override |
||
176 | * @inheritDoc |
||
177 | */ |
||
178 | 15 | public function set($key, $val, $ttl = 0.0) |
|
197 | |||
198 | /** |
||
199 | * @override |
||
200 | * @inheritDoc |
||
201 | */ |
||
202 | 6 | public function get($key) |
|
210 | |||
211 | /** |
||
212 | * @override |
||
213 | * @inheritDoc |
||
214 | */ |
||
215 | 5 | public function remove($key) |
|
233 | |||
234 | /** |
||
235 | * @override |
||
236 | * @inheritDoc |
||
237 | */ |
||
238 | 5 | View Code Duplication | public function exists($key) |
246 | |||
247 | /** |
||
248 | * @override |
||
249 | * @inheritDoc |
||
250 | */ |
||
251 | 6 | public function setTtl($key, $ttl) |
|
270 | |||
271 | /** |
||
272 | * @override |
||
273 | * @inheritDoc |
||
274 | */ |
||
275 | 2 | public function getTtl($key) |
|
287 | |||
288 | /** |
||
289 | * @override |
||
290 | * @inheritDoc |
||
291 | */ |
||
292 | 5 | public function removeTtl($key) |
|
308 | |||
309 | /** |
||
310 | * @override |
||
311 | * @inheritDoc |
||
312 | */ |
||
313 | 2 | public function existsTtl($key) |
|
321 | |||
322 | /** |
||
323 | * @override |
||
324 | * @inheritDoc |
||
325 | */ |
||
326 | 2 | View Code Duplication | public function getKeys() |
337 | |||
338 | /** |
||
339 | * @override |
||
340 | * @inheritDoc |
||
341 | */ |
||
342 | 2 | View Code Duplication | public function getStats() |
352 | |||
353 | /** |
||
354 | * @override |
||
355 | * @inheritDoc |
||
356 | */ |
||
357 | 15 | public function flush() |
|
370 | |||
371 | /** |
||
372 | * Create configuration. |
||
373 | * |
||
374 | * @return mixed[] |
||
375 | */ |
||
376 | 29 | protected function createConfig($config = []) |
|
380 | |||
381 | /** |
||
382 | * Create stats. |
||
383 | * |
||
384 | * @return mixed[] |
||
385 | */ |
||
386 | 29 | protected function createStats() |
|
390 | |||
391 | /** |
||
392 | * Handle start. |
||
393 | */ |
||
394 | 16 | protected function handleStart() |
|
399 | |||
400 | /** |
||
401 | * Handle stop. |
||
402 | */ |
||
403 | 16 | protected function handleStop() |
|
412 | |||
413 | /** |
||
414 | * Handle loop tick. |
||
415 | * |
||
416 | * @internal |
||
417 | */ |
||
418 | 3 | public function handleTick() |
|
439 | } |
||
440 |
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.