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 |
||
36 | trait MySQLiByDanielGPstructures |
||
37 | { |
||
38 | |||
39 | use MySQLiByDanielGP, |
||
40 | MySQLiByDanielGPqueries; |
||
41 | |||
42 | /** |
||
43 | * Ensures table has special quoes and DOT as final char |
||
44 | * (if not empty, of course) |
||
45 | * |
||
46 | * @param string $referenceTable |
||
47 | * @return string |
||
48 | */ |
||
49 | private function correctTableWithQuotesAsFieldPrefix($referenceTable) |
||
56 | |||
57 | /** |
||
58 | * Prepares the output of text fields defined w. FKs |
||
59 | * |
||
60 | * @param array $foreignKeysArray |
||
61 | * @param array $value |
||
62 | * @param array $iar |
||
63 | * @return string |
||
64 | */ |
||
65 | protected function getFieldOutputTextFK($foreignKeysArray, $value, $iar) |
||
85 | |||
86 | /** |
||
87 | * Prepares the output of text fields w/o FKs |
||
88 | * |
||
89 | * @param array $value |
||
90 | * @param array $iar |
||
91 | * @return string |
||
92 | */ |
||
93 | protected function getFieldOutputTextNonFK($value, $iar) |
||
109 | |||
110 | /** |
||
111 | * Return the list of Tables from the MySQL server |
||
112 | * |
||
113 | * @return string |
||
114 | */ |
||
115 | protected function getMySQLStatistics($filterArray = null) |
||
119 | |||
120 | /** |
||
121 | * returns a list of MySQL databases |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | protected function getMySQLactiveDatabases() |
||
129 | |||
130 | /** |
||
131 | * returns a list of active MySQL engines |
||
132 | * |
||
133 | * @return array |
||
134 | */ |
||
135 | protected function getMySQLactiveEngines() |
||
139 | |||
140 | /** |
||
141 | * returns the list of all MySQL global variables |
||
142 | * |
||
143 | * @return array |
||
144 | */ |
||
145 | protected function getMySQLglobalVariables() |
||
149 | |||
150 | /** |
||
151 | * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
||
152 | * |
||
153 | * @return array |
||
154 | */ |
||
155 | protected function getMySQLlistColumns($filterArray = null) |
||
159 | |||
160 | /** |
||
161 | * returns a list of MySQL databases (w. choice of exclude/include the system ones) |
||
162 | * |
||
163 | * @return array |
||
164 | */ |
||
165 | protected function getMySQLlistDatabases($excludeSystemDbs = true) |
||
169 | |||
170 | /** |
||
171 | * returns a list of MySQL engines (w. choice of return only the active ones) |
||
172 | * |
||
173 | * @return array |
||
174 | */ |
||
175 | protected function getMySQLlistEngines($onlyActiveOnes = true) |
||
179 | |||
180 | /** |
||
181 | * returns a list of MySQL indexes (w. choice of to choose any combination of db/table/column) |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | protected function getMySQLlistIndexes($filterArray = null) |
||
189 | |||
190 | /** |
||
191 | * Return various informations (from predefined list) from the MySQL server |
||
192 | * |
||
193 | * @return int|array |
||
194 | */ |
||
195 | private function getMySQLlistMultiple($returnChoice, $returnType, $additionalFeatures = null) |
||
205 | |||
206 | /** |
||
207 | * Return various informations (from predefined list) from the MySQL server |
||
208 | * |
||
209 | * @param string $rChoice |
||
210 | * @param string $returnType |
||
211 | * @param array $additionalFeatures |
||
212 | * @return array |
||
213 | */ |
||
214 | private function getMySQLlistMultipleFinal($rChoice, $returnType, $additionalFeatures = null) |
||
231 | |||
232 | /** |
||
233 | * Return the list of Tables from the MySQL server |
||
234 | * |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function getMySQLlistTables($filterArray = null) |
||
241 | |||
242 | /** |
||
243 | * Return the time from the MySQL server |
||
244 | * |
||
245 | * @return string |
||
246 | */ |
||
247 | protected function getMySQLserverTime() |
||
251 | |||
252 | /** |
||
253 | * Reads data from table into REQUEST super global |
||
254 | * |
||
255 | * @param string $tableName |
||
256 | * @param array $filtersArray |
||
257 | */ |
||
258 | protected function getRowDataFromTable($tableName, $filtersArray) |
||
270 | |||
271 | /** |
||
272 | * Builds an filter string from pair of key and value, where value is array |
||
273 | * |
||
274 | * @param string $key |
||
275 | * @param array $value |
||
276 | * @param string $referenceTable |
||
277 | * @return string |
||
278 | */ |
||
279 | private function setArrayLineArrayToFilter($key, $value, $referenceTable) |
||
288 | |||
289 | /** |
||
290 | * Builds an filter string from pair of key and value, none array |
||
291 | * |
||
292 | * @param string $key |
||
293 | * @param int|float|string $value |
||
294 | * @return string |
||
295 | */ |
||
296 | private function setArrayLineToFilter($key, $value) |
||
304 | |||
305 | /** |
||
306 | * Transforms an array into usable filters |
||
307 | * |
||
308 | * @param array $entryArray |
||
309 | * @param string $referenceTable |
||
310 | * @return array |
||
311 | */ |
||
312 | private function setArrayToFilterValues($entryArray, $referenceTable = '') |
||
325 | |||
326 | private function transformStrIntoFn($queryByChoice, $rChoice) |
||
339 | } |
||
340 |
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.