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:
Complex classes like CI_DB_sqlsrv_driver often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CI_DB_sqlsrv_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class CI_DB_sqlsrv_driver extends CI_DB { |
||
54 | |||
55 | /** |
||
56 | * Database driver |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | public $dbdriver = 'sqlsrv'; |
||
61 | |||
62 | /** |
||
63 | * Scrollable flag |
||
64 | * |
||
65 | * Determines what cursor type to use when executing queries. |
||
66 | * |
||
67 | * FALSE or SQLSRV_CURSOR_FORWARD would increase performance, |
||
68 | * but would disable num_rows() (and possibly insert_id()) |
||
69 | * |
||
70 | * @var mixed |
||
71 | */ |
||
72 | public $scrollable; |
||
73 | |||
74 | // -------------------------------------------------------------------- |
||
75 | |||
76 | /** |
||
77 | * ORDER BY random keyword |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $_random_keyword = array('NEWID()', 'RAND(%d)'); |
||
82 | |||
83 | /** |
||
84 | * Quoted identifier flag |
||
85 | * |
||
86 | * Whether to use SQL-92 standard quoted identifier |
||
87 | * (double quotes) or brackets for identifier escaping. |
||
88 | * |
||
89 | * @var bool |
||
90 | */ |
||
91 | protected $_quoted_identifier = TRUE; |
||
92 | |||
93 | // -------------------------------------------------------------------- |
||
94 | |||
95 | /** |
||
96 | * Class constructor |
||
97 | * |
||
98 | * @param array $params |
||
99 | * @return void |
||
100 | */ |
||
101 | public function __construct($params) |
||
113 | |||
114 | // -------------------------------------------------------------------- |
||
115 | |||
116 | /** |
||
117 | * Database connection |
||
118 | * |
||
119 | * @param bool $pooling |
||
120 | * @return resource |
||
121 | */ |
||
122 | public function db_connect($pooling = FALSE) |
||
155 | |||
156 | // -------------------------------------------------------------------- |
||
157 | |||
158 | /** |
||
159 | * Select the database |
||
160 | * |
||
161 | * @param string $database |
||
162 | * @return bool |
||
163 | */ |
||
164 | View Code Duplication | public function db_select($database = '') |
|
179 | |||
180 | // -------------------------------------------------------------------- |
||
181 | |||
182 | /** |
||
183 | * Execute the query |
||
184 | * |
||
185 | * @param string $sql an SQL query |
||
186 | * @return resource |
||
187 | */ |
||
188 | protected function _execute($sql) |
||
194 | |||
195 | // -------------------------------------------------------------------- |
||
196 | |||
197 | /** |
||
198 | * Begin Transaction |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | protected function _trans_begin() |
||
206 | |||
207 | // -------------------------------------------------------------------- |
||
208 | |||
209 | /** |
||
210 | * Commit Transaction |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | protected function _trans_commit() |
||
218 | |||
219 | // -------------------------------------------------------------------- |
||
220 | |||
221 | /** |
||
222 | * Rollback Transaction |
||
223 | * |
||
224 | * @return bool |
||
225 | */ |
||
226 | protected function _trans_rollback() |
||
230 | |||
231 | // -------------------------------------------------------------------- |
||
232 | |||
233 | /** |
||
234 | * Affected Rows |
||
235 | * |
||
236 | * @return int |
||
237 | */ |
||
238 | public function affected_rows() |
||
242 | |||
243 | // -------------------------------------------------------------------- |
||
244 | |||
245 | /** |
||
246 | * Insert ID |
||
247 | * |
||
248 | * Returns the last id created in the Identity column. |
||
249 | * |
||
250 | * @return string |
||
251 | */ |
||
252 | public function insert_id() |
||
256 | |||
257 | // -------------------------------------------------------------------- |
||
258 | |||
259 | /** |
||
260 | * Database version number |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | View Code Duplication | public function version() |
|
278 | |||
279 | // -------------------------------------------------------------------- |
||
280 | |||
281 | /** |
||
282 | * List table query |
||
283 | * |
||
284 | * Generates a platform-specific query string so that the table names can be fetched |
||
285 | * |
||
286 | * @param bool |
||
287 | * @return string $prefix_limit |
||
288 | */ |
||
289 | View Code Duplication | protected function _list_tables($prefix_limit = FALSE) |
|
303 | |||
304 | // -------------------------------------------------------------------- |
||
305 | |||
306 | /** |
||
307 | * List column query |
||
308 | * |
||
309 | * Generates a platform-specific query string so that the column names can be fetched |
||
310 | * |
||
311 | * @param string $table |
||
312 | * @return string |
||
313 | */ |
||
314 | protected function _list_columns($table = '') |
||
320 | |||
321 | // -------------------------------------------------------------------- |
||
322 | |||
323 | /** |
||
324 | * Returns an object with field data |
||
325 | * |
||
326 | * @param string $table |
||
327 | * @return array |
||
328 | */ |
||
329 | View Code Duplication | public function field_data($table) |
|
353 | |||
354 | // -------------------------------------------------------------------- |
||
355 | |||
356 | /** |
||
357 | * Error |
||
358 | * |
||
359 | * Returns an array containing code and message of the last |
||
360 | * database error that has occured. |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function error() |
||
391 | |||
392 | // -------------------------------------------------------------------- |
||
393 | |||
394 | /** |
||
395 | * Update statement |
||
396 | * |
||
397 | * Generates a platform-specific update string from the supplied data |
||
398 | * |
||
399 | * @param string $table |
||
400 | * @param array $values |
||
401 | * @return string |
||
402 | */ |
||
403 | protected function _update($table, $values) |
||
409 | |||
410 | // -------------------------------------------------------------------- |
||
411 | |||
412 | /** |
||
413 | * Truncate statement |
||
414 | * |
||
415 | * Generates a platform-specific truncate string from the supplied data |
||
416 | * |
||
417 | * If the database does not support the TRUNCATE statement, |
||
418 | * then this method maps to 'DELETE FROM table' |
||
419 | * |
||
420 | * @param string $table |
||
421 | * @return string |
||
422 | */ |
||
423 | protected function _truncate($table) |
||
427 | |||
428 | // -------------------------------------------------------------------- |
||
429 | |||
430 | /** |
||
431 | * Delete statement |
||
432 | * |
||
433 | * Generates a platform-specific delete string from the supplied data |
||
434 | * |
||
435 | * @param string $table |
||
436 | * @return string |
||
437 | */ |
||
438 | View Code Duplication | protected function _delete($table) |
|
447 | |||
448 | // -------------------------------------------------------------------- |
||
449 | |||
450 | /** |
||
451 | * LIMIT |
||
452 | * |
||
453 | * Generates a platform-specific LIMIT clause |
||
454 | * |
||
455 | * @param string $sql SQL Query |
||
456 | * @return string |
||
457 | */ |
||
458 | View Code Duplication | protected function _limit($sql) |
|
506 | |||
507 | // -------------------------------------------------------------------- |
||
508 | |||
509 | /** |
||
510 | * Insert batch statement |
||
511 | * |
||
512 | * Generates a platform-specific insert string from the supplied data. |
||
513 | * |
||
514 | * @param string $table Table name |
||
515 | * @param array $keys INSERT keys |
||
516 | * @param array $values INSERT values |
||
517 | * @return string|bool |
||
518 | */ |
||
519 | View Code Duplication | protected function _insert_batch($table, $keys, $values) |
|
529 | |||
530 | // -------------------------------------------------------------------- |
||
531 | |||
532 | /** |
||
533 | * Close DB Connection |
||
534 | * |
||
535 | * @return void |
||
536 | */ |
||
537 | protected function _close() |
||
541 | |||
542 | } |
||
543 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.