| Total Complexity | 45 |
| Total Lines | 250 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Table 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 Table, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | class Table extends AbstractTable |
||
| 21 | { |
||
| 22 | use TableTrait; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @inheritDoc |
||
| 26 | */ |
||
| 27 | public function isView(TableEntity $tableStatus) |
||
| 28 | { |
||
| 29 | return $tableStatus->engine == 'view'; |
||
| 30 | } |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @inheritDoc |
||
| 34 | */ |
||
| 35 | public function supportForeignKeys(TableEntity $tableStatus) |
||
| 36 | { |
||
| 37 | return !$this->connection->result("SELECT sqlite_compileoption_used('OMIT_FOREIGN_KEY')"); |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @param string $type |
||
| 42 | * |
||
| 43 | * @return string |
||
| 44 | */ |
||
| 45 | private function rowType(string $type) |
||
| 46 | { |
||
| 47 | if (preg_match('~int~i', $type)) { |
||
| 48 | return 'integer'; |
||
| 49 | } |
||
| 50 | if (preg_match('~char|clob|text~i', $type)) { |
||
| 51 | return 'text'; |
||
| 52 | } |
||
| 53 | if (preg_match('~blob~i', $type)) { |
||
| 54 | return 'blob'; |
||
| 55 | } |
||
| 56 | if (preg_match('~real|floa|doub~i', $type)) { |
||
| 57 | return 'real'; |
||
| 58 | } |
||
| 59 | return 'numeric'; |
||
| 60 | } |
||
| 61 | |||
| 62 | private function defaultvalue(array $row) |
||
| 63 | { |
||
| 64 | $default = $row["dflt_value"]; |
||
| 65 | if (preg_match("~'(.*)'~", $default, $match)) { |
||
| 66 | return str_replace("''", "'", $match[1]); |
||
| 67 | } |
||
| 68 | if ($default == "NULL") { |
||
| 69 | return null; |
||
| 70 | } |
||
| 71 | return $default; |
||
| 72 | } |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param array $row |
||
| 76 | * |
||
| 77 | * @return TableFieldEntity |
||
| 78 | */ |
||
| 79 | private function makeFieldEntity(array $row) |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param string $table |
||
| 96 | * |
||
| 97 | * @return array |
||
| 98 | */ |
||
| 99 | private function tableFields(string $table) |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @inheritDoc |
||
| 123 | */ |
||
| 124 | public function fields(string $table) |
||
| 125 | { |
||
| 126 | $fields = $this->tableFields($table); |
||
| 127 | $query = "SELECT sql FROM sqlite_master WHERE type IN ('table', 'view') AND name = " . $this->driver->quote($table); |
||
| 128 | $result = $this->connection->result($query); |
||
| 129 | $pattern = '~(("[^"]*+")+|[a-z0-9_]+)\s+text\s+COLLATE\s+(\'[^\']+\'|\S+)~i'; |
||
| 130 | preg_match_all($pattern, $result, $matches, PREG_SET_ORDER); |
||
| 131 | foreach ($matches as $match) { |
||
| 132 | $name = str_replace('""', '"', preg_replace('~^"|"$~', '', $match[1])); |
||
| 133 | if (isset($fields[$name])) { |
||
| 134 | $fields[$name]->collation = trim($match[3], "'"); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | return $fields; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param array $row |
||
| 142 | * @param array $results |
||
| 143 | * @param string $table |
||
| 144 | * @param ConnectionInterface $connection |
||
| 145 | * |
||
| 146 | * @return IndexEntity |
||
| 147 | */ |
||
| 148 | private function makeIndexEntity(array $row, array $results, string $table, ConnectionInterface $connection) |
||
| 149 | { |
||
| 150 | $index = new IndexEntity(); |
||
| 151 | |||
| 152 | $name = $row["name"]; |
||
| 153 | $index->type = $row["unique"] ? "UNIQUE" : "INDEX"; |
||
| 154 | $index->lengths = []; |
||
| 155 | $index->descs = []; |
||
| 156 | $columns = $this->driver->rows("PRAGMA index_info(" . |
||
| 157 | $this->driver->escapeId($name) . ")", $connection); |
||
| 158 | foreach ($columns as $column) { |
||
| 159 | $index->columns[] = $column["name"]; |
||
| 160 | $index->descs[] = null; |
||
| 161 | } |
||
| 162 | if (preg_match('~^CREATE( UNIQUE)? INDEX ' . preg_quote($this->driver->escapeId($name) . ' ON ' . |
||
| 163 | $this->driver->escapeId($table), '~') . ' \((.*)\)$~i', $results[$name], $regs)) { |
||
| 164 | preg_match_all('/("[^"]*+")+( DESC)?/', $regs[2], $matches); |
||
| 165 | foreach ($matches[2] as $key => $val) { |
||
| 166 | if ($val) { |
||
| 167 | $index->descs[$key] = '1'; |
||
| 168 | } |
||
| 169 | } |
||
| 170 | } |
||
| 171 | return $index; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | * @param string $table |
||
| 176 | * @param ConnectionInterface $connection |
||
| 177 | * |
||
| 178 | * @return IndexEntity|null |
||
| 179 | */ |
||
| 180 | private function queryPrimaryIndex(string $table, ConnectionInterface $connection) |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param string $table |
||
| 200 | * @param ConnectionInterface $connection |
||
| 201 | * |
||
| 202 | * @return IndexEntity |
||
| 203 | */ |
||
| 204 | private function makePrimaryIndex(string $table, ConnectionInterface $connection) |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @inheritDoc |
||
| 227 | */ |
||
| 228 | public function indexes(string $table, ConnectionInterface $connection = null) |
||
| 229 | { |
||
| 230 | if (!$connection) { |
||
| 231 | $connection = $this->connection; |
||
| 232 | } |
||
| 233 | $primaryIndex = $this->makePrimaryIndex($table, $connection); |
||
| 234 | if ($primaryIndex === null) { |
||
| 235 | return []; |
||
| 236 | } |
||
| 237 | |||
| 238 | $indexes = ['' => $primaryIndex]; |
||
| 239 | $query = "SELECT name, sql FROM sqlite_master WHERE type = 'index' AND tbl_name = " . $this->driver->quote($table); |
||
| 240 | $results = $this->driver->keyValues($query, $connection); |
||
| 241 | $rows = $this->driver->rows("PRAGMA index_list(" . $this->driver->table($table) . ")", $connection); |
||
| 242 | foreach ($rows as $row) { |
||
| 243 | $index = $this->makeIndexEntity($row, $results, $table, $connection); |
||
| 244 | $name = $row["name"]; |
||
| 245 | if ($index->type === 'UNIQUE' && $index->columns == $primaryIndex->columns && |
||
| 246 | $index->descs == $primaryIndex->descs && preg_match("~^sqlite_~", $name)) { |
||
| 247 | $indexes[$name] = $index; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | return $indexes; |
||
| 252 | } |
||
| 253 | |||
| 254 | /** |
||
| 255 | * @inheritDoc |
||
| 256 | */ |
||
| 257 | public function foreignKeys(string $table) |
||
| 270 | } |
||
| 271 | } |
||
| 272 |