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_mysql_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_mysql_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
53 | class CI_DB_pdo_mysql_driver extends CI_DB_pdo_driver { |
||
54 | |||
55 | /** |
||
56 | * Sub-driver |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | public $subdriver = 'mysql'; |
||
61 | |||
62 | /** |
||
63 | * Compression flag |
||
64 | * |
||
65 | * @var bool |
||
66 | */ |
||
67 | public $compress = FALSE; |
||
68 | |||
69 | /** |
||
70 | * Strict ON flag |
||
71 | * |
||
72 | * Whether we're running in strict SQL mode. |
||
73 | * |
||
74 | * @var bool |
||
75 | */ |
||
76 | public $stricton; |
||
77 | |||
78 | // -------------------------------------------------------------------- |
||
79 | |||
80 | /** |
||
81 | * Identifier escape character |
||
82 | * |
||
83 | * @var string |
||
84 | */ |
||
85 | protected $_escape_char = '`'; |
||
86 | |||
87 | // -------------------------------------------------------------------- |
||
88 | |||
89 | /** |
||
90 | * Class constructor |
||
91 | * |
||
92 | * Builds the DSN if not already set. |
||
93 | * |
||
94 | * @param array $params |
||
95 | * @return void |
||
96 | */ |
||
97 | View Code Duplication | public function __construct($params) |
|
114 | |||
115 | // -------------------------------------------------------------------- |
||
116 | |||
117 | /** |
||
118 | * Database connection |
||
119 | * |
||
120 | * @param bool $persistent |
||
121 | * @return object |
||
122 | */ |
||
123 | public function db_connect($persistent = FALSE) |
||
202 | |||
203 | // -------------------------------------------------------------------- |
||
204 | |||
205 | /** |
||
206 | * Select the database |
||
207 | * |
||
208 | * @param string $database |
||
209 | * @return bool |
||
210 | */ |
||
211 | View Code Duplication | public function db_select($database = '') |
|
226 | |||
227 | // -------------------------------------------------------------------- |
||
228 | |||
229 | /** |
||
230 | * Show table query |
||
231 | * |
||
232 | * Generates a platform-specific query string so that the table names can be fetched |
||
233 | * |
||
234 | * @param bool $prefix_limit |
||
235 | * @return string |
||
236 | */ |
||
237 | protected function _list_tables($prefix_limit = FALSE) |
||
248 | |||
249 | // -------------------------------------------------------------------- |
||
250 | |||
251 | /** |
||
252 | * Show column query |
||
253 | * |
||
254 | * Generates a platform-specific query string so that the column names can be fetched |
||
255 | * |
||
256 | * @param string $table |
||
257 | * @return string |
||
258 | */ |
||
259 | protected function _list_columns($table = '') |
||
263 | |||
264 | // -------------------------------------------------------------------- |
||
265 | |||
266 | /** |
||
267 | * Returns an object with field data |
||
268 | * |
||
269 | * @param string $table |
||
270 | * @return array |
||
271 | */ |
||
272 | View Code Duplication | public function field_data($table) |
|
297 | |||
298 | // -------------------------------------------------------------------- |
||
299 | |||
300 | /** |
||
301 | * Truncate statement |
||
302 | * |
||
303 | * Generates a platform-specific truncate string from the supplied data |
||
304 | * |
||
305 | * If the database does not support the TRUNCATE statement, |
||
306 | * then this method maps to 'DELETE FROM table' |
||
307 | * |
||
308 | * @param string $table |
||
309 | * @return string |
||
310 | */ |
||
311 | protected function _truncate($table) |
||
315 | |||
316 | // -------------------------------------------------------------------- |
||
317 | |||
318 | /** |
||
319 | * FROM tables |
||
320 | * |
||
321 | * Groups tables in FROM clauses if needed, so there is no confusion |
||
322 | * about operator precedence. |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | View Code Duplication | protected function _from_tables() |
|
335 | |||
336 | } |
||
337 |
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.