Complex classes like Migrator 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 Migrator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Migrator |
||
15 | { |
||
16 | /** |
||
17 | * Migrator configuration array. |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $config; |
||
22 | |||
23 | /** |
||
24 | * Directory to store m. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | protected $dir; |
||
29 | |||
30 | /** |
||
31 | * Directory to store archive m. |
||
32 | * |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $dir_archive; |
||
36 | |||
37 | /** |
||
38 | * Files interactions. |
||
39 | * |
||
40 | * @var FileStorageInterface |
||
41 | */ |
||
42 | protected $files; |
||
43 | |||
44 | /** |
||
45 | * Interface that gives us access to the database. |
||
46 | * |
||
47 | * @var DatabaseStorageInterface |
||
48 | */ |
||
49 | protected $database; |
||
50 | |||
51 | /** |
||
52 | * TemplatesCollection instance. |
||
53 | * |
||
54 | * @var TemplatesCollection |
||
55 | */ |
||
56 | protected $templates; |
||
57 | |||
58 | /** |
||
59 | * Constructor. |
||
60 | * |
||
61 | * @param array $config |
||
62 | * @param TemplatesCollection $templates |
||
63 | * @param DatabaseStorageInterface $database |
||
64 | * @param FileStorageInterface $files |
||
65 | */ |
||
66 | public function __construct($config, TemplatesCollection $templates, DatabaseStorageInterface $database = null, FileStorageInterface $files = null) |
||
76 | |||
77 | /** |
||
78 | * Create migration file. |
||
79 | * |
||
80 | * @param string $name - migration name |
||
81 | * @param string $templateName |
||
82 | * @param array $replace - array of placeholders that should be replaced with a given values. |
||
83 | * @param string $subDir |
||
84 | * |
||
85 | * @return string |
||
86 | */ |
||
87 | public function createMigration($name, $templateName, array $replace = [], $subDir = '') |
||
108 | |||
109 | /** |
||
110 | * Run all migrations that were not run before. |
||
111 | */ |
||
112 | public function runMigrations() |
||
128 | |||
129 | /** |
||
130 | * Run a given migration. |
||
131 | * |
||
132 | * @param string $file |
||
133 | * |
||
134 | * @throws Exception |
||
135 | * |
||
136 | * @return string |
||
137 | */ |
||
138 | public function runMigration($file) |
||
150 | |||
151 | /** |
||
152 | * Log successful migration. |
||
153 | * |
||
154 | * @param string $migration |
||
155 | * |
||
156 | * @return void |
||
157 | */ |
||
158 | public function logSuccessfulMigration($migration) |
||
162 | |||
163 | /** |
||
164 | * Get ran migrations. |
||
165 | * |
||
166 | * @return array |
||
167 | */ |
||
168 | public function getRanMigrations() |
||
172 | |||
173 | /** |
||
174 | * Get all migrations. |
||
175 | * |
||
176 | * @return array |
||
177 | */ |
||
178 | public function getAllMigrations() |
||
182 | |||
183 | /** |
||
184 | * Determine whether migration file for migration exists. |
||
185 | * |
||
186 | * @param string $migration |
||
187 | * |
||
188 | * @return bool |
||
189 | */ |
||
190 | public function doesMigrationFileExist($migration) |
||
194 | |||
195 | /** |
||
196 | * Rollback a given migration. |
||
197 | * |
||
198 | * @param string $file |
||
199 | * |
||
200 | * @throws Exception |
||
201 | * |
||
202 | * @return mixed |
||
203 | */ |
||
204 | public function rollbackMigration($file) |
||
214 | |||
215 | /** |
||
216 | * Remove a migration name from the database so it can be run again. |
||
217 | * |
||
218 | * @param string $file |
||
219 | * |
||
220 | * @return void |
||
221 | */ |
||
222 | public function removeSuccessfulMigrationFromLog($file) |
||
226 | |||
227 | /** |
||
228 | * Delete migration file. |
||
229 | * |
||
230 | * @param string $migration |
||
231 | * |
||
232 | * @return bool |
||
233 | */ |
||
234 | public function deleteMigrationFile($migration) |
||
238 | |||
239 | /** |
||
240 | * Get array of migrations that should be ran. |
||
241 | * |
||
242 | * @return array |
||
243 | */ |
||
244 | public function getMigrationsToRun() |
||
252 | |||
253 | /** |
||
254 | * Move migration files. |
||
255 | * |
||
256 | * @param array $files |
||
257 | * @param string $toDir |
||
258 | * |
||
259 | * @return int |
||
260 | */ |
||
261 | public function moveMigrationFiles($files = [], $toDir = '') |
||
285 | |||
286 | /** |
||
287 | * Construct migration file name from migration name and current time. |
||
288 | * |
||
289 | * @param string $name |
||
290 | * |
||
291 | * @return string |
||
292 | */ |
||
293 | protected function constructFileName($name) |
||
301 | |||
302 | /** |
||
303 | * Get a migration class name by a migration file name. |
||
304 | * |
||
305 | * @param string $file |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | protected function getMigrationClassNameByFileName($file) |
||
318 | |||
319 | /** |
||
320 | * Replace all placeholders in the stub. |
||
321 | * |
||
322 | * @param string $template |
||
323 | * @param array $replace |
||
324 | * |
||
325 | * @return string |
||
326 | */ |
||
327 | protected function replacePlaceholdersInTemplate($template, array $replace) |
||
335 | |||
336 | /** |
||
337 | * Resolve a migration instance from a file. |
||
338 | * |
||
339 | * @param string $file |
||
340 | * |
||
341 | * @throws Exception |
||
342 | * |
||
343 | * @return MigrationInterface |
||
344 | */ |
||
345 | protected function getMigrationObjectByFileName($file) |
||
359 | |||
360 | /** |
||
361 | * Require migration file. |
||
362 | * |
||
363 | * @param string $file |
||
364 | * |
||
365 | * @return void |
||
366 | */ |
||
367 | protected function requireMigrationFile($file) |
||
371 | |||
372 | /** |
||
373 | * Get path to a migration file. |
||
374 | * |
||
375 | * @param string $migration |
||
376 | * |
||
377 | * @return string |
||
378 | */ |
||
379 | protected function getMigrationFilePath($migration) |
||
388 | |||
389 | /** |
||
390 | * If package arrilot/bitrix-iblock-helper is loaded then we should disable its caching to avoid problems. |
||
391 | */ |
||
392 | private function disableBitrixIblockHelperCache() |
||
408 | } |
||
409 |