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() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return bool |
||
| 142 | */ |
||
| 143 | 8 | private function hasMigrationsTable() |
|
| 152 | |||
| 153 | /** |
||
| 154 | */ |
||
| 155 | 8 | protected function createMigrationsTable() |
|
| 168 | |||
| 169 | /** |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 8 | protected function getMigratedNumbers() |
|
| 187 | |||
| 188 | /** |
||
| 189 | * @return int |
||
| 190 | */ |
||
| 191 | 8 | public function getCurrentNumber() |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @param int $toNumber |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | 4 | protected function getDirection($toNumber) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * @param int $to |
||
| 210 | * @return AbstractMigration[] |
||
| 211 | */ |
||
| 212 | 4 | public function getMigrationsTo($to = null) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @param AbstractMigration $migration |
||
| 233 | * @param int $toNumber |
||
| 234 | * @param string $direction |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 4 | private function shouldMigrationBeMigrated(AbstractMigration $migration, $toNumber, $direction) |
|
| 251 | |||
| 252 | /** |
||
| 253 | * @param AbstractMigration $migration |
||
| 254 | */ |
||
| 255 | 2 | protected function addMigratedMigration(AbstractMigration $migration) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param AbstractMigration $migration |
||
| 269 | */ |
||
| 270 | 1 | protected function deleteMigratedMigration(AbstractMigration $migration) |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Empty database. |
||
| 286 | */ |
||
| 287 | 1 | public function emptyDb() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @param int|null $to |
||
| 300 | * @return int |
||
| 301 | * @throws \InvalidArgumentException |
||
| 302 | */ |
||
| 303 | 6 | protected function getToNumber($to = null) |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @param int|null $to A migration number to migrate to (if not provided, latest migration number will be used) |
||
| 322 | * @return bool Returns true if any action was performed |
||
| 323 | * @throws \InvalidArgumentException |
||
| 324 | * @throws \RuntimeException |
||
| 325 | */ |
||
| 326 | 7 | public function migrate($to = null) |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @param AbstractMigration[] $migrations |
||
| 365 | * @param string $direction |
||
| 366 | */ |
||
| 367 | 3 | protected function runMigrations(array $migrations, $direction) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * @param string $info |
||
| 386 | */ |
||
| 387 | 4 | protected function addInfo($info) |
|
| 394 | } |
||
| 395 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.