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 declare(strict_types=1); |
||
18 | class Migrator |
||
19 | { |
||
20 | private const DIRECTION_UP = 'up'; |
||
21 | |||
22 | private const DIRECTION_DOWN = 'down'; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | protected $migrationsTableName = 'migrations'; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $migrationsDirectory; |
||
33 | |||
34 | /** |
||
35 | * @var Db |
||
36 | */ |
||
37 | protected $db; |
||
38 | |||
39 | /** |
||
40 | * @var callable |
||
41 | */ |
||
42 | protected $infoCallback; |
||
43 | |||
44 | /** |
||
45 | * @var AbstractMigration[] |
||
46 | */ |
||
47 | protected $migrations; |
||
48 | |||
49 | /** |
||
50 | * @var int[] |
||
51 | */ |
||
52 | protected $migratedNumbers; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $hasMigrationsTable; |
||
58 | |||
59 | 9 | public function __construct(string $migrationsDirectory, Db $db, callable $infoCallback = null) |
|
65 | |||
66 | /** |
||
67 | * @return \SplFileInfo[] |
||
68 | */ |
||
69 | 7 | protected function findMigrationFiles(): array |
|
81 | |||
82 | /** |
||
83 | * @return AbstractMigration[] |
||
84 | */ |
||
85 | 7 | protected function loadMigrations(): array |
|
97 | |||
98 | 7 | private function instantiateMigration(\SplFileInfo $file): AbstractMigration |
|
107 | |||
108 | /** |
||
109 | * @return AbstractMigration[] |
||
110 | */ |
||
111 | 7 | public function getMigrations(): array |
|
119 | |||
120 | 7 | public function getLatestNumber(): int |
|
128 | |||
129 | 7 | private function hasMigrationsTable(): bool |
|
138 | |||
139 | 7 | protected function createMigrationsTable(): void |
|
152 | |||
153 | 7 | protected function getMigratedNumbers(): array |
|
168 | |||
169 | 7 | public function getCurrentNumber(): int |
|
176 | |||
177 | 4 | protected function getDirection(int $toNumber): string |
|
181 | |||
182 | 4 | public function getMigrationsTo(int $to = null): array |
|
195 | |||
196 | 4 | private function getMigrationsToBeMigrated(array $allMigrations, int $toNumber, string $direction): array |
|
207 | |||
208 | 4 | private function shouldMigrationBeMigrated(AbstractMigration $migration, int $toNumber, string $direction): bool |
|
218 | |||
219 | 4 | private function shouldMigrateUp(AbstractMigration $migration, int $toNumber, string $direction): bool |
|
225 | |||
226 | 4 | private function shouldMigrateDown(AbstractMigration $migration, int $toNumber, string $direction): bool |
|
232 | |||
233 | 2 | protected function addMigratedMigration(AbstractMigration $migration): void |
|
244 | |||
245 | 1 | protected function deleteMigratedMigration(AbstractMigration $migration): void |
|
258 | |||
259 | 1 | public function emptyDb(): void |
|
269 | |||
270 | /** |
||
271 | * @throws \InvalidArgumentException |
||
272 | */ |
||
273 | 5 | protected function getToNumber(int $to = null): int |
|
285 | |||
286 | /** |
||
287 | * @param int|null $to A migration number to migrate to (if not provided, latest migration number will be used) |
||
288 | * @return bool Returns true if any action was performed |
||
289 | * @throws \InvalidArgumentException |
||
290 | * @throws \RuntimeException |
||
291 | */ |
||
292 | 6 | public function migrate(int $to = null): bool |
|
328 | |||
329 | /** |
||
330 | * @param AbstractMigration[] $migrations |
||
331 | * @param string $direction |
||
332 | */ |
||
333 | 3 | protected function runMigrations(array $migrations, string $direction): void |
|
349 | |||
350 | 4 | protected function addInfo(string $info): void |
|
357 | } |
||
358 |