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:
1 | <?php namespace Myth\Bay; |
||
3 | class CI3Cache implements CacheInterface { |
||
4 | |||
5 | protected $ci; |
||
6 | |||
7 | //-------------------------------------------------------------------- |
||
8 | |||
9 | public function __construct() |
||
13 | |||
14 | //-------------------------------------------------------------------- |
||
15 | |||
16 | |||
17 | /** |
||
18 | * Provides a way to retrieve a single item from |
||
19 | * the cache. |
||
20 | * |
||
21 | * Used by the Bay to get the latest cached version of |
||
22 | * the view. |
||
23 | * |
||
24 | * @param $key |
||
25 | * @return string|false |
||
26 | */ |
||
27 | View Code Duplication | public function get($key) |
|
37 | |||
38 | //-------------------------------------------------------------------- |
||
39 | |||
40 | /** |
||
41 | * Provides a way to set a single cache item. |
||
42 | * |
||
43 | * @param string $key |
||
44 | * @param string $content |
||
45 | * @param int $ttl // Time in _minutes_ |
||
46 | * @return mixed |
||
47 | */ |
||
48 | View Code Duplication | public function set($key, $content, $ttl) |
|
58 | |||
59 | //-------------------------------------------------------------------- |
||
60 | } |
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.