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 |
||
26 | abstract class PDODriverAbstract extends DBDriverAbstract{ |
||
27 | |||
28 | /** |
||
29 | * Holds the database resource object |
||
30 | * |
||
31 | * @var PDO |
||
32 | */ |
||
33 | protected $db; |
||
34 | |||
35 | /** |
||
36 | * Some basic PDO options |
||
37 | * |
||
38 | * @see http://php.net/manual/pdo.getattribute.php PDO::getAttribute |
||
39 | * |
||
40 | * @var array |
||
41 | */ |
||
42 | protected $pdo_options |
||
43 | = [ |
||
44 | PDO::ATTR_CASE => PDO::CASE_NATURAL, |
||
45 | PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, |
||
46 | PDO::ATTR_ORACLE_NULLS => PDO::NULL_NATURAL, |
||
47 | PDO::ATTR_STRINGIFY_FETCHES => false, |
||
48 | PDO::ATTR_EMULATE_PREPARES => false, |
||
49 | ]; |
||
50 | |||
51 | /** |
||
52 | * The PDO drivername which is being used in the DSN |
||
53 | * |
||
54 | * @var string |
||
55 | */ |
||
56 | protected $drivername; |
||
57 | |||
58 | /** |
||
59 | * This array holds one or more key=>value pairs to set attribute values for the PDOStatement object that this |
||
60 | * method returns. You would most commonly use this to set the PDO::ATTR_CURSOR value to PDO::CURSOR_SCROLL to |
||
61 | * request a scrollable cursor. |
||
62 | * |
||
63 | * @var array |
||
64 | */ |
||
65 | protected $pdo_stmt_options = []; |
||
66 | |||
67 | /** |
||
68 | * Returns a DSN string using the given options |
||
69 | * |
||
70 | * @link http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#4.4 |
||
71 | * |
||
72 | * @return string DSN |
||
73 | */ |
||
74 | View Code Duplication | protected function getDSN():string{ |
|
89 | |||
90 | /** |
||
91 | * Establishes a database connection and returns the connection object |
||
92 | * |
||
93 | * @return \chillerlan\Database\Drivers\DBDriverInterface |
||
94 | * @throws \chillerlan\Database\DBException |
||
95 | */ |
||
96 | View Code Duplication | public function connect():DBDriverInterface{ |
|
121 | |||
122 | /** |
||
123 | * Closes a database connection |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function disconnect():bool{ |
||
132 | |||
133 | /** |
||
134 | * Returns info about the used php client |
||
135 | * |
||
136 | * @return string php's database client string |
||
137 | */ |
||
138 | public function getClientInfo():string{ |
||
141 | |||
142 | /** |
||
143 | * Returns info about the database server |
||
144 | * |
||
145 | * @return string database's serverinfo string |
||
146 | */ |
||
147 | public function getServerInfo():string{ |
||
150 | |||
151 | /** |
||
152 | * @param $data |
||
153 | * |
||
154 | * @return string |
||
155 | */ |
||
156 | protected function __escape($data){ |
||
159 | |||
160 | /** |
||
161 | * @param \PDOStatement $stmt |
||
162 | * @param array $values |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | View Code Duplication | protected function bindParams(PDOStatement &$stmt, array $values){ |
|
190 | |||
191 | /** |
||
192 | * Returns the last insert id (if present) |
||
193 | * |
||
194 | * @link http://php.net/manual/pdo.lastinsertid.php |
||
195 | * @return string |
||
196 | */ |
||
197 | protected function insertID():string{ |
||
200 | |||
201 | /** |
||
202 | * @param $stmt |
||
203 | * @param string|null $index |
||
204 | * @param bool $assoc |
||
205 | * |
||
206 | * @return bool|\chillerlan\Database\DBResult |
||
207 | */ |
||
208 | protected function __getResult($stmt, string $index = null, bool $assoc = true){ |
||
216 | |||
217 | /** |
||
218 | * @param string $sql |
||
219 | * @param string|null $index |
||
220 | * @param bool $assoc |
||
221 | * |
||
222 | * @return bool|\chillerlan\Database\DBResult |
||
223 | */ |
||
224 | protected function __raw(string $sql, string $index = null, bool $assoc = true){ |
||
227 | |||
228 | /** |
||
229 | * @param string $sql |
||
230 | * @param array $values |
||
231 | * @param string|null $index |
||
232 | * @param bool $assoc |
||
233 | * |
||
234 | * @return bool|\chillerlan\Database\DBResult |
||
235 | */ |
||
236 | protected function __prepared(string $sql, array $values = [], string $index = null, bool $assoc = true){ |
||
247 | |||
248 | /** |
||
249 | * @param string $sql |
||
250 | * @param array $values |
||
251 | * |
||
252 | * @return bool |
||
253 | */ |
||
254 | View Code Duplication | protected function __multi(string $sql, array $values){ |
|
266 | |||
267 | /** |
||
268 | * @param string $sql |
||
269 | * @param array $data |
||
270 | * @param $callback |
||
271 | * |
||
272 | * @return bool |
||
273 | */ |
||
274 | View Code Duplication | protected function __multi_callback(string $sql, array $data, $callback){ |
|
286 | } |
||
287 |
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.