Total Complexity | 64 |
Total Lines | 275 |
Duplicated Lines | 0 % |
Changes | 3 | ||
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 |
||
15 | class Table extends AbstractTable |
||
16 | { |
||
17 | /** |
||
18 | * @param bool $fast |
||
19 | * @param string $table |
||
20 | * |
||
21 | * @return array |
||
22 | */ |
||
23 | private function queryStatus(bool $fast, string $table = '') |
||
31 | } |
||
32 | |||
33 | /** |
||
34 | * @param array $row |
||
35 | * |
||
36 | * @return TableEntity |
||
37 | */ |
||
38 | private function makeStatus(array $row) |
||
51 | } |
||
52 | |||
53 | /** |
||
54 | * @inheritDoc |
||
55 | */ |
||
56 | public function tableStatus(string $table, bool $fast = false) |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @inheritDoc |
||
67 | */ |
||
68 | public function tableStatuses(bool $fast = false) |
||
69 | { |
||
70 | $tables = []; |
||
71 | $rows = $this->queryStatus($fast); |
||
72 | foreach ($rows as $row) { |
||
73 | $tables[$row["Name"]] = $this->makeStatus($row); |
||
74 | } |
||
75 | return $tables; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * @inheritDoc |
||
80 | */ |
||
81 | public function tableNames() |
||
82 | { |
||
83 | $tables = []; |
||
84 | $rows = $this->queryStatus(true); |
||
85 | foreach ($rows as $row) { |
||
86 | $tables[] = $row["Name"]; |
||
87 | } |
||
88 | return $tables; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @inheritDoc |
||
93 | */ |
||
94 | public function fields(string $table) |
||
95 | { |
||
96 | $fields = []; |
||
97 | foreach ($this->driver->rows("SHOW FULL COLUMNS FROM " . $this->driver->table($table)) as $row) { |
||
98 | preg_match('~^([^( ]+)(?:\((.+)\))?( unsigned)?( zerofill)?$~', $row["Type"], $match); |
||
99 | $matchCount = count($match); |
||
100 | $match1 = $matchCount > 1 ? $match[1] : ''; |
||
101 | $match2 = $matchCount > 2 ? $match[2] : ''; |
||
102 | $match3 = $matchCount > 3 ? $match[3] : ''; |
||
103 | $match4 = $matchCount > 4 ? $match[4] : ''; |
||
104 | |||
105 | $field = new TableFieldEntity(); |
||
106 | |||
107 | $field->name = $row["Field"]; |
||
108 | $field->fullType = $row["Type"]; |
||
109 | $field->type = $match1; |
||
110 | $field->length = intval($match2); |
||
111 | $field->unsigned = ltrim($match3 . $match4); |
||
112 | $field->default = ($row["Default"] != "" || preg_match("~char|set~", $match1) ? |
||
113 | (preg_match('~text~', $match1) ? stripslashes(preg_replace("~^'(.*)'\$~", '\1', |
||
114 | $row["Default"])) : $row["Default"]) : null); |
||
115 | $field->null = ($row["Null"] == "YES"); |
||
116 | $field->autoIncrement = ($row["Extra"] == "auto_increment"); |
||
117 | $field->onUpdate = (preg_match('~^on update (.+)~i', $row["Extra"], $match) ? $match1 : ""); //! available since MySQL 5.1.23 |
||
118 | $field->collation = $row["Collation"]; |
||
119 | $field->privileges = array_flip(preg_split('~, *~', $row["Privileges"])); |
||
120 | $field->comment = $row["Comment"]; |
||
121 | $field->primary = ($row["Key"] == "PRI"); |
||
122 | // https://mariadb.com/kb/en/library/show-columns/ |
||
123 | // https://github.com/vrana/adminer/pull/359#pullrequestreview-276677186 |
||
124 | $field->generated = preg_match('~^(VIRTUAL|PERSISTENT|STORED)~', $row["Extra"]) > 0; |
||
125 | |||
126 | $fields[$field->name] = $field; |
||
127 | } |
||
128 | return $fields; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * @inheritDoc |
||
133 | */ |
||
134 | public function supportForeignKeys(TableEntity $tableStatus) |
||
135 | { |
||
136 | return preg_match('~InnoDB|IBMDB2I~i', $tableStatus->engine) |
||
137 | || (preg_match('~NDB~i', $tableStatus->engine) && $this->driver->minVersion(5.6)); |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * @inheritDoc |
||
142 | */ |
||
143 | public function referencableTables(string $table) |
||
144 | { |
||
145 | $fields = []; // table_name => [field] |
||
146 | foreach ($this->tableStatuses(true) as $tableName => $tableStatus) { |
||
147 | if ($tableName === $table || !$this->supportForeignKeys($tableStatus)) { |
||
148 | continue; |
||
149 | } |
||
150 | foreach ($this->fields($tableName) as $field) { |
||
151 | if ($field->primary) { |
||
152 | if (!isset($fields[$tableName])) { |
||
153 | $fields[$tableName] = $field; |
||
154 | } else { |
||
155 | // No multi column primary key |
||
156 | $fields[$tableName] = null; |
||
157 | } |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | return array_filter($fields, function($field) { |
||
162 | return $field !== null; |
||
163 | }); |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * @inheritDoc |
||
168 | */ |
||
169 | public function isView(TableEntity $tableStatus) |
||
170 | { |
||
171 | return $tableStatus->engine === null; |
||
172 | } |
||
173 | |||
174 | /** |
||
175 | * @inheritDoc |
||
176 | */ |
||
177 | public function indexes(string $table, ConnectionInterface $connection = null) |
||
178 | { |
||
179 | $indexes = []; |
||
180 | foreach ($this->driver->rows("SHOW INDEX FROM " . $this->driver->table($table), $connection) as $row) { |
||
181 | $index = new IndexEntity(); |
||
182 | |||
183 | $name = $row["Key_name"]; |
||
184 | $index->type = ($name == "PRIMARY" ? "PRIMARY" : |
||
185 | ($row["Index_type"] == "FULLTEXT" ? "FULLTEXT" : ($row["Non_unique"] ? |
||
186 | ($row["Index_type"] == "SPATIAL" ? "SPATIAL" : "INDEX") : "UNIQUE"))); |
||
187 | $index->columns[] = $row["Column_name"]; |
||
188 | $index->lengths[] = ($row["Index_type"] == "SPATIAL" ? null : $row["Sub_part"]); |
||
189 | $index->descs[] = null; |
||
190 | |||
191 | $indexes[$name] = $index; |
||
192 | } |
||
193 | return $indexes; |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * @inheritDoc |
||
198 | */ |
||
199 | public function foreignKeys(string $table) |
||
200 | { |
||
201 | static $pattern = '(?:`(?:[^`]|``)+`|"(?:[^"]|"")+")'; |
||
202 | $foreignKeys = []; |
||
203 | $onActions = $this->driver->actions(); |
||
204 | $create_table = $this->connection->result("SHOW CREATE TABLE " . $this->driver->table($table), 1); |
||
205 | if ($create_table) { |
||
206 | preg_match_all("~CONSTRAINT ($pattern) FOREIGN KEY ?\\(((?:$pattern,? ?)+)\\) REFERENCES " . |
||
207 | "($pattern)(?:\\.($pattern))? \\(((?:$pattern,? ?)+)\\)(?: ON DELETE ($onActions))" . |
||
208 | "?(?: ON UPDATE ($onActions))?~", $create_table, $matches, PREG_SET_ORDER); |
||
209 | |||
210 | foreach ($matches as $match) { |
||
211 | $matchCount = count($match); |
||
212 | $match1 = $matchCount > 1 ? $match[1] : ''; |
||
213 | $match2 = $matchCount > 2 ? $match[2] : ''; |
||
214 | $match3 = $matchCount > 3 ? $match[3] : ''; |
||
215 | $match4 = $matchCount > 4 ? $match[4] : ''; |
||
216 | $match5 = $matchCount > 5 ? $match[5] : ''; |
||
217 | |||
218 | preg_match_all("~$pattern~", $match2, $source); |
||
219 | preg_match_all("~$pattern~", $match5, $target); |
||
220 | |||
221 | $foreignKey = new ForeignKeyEntity(); |
||
222 | |||
223 | $foreignKey->database = $this->driver->unescapeId($match4 != "" ? $match3 : $match4); |
||
224 | $foreignKey->table = $this->driver->unescapeId($match4 != "" ? $match4 : $match3); |
||
225 | $foreignKey->source = array_map(function ($idf) { |
||
226 | return $this->driver->unescapeId($idf); |
||
227 | }, $source[0]); |
||
228 | $foreignKey->target = array_map(function ($idf) { |
||
229 | return $this->driver->unescapeId($idf); |
||
230 | }, $target[0]); |
||
231 | $foreignKey->onDelete = $matchCount > 6 ? $match[6] : "RESTRICT"; |
||
232 | $foreignKey->onUpdate = $matchCount > 7 ? $match[7] : "RESTRICT"; |
||
233 | |||
234 | $foreignKeys[$this->driver->unescapeId($match1)] = $foreignKey; |
||
235 | } |
||
236 | } |
||
237 | return $foreignKeys; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @inheritDoc |
||
242 | */ |
||
243 | public function trigger(string $name, string $table = '') |
||
244 | { |
||
245 | if ($name == "") { |
||
246 | return null; |
||
247 | } |
||
248 | $rows = $this->driver->rows("SHOW TRIGGERS WHERE `Trigger` = " . $this->driver->quote($name)); |
||
249 | if (!($row = reset($rows))) { |
||
250 | return null; |
||
251 | } |
||
252 | return new TriggerEntity($row["Timing"], $row["Event"], '', '', $row["Trigger"]); |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @inheritDoc |
||
257 | */ |
||
258 | public function triggers(string $table) |
||
259 | { |
||
260 | $triggers = []; |
||
261 | foreach ($this->driver->rows("SHOW TRIGGERS LIKE " . $this->driver->quote(addcslashes($table, "%_\\"))) as $row) { |
||
262 | $triggers[$row["Trigger"]] = new TriggerEntity($row["Timing"], $row["Event"], '', '', $row["Trigger"]); |
||
263 | } |
||
264 | return $triggers; |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * @inheritDoc |
||
269 | */ |
||
270 | public function triggerOptions() |
||
276 | ]; |
||
277 | } |
||
278 | |||
279 | /** |
||
280 | * @inheritDoc |
||
281 | */ |
||
282 | public function tableHelp(string $name) |
||
290 | } |
||
291 | } |
||
292 | } |
||
293 |