Total Complexity | 44 |
Total Lines | 266 |
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) |
||
44 | { |
||
45 | $this->fileSql = $fileSql; |
||
46 | $this->fileYaml = $fileYaml; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Create a yaml file based on a sql file |
||
51 | * |
||
52 | * @param null |
||
53 | * @return bool |
||
54 | */ |
||
55 | public function createSchemaFromSqlfile(): bool |
||
177 | |||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Extract table name from given line |
||
182 | * |
||
183 | * @param string $line |
||
184 | * @return string|bool |
||
185 | */ |
||
186 | private function getTableName (string $line) |
||
195 | |||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Extract columns/fields of table from given line |
||
200 | * |
||
201 | * @param string $line |
||
202 | * @return array|bool |
||
203 | */ |
||
204 | private function getColumns (string $line) |
||
205 | { |
||
206 | |||
207 | $columns = []; |
||
208 | |||
209 | $arrCol = \explode( ' ', \trim($line)); |
||
210 | if (\count($arrCol) > 0) { |
||
211 | $name = \str_replace(['`'], '', $arrCol[0]); |
||
212 | } else { |
||
213 | return false; |
||
214 | } |
||
215 | $attributes = \trim(\str_replace([$name, '`', ','], '', $line)); |
||
216 | |||
217 | $columns['name'] = $name; |
||
218 | // update quotes |
||
219 | if (\strpos($attributes, "''") > 0) { |
||
220 | $attributes = \trim(\str_replace("''", "''''''''" , $attributes)); |
||
221 | } elseif (\strpos($attributes, "'") > 0) { |
||
222 | $attributes = \trim(\str_replace("'", "''" , $attributes)); |
||
223 | } |
||
224 | $columns['attributes'] = "' " . $attributes . " '"; |
||
225 | |||
226 | return $columns; |
||
227 | |||
228 | } |
||
229 | |||
230 | /** |
||
231 | * Extract options of table from given line |
||
232 | * |
||
233 | * @param string $line |
||
234 | * @param string $options |
||
235 | * @return void |
||
236 | */ |
||
237 | private function getOptions (string $line, string &$options): void |
||
238 | { |
||
239 | |||
240 | $lineText = \trim(\str_replace([')', ';'], '', $line)); |
||
241 | // remove all existing ' |
||
242 | $options = \str_replace("'", '', $options); |
||
243 | if ('' != $options) { |
||
244 | $options .= ' '; |
||
245 | } |
||
246 | $options = "'" . $options . $lineText . "'"; |
||
247 | |||
248 | } |
||
249 | |||
250 | /** |
||
251 | * Extract keys of table from given line |
||
252 | * |
||
253 | * @param string $line |
||
254 | * @return array |
||
255 | */ |
||
256 | private function getKey (string $line) |
||
291 | } |
||
292 | } |
||
293 |