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 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 280 | */ |
||
| 281 | public function doStmtSelectIterator($stmt, $skip_query_check = false) { |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param DbQueryConstructor $stmt |
||
| 287 | * @param bool $skip_query_check |
||
| 288 | * |
||
| 289 | * @return array |
||
| 290 | */ |
||
| 291 | public function doStmtSelectRow($stmt, $skip_query_check = false) { |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param DbQueryConstructor $stmt |
||
| 299 | * @param bool $skip_query_check |
||
| 300 | * |
||
| 301 | * @return mixed|null |
||
| 302 | */ |
||
| 303 | public function doStmtSelectValue($stmt, $skip_query_check = false) { |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param DbQueryConstructor $stmt |
||
| 311 | * @param bool $skip_query_check |
||
| 312 | */ |
||
| 313 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 325 | |||
| 326 | // TODO Заменить это на новый логгер |
||
| 327 | protected function security_watch_user_queries($query) { |
||
| 348 | |||
| 349 | |||
| 350 | public function security_query_check_bad_words($query, $skip_query_check = false) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * @param bool $prefixed_only |
||
| 410 | * |
||
| 411 | * @return array |
||
| 412 | */ |
||
| 413 | public function db_get_table_list($prefixed_only = true) { |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @param string $statement |
||
| 436 | * |
||
| 437 | * @return bool|mysqli_stmt |
||
| 438 | */ |
||
| 439 | View Code Duplication | public function db_prepare($statement) { |
|
| 446 | |||
| 447 | |||
| 448 | /** |
||
| 449 | * L1 perform the query |
||
| 450 | * |
||
| 451 | * @param $query_string |
||
| 452 | * |
||
| 453 | * @return bool|mysqli_result |
||
| 454 | */ |
||
| 455 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 462 | |||
| 463 | /** |
||
| 464 | * L1 fetch assoc array |
||
| 465 | * |
||
| 466 | * @param mysqli_result $query |
||
| 467 | * |
||
| 468 | * @return array|null |
||
| 469 | */ |
||
| 470 | View Code Duplication | public function db_fetch(&$query) { |
|
| 477 | |||
| 478 | public function db_fetch_row(&$query) { |
||
| 481 | |||
| 482 | public function db_escape($unescaped_string) { |
||
| 485 | |||
| 486 | public function driver_disconnect() { |
||
| 489 | |||
| 490 | public function db_error() { |
||
| 493 | |||
| 494 | public function db_insert_id() { |
||
| 497 | |||
| 498 | public function db_num_rows(&$result) { |
||
| 501 | |||
| 502 | public function db_affected_rows() { |
||
| 505 | |||
| 506 | /** |
||
| 507 | * @return string |
||
| 508 | */ |
||
| 509 | public function db_get_client_info() { |
||
| 512 | |||
| 513 | /** |
||
| 514 | * @return string |
||
| 515 | */ |
||
| 516 | public function db_get_server_info() { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @return string |
||
| 522 | */ |
||
| 523 | public function db_get_host_info() { |
||
| 526 | |||
| 527 | public function db_get_server_stat() { |
||
| 538 | |||
| 539 | /** |
||
| 540 | * @return array |
||
| 541 | * @throws Exception |
||
| 542 | */ |
||
| 543 | public function db_core_show_status() { |
||
| 556 | |||
| 557 | public function mysql_get_table_list() { |
||
| 560 | |||
| 561 | public function mysql_get_innodb_status() { |
||
| 564 | |||
| 565 | } |
||
| 566 |