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 db_mysql 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 db_mysql, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class db_mysql { |
||
| 9 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 10 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 11 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 12 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * Статус соеднения с MySQL |
||
| 16 | * |
||
| 17 | * @var bool |
||
| 18 | */ |
||
| 19 | public $connected = false; |
||
| 20 | /** |
||
| 21 | * Префикс названий таблиц в БД |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | public $db_prefix = ''; |
||
| 26 | /** |
||
| 27 | * Список таблиц в БД |
||
| 28 | * |
||
| 29 | * @var array |
||
| 30 | */ |
||
| 31 | public $table_list = array(); |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Настройки БД |
||
| 35 | * |
||
| 36 | * @var array |
||
| 37 | */ |
||
| 38 | protected $dbsettings = array(); |
||
| 39 | /** |
||
| 40 | * Драйвер для прямого обращения к MySQL |
||
| 41 | * |
||
| 42 | * @var db_mysql_v5 $driver |
||
| 43 | */ |
||
| 44 | public $driver = null; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * Общее время запросов |
||
| 48 | * |
||
| 49 | * @var float $time_mysql_total |
||
| 50 | */ |
||
| 51 | public $time_mysql_total = 0.0; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Amount of queries on this DB |
||
| 55 | * |
||
| 56 | * @var int |
||
| 57 | */ |
||
| 58 | public $queryCount = 0; |
||
| 59 | |||
| 60 | public $isWatching = false; |
||
| 61 | |||
| 62 | public function __construct() { |
||
| 64 | |||
| 65 | public function load_db_settings() { |
||
| 72 | |||
| 73 | public function sn_db_connect($external_db_settings = null) { |
||
| 102 | |||
| 103 | protected function driver_connect() { |
||
| 114 | |||
| 115 | public function db_disconnect() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $query |
||
| 126 | * |
||
| 127 | * @return mixed|string |
||
| 128 | */ |
||
| 129 | public function replaceTablePlaceholders($query) { |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param $query |
||
| 142 | * @param $fetch |
||
| 143 | */ |
||
| 144 | protected function logQuery($query, $fetch) { |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @return string |
||
| 159 | */ |
||
| 160 | public function queryTrace() { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param string $query |
||
| 182 | * @param string $table |
||
| 183 | * @param bool $fetch |
||
| 184 | * @param bool $skip_query_check |
||
| 185 | * |
||
| 186 | * @return array|bool|mysqli_result|null |
||
| 187 | */ |
||
| 188 | public function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $query |
||
| 231 | * @param bool $skip_query_check |
||
| 232 | * |
||
| 233 | * @return array|null |
||
| 234 | */ |
||
| 235 | public function doQueryFetch($query, $skip_query_check = false) { |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param string $query |
||
| 243 | * @param bool $skip_query_check |
||
| 244 | * |
||
| 245 | * @return mixed|null |
||
| 246 | */ |
||
| 247 | public function doQueryFetchValue($query, $skip_query_check = false) { |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Returns iterator to iterate through mysqli_result |
||
| 255 | * |
||
| 256 | * @param string $query |
||
| 257 | * @param bool $skip_query_check |
||
| 258 | * |
||
| 259 | * return DbResultIterator |
||
| 260 | * |
||
| 261 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 262 | */ |
||
| 263 | public function doQueryIterator($query, $skip_query_check = false) { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * @param DbQueryConstructor $stmt |
||
| 277 | * @param bool $skip_query_check |
||
| 278 | */ |
||
| 279 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 291 | |||
| 292 | // TODO Заменить это на новый логгер |
||
| 293 | protected function security_watch_user_queries($query) { |
||
| 314 | |||
| 315 | |||
| 316 | public function security_query_check_bad_words($query, $skip_query_check = false) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * @param bool $prefixed_only |
||
| 376 | * |
||
| 377 | * @return array |
||
| 378 | */ |
||
| 379 | public function db_get_table_list($prefixed_only = true) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @param string $statement |
||
| 402 | * |
||
| 403 | * @return bool|mysqli_stmt |
||
| 404 | */ |
||
| 405 | View Code Duplication | public function db_prepare($statement) { |
|
| 412 | |||
| 413 | |||
| 414 | /** |
||
| 415 | * L1 perform the query |
||
| 416 | * |
||
| 417 | * @param $query_string |
||
| 418 | * |
||
| 419 | * @return bool|mysqli_result |
||
| 420 | */ |
||
| 421 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 428 | |||
| 429 | /** |
||
| 430 | * L1 fetch assoc array |
||
| 431 | * |
||
| 432 | * @param mysqli_result $query |
||
| 433 | * |
||
| 434 | * @return array|null |
||
| 435 | */ |
||
| 436 | View Code Duplication | public function db_fetch(&$query) { |
|
| 443 | |||
| 444 | public function db_fetch_row(&$query) { |
||
| 447 | |||
| 448 | public function db_escape($unescaped_string) { |
||
| 451 | |||
| 452 | public function driver_disconnect() { |
||
| 455 | |||
| 456 | public function db_error() { |
||
| 459 | |||
| 460 | public function db_insert_id() { |
||
| 463 | |||
| 464 | public function db_num_rows(&$result) { |
||
| 467 | |||
| 468 | public function db_affected_rows() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return string |
||
| 474 | */ |
||
| 475 | public function db_get_client_info() { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * @return string |
||
| 481 | */ |
||
| 482 | public function db_get_server_info() { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @return string |
||
| 488 | */ |
||
| 489 | public function db_get_host_info() { |
||
| 492 | |||
| 493 | public function db_get_server_stat() { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * @return array |
||
| 507 | * @throws Exception |
||
| 508 | */ |
||
| 509 | public function db_core_show_status() { |
||
| 522 | |||
| 523 | public function mysql_get_table_list() { |
||
| 526 | |||
| 527 | public function mysql_get_innodb_status() { |
||
| 530 | |||
| 531 | } |
||
| 532 |