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 |
||
38 | class PMF_DB_Mysqli implements PMF_DB_Driver |
||
39 | { |
||
40 | /** |
||
41 | * The connection object. |
||
42 | * |
||
43 | * @var mysqli |
||
44 | */ |
||
45 | private $conn = false; |
||
46 | |||
47 | /** |
||
48 | * The query log string. |
||
49 | * |
||
50 | * @var string |
||
51 | */ |
||
52 | private $sqllog = ''; |
||
53 | |||
54 | /** |
||
55 | * Tables. |
||
56 | * |
||
57 | * @var array |
||
58 | */ |
||
59 | public $tableNames = []; |
||
60 | |||
61 | /** |
||
62 | * Connects to the database. |
||
63 | * |
||
64 | * @param string $host Hostname or path to socket |
||
65 | * @param string $user Username |
||
66 | * @param string $password Password |
||
67 | * @param string $database Database name |
||
68 | * |
||
69 | * @throws PMF_Exception |
||
70 | * |
||
71 | * @return bool true, if connected, otherwise false |
||
72 | */ |
||
73 | public function connect($host, $user, $password, $database = '') |
||
101 | |||
102 | /** |
||
103 | * This function sends a query to the database. |
||
104 | * |
||
105 | * @param string $query |
||
106 | * @param int $offset |
||
107 | * @param int $rowCount |
||
108 | * |
||
109 | * @return mysqli_result $result |
||
110 | */ |
||
111 | View Code Duplication | public function query($query, $offset = 0, $rowCount = 0) |
|
129 | |||
130 | /** |
||
131 | * Escapes a string for use in a query. |
||
132 | * |
||
133 | * @param string |
||
134 | * |
||
135 | * @return string |
||
136 | */ |
||
137 | public function escape($string) |
||
141 | |||
142 | /** |
||
143 | * Fetch a result row as an object. |
||
144 | * |
||
145 | * This function fetches a result row as an object. |
||
146 | * |
||
147 | * @param mixed $result |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function fetchObject($result) |
||
155 | |||
156 | /** |
||
157 | * Fetch a result row as an object. |
||
158 | * |
||
159 | * This function fetches a result as an associative array. |
||
160 | * |
||
161 | * @param mixed $result |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | public function fetchArray($result) |
||
169 | |||
170 | /** |
||
171 | * Fetches a complete result as an object. |
||
172 | * |
||
173 | * @param resource $result Resultset |
||
174 | * |
||
175 | * @throws Exception |
||
176 | * |
||
177 | * @return array |
||
178 | */ |
||
179 | View Code Duplication | public function fetchAll($result) |
|
192 | |||
193 | /** |
||
194 | * Number of rows in a result. |
||
195 | * |
||
196 | * @param mixed $result |
||
197 | * |
||
198 | * @return int |
||
199 | */ |
||
200 | public function numRows($result) |
||
208 | |||
209 | /** |
||
210 | * Logs the queries. |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | public function log() |
||
218 | |||
219 | /** |
||
220 | * This function returns the table status. |
||
221 | * |
||
222 | * @param string $prefix Table prefix |
||
223 | * |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getTableStatus($prefix = '') |
||
235 | |||
236 | /** |
||
237 | * This function is a replacement for MySQL's auto-increment so that |
||
238 | * we don't need it anymore. |
||
239 | * |
||
240 | * @param string $table The name of the table |
||
241 | * @param string $id The name of the ID column |
||
242 | * |
||
243 | * @return int |
||
244 | */ |
||
245 | public function nextId($table, $id) |
||
265 | |||
266 | /** |
||
267 | * Returns the error string. |
||
268 | * |
||
269 | * @return string |
||
270 | */ |
||
271 | public function error() |
||
275 | |||
276 | /** |
||
277 | * Returns the client version string. |
||
278 | * |
||
279 | * @return string |
||
280 | */ |
||
281 | public function clientVersion() |
||
285 | |||
286 | /** |
||
287 | * Returns the server version string. |
||
288 | * |
||
289 | * @return string |
||
290 | */ |
||
291 | public function serverVersion() |
||
295 | |||
296 | /** |
||
297 | * Returns an array with all table names. |
||
298 | * |
||
299 | * @todo Have to be refactored because of https://github.com/thorsten/phpMyFAQ/issues/965 |
||
300 | * |
||
301 | * @param string $prefix Table prefix |
||
302 | * |
||
303 | * @return array |
||
304 | */ |
||
305 | View Code Duplication | public function getTableNames($prefix = '') |
|
345 | |||
346 | /** |
||
347 | * Closes the connection to the database. |
||
348 | */ |
||
349 | public function close() |
||
355 | |||
356 | /** |
||
357 | * Destructor. |
||
358 | */ |
||
359 | public function __destruct() |
||
365 | |||
366 | /** |
||
367 | * @return string |
||
368 | */ |
||
369 | public function now() |
||
373 | |||
374 | /** |
||
375 | * Returns just one row. |
||
376 | * |
||
377 | * @param string |
||
378 | * |
||
379 | * @return string |
||
380 | */ |
||
381 | private function getOne($query) |
||
387 | |||
388 | } |
||
389 |
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.