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 $ending; |
||
39 | |||
40 | /** |
||
41 | * @var PromiseInterface[] |
||
42 | */ |
||
43 | protected $endingPromises; |
||
44 | |||
45 | /** |
||
46 | * @var bool |
||
47 | */ |
||
48 | protected $paused; |
||
49 | |||
50 | /** |
||
51 | * @var mixed[] |
||
52 | */ |
||
53 | protected $storage; |
||
54 | |||
55 | /** |
||
56 | * @var TimerInterface[] |
||
57 | */ |
||
58 | protected $timers; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $timersCounter; |
||
64 | |||
65 | /** |
||
66 | * @param LoopInterface $loop |
||
67 | * @param mixed[] $config |
||
68 | */ |
||
69 | 24 | public function __construct(LoopInterface $loop, $config = []) |
|
82 | |||
83 | /** |
||
84 | * |
||
85 | */ |
||
86 | 13 | public function __destruct() |
|
87 | { |
||
88 | 13 | $this->stop(); |
|
89 | 13 | parent::__destruct(); |
|
90 | 13 | } |
|
91 | |||
92 | /** |
||
93 | * @override |
||
94 | * @inheritDoc |
||
95 | */ |
||
96 | public function isStarted() |
||
100 | |||
101 | /** |
||
102 | * @override |
||
103 | * @inheritDoc |
||
104 | */ |
||
105 | 11 | View Code Duplication | public function start() |
118 | |||
119 | /** |
||
120 | * @override |
||
121 | * @inheritDoc |
||
122 | */ |
||
123 | 24 | View Code Duplication | public function stop() |
124 | { |
||
125 | 24 | if (!$this->open && !$this->ending) |
|
126 | { |
||
127 | 13 | return Promise::doResolve($this); |
|
128 | } |
||
129 | |||
130 | 11 | $this->open = false; |
|
131 | 11 | $this->ending = false; |
|
132 | 11 | $this->handleStop(); |
|
133 | |||
134 | 11 | return Promise::doResolve($this); |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @override |
||
139 | * @inheritDoc |
||
140 | */ |
||
141 | 2 | public function end() |
|
160 | |||
161 | /** |
||
162 | * @override |
||
163 | * @inheritDoc |
||
164 | */ |
||
165 | public function isPaused() |
||
169 | |||
170 | /** |
||
171 | * @override |
||
172 | * @inheritDoc |
||
173 | */ |
||
174 | 11 | public function pause() |
|
183 | |||
184 | /** |
||
185 | * @override |
||
186 | * @inheritDoc |
||
187 | */ |
||
188 | 11 | public function resume() |
|
196 | |||
197 | /** |
||
198 | * @override |
||
199 | * @inheritDoc |
||
200 | */ |
||
201 | 10 | public function set($key, $val, $ttl = 0.0) |
|
202 | { |
||
203 | 10 | if (!$this->open) |
|
204 | { |
||
205 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
206 | } |
||
207 | 9 | $this->storage[$key] = $val; |
|
208 | |||
209 | 9 | if ($ttl > 0) |
|
210 | { |
||
211 | return $this->setTtl($key, $ttl)->then(function() use($val) { return $val; }); |
||
212 | } |
||
213 | 7 | return Promise::doResolve($val); |
|
214 | } |
||
215 | |||
216 | /** |
||
217 | * @override |
||
218 | * @inheritDoc |
||
219 | */ |
||
220 | 3 | public function get($key) |
|
221 | { |
||
222 | 3 | if (!$this->open) |
|
223 | { |
||
224 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
225 | } |
||
226 | 2 | return Promise::doResolve(array_key_exists($key, $this->storage) ? $this->storage[$key] : null); |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * @override |
||
231 | * @inheritDoc |
||
232 | */ |
||
233 | 6 | public function remove($key) |
|
234 | { |
||
235 | 6 | if (!$this->open && !$this->ending) |
|
236 | { |
||
237 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
238 | } |
||
239 | 5 | if (!array_key_exists($key, $this->storage)) |
|
240 | { |
||
241 | return Promise::doResolve(false); |
||
242 | } |
||
243 | 5 | unset($this->storage[$key]); |
|
244 | |||
245 | 5 | if (isset($this->timers[$key])) |
|
246 | { |
||
247 | return $this->removeTtl($key)->then(function() { return true; }); |
||
248 | } |
||
249 | 1 | return Promise::doResolve(true); |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * @override |
||
254 | * @inheritDoc |
||
255 | */ |
||
256 | 5 | View Code Duplication | public function exists($key) |
257 | { |
||
258 | 5 | if (!$this->open) |
|
259 | { |
||
260 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
261 | } |
||
262 | 4 | return Promise::doResolve(array_key_exists($key, $this->storage)); |
|
263 | } |
||
264 | |||
265 | /** |
||
266 | * @override |
||
267 | * @inheritDoc |
||
268 | */ |
||
269 | 6 | public function setTtl($key, $ttl) |
|
270 | { |
||
271 | 6 | if (!$this->open) |
|
272 | { |
||
273 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
274 | } |
||
275 | 5 | if ($ttl <= 0) { |
|
276 | return Promise::doReject(new WriteException('TTL needs to be higher than 0.')); |
||
277 | } |
||
278 | 5 | if (!array_key_exists($key, $this->storage)) |
|
279 | { |
||
280 | return Promise::doReject(new WriteException('Timeout cannot be set on undefined key.')); |
||
281 | } |
||
282 | |||
283 | 5 | $timer = round($ttl / $this->config['interval']); |
|
284 | 5 | $this->timers[$key] = [ 'timeout' => $timer, 'ttl' => $ttl ]; |
|
285 | 5 | $this->timersCounter++; |
|
286 | 5 | return Promise::doResolve($ttl); |
|
287 | } |
||
288 | |||
289 | /** |
||
290 | * @override |
||
291 | * @inheritDoc |
||
292 | */ |
||
293 | 2 | public function getTtl($key) |
|
294 | { |
||
295 | 2 | if (!$this->open) |
|
296 | { |
||
297 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
298 | } |
||
299 | 1 | if (!isset($this->timers[$key])) |
|
300 | { |
||
301 | 1 | return Promise::doResolve(0); |
|
302 | } |
||
303 | 1 | return Promise::doResolve($this->timers[$key]['ttl']); |
|
304 | } |
||
305 | |||
306 | /** |
||
307 | * @override |
||
308 | * @inheritDoc |
||
309 | */ |
||
310 | 6 | public function removeTtl($key) |
|
311 | { |
||
312 | 6 | if (!$this->open && !$this->ending) |
|
313 | { |
||
314 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
315 | } |
||
316 | 5 | if (!isset($this->timers[$key])) |
|
317 | { |
||
318 | return Promise::doResolve(false); |
||
319 | } |
||
320 | |||
321 | 5 | unset($this->timers[$key]); |
|
322 | 5 | $this->timersCounter--; |
|
323 | |||
324 | 5 | if ($this->ending && $this->timersCounter === 0) |
|
325 | { |
||
326 | return $this->stop()->then(function() { return true; }); |
||
327 | } |
||
328 | 4 | return Promise::doResolve(true); |
|
329 | } |
||
330 | |||
331 | /** |
||
332 | * @override |
||
333 | * @inheritDoc |
||
334 | */ |
||
335 | 2 | public function existsTtl($key) |
|
336 | { |
||
337 | 2 | if (!$this->open) |
|
338 | { |
||
339 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
340 | } |
||
341 | 1 | return Promise::doResolve(isset($this->timers[$key])); |
|
342 | } |
||
343 | |||
344 | /** |
||
345 | * @override |
||
346 | * @inheritDoc |
||
347 | */ |
||
348 | 2 | View Code Duplication | public function getKeys() |
349 | { |
||
350 | 2 | if (!$this->open) |
|
351 | { |
||
352 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
353 | } |
||
354 | 1 | return Promise::doResolve(array_keys($this->storage)); |
|
355 | } |
||
356 | |||
357 | /** |
||
358 | * @override |
||
359 | * @inheritDoc |
||
360 | */ |
||
361 | 1 | public function getStats() |
|
362 | { |
||
363 | 1 | if (!$this->open) |
|
364 | { |
||
365 | 1 | return Promise::doReject(new ReadException('Cache object is not open.')); |
|
366 | } |
||
367 | |||
368 | // TODO |
||
369 | return Promise::doResolve([ |
||
370 | 'keys' => 0, |
||
371 | 'hits' => 0, |
||
372 | 'misses' => 0, |
||
373 | 'ksize' => 0, |
||
374 | 'vsize' => 0, |
||
375 | ]); |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * @override |
||
380 | * @inheritDoc |
||
381 | */ |
||
382 | 1 | public function flush() |
|
383 | { |
||
384 | 1 | if (!$this->open) |
|
385 | { |
||
386 | 1 | return Promise::doReject(new WriteException('Cache object is not open.')); |
|
387 | } |
||
388 | |||
389 | $this->storage = []; |
||
390 | $this->timers = []; |
||
391 | $this->timersCounter = 0; |
||
392 | |||
393 | return Promise::doResolve(); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Create configuration. |
||
398 | * |
||
399 | * @return mixed[] |
||
400 | */ |
||
401 | 24 | protected function createConfig($config = []) |
|
405 | |||
406 | /** |
||
407 | * Handle start. |
||
408 | */ |
||
409 | 11 | protected function handleStart() |
|
414 | |||
415 | /** |
||
416 | * Handle stop. |
||
417 | */ |
||
418 | 11 | protected function handleStop() |
|
434 | |||
435 | /** |
||
436 | * Handle loop tick. |
||
437 | * |
||
438 | * @internal |
||
439 | */ |
||
440 | 4 | public function handleTick() |
|
461 | } |
||
462 |
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.