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 |
||
8 | class DBSqliteFunctions extends DBBaseFunctions |
||
9 | { |
||
10 | |||
11 | function concat($s1, $s2 = null) |
||
15 | |||
16 | /** |
||
17 | * Given a SQL returns it with the proper LIMIT or equivalent method included |
||
18 | * @param string $sql |
||
19 | * @param int $start |
||
20 | * @param int $qty |
||
21 | * @return string |
||
22 | */ |
||
23 | View Code Duplication | function limit($sql, $start, $qty) |
|
31 | |||
32 | /** |
||
33 | * Given a SQL returns it with the proper TOP or equivalent method included |
||
34 | * @param string $sql |
||
35 | * @param int $qty |
||
36 | * @return string |
||
37 | */ |
||
38 | function top($sql, $qty) |
||
42 | |||
43 | /** |
||
44 | * Return if the database provider have a top or similar function |
||
45 | * @return bool |
||
46 | */ |
||
47 | function hasTop() |
||
51 | |||
52 | /** |
||
53 | * Return if the database provider have a limit function |
||
54 | * @return bool |
||
55 | */ |
||
56 | function hasLimit() |
||
60 | |||
61 | /** |
||
62 | * Format date column in sql string given an input format that understands Y M D |
||
63 | * @param string $fmt |
||
64 | * @param string|bool $col |
||
65 | * @return string |
||
66 | * @throws NotImplementedException |
||
67 | * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
||
68 | */ |
||
69 | function sqlDate($fmt, $col = false) |
||
73 | |||
74 | /** |
||
75 | * Format a string date to a string database readable format. |
||
76 | * |
||
77 | * @param string $date |
||
78 | * @param string $dateFormat |
||
79 | * @return string |
||
80 | */ |
||
81 | function toDate($date, $dateFormat) |
||
85 | |||
86 | /** |
||
87 | * Format a string database readable format to a string date in a free format. |
||
88 | * |
||
89 | * @param string $date |
||
90 | * @param string $dateFormat |
||
91 | * @return string |
||
92 | */ |
||
93 | function fromDate($date, $dateFormat) |
||
97 | |||
98 | /** |
||
99 | * |
||
100 | * @param DBDataset $dbdataset |
||
101 | * @param string $sql |
||
102 | * @param array $param |
||
103 | * @return int |
||
104 | */ |
||
105 | View Code Duplication | function executeAndGetInsertedId($dbdataset, $sql, $param) |
|
116 | } |
||
117 |
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.