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 |
||
12 | trait SchemaTrait |
||
13 | { |
||
14 | // Schema functons |
||
15 | |||
16 | /** |
||
17 | * Return all schemas in the current database. |
||
18 | * |
||
19 | * @return \PHPPgAdmin\ADORecordSet All schemas, sorted alphabetically |
||
20 | */ |
||
21 | View Code Duplication | public function getSchemas() |
|
45 | |||
46 | /** |
||
47 | * Sets the current working schema. Will also set Class variable. |
||
48 | * |
||
49 | * @param string $schema The the name of the schema to work in |
||
50 | * |
||
51 | * @return int 0 if operation was successful |
||
52 | */ |
||
53 | public function setSchema($schema) |
||
69 | |||
70 | /** |
||
71 | * Return the current schema search path. |
||
72 | * |
||
73 | * @return array array of schema names |
||
74 | */ |
||
75 | public function getSearchPath() |
||
86 | |||
87 | /** |
||
88 | * Sets the current schema search path. |
||
89 | * |
||
90 | * @param mixed $paths An array of schemas in required search order |
||
91 | * |
||
92 | * @return int 0 if operation was successful |
||
93 | */ |
||
94 | public function setSearchPath($paths) |
||
121 | |||
122 | /** |
||
123 | * Creates a new schema. |
||
124 | * |
||
125 | * @param string $schemaname The name of the schema to create |
||
126 | * @param string $authorization (optional) The username to create the schema for |
||
127 | * @param string $comment (optional) If omitted, defaults to nothing |
||
128 | * |
||
129 | * @return bool|int 0 success |
||
130 | */ |
||
131 | public function createSchema($schemaname, $authorization = '', $comment = '') |
||
170 | |||
171 | /** |
||
172 | * Updates a schema. |
||
173 | * |
||
174 | * @param string $schemaname The name of the schema to drop |
||
175 | * @param string $comment The new comment for this schema |
||
176 | * @param string $name new name for this schema |
||
177 | * @param string $owner The new owner for this schema |
||
178 | * |
||
179 | * @return bool|int 0 success |
||
180 | */ |
||
181 | public function updateSchema($schemaname, $comment, $name, $owner) |
||
226 | |||
227 | /** |
||
228 | * Return all information relating to a schema. |
||
229 | * |
||
230 | * @param string $schema The name of the schema |
||
231 | * |
||
232 | * @return \PHPPgAdmin\ADORecordSet Schema information |
||
233 | */ |
||
234 | public function getSchemaByName($schema) |
||
246 | |||
247 | // Table functions |
||
248 | |||
249 | /** |
||
250 | * Drops a schema. |
||
251 | * |
||
252 | * @param string $schemaname The name of the schema to drop |
||
253 | * @param bool $cascade True to cascade drop, false to restrict |
||
254 | * |
||
255 | * @return int 0 if operation was successful |
||
256 | */ |
||
257 | public function dropSchema($schemaname, $cascade) |
||
268 | |||
269 | abstract public function fieldClean(&$str); |
||
270 | |||
271 | abstract public function beginTransaction(); |
||
272 | |||
273 | abstract public function rollbackTransaction(); |
||
274 | |||
275 | abstract public function endTransaction(); |
||
276 | |||
277 | abstract public function execute($sql); |
||
278 | |||
279 | abstract public function setComment($obj_type, $obj_name, $table, $comment, $basetype = null); |
||
280 | |||
281 | abstract public function selectSet($sql); |
||
282 | |||
283 | abstract public function clean(&$str); |
||
284 | |||
285 | abstract public function phpBool($parameter); |
||
286 | |||
287 | abstract public function hasCreateTableLikeWithConstraints(); |
||
288 | |||
289 | abstract public function hasCreateTableLikeWithIndexes(); |
||
290 | |||
291 | abstract public function hasTablespaces(); |
||
292 | |||
293 | abstract public function delete($table, $conditions, $schema = ''); |
||
294 | |||
295 | abstract public function fieldArrayClean(&$arr); |
||
296 | |||
297 | abstract public function hasCreateFieldWithConstraints(); |
||
298 | |||
299 | abstract public function getAttributeNames($table, $atts); |
||
300 | |||
301 | abstract public function selectField($sql, $field); |
||
302 | |||
303 | abstract public function phpArray($dbarr); |
||
304 | } |
||
305 |
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.