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 |
||
| 6 | class db_mysql { |
||
| 7 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 8 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 9 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 10 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Статус соеднения с MySQL |
||
| 14 | * |
||
| 15 | * @var bool |
||
| 16 | */ |
||
| 17 | public $connected = false; |
||
| 18 | /** |
||
| 19 | * Префикс названий таблиц в БД |
||
| 20 | * |
||
| 21 | * @var string |
||
| 22 | */ |
||
| 23 | public $db_prefix = ''; |
||
| 24 | /** |
||
| 25 | * Список таблиц в БД |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | public $table_list = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Настройки БД |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $dbsettings = array(); |
||
| 37 | /** |
||
| 38 | * Драйвер для прямого обращения к MySQL |
||
| 39 | * |
||
| 40 | * @var db_mysql_v5 $driver |
||
| 41 | */ |
||
| 42 | public $driver = null; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Общее время запросов |
||
| 46 | * |
||
| 47 | * @var float $time_mysql_total |
||
| 48 | */ |
||
| 49 | public $time_mysql_total = 0.0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Amount of queries on this DB |
||
| 53 | * |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | public $queryCount = 0; |
||
| 57 | |||
| 58 | public $isWatching = false; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var \DBAL\DbTransaction $transaction |
||
| 62 | */ |
||
| 63 | protected $transaction; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Should query check be skipped? |
||
| 67 | * |
||
| 68 | * Used for altering sheme of DB |
||
| 69 | * |
||
| 70 | * @var bool $skipQueryCheck |
||
| 71 | */ |
||
| 72 | protected $skipQueryCheck = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * db_mysql constructor. |
||
| 76 | * |
||
| 77 | * @param \Common\GlobalContainer $gc |
||
| 78 | */ |
||
| 79 | public function __construct($gc) { |
||
| 82 | |||
| 83 | public function load_db_settings($configFile = '') { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param null|array $external_db_settings |
||
| 95 | * |
||
| 96 | * @return bool |
||
| 97 | */ |
||
| 98 | public function sn_db_connect($external_db_settings = null) { |
||
| 127 | |||
| 128 | protected function driver_connect() { |
||
| 139 | |||
| 140 | public function db_disconnect() { |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param string $query |
||
| 151 | * |
||
| 152 | * @return mixed|string |
||
| 153 | */ |
||
| 154 | public function replaceTablePlaceholders($query) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param $query |
||
| 167 | */ |
||
| 168 | protected function logQuery($query) { |
||
| 179 | |||
| 180 | |||
| 181 | /** |
||
| 182 | * @return string |
||
| 183 | */ |
||
| 184 | public function queryTrace() { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * @param string $query |
||
| 206 | * |
||
| 207 | * @return array|bool|mysqli_result|null |
||
| 208 | * @internal param bool $skip_query_check |
||
| 209 | * |
||
| 210 | */ |
||
| 211 | protected function doquery($query) { |
||
| 241 | |||
| 242 | |||
| 243 | // Just wrappers to distinguish query types |
||
| 244 | /** |
||
| 245 | * Executes non-data manipulation statements |
||
| 246 | * |
||
| 247 | * Can execute queries with check skip |
||
| 248 | * Honor current state of query checking |
||
| 249 | * |
||
| 250 | * @param string $query |
||
| 251 | * @param bool $skip_query_check |
||
| 252 | * |
||
| 253 | * @return array|bool|mysqli_result|null |
||
| 254 | */ |
||
| 255 | public function doExecute($query, $skip_query_check = false) { |
||
| 268 | |||
| 269 | |||
| 270 | public function doSelect($query) { |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @param string $query |
||
| 276 | * |
||
| 277 | * @return array|null |
||
| 278 | */ |
||
| 279 | public function doSelectFetch($query) { |
||
| 282 | |||
| 283 | |||
| 284 | public function doInsert($query) { |
||
| 287 | |||
| 288 | public function doReplace($query) { |
||
| 291 | |||
| 292 | |||
| 293 | public function doUpdate($query) { |
||
| 296 | |||
| 297 | |||
| 298 | public function doDelete($query) { |
||
| 301 | |||
| 302 | public function doDeleteRow($query) { |
||
| 305 | |||
| 306 | |||
| 307 | /** |
||
| 308 | * @param string $query |
||
| 309 | * |
||
| 310 | * @return mixed|null |
||
| 311 | */ |
||
| 312 | public function doQueryFetchValue($query) { |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns iterator to iterate through mysqli_result |
||
| 320 | * |
||
| 321 | * @param string $query |
||
| 322 | * |
||
| 323 | * return DbResultIterator |
||
| 324 | * |
||
| 325 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 326 | */ |
||
| 327 | public function doSelectIterator($query) { |
||
| 338 | |||
| 339 | /** |
||
| 340 | * @param DbQueryConstructor $stmt |
||
| 341 | * @param bool $skip_query_check |
||
| 342 | */ |
||
| 343 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 353 | |||
| 354 | // TODO Заменить это на новый логгер |
||
| 355 | protected function security_watch_user_queries($query) { |
||
| 376 | |||
| 377 | |||
| 378 | public function security_query_check_bad_words($query) { |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param bool $prefixed_only |
||
| 438 | * |
||
| 439 | * @return array |
||
| 440 | */ |
||
| 441 | public function db_get_table_list($prefixed_only = true) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * @param string $statement |
||
| 464 | * |
||
| 465 | * @return bool|mysqli_stmt |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function db_prepare($statement) { |
|
| 474 | |||
| 475 | |||
| 476 | /** |
||
| 477 | * L1 perform the query |
||
| 478 | * |
||
| 479 | * @param $query_string |
||
| 480 | * |
||
| 481 | * @return bool|mysqli_result |
||
| 482 | */ |
||
| 483 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 490 | |||
| 491 | /** |
||
| 492 | * L1 fetch assoc array |
||
| 493 | * |
||
| 494 | * @param mysqli_result $query |
||
| 495 | * |
||
| 496 | * @return array|null |
||
| 497 | */ |
||
| 498 | View Code Duplication | public function db_fetch(&$query) { |
|
| 505 | |||
| 506 | public function db_fetch_row(&$query) { |
||
| 509 | |||
| 510 | public function db_escape($unescaped_string) { |
||
| 513 | |||
| 514 | public function driver_disconnect() { |
||
| 517 | |||
| 518 | public function db_error() { |
||
| 521 | |||
| 522 | public function db_insert_id() { |
||
| 525 | |||
| 526 | public function db_num_rows(&$result) { |
||
| 529 | |||
| 530 | public function db_affected_rows() { |
||
| 533 | |||
| 534 | /** |
||
| 535 | * @return string |
||
| 536 | */ |
||
| 537 | public function db_get_client_info() { |
||
| 540 | |||
| 541 | /** |
||
| 542 | * @return string |
||
| 543 | */ |
||
| 544 | public function db_get_server_info() { |
||
| 547 | |||
| 548 | /** |
||
| 549 | * @return string |
||
| 550 | */ |
||
| 551 | public function db_get_host_info() { |
||
| 554 | |||
| 555 | public function db_get_server_stat() { |
||
| 566 | |||
| 567 | /** |
||
| 568 | * @return array |
||
| 569 | * @throws Exception |
||
| 570 | */ |
||
| 571 | public function db_core_show_status() { |
||
| 584 | |||
| 585 | public function mysql_get_table_list() { |
||
| 588 | |||
| 589 | public function mysql_get_innodb_status() { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * @return \DBAL\DbTransaction |
||
| 595 | */ |
||
| 596 | public function getTransaction() { |
||
| 599 | |||
| 600 | // Some wrappers to DbTransaction |
||
| 601 | // Unused for now |
||
| 602 | public function transactionCheck($status = null) { |
||
| 605 | |||
| 606 | public function transactionStart($level = '') { |
||
| 609 | |||
| 610 | public function transactionCommit() { |
||
| 613 | |||
| 614 | public function transactionRollback() { |
||
| 617 | |||
| 618 | } |
||
| 619 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.