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 | /** |
||
63 | * @var \DBAL\DbTransaction $transaction |
||
64 | */ |
||
65 | protected $transaction; |
||
66 | |||
67 | /** |
||
68 | * Should query check be skipped? |
||
69 | * |
||
70 | * Used for altering scheme of DB |
||
71 | * |
||
72 | * @var bool $skipQueryCheck |
||
73 | */ |
||
74 | protected $skipQueryCheck = false; |
||
75 | |||
76 | /** |
||
77 | * @var SnCache $snCache |
||
78 | */ |
||
79 | public $snCache; |
||
80 | |||
81 | /** |
||
82 | * db_mysql constructor. |
||
83 | * |
||
84 | * @param \Common\GlobalContainer $gc |
||
85 | */ |
||
86 | public function __construct($gc) { |
||
90 | |||
91 | public function load_db_settings($configFile = '') { |
||
100 | |||
101 | /** |
||
102 | * @param null|array $external_db_settings |
||
103 | * |
||
104 | * @return bool |
||
105 | */ |
||
106 | public function sn_db_connect($external_db_settings = null) { |
||
135 | |||
136 | protected function driver_connect() { |
||
147 | |||
148 | public function db_disconnect() { |
||
156 | |||
157 | /** |
||
158 | * @param string $query |
||
159 | * |
||
160 | * @return mixed|string |
||
161 | */ |
||
162 | public function replaceTablePlaceholders($query) { |
||
172 | |||
173 | /** |
||
174 | * @param $query |
||
175 | */ |
||
176 | protected function logQuery($query) { |
||
187 | |||
188 | |||
189 | /** |
||
190 | * @return string |
||
191 | */ |
||
192 | public function traceQuery() { |
||
193 | if (!defined('DEBUG_SQL_COMMENT') || constant('DEBUG_SQL_ERROR') !== true) { |
||
194 | return ''; |
||
195 | } |
||
196 | |||
197 | $backtrace = debug_backtrace(); |
||
198 | $sql_comment = classSupernova::$debug->compact_backtrace($backtrace, defined('DEBUG_SQL_COMMENT_LONG')); |
||
199 | |||
200 | if (defined('DEBUG_SQL_ERROR') && constant('DEBUG_SQL_ERROR') === true) { |
||
201 | classSupernova::$debug->add_to_array($sql_comment); |
||
202 | } |
||
203 | |||
204 | $sql_commented = implode("\r\n", $sql_comment); |
||
205 | if (defined('DEBUG_SQL_ONLINE') && constant('DEBUG_SQL_ONLINE') === true) { |
||
206 | classSupernova::$debug->warning($sql_commented, 'SQL Debug', LOG_DEBUG_SQL); |
||
207 | } |
||
208 | |||
209 | return $sql_commented; |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * @param string $query |
||
214 | * |
||
215 | * @return array|bool|mysqli_result|null |
||
216 | * @internal param bool $skip_query_check |
||
217 | * |
||
218 | */ |
||
219 | protected function queryDriver($query) { |
||
249 | |||
250 | |||
251 | // Just wrappers to distinguish query types |
||
252 | /** |
||
253 | * Executes non-data manipulation statements |
||
254 | * |
||
255 | * Can execute queries with check skip |
||
256 | * Honor current state of query checking |
||
257 | * |
||
258 | * @param string $query |
||
259 | * @param bool $skip_query_check |
||
260 | * |
||
261 | * @return array|bool|mysqli_result|null |
||
262 | */ |
||
263 | public function doSql($query, $skip_query_check = false) { |
||
277 | |||
278 | |||
279 | public function doSelect($query) { |
||
282 | |||
283 | /** |
||
284 | * @param string $query |
||
285 | * |
||
286 | * @return array|null |
||
287 | */ |
||
288 | public function doSelectFetch($query) { |
||
291 | |||
292 | /** |
||
293 | * @param string $query |
||
294 | * |
||
295 | * @return mixed|null |
||
296 | */ |
||
297 | public function doSelectFetchValue($query) { |
||
302 | |||
303 | |||
304 | protected function doSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
324 | |||
325 | // TODO - batch insert and replace here |
||
326 | // TODO - перед тем, как переделывать данные из депрекейтов - убедится, что |
||
327 | // null - это null, а не строка'NULL' |
||
328 | /** |
||
329 | * Values should be passed as-is |
||
330 | * |
||
331 | * @param string $table |
||
332 | * @param array $fields |
||
333 | * @param string[] $values |
||
334 | * @param bool $replace |
||
335 | * |
||
336 | * @return array|bool|mysqli_result|null |
||
337 | * @deprecated |
||
338 | */ |
||
339 | protected function doValuesDeprecated($table, $fields, &$values, $replace = DB_INSERT_PLAIN) { |
||
348 | |||
349 | |||
350 | |||
351 | // INSERTERS |
||
352 | public function doInsertComplex($query) { |
||
355 | |||
356 | /** |
||
357 | * @param string $table |
||
358 | * @param array $fieldsAndValues |
||
359 | * @param int $replace - DB_INSERT_PLAIN || DB_INSERT_IGNORE |
||
360 | * |
||
361 | * @return array|bool|mysqli_result|null |
||
362 | */ |
||
363 | public function doInsertSet($table, $fieldsAndValues, $replace = DB_INSERT_PLAIN) { |
||
366 | |||
367 | |||
368 | /** |
||
369 | * Values should be passed as-is |
||
370 | * |
||
371 | * @param string $table |
||
372 | * @param array $fields |
||
373 | * @param string[] $values |
||
374 | * |
||
375 | * @return array|bool|mysqli_result|null |
||
376 | * @deprecated |
||
377 | */ |
||
378 | public function doInsertValuesDeprecated($table, $fields, &$values) { |
||
381 | |||
382 | |||
383 | |||
384 | // REPLACERS |
||
385 | /** |
||
386 | * Replaces record in DB |
||
387 | * |
||
388 | * There are no DANGER replace operations |
||
389 | * |
||
390 | * @param string $table |
||
391 | * @param array $fieldsAndValues |
||
392 | * |
||
393 | * @return array|bool|mysqli_result|null |
||
394 | */ |
||
395 | public function doReplaceSet($table, $fieldsAndValues) { |
||
398 | |||
399 | /** |
||
400 | * Values should be passed as-is |
||
401 | * |
||
402 | * @param string $table |
||
403 | * @param array $fields |
||
404 | * @param string[] $values |
||
405 | * |
||
406 | * @return array|bool|mysqli_result|null |
||
407 | * @deprecated |
||
408 | */ |
||
409 | public function doReplaceValuesDeprecated($table, $fields, &$values) { |
||
412 | |||
413 | |||
414 | // UPDATERS |
||
415 | public function doUpdateComplex($query) { |
||
418 | |||
419 | public function doUpdateReallyComplex($query) { |
||
422 | |||
423 | /** |
||
424 | * Executes self-contained SQL UPDATE query |
||
425 | * |
||
426 | * Self-contained - means no params used |
||
427 | * Such queries usually used to make large amount of in-base calculations |
||
428 | * |
||
429 | * @param $query |
||
430 | * |
||
431 | * @return array|bool|mysqli_result|null |
||
432 | */ |
||
433 | public function doUpdateSqlNoParam($query) { |
||
436 | |||
437 | protected function doUpdateWhere($table, $fieldsSet, $fieldsAdjust = array(), $where = array(), $isOneRecord = DB_RECORDS_ALL) { |
||
459 | |||
460 | public function doUpdateRowSet($table, $fieldsAndValues, $where) { |
||
463 | |||
464 | public function doUpdateTableSet($table, $fieldsAndValues, $where = array()) { |
||
467 | |||
468 | public function doUpdateRowAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
471 | |||
472 | public function doUpdateTableAdjust($table, $fieldsSet, $fieldsAdjust, $where) { |
||
475 | |||
476 | /** |
||
477 | * In this call means that used some conditions in $where different than field = value |
||
478 | * |
||
479 | * It should be rewrote later |
||
480 | * |
||
481 | * @param $table |
||
482 | * @param $fieldsSet |
||
483 | * @param $fieldsAdjust |
||
484 | * @param $where |
||
485 | * |
||
486 | * @return array|bool|mysqli_result|null |
||
487 | * @deprecated |
||
488 | */ |
||
489 | public function doUpdateTableAdjustDanger($table, $fieldsSet, $fieldsAdjust, $where) { |
||
492 | |||
493 | /** |
||
494 | * For update_old - would be deprecated |
||
495 | * |
||
496 | * @param string $query |
||
497 | * |
||
498 | * @return array|bool|mysqli_result|null |
||
499 | * @deprecated |
||
500 | */ |
||
501 | public function doUpdateOld($query) { |
||
504 | |||
505 | |||
506 | |||
507 | // DELETERS |
||
508 | /** |
||
509 | * @param string $table |
||
510 | * @param array $where |
||
511 | * @param bool $isOneRecord |
||
512 | * |
||
513 | * @return DbQuery |
||
514 | */ |
||
515 | protected function buildDeleteQuery($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
521 | |||
522 | /** |
||
523 | * @param string $table |
||
524 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
525 | * @param bool $isOneRecord |
||
526 | * |
||
527 | * @return array|bool|mysqli_result|null |
||
528 | */ |
||
529 | public function doDeleteWhere($table, $where, $isOneRecord = DB_RECORDS_ALL) { |
||
532 | |||
533 | /** |
||
534 | * Early deprecated function for complex delete conditions |
||
535 | * |
||
536 | * Used for malformed $where conditions |
||
537 | * Also whereDanger can contain references for other {{tables}} |
||
538 | * |
||
539 | * @param string $table |
||
540 | * @param array $where |
||
541 | * @param array $whereDanger |
||
542 | * |
||
543 | * @return array|bool|mysqli_result|null |
||
544 | * @deprecated |
||
545 | */ |
||
546 | public function doDeleteDanger($table, $where, $whereDanger) { |
||
549 | |||
550 | /** |
||
551 | * @param string $table |
||
552 | * @param array $where - simple WHERE statement list which can be combined with AND |
||
553 | * |
||
554 | * @return array|bool|mysqli_result|null |
||
555 | */ |
||
556 | public function doDeleteRow($table, $where) { |
||
559 | |||
560 | /** |
||
561 | * Perform simple delete queries on fixed tables w/o params |
||
562 | * |
||
563 | * @param string $query |
||
564 | * |
||
565 | * @return array|bool|mysqli_result|null |
||
566 | */ |
||
567 | public function doDeleteSimple($query) { |
||
570 | |||
571 | |||
572 | // Misc functions |
||
573 | // |
||
574 | View Code Duplication | protected function castAsDbValue($value) { |
|
604 | |||
605 | /** |
||
606 | * Make field list safe |
||
607 | * |
||
608 | * Support expressions - expression index should be strictly integer! |
||
609 | * |
||
610 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
611 | * |
||
612 | * @return array |
||
613 | */ |
||
614 | View Code Duplication | protected function safeFieldsEqualValues($fields) { |
|
632 | |||
633 | /** |
||
634 | * Make fields adjustment safe |
||
635 | * |
||
636 | * Convert "key => value" pair to string "`key` = `key` + (value)" |
||
637 | * Supports expressions - expression index should be strictly integer! |
||
638 | * |
||
639 | * @param array $fields - array of pair $fieldName => $fieldValue |
||
640 | * |
||
641 | * @return array |
||
642 | */ |
||
643 | View Code Duplication | protected function safeFieldsAdjust($fields) { |
|
661 | |||
662 | // TODO - redo as callable usage with array_map/array_walk |
||
663 | View Code Duplication | public function safeValues($values) { |
|
676 | |||
677 | // TODO - redo as callable usage with array_map/array_walk |
||
678 | View Code Duplication | public function safeFields($fields) { |
|
691 | |||
692 | |||
693 | /** |
||
694 | * Returns iterator to iterate through mysqli_result |
||
695 | * |
||
696 | * @param string $query |
||
697 | * |
||
698 | * return DbResultIterator |
||
699 | * |
||
700 | * @return DbEmptyIterator|DbMysqliResultIterator |
||
701 | */ |
||
702 | public function doSelectIterator($query) { |
||
713 | |||
714 | /** |
||
715 | * @param DbQueryConstructor $stmt |
||
716 | * @param bool $skip_query_check |
||
717 | */ |
||
718 | public function doStmtLockAll($stmt, $skip_query_check = false) { |
||
728 | |||
729 | // TODO Заменить это на новый логгер |
||
730 | protected function security_watch_user_queries($query) { |
||
751 | |||
752 | |||
753 | public function security_query_check_bad_words($query) { |
||
810 | |||
811 | /** |
||
812 | * @param bool $prefixed_only |
||
813 | * |
||
814 | * @return array |
||
815 | */ |
||
816 | public function db_get_table_list($prefixed_only = true) { |
||
836 | |||
837 | /** |
||
838 | * @param string $statement |
||
839 | * |
||
840 | * @return bool|mysqli_stmt |
||
841 | */ |
||
842 | View Code Duplication | public function db_prepare($statement) { |
|
849 | |||
850 | |||
851 | /** |
||
852 | * L1 perform the query |
||
853 | * |
||
854 | * @param $query_string |
||
855 | * |
||
856 | * @return bool|mysqli_result |
||
857 | */ |
||
858 | View Code Duplication | public function db_sql_query($query_string) { |
|
865 | |||
866 | /** |
||
867 | * L1 fetch assoc array |
||
868 | * |
||
869 | * @param mysqli_result $query |
||
870 | * |
||
871 | * @return array|null |
||
872 | */ |
||
873 | View Code Duplication | public function db_fetch(&$query) { |
|
880 | |||
881 | public function db_fetch_row(&$query) { |
||
884 | |||
885 | public function db_escape($unescaped_string) { |
||
888 | |||
889 | public function driver_disconnect() { |
||
892 | |||
893 | public function db_error() { |
||
896 | |||
897 | public function db_insert_id() { |
||
900 | |||
901 | public function db_num_rows(&$result) { |
||
904 | |||
905 | public function db_affected_rows() { |
||
908 | |||
909 | /** |
||
910 | * @return string |
||
911 | */ |
||
912 | public function db_get_client_info() { |
||
915 | |||
916 | /** |
||
917 | * @return string |
||
918 | */ |
||
919 | public function db_get_server_info() { |
||
922 | |||
923 | /** |
||
924 | * @return string |
||
925 | */ |
||
926 | public function db_get_host_info() { |
||
929 | |||
930 | public function db_get_server_stat() { |
||
941 | |||
942 | /** |
||
943 | * @return array |
||
944 | * @throws Exception |
||
945 | */ |
||
946 | public function db_core_show_status() { |
||
959 | |||
960 | public function mysql_get_table_list() { |
||
963 | |||
964 | public function mysql_get_innodb_status() { |
||
967 | |||
968 | /** |
||
969 | * @return \DBAL\DbTransaction |
||
970 | */ |
||
971 | public function getTransaction() { |
||
974 | |||
975 | // Some wrappers to DbTransaction |
||
976 | // Unused for now |
||
977 | public function transactionCheck($status = null) { |
||
980 | |||
981 | public function transactionStart($level = '') { |
||
984 | |||
985 | public function transactionCommit() { |
||
988 | |||
989 | public function transactionRollback() { |
||
992 | |||
993 | } |
||
994 |