| Total Complexity | 55 |
| Total Lines | 265 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Database 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 Database, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Database extends AbstractDatabase |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param TableEntity $tableAttrs |
||
| 14 | * |
||
| 15 | * @return string |
||
| 16 | */ |
||
| 17 | private function _tableStatus(TableEntity $tableAttrs) |
||
| 18 | { |
||
| 19 | return 'COMMENT=' . $this->driver->quote($tableAttrs->comment) . |
||
|
|
|||
| 20 | ($tableAttrs->engine ? ' ENGINE=' . $this->driver->quote($tableAttrs->engine) : '') . |
||
| 21 | ($tableAttrs->collation ? ' COLLATE ' . $this->driver->quote($tableAttrs->collation) : '') . |
||
| 22 | ($tableAttrs->autoIncrement !== 0 ? " AUTO_INCREMENT=$tableAttrs->autoIncrement" : ''); |
||
| 23 | } |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @inheritDoc |
||
| 27 | */ |
||
| 28 | public function createTable(TableEntity $tableAttrs) |
||
| 29 | { |
||
| 30 | $clauses = []; |
||
| 31 | foreach ($tableAttrs->fields as $field) { |
||
| 32 | $clauses[] = implode($field[1]); |
||
| 33 | } |
||
| 34 | |||
| 35 | $clauses = array_merge($clauses, $tableAttrs->foreign); |
||
| 36 | $status = $this->_tableStatus($tableAttrs); |
||
| 37 | |||
| 38 | $result = $this->driver->execute('CREATE TABLE ' . $this->driver->table($tableAttrs->name) . |
||
| 39 | ' (' . implode(', ', $clauses) . ") $status $tableAttrs->partitioning"); |
||
| 40 | return $result !== false; |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @inheritDoc |
||
| 45 | */ |
||
| 46 | public function alterTable(string $table, TableEntity $tableAttrs) |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @inheritDoc |
||
| 72 | */ |
||
| 73 | public function alterIndexes(string $table, array $alter, array $drop) |
||
| 74 | { |
||
| 75 | $clauses = []; |
||
| 76 | foreach ($drop as $index) { |
||
| 77 | $clauses[] = 'DROP INDEX ' . $this->driver->escapeId($index->name); |
||
| 78 | } |
||
| 79 | foreach ($alter as $index) { |
||
| 80 | $clauses[] = 'ADD ' . ($index->type == 'PRIMARY' ? 'PRIMARY KEY ' : $index->type . ' ') . |
||
| 81 | ($index->name != '' ? $this->driver->escapeId($index->name) . ' ' : '') . |
||
| 82 | '(' . implode(', ', $index->columns) . ')'; |
||
| 83 | } |
||
| 84 | $result = $this->driver->execute('ALTER TABLE ' . $this->driver->table($table) . ' ' . implode(', ', $clauses)); |
||
| 85 | return $result !== false; |
||
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @inheritDoc |
||
| 90 | */ |
||
| 91 | public function tables() |
||
| 92 | { |
||
| 93 | return $this->driver->keyValues($this->driver->minVersion(5) ? |
||
| 94 | 'SELECT TABLE_NAME, TABLE_TYPE FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() ORDER BY TABLE_NAME' : |
||
| 95 | 'SHOW TABLES'); |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @inheritDoc |
||
| 100 | */ |
||
| 101 | public function countTables(array $databases) |
||
| 102 | { |
||
| 103 | $counts = []; |
||
| 104 | foreach ($databases as $database) { |
||
| 105 | $counts[$database] = count($this->driver->values('SHOW TABLES IN ' . $this->driver->escapeId($database))); |
||
| 106 | } |
||
| 107 | return $counts; |
||
| 108 | } |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @inheritDoc |
||
| 112 | */ |
||
| 113 | public function dropViews(array $views) |
||
| 114 | { |
||
| 115 | $this->driver->execute('DROP VIEW ' . implode(', ', array_map(function ($view) { |
||
| 116 | return $this->driver->table($view); |
||
| 117 | }, $views))); |
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritDoc |
||
| 123 | */ |
||
| 124 | public function dropTables(array $tables) |
||
| 125 | { |
||
| 126 | $this->driver->execute('DROP TABLE ' . implode(', ', array_map(function ($table) { |
||
| 127 | return $this->driver->table($table); |
||
| 128 | }, $tables))); |
||
| 129 | return true; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @inheritDoc |
||
| 134 | */ |
||
| 135 | public function truncateTables(array $tables) |
||
| 136 | { |
||
| 137 | return $this->driver->applyQueries('TRUNCATE TABLE', $tables); |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @inheritDoc |
||
| 142 | */ |
||
| 143 | public function moveTables(array $tables, array $views, string $target) |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @inheritDoc |
||
| 170 | */ |
||
| 171 | public function copyTables(array $tables, array $views, string $target) |
||
| 203 | } |
||
| 204 | |||
| 205 | /** |
||
| 206 | * @inheritDoc |
||
| 207 | */ |
||
| 208 | public function events() |
||
| 209 | { |
||
| 210 | return $this->driver->rows('SHOW EVENTS'); |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @inheritDoc |
||
| 215 | */ |
||
| 216 | public function routine(string $name, string $type) |
||
| 217 | { |
||
| 218 | $enumLength = $this->driver->enumLength(); |
||
| 219 | $aliases = ['bool', 'boolean', 'integer', 'double precision', 'real', 'dec', |
||
| 220 | 'numeric', 'fixed', 'national char', 'national varchar']; |
||
| 221 | $space = "(?:\\s|/\\*[\s\S]*?\\*/|(?:#|-- )[^\n]*\n?|--\r?\n)"; |
||
| 222 | $type_pattern = '((' . implode('|', array_merge(array_keys($this->driver->types()), $aliases)) . |
||
| 223 | ")\\b(?:\\s*\\(((?:[^'\")]|$enumLength)++)\\))?\\s*(zerofill\\s*)?(unsigned" . |
||
| 224 | "(?:\\s+zerofill)?)?)(?:\\s*(?:CHARSET|CHARACTER\\s+SET)\\s*['\"]?([^'\"\\s,]+)['\"]?)?"; |
||
| 225 | $pattern = "$space*(" . ($type == 'FUNCTION' ? '' : $this->driver->inout()) . |
||
| 226 | ")?\\s*(?:`((?:[^`]|``)*)`\\s*|\\b(\\S+)\\s+)$type_pattern"; |
||
| 227 | $create = $this->connection->result("SHOW CREATE $type " . $this->driver->escapeId($name), 2); |
||
| 228 | preg_match("~\\(((?:$pattern\\s*,?)*)\\)\\s*" . |
||
| 229 | ($type == "FUNCTION" ? "RETURNS\\s+$type_pattern\\s+" : '') . "(.*)~is", $create, $match); |
||
| 230 | $fields = []; |
||
| 231 | preg_match_all("~$pattern\\s*,?~is", $match[1], $matches, PREG_SET_ORDER); |
||
| 232 | foreach ($matches as $param) { |
||
| 233 | $fields[] = [ |
||
| 234 | 'field' => str_replace('``', '`', $param[2]) . $param[3], |
||
| 235 | 'type' => strtolower($param[5]), |
||
| 236 | 'length' => preg_replace_callback("~$enumLength~s", 'normalize_enum', $param[6]), |
||
| 237 | 'unsigned' => strtolower(preg_replace('~\s+~', ' ', trim("$param[8] $param[7]"))), |
||
| 238 | 'null' => 1, |
||
| 239 | 'full_type' => $param[4], |
||
| 240 | 'inout' => strtoupper($param[1]), |
||
| 241 | 'collation' => strtolower($param[9]), |
||
| 242 | ]; |
||
| 243 | } |
||
| 244 | if ($type != 'FUNCTION') { |
||
| 245 | return ['fields' => $fields, 'definition' => $match[11]]; |
||
| 246 | } |
||
| 247 | return [ |
||
| 248 | 'fields' => $fields, |
||
| 249 | 'returns' => ['type' => $match[12], 'length' => $match[13], 'unsigned' => $match[15], 'collation' => $match[16]], |
||
| 250 | 'definition' => $match[17], |
||
| 251 | 'language' => 'SQL', // available in information_schema.ROUTINES.PARAMETER_STYLE |
||
| 252 | ]; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * @inheritDoc |
||
| 257 | */ |
||
| 258 | public function routines() |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * @inheritDoc |
||
| 271 | */ |
||
| 272 | public function routineId(string $name, array $row) |
||
| 275 | } |
||
| 276 | } |
||
| 277 |
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.