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 |
||
| 10 | class db_mysql { |
||
| 11 | const DB_MYSQL_TRANSACTION_SERIALIZABLE = 'SERIALIZABLE'; |
||
| 12 | const DB_MYSQL_TRANSACTION_REPEATABLE_READ = 'REPEATABLE READ'; |
||
| 13 | const DB_MYSQL_TRANSACTION_READ_COMMITTED = 'READ COMMITTED'; |
||
| 14 | const DB_MYSQL_TRANSACTION_READ_UNCOMMITTED = 'READ UNCOMMITTED'; |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Статус соеднения с MySQL |
||
| 18 | * |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | public $connected = false; |
||
| 22 | /** |
||
| 23 | * Префикс названий таблиц в БД |
||
| 24 | * |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | public $db_prefix = ''; |
||
| 28 | /** |
||
| 29 | * Настройки БД |
||
| 30 | * |
||
| 31 | * @var array |
||
| 32 | */ |
||
| 33 | protected $dbsettings = array(); |
||
| 34 | /** |
||
| 35 | * Драйвер для прямого обращения к MySQL |
||
| 36 | * |
||
| 37 | * @var db_mysql_v5 $driver |
||
| 38 | */ |
||
| 39 | public $driver = null; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Общее время запросов |
||
| 43 | * |
||
| 44 | * @var float $time_mysql_total |
||
| 45 | */ |
||
| 46 | public $time_mysql_total = 0.0; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * DB schemes |
||
| 50 | * |
||
| 51 | * @var \DBAL\Schema|null $schema |
||
| 52 | */ |
||
| 53 | protected static $schema = null; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * db_mysql constructor. |
||
| 57 | * |
||
| 58 | * @param GlobalContainer $gc |
||
| 59 | */ |
||
| 60 | public function __construct($gc) { |
||
|
|
|||
| 61 | // $this->transaction = new \DBAL\DbTransaction($gc, $this); |
||
| 62 | // $this->snCache = new $gc->snCacheClass($gc, $this); |
||
| 63 | // $this->operator = new DbRowDirectOperator($this); |
||
| 64 | } |
||
| 65 | |||
| 66 | public function schema() { |
||
| 67 | if(!isset(self::$schema)) { |
||
| 68 | self::$schema = new \DBAL\Schema($this); |
||
| 69 | } |
||
| 70 | |||
| 71 | return self::$schema; |
||
| 72 | } |
||
| 73 | |||
| 74 | function load_db_settings() { |
||
| 75 | $dbsettings = array(); |
||
| 76 | |||
| 77 | require(SN_ROOT_PHYSICAL . "config" . DOT_PHP_EX); |
||
| 78 | |||
| 79 | $this->dbsettings = $dbsettings; |
||
| 80 | } |
||
| 81 | |||
| 82 | function sn_db_connect($external_db_settings = null) { |
||
| 83 | $this->db_disconnect(); |
||
| 84 | |||
| 85 | if(!empty($external_db_settings) && is_array($external_db_settings)) { |
||
| 86 | $this->dbsettings = $external_db_settings; |
||
| 87 | } |
||
| 88 | |||
| 89 | if(empty($this->dbsettings)) { |
||
| 90 | $this->load_db_settings(); |
||
| 91 | } |
||
| 92 | |||
| 93 | // TODO - фатальные (?) ошибки на каждом шагу. Хотя - скорее Эксепшны |
||
| 94 | if(!empty($this->dbsettings)) { |
||
| 95 | $driver_name = empty($this->dbsettings['sn_driver']) ? 'db_mysql_v5' : $this->dbsettings['sn_driver']; |
||
| 96 | $this->driver = new $driver_name(); |
||
| 97 | $this->db_prefix = $this->dbsettings['prefix']; |
||
| 98 | |||
| 99 | $this->connected = $this->connected || $this->driver_connect(); |
||
| 100 | |||
| 101 | if($this->connected && empty($this->schema()->getSnTables())) { |
||
| 102 | die('DB error - cannot find any table. Halting...'); |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $this->connected = false; |
||
| 106 | } |
||
| 107 | |||
| 108 | return $this->connected; |
||
| 109 | } |
||
| 110 | |||
| 111 | function driver_connect() { |
||
| 124 | |||
| 125 | function db_disconnect() { |
||
| 133 | |||
| 134 | function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
||
| 135 | global $numqueries, $debug, $sn_cache, $config; |
||
| 136 | |||
| 137 | if(!is_string($table)) { |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param \DBAL\DbQuery $dbQuery |
||
| 188 | * |
||
| 189 | * @return array|null |
||
| 190 | */ |
||
| 191 | public function dbqSelectAndFetch(\DBAL\DbQuery $dbQuery) { |
||
| 194 | |||
| 195 | |||
| 196 | function security_watch_user_queries($query) { |
||
| 216 | |||
| 217 | |||
| 218 | function security_query_check_bad_words($query) { |
||
| 271 | |||
| 272 | function mysql_get_table_list() { |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @param string $tableName_unsafe |
||
| 281 | * |
||
| 282 | * @return array[] |
||
| 283 | */ |
||
| 284 | public function mysql_get_fields($tableName_unsafe) { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * @param string $tableName_unsafe |
||
| 297 | * |
||
| 298 | * @return array[] |
||
| 299 | */ |
||
| 300 | public function mysql_get_indexes($tableName_unsafe) { |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param string $tableName_unsafe |
||
| 322 | * |
||
| 323 | * @return array[] |
||
| 324 | */ |
||
| 325 | public function mysql_get_constraints($tableName_unsafe) { |
||
| 349 | |||
| 350 | |||
| 351 | View Code Duplication | function db_sql_query($query_string) { |
|
| 358 | |||
| 359 | /** |
||
| 360 | * @param mysqli_result $query_result |
||
| 361 | * |
||
| 362 | * @return array|null |
||
| 363 | */ |
||
| 364 | View Code Duplication | function db_fetch(&$query_result) { |
|
| 404 | |||
| 405 | } |
||
| 406 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.