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 |
||
21 | class sqlite_cache extends cache_api |
||
22 | { |
||
23 | /** |
||
24 | * @var string The path to the current $cachedir directory. |
||
25 | */ |
||
26 | private $cachedir = null; |
||
27 | |||
28 | /** |
||
29 | * @var SQLite3 |
||
30 | */ |
||
31 | private $cacheDB = null; |
||
32 | |||
33 | /** |
||
34 | * @var int |
||
35 | */ |
||
36 | private $cacheTime = 0; |
||
37 | |||
38 | public function __construct() |
||
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | public function connect() |
||
63 | |||
64 | /** |
||
65 | * {@inheritDoc} |
||
66 | */ |
||
67 | View Code Duplication | public function isSupported($test = false) |
|
76 | |||
77 | /** |
||
78 | * {@inheritDoc} |
||
79 | */ |
||
80 | public function getData($key, $ttl = null) |
||
92 | |||
93 | /** |
||
94 | * {@inheritDoc} |
||
95 | */ |
||
96 | public function putData($key, $value, $ttl = null) |
||
105 | |||
106 | /** |
||
107 | * {@inheritDoc} |
||
108 | */ |
||
109 | public function cleanCache($type = '') |
||
123 | |||
124 | /** |
||
125 | * {@inheritDoc} |
||
126 | */ |
||
127 | View Code Duplication | public function cacheSettings(array &$config_vars) |
|
143 | |||
144 | /** |
||
145 | * Sets the $cachedir or uses the SMF default $cachedir.. |
||
146 | * |
||
147 | * @access public |
||
148 | * |
||
149 | * @param string $dir A valid path |
||
150 | * |
||
151 | * @return boolean If this was successful or not. |
||
152 | */ |
||
153 | public function setCachedir($dir = null) |
||
166 | |||
167 | /** |
||
168 | * {@inheritDoc} |
||
169 | */ |
||
170 | public function getVersion() |
||
175 | |||
176 | /** |
||
177 | * {@inheritDoc} |
||
178 | */ |
||
179 | public function housekeeping() |
||
183 | } |
||
184 | |||
186 |
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.