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_oci8_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_oci8_driver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
62 | class CI_DB_oci8_driver extends CI_DB { |
||
63 | |||
64 | /** |
||
65 | * Database driver |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | public $dbdriver = 'oci8'; |
||
70 | |||
71 | /** |
||
72 | * Statement ID |
||
73 | * |
||
74 | * @var resource |
||
75 | */ |
||
76 | public $stmt_id; |
||
77 | |||
78 | /** |
||
79 | * Cursor ID |
||
80 | * |
||
81 | * @var resource |
||
82 | */ |
||
83 | public $curs_id; |
||
84 | |||
85 | /** |
||
86 | * Commit mode flag |
||
87 | * |
||
88 | * @var int |
||
89 | */ |
||
90 | public $commit_mode = OCI_COMMIT_ON_SUCCESS; |
||
91 | |||
92 | /** |
||
93 | * Limit used flag |
||
94 | * |
||
95 | * If we use LIMIT, we'll add a field that will |
||
96 | * throw off num_fields later. |
||
97 | * |
||
98 | * @var bool |
||
99 | */ |
||
100 | public $limit_used; |
||
101 | |||
102 | // -------------------------------------------------------------------- |
||
103 | |||
104 | /** |
||
105 | * Reset $stmt_id flag |
||
106 | * |
||
107 | * Used by stored_procedure() to prevent _execute() from |
||
108 | * re-setting the statement ID. |
||
109 | */ |
||
110 | protected $_reset_stmt_id = TRUE; |
||
111 | |||
112 | /** |
||
113 | * List of reserved identifiers |
||
114 | * |
||
115 | * Identifiers that must NOT be escaped. |
||
116 | * |
||
117 | * @var string[] |
||
118 | */ |
||
119 | protected $_reserved_identifiers = array('*', 'rownum'); |
||
120 | |||
121 | /** |
||
122 | * ORDER BY random keyword |
||
123 | * |
||
124 | * @var array |
||
125 | */ |
||
126 | protected $_random_keyword = array('ASC', 'ASC'); // not currently supported |
||
127 | |||
128 | /** |
||
129 | * COUNT string |
||
130 | * |
||
131 | * @used-by CI_DB_driver::count_all() |
||
132 | * @used-by CI_DB_query_builder::count_all_results() |
||
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $_count_string = 'SELECT COUNT(1) AS '; |
||
137 | |||
138 | // -------------------------------------------------------------------- |
||
139 | |||
140 | /** |
||
141 | * Class constructor |
||
142 | * |
||
143 | * @param array $params |
||
144 | * @return void |
||
145 | */ |
||
146 | public function __construct($params) |
||
224 | |||
225 | // -------------------------------------------------------------------- |
||
226 | |||
227 | /** |
||
228 | * Non-persistent database connection |
||
229 | * |
||
230 | * @param bool $persistent |
||
231 | * @return resource |
||
232 | */ |
||
233 | public function db_connect($persistent = FALSE) |
||
240 | |||
241 | // -------------------------------------------------------------------- |
||
242 | |||
243 | /** |
||
244 | * Database version number |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function version() |
||
266 | |||
267 | // -------------------------------------------------------------------- |
||
268 | |||
269 | /** |
||
270 | * Execute the query |
||
271 | * |
||
272 | * @param string $sql an SQL query |
||
273 | * @return resource |
||
274 | */ |
||
275 | protected function _execute($sql) |
||
288 | |||
289 | // -------------------------------------------------------------------- |
||
290 | |||
291 | /** |
||
292 | * Get cursor. Returns a cursor from the database |
||
293 | * |
||
294 | * @return resource |
||
295 | */ |
||
296 | public function get_cursor() |
||
300 | |||
301 | // -------------------------------------------------------------------- |
||
302 | |||
303 | /** |
||
304 | * Stored Procedure. Executes a stored procedure |
||
305 | * |
||
306 | * @param string package name in which the stored procedure is in |
||
307 | * @param string stored procedure name to execute |
||
308 | * @param array parameters |
||
309 | * @return mixed |
||
310 | * |
||
311 | * params array keys |
||
312 | * |
||
313 | * KEY OPTIONAL NOTES |
||
314 | * name no the name of the parameter should be in :<param_name> format |
||
315 | * value no the value of the parameter. If this is an OUT or IN OUT parameter, |
||
316 | * this should be a reference to a variable |
||
317 | * type yes the type of the parameter |
||
318 | * length yes the max size of the parameter |
||
319 | */ |
||
320 | public function stored_procedure($package, $procedure, array $params) |
||
350 | |||
351 | // -------------------------------------------------------------------- |
||
352 | |||
353 | /** |
||
354 | * Bind parameters |
||
355 | * |
||
356 | * @param array $params |
||
357 | * @return void |
||
358 | */ |
||
359 | protected function _bind_params($params) |
||
379 | |||
380 | // -------------------------------------------------------------------- |
||
381 | |||
382 | /** |
||
383 | * Begin Transaction |
||
384 | * |
||
385 | * @return bool |
||
386 | */ |
||
387 | protected function _trans_begin() |
||
392 | |||
393 | // -------------------------------------------------------------------- |
||
394 | |||
395 | /** |
||
396 | * Commit Transaction |
||
397 | * |
||
398 | * @return bool |
||
399 | */ |
||
400 | protected function _trans_commit() |
||
406 | |||
407 | // -------------------------------------------------------------------- |
||
408 | |||
409 | /** |
||
410 | * Rollback Transaction |
||
411 | * |
||
412 | * @return bool |
||
413 | */ |
||
414 | protected function _trans_rollback() |
||
419 | |||
420 | // -------------------------------------------------------------------- |
||
421 | |||
422 | /** |
||
423 | * Affected Rows |
||
424 | * |
||
425 | * @return int |
||
426 | */ |
||
427 | public function affected_rows() |
||
431 | |||
432 | // -------------------------------------------------------------------- |
||
433 | |||
434 | /** |
||
435 | * Insert ID |
||
436 | * |
||
437 | * @return int |
||
438 | */ |
||
439 | public function insert_id() |
||
444 | |||
445 | // -------------------------------------------------------------------- |
||
446 | |||
447 | /** |
||
448 | * Show table query |
||
449 | * |
||
450 | * Generates a platform-specific query string so that the table names can be fetched |
||
451 | * |
||
452 | * @param bool $prefix_limit |
||
453 | * @return string |
||
454 | */ |
||
455 | View Code Duplication | protected function _list_tables($prefix_limit = FALSE) |
|
467 | |||
468 | // -------------------------------------------------------------------- |
||
469 | |||
470 | /** |
||
471 | * Show column query |
||
472 | * |
||
473 | * Generates a platform-specific query string so that the column names can be fetched |
||
474 | * |
||
475 | * @param string $table |
||
476 | * @return string |
||
477 | */ |
||
478 | View Code Duplication | protected function _list_columns($table = '') |
|
493 | |||
494 | // -------------------------------------------------------------------- |
||
495 | |||
496 | /** |
||
497 | * Returns an object with field data |
||
498 | * |
||
499 | * @param string $table |
||
500 | * @return array |
||
501 | */ |
||
502 | View Code Duplication | public function field_data($table) |
|
549 | |||
550 | // -------------------------------------------------------------------- |
||
551 | |||
552 | /** |
||
553 | * Error |
||
554 | * |
||
555 | * Returns an array containing code and message of the last |
||
556 | * database error that has occured. |
||
557 | * |
||
558 | * @return array |
||
559 | */ |
||
560 | public function error() |
||
580 | |||
581 | // -------------------------------------------------------------------- |
||
582 | |||
583 | /** |
||
584 | * Insert batch statement |
||
585 | * |
||
586 | * Generates a platform-specific insert string from the supplied data |
||
587 | * |
||
588 | * @param string $table Table name |
||
589 | * @param array $keys INSERT keys |
||
590 | * @param array $values INSERT values |
||
591 | * @return string |
||
592 | */ |
||
593 | View Code Duplication | protected function _insert_batch($table, $keys, $values) |
|
605 | |||
606 | // -------------------------------------------------------------------- |
||
607 | |||
608 | /** |
||
609 | * Truncate statement |
||
610 | * |
||
611 | * Generates a platform-specific truncate string from the supplied data |
||
612 | * |
||
613 | * If the database does not support the TRUNCATE statement, |
||
614 | * then this method maps to 'DELETE FROM table' |
||
615 | * |
||
616 | * @param string $table |
||
617 | * @return string |
||
618 | */ |
||
619 | protected function _truncate($table) |
||
623 | |||
624 | // -------------------------------------------------------------------- |
||
625 | |||
626 | /** |
||
627 | * Delete statement |
||
628 | * |
||
629 | * Generates a platform-specific delete string from the supplied data |
||
630 | * |
||
631 | * @param string $table |
||
632 | * @return string |
||
633 | */ |
||
634 | View Code Duplication | protected function _delete($table) |
|
644 | |||
645 | // -------------------------------------------------------------------- |
||
646 | |||
647 | /** |
||
648 | * LIMIT |
||
649 | * |
||
650 | * Generates a platform-specific LIMIT clause |
||
651 | * |
||
652 | * @param string $sql SQL Query |
||
653 | * @return string |
||
654 | */ |
||
655 | protected function _limit($sql) |
||
661 | |||
662 | // -------------------------------------------------------------------- |
||
663 | |||
664 | /** |
||
665 | * Close DB Connection |
||
666 | * |
||
667 | * @return void |
||
668 | */ |
||
669 | protected function _close() |
||
673 | |||
674 | } |
||
675 |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.