| Total Complexity | 59 |
| Total Lines | 272 |
| Duplicated Lines | 31.25 % |
| Changes | 0 | ||
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_manager 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.
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_manager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 33 | class db_manager |
||
| 34 | { |
||
| 35 | public $s_tables = array(); |
||
| 36 | public $f_tables = array(); |
||
| 37 | public $db; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * db_manager constructor. |
||
| 41 | */ |
||
| 42 | public function __construct() |
||
| 43 | { |
||
| 44 | $this->db = XoopsDatabaseFactory::getDatabase(); |
||
| 45 | $this->db->setPrefix(XOOPS_DB_PREFIX); |
||
| 46 | $this->db->setLogger(XoopsLogger::instance()); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @return bool |
||
| 51 | */ |
||
| 52 | public function isConnectable() |
||
| 53 | { |
||
| 54 | return ($this->db->connect(false) !== false) ? true : false; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @return bool |
||
| 59 | */ |
||
| 60 | public function dbExists() |
||
| 61 | { |
||
| 62 | return ($this->db->connect() !== false) ? true : false; |
||
| 63 | } |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @return bool |
||
| 67 | */ |
||
| 68 | public function createDB() |
||
| 69 | { |
||
| 70 | $this->db->connect(false); |
||
| 71 | |||
| 72 | $result = $this->db->query('CREATE DATABASE ' . XOOPS_DB_NAME); |
||
| 73 | |||
| 74 | return ($result !== false) ? true : false; |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @param $sql_file_path |
||
| 79 | * @return bool |
||
| 80 | */ |
||
| 81 | public function queryFromFile($sql_file_path) |
||
| 82 | { |
||
| 83 | global $pieces; |
||
| 84 | $tables = array(); |
||
| 85 | |||
| 86 | if (!file_exists($sql_file_path)) { |
||
| 87 | return false; |
||
| 88 | } |
||
| 89 | $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
||
| 90 | SqlUtility::splitMySqlFile($pieces, $sql_query); |
||
| 91 | $this->db->connect(); |
||
| 92 | foreach ($pieces as $piece) { |
||
| 93 | $piece = trim($piece); |
||
| 94 | // [0] contains the prefixed query |
||
| 95 | // [4] contains unprefixed table name |
||
| 96 | $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
||
| 97 | if ($prefixed_query !== false) { |
||
| 98 | $table = $this->db->prefix($prefixed_query[4]); |
||
| 99 | if ($prefixed_query[1] === 'CREATE TABLE') { |
||
| 100 | View Code Duplication | if ($this->db->query($prefixed_query[0]) !== false) { |
|
| 101 | if (!isset($this->s_tables['create'][$table])) { |
||
| 102 | $this->s_tables['create'][$table] = 1; |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | if (!isset($this->f_tables['create'][$table])) { |
||
| 106 | $this->f_tables['create'][$table] = 1; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | } elseif ($prefixed_query[1] === 'INSERT INTO') { |
||
| 110 | if ($this->db->query($prefixed_query[0]) !== false) { |
||
| 111 | View Code Duplication | if (!isset($this->s_tables['insert'][$table])) { |
|
| 112 | $this->s_tables['insert'][$table] = 1; |
||
| 113 | } else { |
||
| 114 | $this->s_tables['insert'][$table]++; |
||
| 115 | } |
||
| 116 | View Code Duplication | } else { |
|
| 117 | if (!isset($this->f_tables['insert'][$table])) { |
||
| 118 | $this->f_tables['insert'][$table] = 1; |
||
| 119 | } else { |
||
| 120 | $this->f_tables['insert'][$table]++; |
||
| 121 | } |
||
| 122 | } |
||
| 123 | View Code Duplication | } elseif ($prefixed_query[1] === 'ALTER TABLE') { |
|
| 124 | if ($this->db->query($prefixed_query[0]) !== false) { |
||
| 125 | if (!isset($this->s_tables['alter'][$table])) { |
||
| 126 | $this->s_tables['alter'][$table] = 1; |
||
| 127 | } |
||
| 128 | } else { |
||
| 129 | if (!isset($this->s_tables['alter'][$table])) { |
||
| 130 | $this->f_tables['alter'][$table] = 1; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } elseif ($prefixed_query[1] === 'DROP TABLE') { |
||
| 134 | if ($this->db->query('DROP TABLE ' . $table) !== false) { |
||
| 135 | if (!isset($this->s_tables['drop'][$table])) { |
||
| 136 | $this->s_tables['drop'][$table] = 1; |
||
| 137 | } |
||
| 138 | } else { |
||
| 139 | if (!isset($this->s_tables['drop'][$table])) { |
||
| 140 | $this->f_tables['drop'][$table] = 1; |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | |||
| 147 | return true; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @return string |
||
| 152 | */ |
||
| 153 | public function report() |
||
| 154 | { |
||
| 155 | $content = "<table align='center'><tr><td align='left'>\n"; |
||
| 156 | View Code Duplication | if (isset($this->s_tables['create'])) { |
|
| 157 | foreach ($this->s_tables['create'] as $key => $val) { |
||
| 158 | $content .= sprintf(_INSTALL_L45, "<b>$key</b>") . "<br>\n"; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | View Code Duplication | if (isset($this->s_tables['insert'])) { |
|
| 162 | foreach ($this->s_tables['insert'] as $key => $val) { |
||
| 163 | $content .= sprintf(_INSTALL_L119, $val, "<b>$key</b>") . "<br>\n"; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | View Code Duplication | if (isset($this->s_tables['alter'])) { |
|
| 167 | foreach ($this->s_tables['alter'] as $key => $val) { |
||
| 168 | $content .= sprintf(_INSTALL_L133, "<b>$key</b>") . "<br>\n"; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | View Code Duplication | if (isset($this->s_tables['drop'])) { |
|
| 172 | foreach ($this->s_tables['drop'] as $key => $val) { |
||
| 173 | $content .= sprintf(_INSTALL_L163, "<b>$key</b>") . "<br>\n"; |
||
| 174 | } |
||
| 175 | } |
||
| 176 | $content .= "<br>\n"; |
||
| 177 | View Code Duplication | if (isset($this->f_tables['create'])) { |
|
| 178 | foreach ($this->f_tables['create'] as $key => $val) { |
||
| 179 | $content .= sprintf(_INSTALL_L118, "<b>$key</b>") . "<br>\n"; |
||
| 180 | } |
||
| 181 | } |
||
| 182 | View Code Duplication | if (isset($this->f_tables['insert'])) { |
|
| 183 | foreach ($this->f_tables['insert'] as $key => $val) { |
||
| 184 | $content .= sprintf(_INSTALL_L120, $val, "<b>$key</b>") . "<br>\n"; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | View Code Duplication | if (isset($this->f_tables['alter'])) { |
|
| 188 | foreach ($this->f_tables['alter'] as $key => $val) { |
||
| 189 | $content .= sprintf(_INSTALL_L134, "<b>$key</b>") . "<br>\n"; |
||
| 190 | } |
||
| 191 | } |
||
| 192 | View Code Duplication | if (isset($this->f_tables['drop'])) { |
|
| 193 | foreach ($this->f_tables['drop'] as $key => $val) { |
||
| 194 | $content .= sprintf(_INSTALL_L164, "<b>$key</b>") . "<br>\n"; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | $content .= "</td></tr></table>\n"; |
||
| 198 | |||
| 199 | return $content; |
||
| 200 | } |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param $sql |
||
| 204 | * @return mixed |
||
| 205 | */ |
||
| 206 | public function query($sql) |
||
| 207 | { |
||
| 208 | $this->db->connect(); |
||
| 209 | |||
| 210 | return $this->db->query($sql); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @param $table |
||
| 215 | * @return mixed |
||
| 216 | */ |
||
| 217 | public function prefix($table) |
||
| 218 | { |
||
| 219 | $this->db->connect(); |
||
| 220 | |||
| 221 | return $this->db->prefix($table); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param $ret |
||
| 226 | * @return mixed |
||
| 227 | */ |
||
| 228 | public function fetchArray($ret) |
||
| 229 | { |
||
| 230 | $this->db->connect(); |
||
| 231 | |||
| 232 | return $this->db->fetchArray($ret); |
||
| 233 | } |
||
| 234 | |||
| 235 | /** |
||
| 236 | * @param $table |
||
| 237 | * @param $query |
||
| 238 | * @return bool |
||
| 239 | */ |
||
| 240 | public function insert($table, $query) |
||
| 261 | } |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | public function isError() |
||
| 268 | { |
||
| 269 | return isset($this->f_tables) ? true : false; |
||
| 270 | } |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param $tables |
||
| 274 | * @return array |
||
| 275 | */ |
||
| 276 | public function deleteTables($tables) |
||
| 288 | } |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param $table |
||
| 292 | * @return bool |
||
| 293 | */ |
||
| 294 | public function tableExists($table) |
||
| 307 |