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 | class DBMySQLFunctions extends DBBaseFunctions |
||
8 | { |
||
9 | |||
10 | private $sysTimeStamp = 'NOW()'; |
||
11 | |||
12 | View Code Duplication | function concat($s1, $s2 = null) |
|
13 | { |
||
14 | $sql = "concat("; |
||
15 | for ($i = 0, $numArgs = func_num_args(); $i < $numArgs; $i++) { |
||
16 | $var = func_get_arg($i); |
||
17 | $sql .= ($i == 0 ? "" : ",") . $var; |
||
18 | } |
||
19 | $sql .= ")"; |
||
20 | |||
21 | return $sql; |
||
22 | } |
||
23 | |||
24 | /** |
||
25 | * Given a SQL returns it with the proper LIMIT or equivalent method included |
||
26 | * @param string $sql |
||
27 | * @param int $start |
||
28 | * @param int $qty |
||
29 | * @return string |
||
30 | */ |
||
31 | View Code Duplication | function limit($sql, $start, $qty) |
|
39 | |||
40 | /** |
||
41 | * Given a SQL returns it with the proper TOP or equivalent method included |
||
42 | * @param string $sql |
||
43 | * @param int $qty |
||
44 | * @return string |
||
45 | */ |
||
46 | function top($sql, $qty) |
||
50 | |||
51 | /** |
||
52 | * Return if the database provider have a top or similar function |
||
53 | * @return bool |
||
54 | */ |
||
55 | function hasTop() |
||
59 | |||
60 | /** |
||
61 | * Return if the database provider have a limit function |
||
62 | * @return bool |
||
63 | */ |
||
64 | function hasLimit() |
||
68 | |||
69 | /** |
||
70 | * Format date column in sql string given an input format that understands Y M D |
||
71 | * @param string $fmt |
||
72 | * @param string|bool $col |
||
73 | * @return string |
||
74 | * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
||
75 | */ |
||
76 | function sqlDate($fmt, $col = false) |
||
144 | |||
145 | /** |
||
146 | * Format a string date to a string database readable format. |
||
147 | * |
||
148 | * @param string $date |
||
149 | * @param string $dateFormat |
||
150 | * @return string |
||
151 | */ |
||
152 | function toDate($date, $dateFormat) |
||
156 | |||
157 | /** |
||
158 | * Format a string database readable format to a string date in a free format. |
||
159 | * |
||
160 | * @param string $date |
||
161 | * @param string $dateFormat |
||
162 | * @return string |
||
163 | */ |
||
164 | function fromDate($date, $dateFormat) |
||
168 | |||
169 | /** |
||
170 | * |
||
171 | * @param DBDataset $dbdataset |
||
172 | * @param string $sql |
||
173 | * @param array $param |
||
174 | * @return int |
||
175 | */ |
||
176 | View Code Duplication | function executeAndGetInsertedId($dbdataset, $sql, $param) |
|
187 | } |
||
188 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.