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 |
||
7 | View Code Duplication | class DbMysqlFunctions extends DbBaseFunctions |
|
|
|||
8 | { |
||
9 | |||
10 | public function concat($str1, $str2 = null) |
||
14 | |||
15 | /** |
||
16 | * Given a SQL returns it with the proper LIMIT or equivalent method included |
||
17 | * @param string $sql |
||
18 | * @param int $start |
||
19 | * @param int $qty |
||
20 | * @return string |
||
21 | */ |
||
22 | public function limit($sql, $start, $qty = null) |
||
38 | |||
39 | /** |
||
40 | * Given a SQL returns it with the proper TOP or equivalent method included |
||
41 | * @param string $sql |
||
42 | * @param int $qty |
||
43 | * @return string |
||
44 | */ |
||
45 | public function top($sql, $qty) |
||
49 | |||
50 | /** |
||
51 | * Return if the database provider have a top or similar function |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function hasTop() |
||
58 | |||
59 | /** |
||
60 | * Return if the database provider have a limit function |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function hasLimit() |
||
67 | |||
68 | /** |
||
69 | * Format date column in sql string given an input format that understands Y M D |
||
70 | * |
||
71 | * @param string $format |
||
72 | * @param string|null $column |
||
73 | * @return string |
||
74 | * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
||
75 | */ |
||
76 | public function sqlDate($format, $column = null) |
||
107 | |||
108 | /** |
||
109 | * Format a string date to a string database readable format. |
||
110 | * |
||
111 | * @param string $date |
||
112 | * @param string $dateFormat |
||
113 | * @return string |
||
114 | */ |
||
115 | public function toDate($date, $dateFormat) |
||
119 | |||
120 | /** |
||
121 | * Format a string database readable format to a string date in a free format. |
||
122 | * |
||
123 | * @param string $date |
||
124 | * @param string $dateFormat |
||
125 | * @return string |
||
126 | */ |
||
127 | public function fromDate($date, $dateFormat) |
||
131 | |||
132 | /** |
||
133 | * |
||
134 | * @param DBDataset $dbdataset |
||
135 | * @param string $sql |
||
136 | * @param array $param |
||
137 | * @return int |
||
138 | */ |
||
139 | public function executeAndGetInsertedId($dbdataset, $sql, $param) |
||
150 | } |
||
151 |
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.