| Total Complexity | 51 |
| Total Lines | 205 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
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 |
||
| 30 | class db_manager |
||
| 31 | { |
||
| 32 | public $s_tables = array(); |
||
| 33 | public $f_tables = array(); |
||
| 34 | public $db; |
||
| 35 | |||
| 36 | public function __construct() |
||
| 37 | { |
||
| 38 | $this->db = XoopsDatabaseFactory::getDatabase(); |
||
|
|
|||
| 39 | $this->db->setPrefix(XOOPS_DB_PREFIX); |
||
| 40 | } |
||
| 41 | |||
| 42 | public function isConnectable() |
||
| 43 | { |
||
| 44 | return ($this->db->connect(false) != false) ? true : false; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function dbExists() |
||
| 48 | { |
||
| 49 | return ($this->db->connect() != false) ? true : false; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function createDB() |
||
| 53 | { |
||
| 54 | $this->db->connect(false); |
||
| 55 | |||
| 56 | $result = $this->db->query("CREATE DATABASE " . XOOPS_DB_NAME); |
||
| 57 | |||
| 58 | return ($result != false) ? true : false; |
||
| 59 | } |
||
| 60 | |||
| 61 | public function queryFromFile($sql_file_path) |
||
| 62 | { |
||
| 63 | $tables = array(); |
||
| 64 | |||
| 65 | if (!file_exists($sql_file_path)) { |
||
| 66 | return false; |
||
| 67 | } |
||
| 68 | $sql_query = trim(fread(fopen($sql_file_path, 'r'), filesize($sql_file_path))); |
||
| 69 | SqlUtility::splitMySqlFile($pieces, $sql_query); |
||
| 70 | $this->db->connect(); |
||
| 71 | foreach ($pieces as $piece) { |
||
| 72 | $piece = trim($piece); |
||
| 73 | // [0] contains the prefixed query |
||
| 74 | // [4] contains unprefixed table name |
||
| 75 | $prefixed_query = SqlUtility::prefixQuery($piece, $this->db->prefix()); |
||
| 76 | if ($prefixed_query != false) { |
||
| 77 | $table = $this->db->prefix($prefixed_query[4]); |
||
| 78 | if ($prefixed_query[1] == 'CREATE TABLE') { |
||
| 79 | if ($this->db->query($prefixed_query[0]) != false) { |
||
| 80 | if (!isset($this->s_tables['create'][$table])) { |
||
| 81 | $this->s_tables['create'][$table] = 1; |
||
| 82 | } |
||
| 83 | } else { |
||
| 84 | if (!isset($this->f_tables['create'][$table])) { |
||
| 85 | $this->f_tables['create'][$table] = 1; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | } elseif ($prefixed_query[1] == 'INSERT INTO') { |
||
| 89 | if ($this->db->query($prefixed_query[0]) != false) { |
||
| 90 | if (!isset($this->s_tables['insert'][$table])) { |
||
| 91 | $this->s_tables['insert'][$table] = 1; |
||
| 92 | } else { |
||
| 93 | $this->s_tables['insert'][$table]++; |
||
| 94 | } |
||
| 95 | } else { |
||
| 96 | if (!isset($this->f_tables['insert'][$table])) { |
||
| 97 | $this->f_tables['insert'][$table] = 1; |
||
| 98 | } else { |
||
| 99 | $this->f_tables['insert'][$table]++; |
||
| 100 | } |
||
| 101 | } |
||
| 102 | } elseif ($prefixed_query[1] == 'ALTER TABLE') { |
||
| 103 | if ($this->db->query($prefixed_query[0]) != false) { |
||
| 104 | if (!isset($this->s_tables['alter'][$table])) { |
||
| 105 | $this->s_tables['alter'][$table] = 1; |
||
| 106 | } |
||
| 107 | } else { |
||
| 108 | if (!isset($this->s_tables['alter'][$table])) { |
||
| 109 | $this->f_tables['alter'][$table] = 1; |
||
| 110 | } |
||
| 111 | } |
||
| 112 | } elseif ($prefixed_query[1] == 'DROP TABLE') { |
||
| 113 | if ($this->db->query('DROP TABLE '.$table) != false) { |
||
| 114 | if (!isset($this->s_tables['drop'][$table])) { |
||
| 115 | $this->s_tables['drop'][$table] = 1; |
||
| 116 | } |
||
| 117 | } else { |
||
| 118 | if (!isset($this->s_tables['drop'][$table])) { |
||
| 119 | $this->f_tables['drop'][$table] = 1; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | } |
||
| 125 | return true; |
||
| 126 | } |
||
| 127 | |||
| 128 | public $successStrings = array( |
||
| 129 | 'create' => 'create', |
||
| 130 | 'insert' => 'insert', |
||
| 131 | 'alter' => 'alter', |
||
| 132 | 'drop' => 'drop', |
||
| 133 | ); |
||
| 134 | public $failureStrings = array( |
||
| 135 | 'create' => 'fail', |
||
| 136 | 'insert' => 'fail', |
||
| 137 | 'alter' => 'error', |
||
| 138 | 'drop' => 'error', |
||
| 139 | ); |
||
| 140 | |||
| 141 | |||
| 142 | public function report() |
||
| 143 | { |
||
| 144 | $commands = array( 'create', 'insert', 'alter', 'drop' ); |
||
| 145 | $content = '<ul class="log">'; |
||
| 146 | foreach ($commands as $cmd) { |
||
| 147 | if (!@empty($this->s_tables[$cmd])) { |
||
| 148 | foreach ($this->s_tables[$cmd] as $key => $val) { |
||
| 149 | $content .= '<li class="success">'; |
||
| 150 | $content .= ($cmd != 'insert') ? sprintf($this->successStrings[$cmd], $key) : sprintf($this->successStrings[$cmd], $val, $key); |
||
| 151 | $content .= "</li>\n"; |
||
| 152 | } |
||
| 153 | } |
||
| 154 | } |
||
| 155 | foreach ($commands as $cmd) { |
||
| 156 | if (!@empty($this->f_tables[$cmd])) { |
||
| 157 | foreach ($this->f_tables[$cmd] as $key => $val) { |
||
| 158 | $content .= '<li class="failure">'; |
||
| 159 | $content .= ($cmd != 'insert') ? sprintf($this->failureStrings[$cmd], $key) : sprintf($this->failureStrings[$cmd], $val, $key); |
||
| 160 | $content .= "</li>\n"; |
||
| 161 | } |
||
| 162 | } |
||
| 163 | } |
||
| 164 | $content .= '</ul>'; |
||
| 165 | return $content; |
||
| 166 | } |
||
| 167 | |||
| 168 | public function query($sql) |
||
| 169 | { |
||
| 170 | $this->db->connect(); |
||
| 171 | return $this->db->query($sql); |
||
| 172 | } |
||
| 173 | |||
| 174 | public function prefix($table) |
||
| 175 | { |
||
| 176 | $this->db->connect(); |
||
| 177 | return $this->db->prefix($table); |
||
| 178 | } |
||
| 179 | |||
| 180 | public function fetchArray($ret) |
||
| 181 | { |
||
| 182 | $this->db->connect(); |
||
| 183 | return $this->db->fetchArray($ret); |
||
| 184 | } |
||
| 185 | |||
| 186 | public function insert($table, $query) |
||
| 205 | } |
||
| 206 | } |
||
| 207 | |||
| 208 | public function isError() |
||
| 209 | { |
||
| 210 | return (isset($this->f_tables)) ? true : false; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function deleteTables($tables) |
||
| 223 | } |
||
| 224 | |||
| 225 | public function tableExists($table) |
||
| 226 | { |
||
| 227 | $table = trim($table); |
||
| 235 | } |
||
| 236 | } |
||
| 237 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.