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 | public function doUpdateAdjustDeprecated($query) { |
||
| 468 | |||
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * For update_old - would be deprecated |
||
| 473 | * |
||
| 474 | * @param string $query |
||
| 475 | * |
||
| 476 | * @return array|bool|mysqli_result|null |
||
| 477 | * @deprecated |
||
| 478 | */ |
||
| 479 | public function doUpdateOld($query) { |
||
| 482 | |||
| 483 | |||
| 484 | /** |
||
| 485 | * @param string $query |
||
| 486 | * |
||
| 487 | * @return array|bool|mysqli_result|null |
||
| 488 | */ |
||
| 489 | public function doDelete($query) { |
||
| 492 | |||
| 493 | /** |
||
| 494 | * @param string $table |
||
| 495 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 496 | * @param bool $isOneRecord |
||
| 497 | * |
||
| 498 | * @return array|bool|mysqli_result|null |
||
| 499 | */ |
||
| 500 | public function doDeleteWhere($table, $where, $isOneRecord = false) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * @param string $table |
||
| 511 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
| 512 | * |
||
| 513 | * @return array|bool|mysqli_result|null |
||
| 514 | */ |
||
| 515 | public function doDeleteRowWhere($table, $where) { |
||
| 518 | |||
| 519 | /** |
||
| 520 | * @param string $query |
||
| 521 | * |
||
| 522 | * @return array|bool|mysqli_result|null |
||
| 523 | */ |
||
| 524 | public function doDeleteComplex($query) { |
||
| 527 | |||
| 528 | /** |
||
| 529 | * Early deprecated function for complex delete conditions |
||
| 530 | * |
||
| 531 | * Usually used for mallformed $where conditions |
||
| 532 | * |
||
| 533 | * @param $table |
||
| 534 | * @param $where |
||
| 535 | * |
||
| 536 | * @return array|bool|mysqli_result|null |
||
| 537 | * @deprecated |
||
| 538 | */ |
||
| 539 | public function doDeleteDeprecated($table, $where) { |
||
| 542 | |||
| 543 | |||
| 544 | protected function castAsDbValue($value) { |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Make field list safe |
||
| 577 | * |
||
| 578 | * Support expressions - expression index should be strictly integer! |
||
| 579 | * |
||
| 580 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 581 | * |
||
| 582 | * @return array |
||
| 583 | */ |
||
| 584 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
| 602 | |||
| 603 | /** |
||
| 604 | * Make fields adjustment safe |
||
| 605 | * |
||
| 606 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
| 607 | * Supports expressions - expression index should be strictly integer! |
||
| 608 | * |
||
| 609 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
| 610 | * |
||
| 611 | * @return array |
||
| 612 | */ |
||
| 613 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
| 631 | |||
| 632 | // TODO - redo as callable usage with array_map/array_walk |
||
| 633 | View Code Duplication | public function safeValues($values) { |
|
| 646 | |||
| 647 | // TODO - redo as callable usage with array_map/array_walk |
||
| 648 | View Code Duplication | public function safeFields($fields) { |
|
| 661 | |||
| 662 | |||
| 663 | /** |
||
| 664 | * Returns iterator to iterate through mysqli_result |
||
| 665 | * |
||
| 666 | * @param string $query |
||
| 667 | * |
||
| 668 | * return DbResultIterator |
||
| 669 | * |
||
| 670 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
| 671 | */ |
||
| 672 | public function doSelectIterator($query) { |
||
| 683 | |||
| 684 | /** |
||
| 685 | * @param DbQueryConstructor $stmt |
||
| 686 | * @param bool $skip_query_check |
||
| 687 | */ |
||
| 688 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
| 698 | |||
| 699 | // TODO Заменить это на новый логгер |
||
| 700 | protected function security_watch_user_queries($query) { |
||
| 721 | |||
| 722 | |||
| 723 | public function security_query_check_bad_words($query) { |
||
| 780 | |||
| 781 | /** |
||
| 782 | * @param bool $prefixed_only |
||
| 783 | * |
||
| 784 | * @return array |
||
| 785 | */ |
||
| 786 | public function db_get_table_list($prefixed_only = true) { |
||
| 806 | |||
| 807 | /** |
||
| 808 | * @param string $statement |
||
| 809 | * |
||
| 810 | * @return bool|mysqli_stmt |
||
| 811 | */ |
||
| 812 | View Code Duplication | public function db_prepare($statement) { |
|
| 819 | |||
| 820 | |||
| 821 | /** |
||
| 822 | * L1 perform the query |
||
| 823 | * |
||
| 824 | * @param $query_string |
||
| 825 | * |
||
| 826 | * @return bool|mysqli_result |
||
| 827 | */ |
||
| 828 | View Code Duplication | public function db_sql_query($query_string) { |
|
| 835 | |||
| 836 | /** |
||
| 837 | * L1 fetch assoc array |
||
| 838 | * |
||
| 839 | * @param mysqli_result $query |
||
| 840 | * |
||
| 841 | * @return array|null |
||
| 842 | */ |
||
| 843 | View Code Duplication | public function db_fetch(&$query) { |
|
| 850 | |||
| 851 | public function db_fetch_row(&$query) { |
||
| 854 | |||
| 855 | public function db_escape($unescaped_string) { |
||
| 858 | |||
| 859 | public function driver_disconnect() { |
||
| 862 | |||
| 863 | public function db_error() { |
||
| 866 | |||
| 867 | public function db_insert_id() { |
||
| 870 | |||
| 871 | public function db_num_rows(&$result) { |
||
| 874 | |||
| 875 | public function db_affected_rows() { |
||
| 878 | |||
| 879 | /** |
||
| 880 | * @return string |
||
| 881 | */ |
||
| 882 | public function db_get_client_info() { |
||
| 885 | |||
| 886 | /** |
||
| 887 | * @return string |
||
| 888 | */ |
||
| 889 | public function db_get_server_info() { |
||
| 892 | |||
| 893 | /** |
||
| 894 | * @return string |
||
| 895 | */ |
||
| 896 | public function db_get_host_info() { |
||
| 899 | |||
| 900 | public function db_get_server_stat() { |
||
| 911 | |||
| 912 | /** |
||
| 913 | * @return array |
||
| 914 | * @throws Exception |
||
| 915 | */ |
||
| 916 | public function db_core_show_status() { |
||
| 929 | |||
| 930 | public function mysql_get_table_list() { |
||
| 933 | |||
| 934 | public function mysql_get_innodb_status() { |
||
| 937 | |||
| 938 | /** |
||
| 939 | * @return \DBAL\DbTransaction |
||
| 940 | */ |
||
| 941 | public function getTransaction() { |
||
| 944 | |||
| 945 | // Some wrappers to DbTransaction |
||
| 946 | // Unused for now |
||
| 947 | public function transactionCheck($status = null) { |
||
| 950 | |||
| 951 | public function transactionStart($level = '') { |
||
| 954 | |||
| 955 | public function transactionCommit() { |
||
| 958 | |||
| 959 | public function transactionRollback() { |
||
| 962 | |||
| 963 | } |
||
| 964 |