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_pdo_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_pdo_sqlsrv_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class CI_DB_pdo_sqlsrv_driver extends CI_DB_pdo_driver { |
||
54 | |||
55 | /** |
||
56 | * Sub-driver |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | public $subdriver = 'sqlsrv'; |
||
61 | |||
62 | // -------------------------------------------------------------------- |
||
63 | |||
64 | /** |
||
65 | * ORDER BY random keyword |
||
66 | * |
||
67 | * @var array |
||
68 | */ |
||
69 | protected $_random_keyword = array('NEWID()', 'RAND(%d)'); |
||
70 | |||
71 | /** |
||
72 | * Quoted identifier flag |
||
73 | * |
||
74 | * Whether to use SQL-92 standard quoted identifier |
||
75 | * (double quotes) or brackets for identifier escaping. |
||
76 | * |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $_quoted_identifier; |
||
80 | |||
81 | // -------------------------------------------------------------------- |
||
82 | |||
83 | /** |
||
84 | * Class constructor |
||
85 | * |
||
86 | * Builds the DSN if not already set. |
||
87 | * |
||
88 | * @param array $params |
||
89 | * @return void |
||
90 | */ |
||
91 | public function __construct($params) |
||
142 | |||
143 | // -------------------------------------------------------------------- |
||
144 | |||
145 | /** |
||
146 | * Database connection |
||
147 | * |
||
148 | * @param bool $persistent |
||
149 | * @return object |
||
150 | */ |
||
151 | public function db_connect($persistent = FALSE) |
||
173 | |||
174 | // -------------------------------------------------------------------- |
||
175 | |||
176 | /** |
||
177 | * Show table query |
||
178 | * |
||
179 | * Generates a platform-specific query string so that the table names can be fetched |
||
180 | * |
||
181 | * @param bool $prefix_limit |
||
182 | * @return string |
||
183 | */ |
||
184 | View Code Duplication | protected function _list_tables($prefix_limit = FALSE) |
|
198 | |||
199 | // -------------------------------------------------------------------- |
||
200 | |||
201 | /** |
||
202 | * Show column query |
||
203 | * |
||
204 | * Generates a platform-specific query string so that the column names can be fetched |
||
205 | * |
||
206 | * @param string $table |
||
207 | * @return string |
||
208 | */ |
||
209 | protected function _list_columns($table = '') |
||
215 | |||
216 | // -------------------------------------------------------------------- |
||
217 | |||
218 | /** |
||
219 | * Returns an object with field data |
||
220 | * |
||
221 | * @param string $table |
||
222 | * @return array |
||
223 | */ |
||
224 | View Code Duplication | public function field_data($table) |
|
248 | |||
249 | // -------------------------------------------------------------------- |
||
250 | |||
251 | /** |
||
252 | * Update statement |
||
253 | * |
||
254 | * Generates a platform-specific update string from the supplied data |
||
255 | * |
||
256 | * @param string $table |
||
257 | * @param array $values |
||
258 | * @return string |
||
259 | */ |
||
260 | protected function _update($table, $values) |
||
266 | |||
267 | // -------------------------------------------------------------------- |
||
268 | |||
269 | /** |
||
270 | * Delete statement |
||
271 | * |
||
272 | * Generates a platform-specific delete string from the supplied data |
||
273 | * |
||
274 | * @param string $table |
||
275 | * @return string |
||
276 | */ |
||
277 | View Code Duplication | protected function _delete($table) |
|
286 | |||
287 | // -------------------------------------------------------------------- |
||
288 | |||
289 | /** |
||
290 | * LIMIT |
||
291 | * |
||
292 | * Generates a platform-specific LIMIT clause |
||
293 | * |
||
294 | * @param string $sql SQL Query |
||
295 | * @return string |
||
296 | */ |
||
297 | View Code Duplication | protected function _limit($sql) |
|
345 | |||
346 | // -------------------------------------------------------------------- |
||
347 | |||
348 | /** |
||
349 | * Insert batch statement |
||
350 | * |
||
351 | * Generates a platform-specific insert string from the supplied data. |
||
352 | * |
||
353 | * @param string $table Table name |
||
354 | * @param array $keys INSERT keys |
||
355 | * @param array $values INSERT values |
||
356 | * @return string|bool |
||
357 | */ |
||
358 | View Code Duplication | protected function _insert_batch($table, $keys, $values) |
|
368 | |||
369 | } |
||
370 |
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.