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 |
||
23 | class SQLite3Driver extends DBDriverAbstract{ |
||
24 | |||
25 | /** |
||
26 | * Holds the database resource object |
||
27 | * |
||
28 | * @var SQLite3 |
||
29 | */ |
||
30 | protected $db; |
||
31 | |||
32 | /** |
||
33 | * Establishes a database connection and returns the connection object |
||
34 | * |
||
35 | * @return \SQLite3 the database resource object |
||
36 | * @throws \chillerlan\Database\DBException |
||
37 | */ |
||
38 | public function connect():SQLite3{ |
||
57 | |||
58 | /** |
||
59 | * Closes a database connection |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function disconnect():bool{ |
||
66 | |||
67 | /** |
||
68 | * Returns info about the used php client |
||
69 | * |
||
70 | * @return string php's database client string |
||
71 | */ |
||
72 | public function getClientInfo():string{ |
||
75 | |||
76 | /** |
||
77 | * Returns info about the database server |
||
78 | * |
||
79 | * @return string database's serverinfo string |
||
80 | */ |
||
81 | public function getServerInfo():string{ |
||
84 | |||
85 | /** |
||
86 | * Sanitizer. |
||
87 | * |
||
88 | * Recursively escapes string values, optional htmlspecialchars() |
||
89 | * |
||
90 | * @param array|string $data array or string to escape |
||
91 | * @param bool $specialchars [optional] if true, it performs a htmlspecialchars() on each value given |
||
92 | * |
||
93 | * @return array|string array or string. escaped. obviously. |
||
94 | */ |
||
95 | View Code Duplication | public function escape($data, bool $specialchars = false){ |
|
122 | |||
123 | /** |
||
124 | * Basic SQL query for non prepared statements |
||
125 | * |
||
126 | * There is no escaping in here, so make sure, your SQL is clean/escaped. |
||
127 | * Also, your SQL should NEVER contain user input, use prepared statements in this case. |
||
128 | * |
||
129 | * If the query was successful it returns either an array of results or true |
||
130 | * if it was a void query. On errors, a false will be returned, obviously. |
||
131 | * |
||
132 | * @param string $sql The SQL statement |
||
133 | * @param string $index [optional] an index column to assingn as the result's keys |
||
134 | * @param bool $assoc [optional] If true, the fields are named with the respective column names, otherwise numbered |
||
135 | * @param bool $fetch_array [optional] fetch the vaues as array instead of object |
||
136 | * |
||
137 | * @return array|bool array with results, true on void query success, otherwise false. |
||
138 | * @throws \mysqli_sql_exception |
||
139 | */ |
||
140 | public function raw(string $sql, string $index = null, bool $assoc = true, bool $fetch_array = false){ |
||
144 | |||
145 | /** |
||
146 | * Prepared statements wrapper |
||
147 | * |
||
148 | * Does everything for you: prepares the statement and fetches the results as an object or array |
||
149 | * just pass a query along with values and you're done. Not meant for multi-inserts. |
||
150 | * |
||
151 | * @param string $sql The SQL statement to prepare |
||
152 | * @param array $values [optional] the value for each "?" in the statement - in the respective order, of course |
||
153 | * @param string $index [optional] an index column to assingn as the result's keys |
||
154 | * @param bool $assoc [optional] If true, the fields are named with the respective column names, otherwise numbered |
||
155 | * @param bool $fetch_array [optional] fetch the vaues as array instead of object |
||
156 | * |
||
157 | * @return array|bool Array with results, true on void query success, otherwise false |
||
158 | */ |
||
159 | public function prepared(string $sql, array $values = [], string $index = null, bool $assoc = true, bool $fetch_array = false){ |
||
163 | |||
164 | /** |
||
165 | * Prepared multi line insert |
||
166 | * |
||
167 | * Prepared statement multi insert/update |
||
168 | * |
||
169 | * @param string $sql The SQL statement to prepare |
||
170 | * @param array $values a multidimensional array with the values, each row represents one line to insert. |
||
171 | * |
||
172 | * @return bool true query success, otherwise false |
||
173 | */ |
||
174 | public function multi(string $sql, array $values){ |
||
178 | |||
179 | /** |
||
180 | * Prepared multi line insert/update with callback |
||
181 | * |
||
182 | * @todo: multi treading |
||
183 | * @see https://gist.github.com/krakjoe/6437782 |
||
184 | * @see https://gist.github.com/krakjoe/9384409 |
||
185 | * |
||
186 | * @param string $sql The SQL statement to prepare |
||
187 | * @param array $data an array with the (raw) data to insert, each row represents one line to insert. |
||
188 | * @param callable|array $callback a callback that processes the values for each row. |
||
189 | * |
||
190 | * @return bool true query success, otherwise false |
||
191 | * @throws \chillerlan\Database\DBException |
||
192 | */ |
||
193 | public function multi_callback(string $sql, array $data, $callback){ |
||
197 | } |
||
198 |
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.