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 | 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 string $query |
||
126 | * |
||
127 | * @return mixed|string |
||
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 | * @return string |
||
159 | */ |
||
160 | public function queryTrace() { |
||
179 | |||
180 | /** |
||
181 | * Prepares sql string to execution and execute it |
||
182 | * |
||
183 | * @param string $sqlQuery |
||
184 | * @param array $values |
||
185 | * |
||
186 | * @return array|bool|mysqli_result|null |
||
187 | */ |
||
188 | public function sqlPrepareAndExecute($sqlQuery, $values = array()) { |
||
191 | |||
192 | |||
193 | |||
194 | /** |
||
195 | * @param string|DbSqlStatement $statement |
||
196 | * |
||
197 | * @return array|bool|mysqli_result|null |
||
198 | */ |
||
199 | public function execute($statement) { |
||
202 | |||
203 | /** |
||
204 | * @param DbSqlStatement $statement |
||
205 | * |
||
206 | * @return array|null |
||
207 | */ |
||
208 | public function fetchOne($statement) { |
||
213 | |||
214 | /** |
||
215 | * @param string|DbSqlPrepare $query |
||
216 | * @param string $table |
||
217 | * @param bool $fetch |
||
218 | * @param bool $skip_query_check |
||
219 | * |
||
220 | * @return array|bool|mysqli_result|null |
||
221 | */ |
||
222 | public function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
||
271 | |||
272 | |||
273 | // TODO Заменить это на новый логгер |
||
274 | protected function security_watch_user_queries($query) { |
||
295 | |||
296 | |||
297 | public function security_query_check_bad_words($query, $skip_query_check = false) { |
||
354 | |||
355 | /** |
||
356 | * @param bool $prefixed_only |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | public function db_get_table_list($prefixed_only = true) { |
||
380 | |||
381 | /** |
||
382 | * @param string $statement |
||
383 | * |
||
384 | * @return bool|mysqli_stmt |
||
385 | */ |
||
386 | View Code Duplication | public function db_prepare($statement) { |
|
393 | |||
394 | |||
395 | /** |
||
396 | * L1 perform the query |
||
397 | * |
||
398 | * @param $query_string |
||
399 | * |
||
400 | * @return bool|mysqli_result |
||
401 | */ |
||
402 | View Code Duplication | public function db_sql_query($query_string) { |
|
409 | |||
410 | /** |
||
411 | * L1 fetch assoc array |
||
412 | * |
||
413 | * @param $query |
||
414 | * |
||
415 | * @return array|null |
||
416 | */ |
||
417 | View Code Duplication | public function db_fetch(&$query) { |
|
424 | |||
425 | public function db_fetch_row(&$query) { |
||
428 | |||
429 | public function db_escape($unescaped_string) { |
||
432 | |||
433 | public function driver_disconnect() { |
||
436 | |||
437 | public function db_error() { |
||
440 | |||
441 | public function db_insert_id() { |
||
444 | |||
445 | public function db_num_rows(&$result) { |
||
448 | |||
449 | public function db_affected_rows() { |
||
452 | |||
453 | /** |
||
454 | * @return string |
||
455 | */ |
||
456 | public function db_get_client_info() { |
||
459 | |||
460 | /** |
||
461 | * @return string |
||
462 | */ |
||
463 | public function db_get_server_info() { |
||
466 | |||
467 | /** |
||
468 | * @return string |
||
469 | */ |
||
470 | public function db_get_host_info() { |
||
473 | |||
474 | public function db_get_server_stat() { |
||
485 | |||
486 | /** |
||
487 | * @return array |
||
488 | * @throws Exception |
||
489 | */ |
||
490 | public function db_core_show_status() { |
||
503 | |||
504 | public function mysql_get_table_list() { |
||
507 | |||
508 | public function mysql_get_innodb_status() { |
||
511 | |||
512 | } |
||
513 |