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 DB_MYSQL_TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 10 | const DB_MYSQL_TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 11 | const DB_MYSQL_TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 12 | const DB_MYSQL_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 $query |
||
| 126 | * |
||
| 127 | * @return mixed |
||
| 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 | * @param $sql |
||
| 159 | * |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | protected function commentQuery(&$sql) { |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param DbSqlStatement $statement |
||
| 184 | * |
||
| 185 | * @return array|bool|mysqli_result|null |
||
| 186 | */ |
||
| 187 | public function execute($statement) { |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param DbSqlStatement $statement |
||
| 193 | * |
||
| 194 | * @return array|null |
||
| 195 | */ |
||
| 196 | public function fetchOne($statement) { |
||
| 200 | |||
| 201 | public function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
||
| 221 | |||
| 222 | |||
| 223 | // TODO Заменить это на новый логгер |
||
| 224 | protected function security_watch_user_queries($query) { |
||
| 245 | |||
| 246 | |||
| 247 | public function security_query_check_bad_words($query) { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * @param bool $prefixed_only |
||
| 303 | * |
||
| 304 | * @return array |
||
| 305 | */ |
||
| 306 | public function db_get_table_list($prefixed_only = true) { |
||
| 326 | |||
| 327 | |||
| 328 | /** |
||
| 329 | * L1 perform the query |
||
| 330 | * |
||
| 331 | * @param $query_string |
||
| 332 | * |
||
| 333 | * @return bool|mysqli_result |
||
| 334 | */ |
||
| 335 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 342 | |||
| 343 | /** |
||
| 344 | * L1 fetch assoc array |
||
| 345 | * |
||
| 346 | * @param $query |
||
| 347 | * |
||
| 348 | * @return array|null |
||
| 349 | */ |
||
| 350 | View Code Duplication | public function db_fetch(&$query) { |
|
| 357 | |||
| 358 | public function db_fetch_row(&$query) { |
||
| 361 | |||
| 362 | public function db_escape($unescaped_string) { |
||
| 365 | |||
| 366 | public function driver_disconnect() { |
||
| 369 | |||
| 370 | public function db_error() { |
||
| 373 | |||
| 374 | public function db_insert_id() { |
||
| 377 | |||
| 378 | public function db_num_rows(&$result) { |
||
| 381 | |||
| 382 | public function db_affected_rows() { |
||
| 385 | |||
| 386 | /** |
||
| 387 | * @return string |
||
| 388 | */ |
||
| 389 | public function db_get_client_info() { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * @return string |
||
| 395 | */ |
||
| 396 | public function db_get_server_info() { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * @return string |
||
| 402 | */ |
||
| 403 | public function db_get_host_info() { |
||
| 406 | |||
| 407 | public function db_get_server_stat() { |
||
| 418 | |||
| 419 | /** |
||
| 420 | * @return array |
||
| 421 | * @throws Exception |
||
| 422 | */ |
||
| 423 | public function db_core_show_status() { |
||
| 436 | |||
| 437 | public function mysql_get_table_list() { |
||
| 440 | |||
| 441 | public function mysql_get_innodb_status() { |
||
| 444 | |||
| 445 | } |
||
| 446 |