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) { |
||
| 74 | $this->db_disconnect(); |
||
| 75 | |||
| 76 | if (!empty($external_db_settings) && is_array($external_db_settings)) { |
||
| 77 | $this->dbsettings = $external_db_settings; |
||
| 78 | } |
||
| 79 | |||
| 80 | if (empty($this->dbsettings)) { |
||
| 81 | $this->load_db_settings(); |
||
| 82 | } |
||
| 83 | |||
| 84 | // TODO - фатальные (?) ошибки на каждом шагу. Хотя - скорее Эксепшны |
||
| 85 | if (!empty($this->dbsettings)) { |
||
| 86 | $driver_name = empty($this->dbsettings['sn_driver']) ? 'db_mysql_v5' : $this->dbsettings['sn_driver']; |
||
| 87 | $this->driver = new $driver_name(); |
||
| 88 | $this->db_prefix = $this->dbsettings['prefix']; |
||
| 89 | |||
| 90 | $this->connected = $this->connected || $this->driver_connect(); |
||
| 91 | |||
| 92 | if ($this->connected) { |
||
| 93 | $this->table_list = $this->db_get_table_list(); |
||
| 94 | // TODO Проверка на пустоту |
||
| 95 | } |
||
| 96 | } else { |
||
| 97 | $this->connected = false; |
||
| 98 | } |
||
| 99 | |||
| 100 | return $this->connected; |
||
| 101 | } |
||
| 102 | |||
| 103 | protected function driver_connect() { |
||
| 104 | if (!is_object($this->driver)) { |
||
| 105 | classSupernova::$debug->error_fatal('DB Error - No driver for MySQL found!'); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (!method_exists($this->driver, 'mysql_connect')) { |
||
| 109 | classSupernova::$debug->error_fatal('DB Error - WRONG MySQL driver!'); |
||
| 110 | } |
||
| 111 | |||
| 112 | return $this->driver->mysql_connect($this->dbsettings); |
||
| 113 | } |
||
| 114 | |||
| 115 | public function db_disconnect() { |
||
| 123 | |||
| 124 | /** |
||
| 125 | * @param string $query |
||
| 126 | * |
||
| 127 | * @return mixed|string |
||
| 128 | */ |
||
| 129 | public function replaceTablePlaceholders($query) { |
||
| 130 | $sql = $query; |
||
| 131 | if (strpos($sql, '{{') !== false) { |
||
| 132 | foreach ($this->table_list as $tableName) { |
||
| 133 | $sql = str_replace("{{{$tableName}}}", $this->db_prefix . $tableName, $sql); |
||
| 134 | } |
||
| 135 | } |
||
| 136 | |||
| 137 | return $sql; |
||
| 138 | } |
||
| 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 DbSqlStatement $statement |
||
| 182 | * |
||
| 183 | * @return array|bool|mysqli_result|null |
||
| 184 | */ |
||
| 185 | public function execute($statement) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param DbSqlStatement $statement |
||
| 191 | * |
||
| 192 | * @return array|null |
||
| 193 | */ |
||
| 194 | public function fetchOne($statement) { |
||
| 199 | |||
| 200 | /** |
||
| 201 | * @param string|DbSqlPrepare $query |
||
| 202 | * @param string $table |
||
| 203 | * @param bool $fetch |
||
| 204 | * @param bool $skip_query_check |
||
| 205 | * |
||
| 206 | * @return array|bool|mysqli_result|null |
||
| 207 | */ |
||
| 208 | public function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
||
| 257 | |||
| 258 | |||
| 259 | // TODO Заменить это на новый логгер |
||
| 260 | protected function security_watch_user_queries($query) { |
||
| 281 | |||
| 282 | |||
| 283 | public function security_query_check_bad_words($query, $skip_query_check = false) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param bool $prefixed_only |
||
| 343 | * |
||
| 344 | * @return array |
||
| 345 | */ |
||
| 346 | public function db_get_table_list($prefixed_only = true) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param string $statement |
||
| 369 | * |
||
| 370 | * @return bool|mysqli_stmt |
||
| 371 | */ |
||
| 372 | View Code Duplication | public function db_prepare($statement) { |
|
| 379 | |||
| 380 | |||
| 381 | /** |
||
| 382 | * L1 perform the query |
||
| 383 | * |
||
| 384 | * @param $query_string |
||
| 385 | * |
||
| 386 | * @return bool|mysqli_result |
||
| 387 | */ |
||
| 388 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 395 | |||
| 396 | /** |
||
| 397 | * L1 fetch assoc array |
||
| 398 | * |
||
| 399 | * @param $query |
||
| 400 | * |
||
| 401 | * @return array|null |
||
| 402 | */ |
||
| 403 | View Code Duplication | public function db_fetch(&$query) { |
|
| 410 | |||
| 411 | public function db_fetch_row(&$query) { |
||
| 414 | |||
| 415 | public function db_escape($unescaped_string) { |
||
| 418 | |||
| 419 | public function driver_disconnect() { |
||
| 422 | |||
| 423 | public function db_error() { |
||
| 426 | |||
| 427 | public function db_insert_id() { |
||
| 430 | |||
| 431 | public function db_num_rows(&$result) { |
||
| 434 | |||
| 435 | public function db_affected_rows() { |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function db_get_client_info() { |
||
| 445 | |||
| 446 | /** |
||
| 447 | * @return string |
||
| 448 | */ |
||
| 449 | public function db_get_server_info() { |
||
| 452 | |||
| 453 | /** |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function db_get_host_info() { |
||
| 459 | |||
| 460 | public function db_get_server_stat() { |
||
| 471 | |||
| 472 | /** |
||
| 473 | * @return array |
||
| 474 | * @throws Exception |
||
| 475 | */ |
||
| 476 | public function db_core_show_status() { |
||
| 489 | |||
| 490 | public function mysql_get_table_list() { |
||
| 493 | |||
| 494 | public function mysql_get_innodb_status() { |
||
| 497 | |||
| 498 | } |
||
| 499 |