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 /** MysqlDriverMicro */ |
||
17 | class MysqlDriver extends Driver |
||
18 | { |
||
19 | /** |
||
20 | * Set current database |
||
21 | * |
||
22 | * @access public |
||
23 | * |
||
24 | * @param string $dbName Database name |
||
25 | * |
||
26 | * @return boolean |
||
27 | */ |
||
28 | public function switchDatabase($dbName) |
||
32 | |||
33 | /** |
||
34 | * Info of database |
||
35 | * |
||
36 | * @access public |
||
37 | * @param string $dbName Database name |
||
38 | * @return array |
||
39 | */ |
||
40 | public function infoDatabase($dbName) |
||
58 | |||
59 | /** |
||
60 | * List database names on this connection |
||
61 | * |
||
62 | * @access public |
||
63 | * @return array |
||
64 | */ |
||
65 | public function listDatabases() |
||
82 | |||
83 | /** |
||
84 | * List tables in db |
||
85 | * |
||
86 | * @access public |
||
87 | * @return array |
||
88 | */ |
||
89 | public function listTables() |
||
93 | |||
94 | /** |
||
95 | * Create a new table |
||
96 | * |
||
97 | * @param string $name Table name |
||
98 | * @param array $elements Table elements |
||
99 | * @param string $params Table params |
||
100 | * |
||
101 | * @return int |
||
102 | */ |
||
103 | public function createTable($name, array $elements = [], $params = '') |
||
109 | |||
110 | /** |
||
111 | * Remove table from database |
||
112 | * |
||
113 | * @access public |
||
114 | * |
||
115 | * @param string $name Table name |
||
116 | * |
||
117 | * @return mixed |
||
118 | */ |
||
119 | public function removeTable($name) |
||
123 | |||
124 | /** |
||
125 | * Get array fields into table |
||
126 | * |
||
127 | * @access public |
||
128 | * |
||
129 | * @param string $table Table name |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | public function listFields($table) |
||
151 | |||
152 | /** |
||
153 | * Get info of a field |
||
154 | * |
||
155 | * @access public |
||
156 | * |
||
157 | * @param string $field Field name |
||
158 | * @param string $table Table name |
||
159 | * |
||
160 | * @return array|boolean |
||
161 | */ |
||
162 | public function fieldInfo($field, $table) |
||
170 | |||
171 | /** |
||
172 | * Insert row into table |
||
173 | * |
||
174 | * @access public |
||
175 | * |
||
176 | * @param string $table Table name |
||
177 | * @param array $line Line or lines to added |
||
178 | * @param bool $multi Is multi rows |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | View Code Duplication | public function insert($table, array $line = [], $multi = false) |
|
204 | |||
205 | /** |
||
206 | * Update row in table |
||
207 | * |
||
208 | * @access public |
||
209 | * |
||
210 | * @param string $table Table name |
||
211 | * @param array $elements Elements to update |
||
212 | * @param string $conditions Conditions for search |
||
213 | * |
||
214 | * @return bool |
||
215 | */ |
||
216 | public function update($table, array $elements = [], $conditions = '') |
||
235 | |||
236 | /** |
||
237 | * Delete row from table |
||
238 | * |
||
239 | * @access public |
||
240 | * |
||
241 | * @param string $table Table name |
||
242 | * @param string $conditions Conditions to search |
||
243 | * @param array $params Params array |
||
244 | * |
||
245 | * @return bool |
||
246 | */ |
||
247 | public function delete($table, $conditions, array $params = []) |
||
251 | |||
252 | /** |
||
253 | * Count element in sub-query |
||
254 | * |
||
255 | * @access public |
||
256 | * |
||
257 | * @param string $query Query |
||
258 | * @param string $table Table name |
||
259 | * |
||
260 | * @return integer|boolean |
||
261 | */ |
||
262 | public function count($query = '', $table = '') |
||
277 | |||
278 | /** |
||
279 | * Exists element in the table by params |
||
280 | * |
||
281 | * @access public |
||
282 | * |
||
283 | * @param string $table Table name |
||
284 | * @param array $params Params array |
||
285 | * |
||
286 | * @return bool |
||
287 | */ |
||
288 | public function exists($table, array $params = []) |
||
302 | } |
||
303 |
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.