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 |
||
19 | class DatabaseQuery implements DatabaseQueryInterface |
||
20 | { |
||
21 | /** |
||
22 | * connect Setup database connection |
||
23 | */ |
||
24 | protected static function connect() |
||
28 | |||
29 | /** |
||
30 | * sanitize(argument) Removes unwanted characters |
||
31 | * |
||
32 | * @param $value |
||
33 | * |
||
34 | * @return string |
||
35 | */ |
||
36 | protected static function sanitize($value) |
||
42 | |||
43 | /** |
||
44 | * checkConnection |
||
45 | * |
||
46 | * @param $con |
||
47 | * |
||
48 | * @return string |
||
49 | */ |
||
50 | protected static function checkConnection($con) |
||
58 | |||
59 | /** |
||
60 | * checkTableExist Check if table already in the database |
||
61 | * |
||
62 | * @param $tablename |
||
63 | * @param $con |
||
64 | * |
||
65 | * @return bool |
||
66 | */ |
||
67 | View Code Duplication | public function checkTableExist($table, $con=NULL) |
|
76 | |||
77 | /** |
||
78 | * checkTableName Return the table name |
||
79 | * |
||
80 | * @param $tablename |
||
81 | * @param $con |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | View Code Duplication | public static function checkTableName($tableName, $con=NULL) |
|
99 | |||
100 | /** |
||
101 | * checkColumn Check if column exist in table |
||
102 | * |
||
103 | * @param $tableName |
||
104 | * @param $columnName |
||
105 | * @param $con |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | protected static function checkColumn($tableName, $columnName, $con=NULL) |
||
121 | |||
122 | /** |
||
123 | * buildColumn Build the column name |
||
124 | * |
||
125 | * @param $data |
||
126 | * |
||
127 | * @return string |
||
128 | */ |
||
129 | View Code Duplication | public static function buildColumn($data) |
|
144 | |||
145 | /** |
||
146 | * buildValues Build the column values |
||
147 | * |
||
148 | * @param $data |
||
149 | * |
||
150 | * @return string |
||
151 | */ |
||
152 | View Code Duplication | public static function buildValues($data) |
|
167 | |||
168 | /** |
||
169 | * buildClause Build the clause value |
||
170 | * |
||
171 | * @param $data |
||
172 | * |
||
173 | * @return string |
||
174 | */ |
||
175 | View Code Duplication | protected static function buildClause($tableName, $data) |
|
193 | |||
194 | /** |
||
195 | * selectAllQuery |
||
196 | * |
||
197 | * @return string |
||
198 | */ |
||
199 | public static function selectAllQuery($tableName) |
||
203 | |||
204 | /** |
||
205 | * whereAndClause |
||
206 | * |
||
207 | * @return string |
||
208 | */ |
||
209 | View Code Duplication | public static function whereAndClause($tableName, $data, $condition) |
|
228 | |||
229 | /** |
||
230 | * selectQuery |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public static function selectQuery($tableName, $data, $condition, $connection) |
||
255 | |||
256 | /** |
||
257 | * insertQuery |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | public function insertQuery($tableName) |
||
272 | |||
273 | /** |
||
274 | * updateQuery |
||
275 | * |
||
276 | * @return string |
||
277 | */ |
||
278 | public function updateQuery($tableName) |
||
288 | } |
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.