Total Complexity | 47 |
Total Lines | 238 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like TableDumpTrait 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 TableDumpTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | trait TableDumpTrait |
||
18 | { |
||
19 | /** |
||
20 | * The dump options |
||
21 | * |
||
22 | * @var array |
||
23 | */ |
||
24 | private $options; |
||
25 | |||
26 | /** |
||
27 | * The queries generated by the dump |
||
28 | * |
||
29 | * @var array |
||
30 | */ |
||
31 | private $queries = []; |
||
32 | |||
33 | // Temp vars for data dumps |
||
34 | private $insert = ''; |
||
35 | private $buffer = ''; |
||
36 | private $suffix = ''; |
||
37 | private $views = []; |
||
38 | private $fkeys = []; |
||
39 | |||
40 | /** |
||
41 | * Print CSV row |
||
42 | * |
||
43 | * @param array $row |
||
44 | * |
||
45 | * @return void |
||
46 | */ |
||
47 | private function dumpCsv(array $row) |
||
48 | { |
||
49 | // From functions.inc.php |
||
50 | foreach ($row as $key => $val) { |
||
51 | if (preg_match('~["\n,;\t]|^0|\.\d*0$~', $val) || $val === '') { |
||
52 | $row[$key] = '"' . str_replace('"', '""', $val) . '"'; |
||
53 | } |
||
54 | } |
||
55 | $separator = $this->options['format'] === 'csv' ? ',' : |
||
56 | ($this->options['format'] === 'tsv' ? "\t" : ';'); |
||
57 | $this->queries[] = implode($separator, $row); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * Convert a value to string |
||
62 | * |
||
63 | * @param mixed $value |
||
64 | * @param TableFieldEntity $field |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | private function convertToString($value, TableFieldEntity $field): string |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * @param string $table |
||
83 | * @param string $style |
||
84 | * @param int $tableType |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | private function getCreateTableQuery(string $table, string $style, int $tableType): string |
||
89 | { |
||
90 | if ($tableType !== 2) { |
||
91 | return $this->driver->sqlForCreateTable($table, $this->options['autoIncrement'], $style); |
||
92 | } |
||
93 | $fields = []; |
||
94 | foreach ($this->driver->fields($table) as $name => $field) { |
||
95 | $fields[] = $this->driver->escapeId($name) . ' ' . $field->fullType; |
||
96 | } |
||
97 | return 'CREATE TABLE ' . $this->driver->table($table) . ' (' . implode(', ', $fields) . ')'; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Export table structure |
||
102 | * |
||
103 | * @param string $table |
||
104 | * @param string $style |
||
105 | * @param int $tableType 0 table, 1 view, 2 temporary view table |
||
106 | * |
||
107 | * @return void |
||
108 | */ |
||
109 | private function dumpTableOrView(string $table, string $style, int $tableType = 0) |
||
110 | { |
||
111 | // From adminer.inc.php |
||
112 | if ($this->options['format'] !== 'sql') { |
||
113 | $this->queries[] = "\xef\xbb\xbf"; // UTF-8 byte order mark |
||
114 | if ($style) { |
||
115 | $this->dumpCsv(array_keys($this->driver->fields($table))); |
||
116 | } |
||
117 | return; |
||
118 | } |
||
119 | if (!$style) { |
||
120 | return; |
||
121 | } |
||
122 | |||
123 | $create = $this->getCreateTableQuery($table, $style, $tableType); |
||
124 | $this->driver->setUtf8mb4($create); |
||
125 | if (!$create) { |
||
126 | return; |
||
127 | } |
||
128 | if ($style === 'DROP+CREATE' || $tableType === 1) { |
||
129 | $this->queries[] = 'DROP ' . ($tableType === 2 ? 'VIEW' : 'TABLE') . |
||
130 | ' IF EXISTS ' . $this->driver->table($table) . ';'; |
||
131 | } |
||
132 | if ($tableType === 1) { |
||
133 | $create = $this->admin->removeDefiner($create); |
||
134 | } |
||
135 | $this->queries[] = $create . ';'; |
||
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param array $row |
||
140 | * @param StatementInterface $statement |
||
141 | * |
||
142 | * @return array |
||
143 | */ |
||
144 | private function getDataRowKeys(array $row, StatementInterface $statement): array |
||
145 | { |
||
146 | $values = []; |
||
147 | $keys = []; |
||
148 | // For is preferred to foreach because the values are not used. |
||
149 | // foreach ($row as $val) { |
||
150 | // } |
||
151 | for ($i = 0; $i < count($row); $i++) { |
||
|
|||
152 | $field = $statement->fetchField(); |
||
153 | $keys[] = $field->name(); |
||
154 | $key = $this->driver->escapeId($field->name()); |
||
155 | $values[] = "$key = VALUES($key)"; |
||
156 | } |
||
157 | $this->suffix = ";\n"; |
||
158 | if ($this->options['data_style'] === 'INSERT+UPDATE') { |
||
159 | $this->suffix = "\nON DUPLICATE KEY UPDATE " . implode(', ', $values) . ";\n"; |
||
160 | } |
||
161 | return $keys; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @param array $row |
||
166 | * |
||
167 | * @return void |
||
168 | */ |
||
169 | private function saveRowInBuffer(array $row) |
||
170 | { |
||
171 | $max_packet = ($this->driver->jush() === 'sqlite' ? 0 : 1048576); // default, minimum is 1024 |
||
172 | $s = ($max_packet ? "\n" : ' ') . '(' . implode(",\t", $row) . ')'; |
||
173 | if (!$this->buffer) { |
||
174 | $this->buffer = $this->insert . $s; |
||
175 | return; |
||
176 | } |
||
177 | if (strlen($this->buffer) + 4 + strlen($s) + strlen($this->suffix) < $max_packet) { // 4 - length specification |
||
178 | $this->buffer .= ",$s"; |
||
179 | return; |
||
180 | } |
||
181 | $this->queries[] = $this->buffer . $this->suffix; |
||
182 | $this->buffer = $this->insert . $s; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param string $table |
||
187 | * @param array $fields |
||
188 | * @param array $row |
||
189 | * @param array $keys |
||
190 | * |
||
191 | * @return void |
||
192 | */ |
||
193 | private function dumpRow(string $table, array $fields, array $row, array $keys) |
||
214 | } |
||
215 | |||
216 | /** Export table data |
||
217 | * |
||
218 | * @param string $table |
||
219 | * @param string $query |
||
220 | * |
||
221 | * @return void |
||
222 | */ |
||
223 | private function dumpData(string $table, string $query) |
||
255 | } |
||
256 | } |
||
257 | } |
||
258 |
If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration: