| Total Complexity | 55 |
| Total Lines | 296 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ExportAdmin 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 ExportAdmin, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class ExportAdmin extends AbstractAdmin |
||
| 19 | { |
||
| 20 | use Traits\TableExportTrait; |
||
| 21 | use Traits\TableDumpTrait; |
||
|
1 ignored issue
–
show
|
|||
| 22 | |||
| 23 | /** |
||
| 24 | * The databases to dump |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $databases; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The tables to dump |
||
| 32 | * |
||
| 33 | * @var array |
||
| 34 | */ |
||
| 35 | private $tables; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Get data for export |
||
| 39 | * |
||
| 40 | * @param string $database The database name |
||
| 41 | * @param string $table |
||
| 42 | * |
||
| 43 | * @return array |
||
| 44 | */ |
||
| 45 | public function getExportOptions(string $database, string $table = ''): array |
||
| 46 | { |
||
| 47 | $results = [ |
||
| 48 | 'options' => $this->getBaseOptions($database, $table), |
||
| 49 | 'prefixes' => [], |
||
| 50 | 'labels' => [ |
||
| 51 | 'export' => $this->trans->lang('Export'), |
||
| 52 | ], |
||
| 53 | ]; |
||
| 54 | if (($database)) { |
||
| 55 | $results['tables'] = $this->getDbTables(); |
||
| 56 | } else { |
||
| 57 | $results['databases'] = $this->getDatabases(); |
||
| 58 | } |
||
| 59 | return $results; |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Dump routines in the connected database |
||
| 64 | * |
||
| 65 | * @param string $database The database name |
||
| 66 | * |
||
| 67 | * @return void |
||
| 68 | */ |
||
| 69 | private function dumpRoutines(string $database) |
||
| 70 | { |
||
| 71 | // From dump.inc.php |
||
| 72 | $style = $this->options['db_style']; |
||
| 73 | |||
| 74 | if ($this->options['routines']) { |
||
| 75 | $sql = 'SHOW FUNCTION STATUS WHERE Db = ' . $this->driver->quote($database); |
||
| 76 | foreach ($this->driver->rows($sql) as $row) { |
||
| 77 | $sql = 'SHOW CREATE FUNCTION ' . $this->driver->escapeId($row['Name']); |
||
| 78 | $create = $this->admin->removeDefiner($this->driver->result($sql, 2)); |
||
| 79 | $this->queries[] = $this->driver->setUtf8mb4($create); |
||
| 80 | if ($style != 'DROP+CREATE') { |
||
| 81 | $this->queries[] = 'DROP FUNCTION IF EXISTS ' . $this->driver->escapeId($row['Name']) . ';;'; |
||
| 82 | } |
||
| 83 | $this->queries[] = "$create;;\n"; |
||
| 84 | } |
||
| 85 | $sql = 'SHOW PROCEDURE STATUS WHERE Db = ' . $this->driver->quote($database); |
||
| 86 | foreach ($this->driver->rows($sql) as $row) { |
||
| 87 | $sql = 'SHOW CREATE PROCEDURE ' . $this->driver->escapeId($row['Name']); |
||
| 88 | $create = $this->admin->removeDefiner($this->driver->result($sql, 2)); |
||
| 89 | $this->queries[] = $this->driver->setUtf8mb4($create); |
||
| 90 | if ($style != 'DROP+CREATE') { |
||
| 91 | $this->queries[] = 'DROP PROCEDURE IF EXISTS ' . $this->driver->escapeId($row['Name']) . ';;'; |
||
| 92 | } |
||
| 93 | $this->queries[] = "$create;;\n"; |
||
| 94 | } |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Dump events in the connected database |
||
| 100 | * |
||
| 101 | * @return void |
||
| 102 | */ |
||
| 103 | private function dumpEvents() |
||
| 104 | { |
||
| 105 | // From dump.inc.php |
||
| 106 | $style = $this->options['db_style']; |
||
| 107 | |||
| 108 | if ($this->options['events']) { |
||
| 109 | foreach ($this->driver->rows('SHOW EVENTS') as $row) { |
||
| 110 | $sql = 'SHOW CREATE EVENT ' . $this->driver->escapeId($row['Name']); |
||
| 111 | $create = $this->admin->removeDefiner($this->driver->result($sql, 3)); |
||
| 112 | $this->queries[] = $this->driver->setUtf8mb4($create); |
||
| 113 | if ($style != 'DROP+CREATE') { |
||
| 114 | $this->queries[] = 'DROP EVENT IF EXISTS ' . $this->driver->escapeId($row['Name']) . ';;'; |
||
| 115 | } |
||
| 116 | $this->queries[] = "$create;;\n"; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $table |
||
| 123 | * @param bool $dumpTable |
||
| 124 | * @param bool $dumpData |
||
| 125 | * |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | private function dumpTable(string $table, bool $dumpTable, bool $dumpData) |
||
| 129 | { |
||
| 130 | $this->dumpTableOrView($table, ($dumpTable ? $this->options['table_style'] : '')); |
||
| 131 | if ($dumpData) { |
||
| 132 | $fields = $this->driver->fields($table); |
||
| 133 | $query = 'SELECT *' . $this->driver->convertFields($fields, $fields) . |
||
| 134 | ' FROM ' . $this->driver->table($table); |
||
| 135 | $this->dumpData($table, $query); |
||
| 136 | } |
||
| 137 | if ($this->options['is_sql'] && $this->options['triggers'] && $dumpTable && |
||
| 138 | ($triggers = $this->driver->sqlForCreateTrigger($table))) { |
||
| 139 | $this->queries[] = 'DELIMITER ;'; |
||
| 140 | $this->queries[] = $triggers; |
||
| 141 | $this->queries[] = 'DELIMITER ;'; |
||
| 142 | } |
||
| 143 | if ($this->options['is_sql']) { |
||
| 144 | $this->queries[] = ''; |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $database The database name |
||
| 150 | * |
||
| 151 | * @return void |
||
| 152 | */ |
||
| 153 | private function dumpTables(string $database) |
||
| 154 | { |
||
| 155 | $dbDumpTable = $this->tables['list'] === '*' && in_array($database, $this->databases['list']); |
||
| 156 | $dbDumpData = in_array($database, $this->databases['data']); |
||
| 157 | $this->views = []; // View names |
||
| 158 | $this->fkeys = []; // Table names for foreign keys |
||
| 159 | $dbTables = $this->driver->tableStatuses(true); |
||
| 160 | foreach ($dbTables as $table => $tableStatus) { |
||
| 161 | $isView = $this->driver->isView($tableStatus); |
||
| 162 | if ($isView) { |
||
| 163 | // The views will be dumped after the tables |
||
| 164 | $this->views[] = $table; |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | $this->fkeys[] = $table; |
||
| 168 | $dumpTable = $dbDumpTable || in_array($table, $this->tables['list']); |
||
| 169 | $dumpData = $dbDumpData || in_array($table, $this->tables['data']); |
||
| 170 | if ($dumpTable || $dumpData) { |
||
| 171 | $this->dumpTable($table, $dumpTable, $dumpData); |
||
| 172 | } |
||
| 173 | } |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @return void |
||
| 178 | */ |
||
| 179 | private function dumpViewsAndFKeys() |
||
| 180 | { |
||
| 181 | // Add FKs after creating tables (except in MySQL which uses SET FOREIGN_KEY_CHECKS=0) |
||
| 182 | if ($this->driver->support('fkeys_sql')) { |
||
| 183 | foreach ($this->fkeys as $table) { |
||
| 184 | $this->queries[] = $this->driver->sqlForForeignKeys($table); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | // Dump the views after all the tables |
||
| 188 | foreach ($this->views as $view) { |
||
| 189 | $this->dumpTableOrView($view, $this->options['table_style'], 1); |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param string $database |
||
| 195 | * |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | private function dumpDatabaseCreation(string $database) |
||
| 199 | { |
||
| 200 | $style = $this->options['db_style']; |
||
| 201 | $this->driver->connect($database, ''); |
||
| 202 | $sql = 'SHOW CREATE DATABASE ' . $this->driver->escapeId($database); |
||
| 203 | if ($this->options['is_sql'] && preg_match('~CREATE~', $style) && |
||
| 204 | ($create = $this->driver->result($sql, 1))) { |
||
| 205 | $this->driver->setUtf8mb4($create); |
||
| 206 | if ($style == 'DROP+CREATE') { |
||
| 207 | $this->queries[] = 'DROP DATABASE IF EXISTS ' . $this->driver->escapeId($database) . ';'; |
||
| 208 | } |
||
| 209 | $this->queries[] = $create . ';'; |
||
| 210 | $this->queries[] = ''; // Empty line |
||
| 211 | } |
||
| 212 | if ($this->options['is_sql'] && $this->options['db_style'] && $this->driver->jush() === 'sql') { |
||
| 213 | if (($query = $this->driver->sqlForUseDatabase($database))) { |
||
| 214 | $this->queries[] = $query . ';'; |
||
| 215 | } |
||
| 216 | $this->queries[] = ''; // Empty line |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @param string $database |
||
| 222 | * |
||
| 223 | * @return void |
||
| 224 | */ |
||
| 225 | private function dumpDatabase(string $database) |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @return array|null |
||
| 252 | */ |
||
| 253 | private function getDatabaseExportHeaders() |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Export databases |
||
| 279 | * |
||
| 280 | * @param array $databases The databases to dump |
||
| 281 | * @param array $tables The tables to dump |
||
| 282 | * @param array $options The export options |
||
| 283 | * |
||
| 284 | * @return array|string |
||
| 285 | */ |
||
| 286 | public function exportDatabases(array $databases, array $tables, array $options) |
||
| 314 | ]; |
||
| 315 | } |
||
| 317 |