Complex classes like Migration 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 Migration, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Migration |
||
| 13 | { |
||
| 14 | const VERSION_STATUS_UNKNOWN = "unknown"; |
||
| 15 | const VERSION_STATUS_PARTIAL = "partial"; |
||
| 16 | const VERSION_STATUS_COMPLETE = "complete"; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var UriInterface |
||
| 20 | */ |
||
| 21 | protected $uri; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $folder; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var DbDriverInterface |
||
| 30 | */ |
||
| 31 | protected $dbDriver; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var DatabaseInterface |
||
| 35 | */ |
||
| 36 | protected $dbCommand; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var Callable |
||
| 40 | */ |
||
| 41 | protected $callableProgress; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var array |
||
| 45 | */ |
||
| 46 | protected $databases = []; |
||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $migrationTable; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Migration constructor. |
||
| 54 | * |
||
| 55 | * @param UriInterface $uri |
||
| 56 | * @param string $folder |
||
| 57 | * @param bool $requiredBase Define if base.sql is required |
||
| 58 | * @param string $migrationTable |
||
| 59 | * @throws InvalidMigrationFile |
||
| 60 | */ |
||
| 61 | public function __construct(UriInterface $uri, $folder, $requiredBase = true, $migrationTable = 'migration_version') |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @param $scheme |
||
| 73 | * @param $className |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function registerDatabase($scheme, $className) |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @return DbDriverInterface |
||
| 84 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 85 | */ |
||
| 86 | public function getDbDriver() |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return DatabaseInterface |
||
| 93 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 94 | */ |
||
| 95 | public function getDbCommand() |
||
| 103 | |||
| 104 | public function getMigrationTable() |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return mixed |
||
| 111 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 112 | */ |
||
| 113 | protected function getDatabaseClassName() |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Get the full path and name of the "base.sql" script |
||
| 125 | * |
||
| 126 | * @return string |
||
| 127 | */ |
||
| 128 | public function getBaseSql() |
||
| 132 | |||
| 133 | /** |
||
| 134 | * Get the full path script based on the version |
||
| 135 | * |
||
| 136 | * @param $version |
||
| 137 | * @param $increment |
||
| 138 | * @return string |
||
| 139 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 140 | */ |
||
| 141 | public function getMigrationSql($version, $increment) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the file contents and metainfo |
||
| 175 | * @param $file |
||
| 176 | * @return array |
||
| 177 | */ |
||
| 178 | public function getFileContent($file) |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Create the database it it does not exists. Does not use this methos in a production environment |
||
| 205 | * |
||
| 206 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 207 | */ |
||
| 208 | public function prepareEnvironment() |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Restore the database using the "base.sql" script and run all migration scripts |
||
| 216 | * Note: the database must exists. If dont exist run the method prepareEnvironment |
||
| 217 | * |
||
| 218 | * @param int $upVersion |
||
| 219 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 220 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 221 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 222 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 223 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 224 | */ |
||
| 225 | public function reset($upVersion = null) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 246 | */ |
||
| 247 | public function createVersion() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 254 | */ |
||
| 255 | public function updateTableVersion() |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the current database version |
||
| 262 | * |
||
| 263 | * @return string[] The current 'version' and 'status' as an associative array |
||
| 264 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 265 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 266 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 267 | */ |
||
| 268 | public function getCurrentVersion() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * @param $currentVersion |
||
| 275 | * @param $upVersion |
||
| 276 | * @param $increment |
||
| 277 | * @return bool |
||
| 278 | */ |
||
| 279 | protected function canContinue($currentVersion, $upVersion, $increment) |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Method for execute the migration. |
||
| 292 | * |
||
| 293 | * @param int $upVersion |
||
| 294 | * @param int $increment Can accept 1 for UP or -1 for down |
||
| 295 | * @param bool $force |
||
| 296 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 297 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 298 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 299 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 300 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 301 | */ |
||
| 302 | protected function migrate($upVersion, $increment, $force) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Run all scripts to up the database version from current up to latest version or the specified version. |
||
| 332 | * |
||
| 333 | * @param int $upVersion |
||
| 334 | * @param bool $force |
||
| 335 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 336 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 337 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 338 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 339 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 340 | */ |
||
| 341 | public function up($upVersion = null, $force = false) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Run all scripts to up or down the database version from current up to latest version or the specified version. |
||
| 348 | * |
||
| 349 | * @param int $upVersion |
||
| 350 | * @param bool $force |
||
| 351 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 352 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 353 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 354 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 355 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 356 | */ |
||
| 357 | public function update($upVersion = null, $force = false) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Run all scripts to down the database version from current version up to the specified version. |
||
| 370 | * |
||
| 371 | * @param int $upVersion |
||
| 372 | * @param bool $force |
||
| 373 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 374 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 375 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 376 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 377 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 378 | */ |
||
| 379 | public function down($upVersion, $force = false) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param callable $callable |
||
| 386 | */ |
||
| 387 | public function addCallbackProgress(callable $callable) |
||
| 391 | |||
| 392 | public function isDatabaseVersioned() |
||
| 396 | } |
||
| 397 |