Total Complexity | 45 |
Total Lines | 268 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like MigrateHelper 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 MigrateHelper, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class MigrateHelper |
||
26 | { |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $fileYaml; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $fileSql; |
||
37 | |||
38 | |||
39 | /** |
||
40 | * @param string $fileSql |
||
41 | * @param string $fileYaml |
||
42 | */ |
||
43 | public function __construct(string $fileSql, string $fileYaml) |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Create a yaml file based on a sql file |
||
51 | * |
||
52 | * @return bool |
||
53 | */ |
||
54 | public function createSchemaFromSqlfile(): bool |
||
55 | { |
||
56 | if (!\file_exists($this->fileSql)) { |
||
57 | \xoops_error('Error: Sql file not found!'); |
||
58 | return false; |
||
59 | } |
||
60 | |||
61 | $tables = []; |
||
62 | $schema = []; |
||
63 | $tableName = ''; |
||
64 | |||
65 | // read sql file |
||
66 | $lines = \file($this->fileSql); |
||
67 | |||
68 | // remove unnecessary lines |
||
69 | foreach ($lines as $key => $value) { |
||
70 | $line = \trim($value); |
||
71 | // remove blank lines |
||
72 | if ('' === $line) { |
||
73 | unset($lines[$key]); |
||
74 | } |
||
75 | // remove comment lines |
||
76 | if ('#' === \substr($line, 0, 1)) { |
||
77 | unset($lines[$key]); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | $skip = true; |
||
82 | $skipWords = ['CREATE DATABASE ', 'CREATE VIEW ', 'INSERT INTO ', 'SELECT ', 'DELETE ', 'UPDATE ', 'ALTER ', 'DROP ']; |
||
83 | $options = ''; |
||
84 | // read remaining lines line by line and create new schema |
||
85 | foreach ($lines as $key => $value) { |
||
86 | $line = \trim($value); |
||
87 | foreach ($skipWords as $skipWord) { |
||
88 | if ($skipWord === \mb_strtoupper(\substr($line, 0, \strlen($skipWord)))) { |
||
89 | $skip = true; |
||
90 | } |
||
91 | } |
||
92 | if ('CREATE TABLE' === \mb_strtoupper(\substr($line, 0, 12))) { |
||
93 | $skip = false; |
||
94 | $options = ''; |
||
95 | // start table definition |
||
96 | $tableName = $this->getTableName ($line); |
||
97 | $tables[$tableName] = []; |
||
98 | $tables[$tableName]['options'] = ''; |
||
99 | $tables[$tableName]['columns'] = []; |
||
100 | $tables[$tableName]['keys'] = []; |
||
101 | } else { |
||
102 | if (!$skip) { |
||
103 | if (')' === \mb_strtoupper(\substr($line, 0, 1))) { |
||
104 | // end of table definition |
||
105 | // get options |
||
106 | $this->getOptions($line, $options); |
||
107 | $tables[$tableName]['options'] = $options; |
||
108 | } elseif ('ENGINE' === \mb_strtoupper(\substr($line, 0, 6))) { |
||
109 | $this->getOptions($line, $options); |
||
110 | $tables[$tableName]['options'] = $options; |
||
111 | } elseif ('DEFAULT CHARSET ' === \mb_strtoupper(\substr($line, 0, 16))) { |
||
112 | $this->getOptions($line, $options); |
||
113 | $tables[$tableName]['options'] = $options; |
||
114 | } else { |
||
115 | // get keys and fields |
||
116 | switch (\mb_strtoupper(\substr($line, 0, 3))) { |
||
117 | case 'KEY': |
||
118 | case 'PRI': |
||
119 | case 'UNI': |
||
120 | $tables[$tableName]['keys'][] = $this->getKey($line); |
||
121 | break; |
||
122 | case 'else': |
||
123 | default: |
||
124 | $columns = $this->getColumns($line); |
||
125 | $tables[$tableName]['columns'][] = $columns; |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | } |
||
132 | |||
133 | // create array for new schema |
||
134 | $level1 = \str_repeat(' ', 4); |
||
135 | $level2 = \str_repeat(' ', 8); |
||
136 | $level3 = \str_repeat(' ', 12); |
||
137 | |||
138 | foreach ($tables as $tkey => $table) { |
||
139 | $schema[] = "$tkey:\n"; |
||
140 | foreach ($table as $lkey => $line) { |
||
141 | if ('keys' == $lkey) { |
||
142 | $schema[] = $level1 . "keys:\n"; |
||
143 | foreach ($line as $kkey => $kvalue) { |
||
144 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
145 | $schema[] = $level2 . $kkey2 . ":\n"; |
||
146 | $schema[] = $level3 . 'columns: ' . $kvalue2['columns'] . "\n"; |
||
147 | $schema[] = $level3 . 'unique: ' . $kvalue2['unique'] . "\n"; |
||
148 | } |
||
149 | } |
||
150 | } elseif ('options' == $lkey) { |
||
151 | $schema[] = $level1 . 'options: ' . $line . "\n"; |
||
152 | } else { |
||
153 | $schema[] = $level1 . 'columns: ' . "\n"; |
||
154 | foreach ($line as $kkey => $kvalue) { |
||
155 | $schema[] = $level2 . '-' . "\n"; |
||
156 | foreach ($kvalue as $kkey2 => $kvalue2) { |
||
157 | $schema[] = $level3 . $kkey2 . ": " . $kvalue2 . "\n"; |
||
158 | } |
||
159 | } |
||
160 | } |
||
161 | } |
||
162 | } |
||
163 | |||
164 | // create new file and write schema array into this file |
||
165 | $myfile = \fopen($this->fileYaml, "w"); |
||
166 | if (!$myfile || \is_null($myfile)) { |
||
|
|||
167 | \xoops_error('Error: Unable to open sql file!'); |
||
168 | return false; |
||
169 | } |
||
170 | foreach ($schema as $line) { |
||
171 | \fwrite($myfile, $line); |
||
172 | } |
||
173 | \fclose($myfile); |
||
174 | |||
175 | return true; |
||
176 | |||
177 | } |
||
178 | |||
179 | /** |
||
180 | * Extract table name from given line |
||
181 | * |
||
182 | * @param string $line |
||
183 | * @return string|bool |
||
184 | */ |
||
185 | private function getTableName (string $line) |
||
186 | { |
||
187 | |||
188 | $arrLine = \explode( '`', $line); |
||
189 | if (\count($arrLine) > 0) { |
||
190 | return $arrLine[1]; |
||
191 | } |
||
192 | |||
193 | return false; |
||
194 | |||
195 | } |
||
196 | |||
197 | /** |
||
198 | * Extract columns/fields of table from given line |
||
199 | * |
||
200 | * @param string $line |
||
201 | * @return array|bool |
||
202 | */ |
||
203 | private function getColumns (string $line) |
||
229 | |||
230 | } |
||
231 | |||
232 | /** |
||
233 | * Extract options of table from given line |
||
234 | * |
||
235 | * @param string $line |
||
236 | * @param string $options |
||
237 | * @return void |
||
238 | */ |
||
239 | private function getOptions (string $line, string &$options): void |
||
240 | { |
||
241 | |||
242 | $lineText = \trim(\str_replace([')', ';'], '', $line)); |
||
243 | // remove all existing ' |
||
244 | $options = \str_replace("'", '', $options); |
||
245 | if ('' != $options) { |
||
246 | $options .= ' '; |
||
247 | } |
||
248 | $options = "'" . $options . $lineText . "'"; |
||
249 | |||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Extract keys of table from given line |
||
254 | * |
||
255 | * @param string $line |
||
256 | * @return array |
||
257 | */ |
||
258 | private function getKey (string $line) |
||
293 | } |
||
294 | } |
||
295 |