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 |
||
23 | class Dba extends Storage |
||
24 | { |
||
25 | /** |
||
26 | * @var resource |
||
27 | */ |
||
28 | protected $dba; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $handler; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | protected $file; |
||
39 | |||
40 | /** |
||
41 | * @param string $file the cache-file. |
||
42 | * |
||
43 | * @param string $handler the dba handler. |
||
44 | * |
||
45 | * You have to install one of this handlers before use. |
||
46 | * |
||
47 | * cdb = Tiny Constant Database - for reading. |
||
48 | * cdb_make = Tiny Constant Database - for writing. |
||
49 | * db4 = Oracle Berkeley DB 4 - for reading and writing. |
||
50 | * qdbm = Quick Database Manager - for reading and writing. |
||
51 | * gdbm = GNU Database Manager - for reading and writing. |
||
52 | * flatfile = default dba extension - for reading and writing. |
||
53 | * |
||
54 | * Use flatfile-handler only when you cannot install one, |
||
55 | * of the libraries required by the other handlers, |
||
56 | * and when you cannot use bundled cdb handler. |
||
57 | * |
||
58 | * @param string $mode For read/write access, database creation if it doesn't currently exist. |
||
59 | * |
||
60 | * @param boolean $persistently |
||
61 | * |
||
62 | * @throws \RuntimeException If no DBA extension or handler installed. |
||
63 | */ |
||
64 | public function __construct($file, $handler = 'flatfile', $mode = 'c', $persistently = true) |
||
79 | |||
80 | /** |
||
81 | * Closes an open dba resource |
||
82 | * |
||
83 | * @return void |
||
84 | */ |
||
85 | public function __destruct() |
||
92 | |||
93 | /** |
||
94 | * @param string $key |
||
95 | * @param mixed $value |
||
96 | * @param int $minutes |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | public function put($key, $value, $minutes) |
||
114 | |||
115 | /** |
||
116 | * @param string $key |
||
117 | * @param null $default |
||
118 | * |
||
119 | * @return bool|mixed|null |
||
120 | */ |
||
121 | public function get($key, $default = null) |
||
133 | |||
134 | /** |
||
135 | * @param string $key |
||
136 | * |
||
137 | * @return bool|mixed |
||
138 | */ |
||
139 | View Code Duplication | protected function retrieve($key) |
|
155 | |||
156 | /** |
||
157 | * @param string $key |
||
158 | * |
||
159 | * @return boolean |
||
160 | */ |
||
161 | public function forget($key) |
||
169 | |||
170 | /** |
||
171 | * @param string $key |
||
172 | * |
||
173 | * @return boolean |
||
174 | */ |
||
175 | public function has($key) |
||
179 | |||
180 | /** |
||
181 | * Write an item to the cache for five years. |
||
182 | * |
||
183 | * @param $key |
||
184 | * @param $value |
||
185 | * |
||
186 | * @return boolean |
||
187 | */ |
||
188 | public function forever($key, $value) |
||
192 | |||
193 | /** |
||
194 | * Cleans and optimizes the cache from all expired entries. |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | public function clean() |
||
210 | |||
211 | /** |
||
212 | * Flush the whole storage. |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function flush() |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | public function getFile() |
||
241 | } |
||
242 |
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.