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 |
||
| 18 | class Migrator |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * @const string |
||
| 22 | */ |
||
| 23 | const DIRECTION_UP = 'up'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @const string |
||
| 27 | */ |
||
| 28 | const DIRECTION_DOWN = 'down'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $migrationsTableName = 'migrations'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var string |
||
| 37 | */ |
||
| 38 | protected $migrationsDirectory; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var Db |
||
| 42 | */ |
||
| 43 | protected $db; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var callable |
||
| 47 | */ |
||
| 48 | protected $infoCallback; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var AbstractMigration[] |
||
| 52 | */ |
||
| 53 | protected $migrations; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var int[] |
||
| 57 | */ |
||
| 58 | protected $migratedNumbers; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | private $hasMigrationsTable; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param string $migrationsDirectory |
||
| 67 | * @param Db $db |
||
| 68 | * @param callable $infoCallback |
||
| 69 | */ |
||
| 70 | 10 | public function __construct( |
|
| 79 | |||
| 80 | /** |
||
| 81 | * @return \SplFileInfo[] |
||
| 82 | */ |
||
| 83 | 8 | protected function findMigrationFiles() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * @return AbstractMigration[] |
||
| 98 | */ |
||
| 99 | 8 | protected function loadMigrations() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return AbstractMigration[] |
||
| 118 | */ |
||
| 119 | 8 | public function getMigrations() |
|
| 127 | |||
| 128 | /** |
||
| 129 | * @return int |
||
| 130 | */ |
||
| 131 | 8 | public function getLatestNumber() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 8 | private function hasMigrationsTable() |
|
| 151 | |||
| 152 | /** |
||
| 153 | */ |
||
| 154 | 8 | protected function createMigrationsTable() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | 8 | protected function getMigratedNumbers() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return int |
||
| 189 | */ |
||
| 190 | 8 | public function getCurrentNumber() |
|
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $toNumber |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | 4 | protected function getDirection($toNumber) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param int $to |
||
| 209 | * @return AbstractMigration[] |
||
| 210 | */ |
||
| 211 | 4 | public function getMigrationsTo($to = null) |
|
| 229 | |||
| 230 | /** |
||
| 231 | * @param AbstractMigration $migration |
||
| 232 | * @param int $toNumber |
||
| 233 | * @param string $direction |
||
| 234 | * @return bool |
||
| 235 | */ |
||
| 236 | 4 | private function shouldMigrationBeMigrated(AbstractMigration $migration, $toNumber, $direction) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param AbstractMigration $migration |
||
| 253 | */ |
||
| 254 | 2 | protected function addMigratedMigration(AbstractMigration $migration) |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @param AbstractMigration $migration |
||
| 268 | */ |
||
| 269 | 1 | protected function deleteMigratedMigration(AbstractMigration $migration) |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Empty database. |
||
| 285 | */ |
||
| 286 | 1 | public function emptyDb() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param int|null $to |
||
| 299 | * @return int |
||
| 300 | * @throws \InvalidArgumentException |
||
| 301 | */ |
||
| 302 | 6 | protected function getToNumber($to = null) |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param int|null $to A migration number to migrate to (if not provided, latest migration number will be used) |
||
| 321 | * @return bool Returns true if any action was performed |
||
| 322 | * @throws \InvalidArgumentException |
||
| 323 | * @throws \RuntimeException |
||
| 324 | */ |
||
| 325 | 7 | public function migrate($to = null) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @param AbstractMigration[] $migrations |
||
| 364 | * @param string $direction |
||
| 365 | */ |
||
| 366 | 3 | protected function runMigrations(array $migrations, $direction) |
|
| 380 | |||
| 381 | /** |
||
| 382 | * @param string $info |
||
| 383 | */ |
||
| 384 | 4 | protected function addInfo($info) |
|
| 391 | } |
||
| 392 |