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