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  | 
            ||
| 13 | class db_mysql { | 
            ||
| 14 | const TRANSACTION_SERIALIZABLE = 'SERIALIZABLE';  | 
            ||
| 15 | const TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ';  | 
            ||
| 16 | const TRANSACTION_READ_COMMITTED = 'READ COMMITTED';  | 
            ||
| 17 | const TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED';  | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * Статус соеднения с MySQL  | 
            ||
| 21 | *  | 
            ||
| 22 | * @var bool  | 
            ||
| 23 | */  | 
            ||
| 24 | public $connected = false;  | 
            ||
| 25 | /**  | 
            ||
| 26 | * Префикс названий таблиц в БД  | 
            ||
| 27 | *  | 
            ||
| 28 | * @var string  | 
            ||
| 29 | */  | 
            ||
| 30 | public $db_prefix = '';  | 
            ||
| 31 | /**  | 
            ||
| 32 | * Список таблиц в БД  | 
            ||
| 33 | *  | 
            ||
| 34 | * @var array  | 
            ||
| 35 | */  | 
            ||
| 36 | public $table_list = array();  | 
            ||
| 37 | |||
| 38 | /**  | 
            ||
| 39 | * Настройки БД  | 
            ||
| 40 | *  | 
            ||
| 41 | * @var array  | 
            ||
| 42 | */  | 
            ||
| 43 | protected $dbsettings = array();  | 
            ||
| 44 | /**  | 
            ||
| 45 | * Драйвер для прямого обращения к MySQL  | 
            ||
| 46 | *  | 
            ||
| 47 | * @var db_mysql_v5 $driver  | 
            ||
| 48 | */  | 
            ||
| 49 | public $driver = null;  | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * Общее время запросов  | 
            ||
| 53 | *  | 
            ||
| 54 | * @var float $time_mysql_total  | 
            ||
| 55 | */  | 
            ||
| 56 | public $time_mysql_total = 0.0;  | 
            ||
| 57 | |||
| 58 | /**  | 
            ||
| 59 | * Amount of queries on this DB  | 
            ||
| 60 | *  | 
            ||
| 61 | * @var int  | 
            ||
| 62 | */  | 
            ||
| 63 | public $queryCount = 0;  | 
            ||
| 64 | |||
| 65 | public $isWatching = false;  | 
            ||
| 66 | |||
| 67 | /**  | 
            ||
| 68 | * @var \DBAL\DbTransaction $transaction  | 
            ||
| 69 | */  | 
            ||
| 70 | protected $transaction;  | 
            ||
| 71 | |||
| 72 | /**  | 
            ||
| 73 | * Should query check be skipped?  | 
            ||
| 74 | *  | 
            ||
| 75 | * Used for altering scheme of DB  | 
            ||
| 76 | *  | 
            ||
| 77 | * @var bool $skipQueryCheck  | 
            ||
| 78 | */  | 
            ||
| 79 | protected $skipQueryCheck = false;  | 
            ||
| 80 | |||
| 81 | /**  | 
            ||
| 82 | * @var SnCache $snCache  | 
            ||
| 83 | */  | 
            ||
| 84 | public $snCache;  | 
            ||
| 85 | |||
| 86 | /**  | 
            ||
| 87 | * db_mysql constructor.  | 
            ||
| 88 | *  | 
            ||
| 89 | * @param \Common\GlobalContainer $gc  | 
            ||
| 90 | */  | 
            ||
| 91 |   public function __construct($gc) { | 
            ||
| 95 | |||
| 96 |   public function load_db_settings($configFile = '') { | 
            ||
| 105 | |||
| 106 | /**  | 
            ||
| 107 | * @param null|array $external_db_settings  | 
            ||
| 108 | *  | 
            ||
| 109 | * @return bool  | 
            ||
| 110 | */  | 
            ||
| 111 |   public function sn_db_connect($external_db_settings = null) { | 
            ||
| 140 | |||
| 141 |   protected function driver_connect() { | 
            ||
| 152 | |||
| 153 |   public function db_disconnect() { | 
            ||
| 161 | |||
| 162 | /**  | 
            ||
| 163 | * @param string $query  | 
            ||
| 164 | *  | 
            ||
| 165 | * @return mixed|string  | 
            ||
| 166 | */  | 
            ||
| 167 |   public function replaceTablePlaceholders($query) { | 
            ||
| 177 | |||
| 178 | /**  | 
            ||
| 179 | * @param $query  | 
            ||
| 180 | */  | 
            ||
| 181 |   protected function logQuery($query) { | 
            ||
| 192 | |||
| 193 | |||
| 194 | /**  | 
            ||
| 195 | * @return string  | 
            ||
| 196 | */  | 
            ||
| 197 |   public function queryTrace() { | 
            ||
| 216 | |||
| 217 | /**  | 
            ||
| 218 | * @param string $query  | 
            ||
| 219 | *  | 
            ||
| 220 | * @return array|bool|mysqli_result|null  | 
            ||
| 221 | * @internal param bool $skip_query_check  | 
            ||
| 222 | *  | 
            ||
| 223 | */  | 
            ||
| 224 |   protected function doquery($query) { | 
            ||
| 254 | |||
| 255 | |||
| 256 | // Just wrappers to distinguish query types  | 
            ||
| 257 | /**  | 
            ||
| 258 | * Executes non-data manipulation statements  | 
            ||
| 259 | *  | 
            ||
| 260 | * Can execute queries with check skip  | 
            ||
| 261 | * Honor current state of query checking  | 
            ||
| 262 | *  | 
            ||
| 263 | * @param string $query  | 
            ||
| 264 | * @param bool $skip_query_check  | 
            ||
| 265 | *  | 
            ||
| 266 | * @return array|bool|mysqli_result|null  | 
            ||
| 267 | */  | 
            ||
| 268 |   public function doExecute($query, $skip_query_check = false) { | 
            ||
| 282 | |||
| 283 | |||
| 284 |   public function doSelect($query) { | 
            ||
| 287 | |||
| 288 | /**  | 
            ||
| 289 | * @param string $query  | 
            ||
| 290 | *  | 
            ||
| 291 | * @return array|null  | 
            ||
| 292 | */  | 
            ||
| 293 |   public function doSelectFetch($query) { | 
            ||
| 296 | |||
| 297 | /**  | 
            ||
| 298 | * @param string $query  | 
            ||
| 299 | *  | 
            ||
| 300 | * @return mixed|null  | 
            ||
| 301 | */  | 
            ||
| 302 |   public function doSelectFetchValue($query) { | 
            ||
| 307 | |||
| 308 | |||
| 309 |   public function doInsertComplex($query) { | 
            ||
| 312 | |||
| 313 |   protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { | 
            ||
| 333 | |||
| 334 | /**  | 
            ||
| 335 | * @param string $table  | 
            ||
| 336 | * @param array $fieldsAndValues  | 
            ||
| 337 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE  | 
            ||
| 338 | *  | 
            ||
| 339 | * @return array|bool|mysqli_result|null  | 
            ||
| 340 | */  | 
            ||
| 341 |   public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { | 
            ||
| 344 | |||
| 345 |   public function doReplaceSet($table, $fieldsAndValues) { | 
            ||
| 348 | |||
| 349 | /**  | 
            ||
| 350 | * Values should be passed as-is  | 
            ||
| 351 | *  | 
            ||
| 352 | * @param string $table  | 
            ||
| 353 | * @param array $fields  | 
            ||
| 354 | * @param string[] $values  | 
            ||
| 355 | * @param bool $replace  | 
            ||
| 356 | *  | 
            ||
| 357 | * @return array|bool|mysqli_result|null  | 
            ||
| 358 | * @deprecated  | 
            ||
| 359 | */  | 
            ||
| 360 |   protected function doValuesDeprecated($table, $fields, &$values, $replace = DB_INSERT_PLAIN) { | 
            ||
| 369 | |||
| 370 | // TODO - batch insert and replace here  | 
            ||
| 371 | |||
| 372 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что  | 
            ||
| 373 | // null - это null, а не строка'NULL'  | 
            ||
| 374 | |||
| 375 | /**  | 
            ||
| 376 | * Values should be passed as-is  | 
            ||
| 377 | *  | 
            ||
| 378 | * @param string $table  | 
            ||
| 379 | * @param array $fields  | 
            ||
| 380 | * @param string[] $values  | 
            ||
| 381 | *  | 
            ||
| 382 | * @return array|bool|mysqli_result|null  | 
            ||
| 383 | * @deprecated  | 
            ||
| 384 | */  | 
            ||
| 385 |   public function doInsertValuesDeprecated($table, $fields, &$values) { | 
            ||
| 388 | |||
| 389 | /**  | 
            ||
| 390 | * Values should be passed as-is  | 
            ||
| 391 | *  | 
            ||
| 392 | * @param string $table  | 
            ||
| 393 | * @param array $fields  | 
            ||
| 394 | * @param string[] $values  | 
            ||
| 395 | *  | 
            ||
| 396 | * @return array|bool|mysqli_result|null  | 
            ||
| 397 | * @deprecated  | 
            ||
| 398 | */  | 
            ||
| 399 |   public function doReplaceValuesDeprecated($table, $fields, &$values) { | 
            ||
| 402 | |||
| 403 | |||
| 404 |   public function doUpdateComplex($query) { | 
            ||
| 407 | |||
| 408 |   public function doUpdateReallyComplex($query) { | 
            ||
| 411 | |||
| 412 | /**  | 
            ||
| 413 | * Executes self-contained SQL UPDATE query  | 
            ||
| 414 | *  | 
            ||
| 415 | * Self-contained - means no params used  | 
            ||
| 416 | * Such queries usually used to make large amount of in-base calculations  | 
            ||
| 417 | *  | 
            ||
| 418 | * @param $query  | 
            ||
| 419 | *  | 
            ||
| 420 | * @return array|bool|mysqli_result|null  | 
            ||
| 421 | */  | 
            ||
| 422 |   public function doUpdateSqlNoParam($query) { | 
            ||
| 425 | |||
| 426 |   protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL) { | 
            ||
| 448 | |||
| 449 |   public function doUpdateRowSet($table, $fieldsAndValues, $where) { | 
            ||
| 452 | |||
| 453 |   public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { | 
            ||
| 456 | |||
| 457 |   public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { | 
            ||
| 460 | |||
| 461 |   public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where) { | 
            ||
| 464 | |||
| 465 | /**  | 
            ||
| 466 | * In this call means that used some conditions in $where different than field = value  | 
            ||
| 467 | *  | 
            ||
| 468 | * It should be rewrote later  | 
            ||
| 469 | *  | 
            ||
| 470 | * @param $table  | 
            ||
| 471 | * @param $fieldsSet  | 
            ||
| 472 | * @param $fieldsAdjust  | 
            ||
| 473 | * @param $where  | 
            ||
| 474 | *  | 
            ||
| 475 | * @return array|bool|mysqli_result|null  | 
            ||
| 476 | * @deprecated  | 
            ||
| 477 | */  | 
            ||
| 478 |   public function doUpdateTableAdjustDanger($table, $fieldsSet, $fieldsAdjust, $where) { | 
            ||
| 481 | |||
| 482 |   public function doUpdateAdjustDeprecated($query) { | 
            ||
| 485 | |||
| 486 | |||
| 487 | |||
| 488 | /**  | 
            ||
| 489 | * For update_old - would be deprecated  | 
            ||
| 490 | *  | 
            ||
| 491 | * @param string $query  | 
            ||
| 492 | *  | 
            ||
| 493 | * @return array|bool|mysqli_result|null  | 
            ||
| 494 | * @deprecated  | 
            ||
| 495 | */  | 
            ||
| 496 |   public function doUpdateOld($query) { | 
            ||
| 499 | |||
| 500 | |||
| 501 | /**  | 
            ||
| 502 | * @param string $query  | 
            ||
| 503 | *  | 
            ||
| 504 | * @return array|bool|mysqli_result|null  | 
            ||
| 505 | */  | 
            ||
| 506 |   public function doDelete($query) { | 
            ||
| 509 | |||
| 510 | /**  | 
            ||
| 511 | * @param string $table  | 
            ||
| 512 | * @param array $where - simple WHERE statement list which can be combined with AND  | 
            ||
| 513 | * @param bool $isOneRecord  | 
            ||
| 514 | *  | 
            ||
| 515 | * @return array|bool|mysqli_result|null  | 
            ||
| 516 | */  | 
            ||
| 517 |   public function doDeleteWhere($table, $where, $isOneRecord = false) { | 
            ||
| 525 | |||
| 526 | /**  | 
            ||
| 527 | * @param string $table  | 
            ||
| 528 | * @param array $where - simple WHERE statement list which can be combined with AND  | 
            ||
| 529 | *  | 
            ||
| 530 | * @return array|bool|mysqli_result|null  | 
            ||
| 531 | */  | 
            ||
| 532 |   public function doDeleteRowWhere($table, $where) { | 
            ||
| 535 | |||
| 536 | /**  | 
            ||
| 537 | * @param string $query  | 
            ||
| 538 | *  | 
            ||
| 539 | * @return array|bool|mysqli_result|null  | 
            ||
| 540 | */  | 
            ||
| 541 |   public function doDeleteComplex($query) { | 
            ||
| 544 | |||
| 545 | /**  | 
            ||
| 546 | * Early deprecated function for complex delete conditions  | 
            ||
| 547 | *  | 
            ||
| 548 | * Usually used for mallformed $where conditions  | 
            ||
| 549 | *  | 
            ||
| 550 | * @param $table  | 
            ||
| 551 | * @param $where  | 
            ||
| 552 | *  | 
            ||
| 553 | * @return array|bool|mysqli_result|null  | 
            ||
| 554 | * @deprecated  | 
            ||
| 555 | */  | 
            ||
| 556 |   public function doDeleteDeprecated($table, $where) { | 
            ||
| 559 | |||
| 560 | |||
| 561 |   protected function castAsDbValue($value) { | 
            ||
| 591 | |||
| 592 | /**  | 
            ||
| 593 | * Make field list safe  | 
            ||
| 594 | *  | 
            ||
| 595 | * Support expressions - expression index should be strictly integer!  | 
            ||
| 596 | *  | 
            ||
| 597 | * @param array $fields - array of pair $fieldName => $fieldValue  | 
            ||
| 598 | *  | 
            ||
| 599 | * @return array  | 
            ||
| 600 | */  | 
            ||
| 601 | View Code Duplication |   protected function safeFieldsEqualValues($fields) { | 
            |
| 619 | |||
| 620 | /**  | 
            ||
| 621 | * Make fields adjustment safe  | 
            ||
| 622 | *  | 
            ||
| 623 | * Convert "key => value" pair to string "`key` = `key` + (value)"  | 
            ||
| 624 | * Supports expressions - expression index should be strictly integer!  | 
            ||
| 625 | *  | 
            ||
| 626 | * @param array $fields - array of pair $fieldName => $fieldValue  | 
            ||
| 627 | *  | 
            ||
| 628 | * @return array  | 
            ||
| 629 | */  | 
            ||
| 630 | View Code Duplication |   protected function safeFieldsAdjust($fields) { | 
            |
| 648 | |||
| 649 | // TODO - redo as callable usage with array_map/array_walk  | 
            ||
| 650 | View Code Duplication |   public function safeValues($values) { | 
            |
| 663 | |||
| 664 | // TODO - redo as callable usage with array_map/array_walk  | 
            ||
| 665 | View Code Duplication |   public function safeFields($fields) { | 
            |
| 678 | |||
| 679 | |||
| 680 | /**  | 
            ||
| 681 | * Returns iterator to iterate through mysqli_result  | 
            ||
| 682 | *  | 
            ||
| 683 | * @param string $query  | 
            ||
| 684 | *  | 
            ||
| 685 | * return DbResultIterator  | 
            ||
| 686 | *  | 
            ||
| 687 | * @return DbEmptyIterator|DbMysqliResultIterator  | 
            ||
| 688 | */  | 
            ||
| 689 |   public function doSelectIterator($query) { | 
            ||
| 700 | |||
| 701 | /**  | 
            ||
| 702 | * @param DbQueryConstructor $stmt  | 
            ||
| 703 | * @param bool $skip_query_check  | 
            ||
| 704 | */  | 
            ||
| 705 |   public function doStmtLockAll($stmt, $skip_query_check = false) { | 
            ||
| 715 | |||
| 716 | // TODO Заменить это на новый логгер  | 
            ||
| 717 |   protected function security_watch_user_queries($query) { | 
            ||
| 738 | |||
| 739 | |||
| 740 |   public function security_query_check_bad_words($query) { | 
            ||
| 797 | |||
| 798 | /**  | 
            ||
| 799 | * @param bool $prefixed_only  | 
            ||
| 800 | *  | 
            ||
| 801 | * @return array  | 
            ||
| 802 | */  | 
            ||
| 803 |   public function db_get_table_list($prefixed_only = true) { | 
            ||
| 823 | |||
| 824 | /**  | 
            ||
| 825 | * @param string $statement  | 
            ||
| 826 | *  | 
            ||
| 827 | * @return bool|mysqli_stmt  | 
            ||
| 828 | */  | 
            ||
| 829 | View Code Duplication |   public function db_prepare($statement) { | 
            |
| 836 | |||
| 837 | |||
| 838 | /**  | 
            ||
| 839 | * L1 perform the query  | 
            ||
| 840 | *  | 
            ||
| 841 | * @param $query_string  | 
            ||
| 842 | *  | 
            ||
| 843 | * @return bool|mysqli_result  | 
            ||
| 844 | */  | 
            ||
| 845 | View Code Duplication |   public function db_sql_query($query_string) { | 
            |
| 852 | |||
| 853 | /**  | 
            ||
| 854 | * L1 fetch assoc array  | 
            ||
| 855 | *  | 
            ||
| 856 | * @param mysqli_result $query  | 
            ||
| 857 | *  | 
            ||
| 858 | * @return array|null  | 
            ||
| 859 | */  | 
            ||
| 860 | View Code Duplication |   public function db_fetch(&$query) { | 
            |
| 867 | |||
| 868 |   public function db_fetch_row(&$query) { | 
            ||
| 871 | |||
| 872 |   public function db_escape($unescaped_string) { | 
            ||
| 875 | |||
| 876 |   public function driver_disconnect() { | 
            ||
| 879 | |||
| 880 |   public function db_error() { | 
            ||
| 883 | |||
| 884 |   public function db_insert_id() { | 
            ||
| 887 | |||
| 888 |   public function db_num_rows(&$result) { | 
            ||
| 891 | |||
| 892 |   public function db_affected_rows() { | 
            ||
| 895 | |||
| 896 | /**  | 
            ||
| 897 | * @return string  | 
            ||
| 898 | */  | 
            ||
| 899 |   public function db_get_client_info() { | 
            ||
| 902 | |||
| 903 | /**  | 
            ||
| 904 | * @return string  | 
            ||
| 905 | */  | 
            ||
| 906 |   public function db_get_server_info() { | 
            ||
| 909 | |||
| 910 | /**  | 
            ||
| 911 | * @return string  | 
            ||
| 912 | */  | 
            ||
| 913 |   public function db_get_host_info() { | 
            ||
| 916 | |||
| 917 |   public function db_get_server_stat() { | 
            ||
| 928 | |||
| 929 | /**  | 
            ||
| 930 | * @return array  | 
            ||
| 931 | * @throws Exception  | 
            ||
| 932 | */  | 
            ||
| 933 |   public function db_core_show_status() { | 
            ||
| 946 | |||
| 947 |   public function mysql_get_table_list() { | 
            ||
| 950 | |||
| 951 |   public function mysql_get_innodb_status() { | 
            ||
| 954 | |||
| 955 | /**  | 
            ||
| 956 | * @return \DBAL\DbTransaction  | 
            ||
| 957 | */  | 
            ||
| 958 |   public function getTransaction() { | 
            ||
| 961 | |||
| 962 | // Some wrappers to DbTransaction  | 
            ||
| 963 | // Unused for now  | 
            ||
| 964 |   public function transactionCheck($status = null) { | 
            ||
| 967 | |||
| 968 |   public function transactionStart($level = '') { | 
            ||
| 971 | |||
| 972 |   public function transactionCommit() { | 
            ||
| 975 | |||
| 976 |   public function transactionRollback() { | 
            ||
| 979 | |||
| 980 | }  | 
            ||
| 981 |