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 DbDblibFunctions extends DbBaseFunctions |
||
9 | { |
||
10 | |||
11 | 9 | public function __construct() |
|
18 | |||
19 | 1 | public function concat($str1, $str2 = null) |
|
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 | * @throws NotAvailableException |
||
31 | */ |
||
32 | 1 | public function limit($sql, $start, $qty = null) |
|
36 | |||
37 | /** |
||
38 | * Given a SQL returns it with the proper TOP or equivalent method included |
||
39 | * @param string $sql |
||
40 | * @param int $qty |
||
41 | * @return string |
||
42 | */ |
||
43 | 1 | public function top($sql, $qty) |
|
55 | |||
56 | /** |
||
57 | * Return if the database provider have a top or similar function |
||
58 | * @return bool |
||
59 | */ |
||
60 | 1 | public function hasTop() |
|
64 | |||
65 | /** |
||
66 | * Return if the database provider have a limit function |
||
67 | * @return bool |
||
68 | */ |
||
69 | 1 | public function hasLimit() |
|
73 | |||
74 | /** |
||
75 | * Format date column in sql string given an input format that understands Y M D |
||
76 | |||
77 | * |
||
78 | *@param string $format |
||
79 | * @param bool|string $column |
||
80 | * @return string |
||
81 | * @example $db->getDbFunctions()->SQLDate("d/m/Y H:i", "dtcriacao") |
||
82 | */ |
||
83 | 1 | public function sqlDate($format, $column = null) |
|
84 | { |
||
85 | 1 | if (is_null($column)) { |
|
86 | 1 | $column = "getdate()"; |
|
87 | } |
||
88 | |||
89 | $pattern = [ |
||
90 | 1 | 'Y' => "YYYY", |
|
91 | 'y' => "YY", |
||
92 | 'M' => "MM", |
||
93 | 'm' => "M", |
||
94 | 'Q' => "", |
||
95 | 'q' => "", |
||
96 | 'D' => "dd", |
||
97 | 'd' => "dd", |
||
98 | 'h' => "H", |
||
99 | 'H' => "HH", |
||
100 | 'i' => "mm", |
||
101 | 's' => "ss", |
||
102 | 'a' => "", |
||
103 | 'A' => "", |
||
104 | ]; |
||
105 | |||
106 | 1 | $preparedSql = $this->prepareSqlDate($format, $pattern, ''); |
|
107 | |||
108 | 1 | return sprintf( |
|
109 | 1 | "FORMAT(%s, '%s')", |
|
110 | 1 | $column, |
|
111 | 1 | implode('', $preparedSql) |
|
112 | ); |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Format a string date to a string database readable format. |
||
117 | * |
||
118 | * @param string $date |
||
119 | * @param string $dateFormat |
||
120 | * @return string |
||
121 | */ |
||
122 | public function toDate($date, $dateFormat) |
||
126 | |||
127 | /** |
||
128 | * Format a string database readable format to a string date in a free format. |
||
129 | * |
||
130 | * @param string $date |
||
131 | * @param string $dateFormat |
||
132 | * @return string |
||
133 | */ |
||
134 | public function fromDate($date, $dateFormat) |
||
138 | |||
139 | /** |
||
140 | * |
||
141 | * @param DbDriverInterface $dbdataset |
||
142 | * @param string $sql |
||
143 | * @param array $param |
||
144 | * @return int |
||
145 | */ |
||
146 | View Code Duplication | public function executeAndGetInsertedId(DbDriverInterface $dbdataset, $sql, $param) |
|
|
|||
147 | { |
||
148 | $insertedId = parent::executeAndGetInsertedId($dbdataset, $sql, $param); |
||
149 | $iterator = $dbdataset->getIterator("select @@identity id"); |
||
150 | if ($iterator->hasNext()) { |
||
151 | $singleRow = $iterator->moveNext(); |
||
152 | $insertedId = $singleRow->get("id"); |
||
153 | } |
||
154 | |||
155 | return $insertedId; |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param $sql |
||
160 | * @return string|void |
||
161 | * @throws \ByJG\AnyDataset\Exception\NotAvailableException |
||
162 | */ |
||
163 | 1 | public function forUpdate($sql) |
|
167 | |||
168 | 1 | public function hasForUpdate() |
|
172 | } |
||
173 |
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.