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 | * Create the database it it does not exists. Does not use this methos in a production environment |
||
| 175 | * |
||
| 176 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 177 | */ |
||
| 178 | public function prepareEnvironment() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Restore the database using the "base.sql" script and run all migration scripts |
||
| 186 | * Note: the database must exists. If dont exist run the method prepareEnvironment |
||
| 187 | * |
||
| 188 | * @param int $upVersion |
||
| 189 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 190 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 191 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 192 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 193 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 194 | */ |
||
| 195 | public function reset($upVersion = null) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 214 | */ |
||
| 215 | public function createVersion() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 222 | */ |
||
| 223 | public function updateTableVersion() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Get the current database version |
||
| 230 | * |
||
| 231 | * @return string[] The current 'version' and 'status' as an associative array |
||
| 232 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 233 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 234 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 235 | */ |
||
| 236 | public function getCurrentVersion() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * @param $currentVersion |
||
| 243 | * @param $upVersion |
||
| 244 | * @param $increment |
||
| 245 | * @return bool |
||
| 246 | */ |
||
| 247 | protected function canContinue($currentVersion, $upVersion, $increment) |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Method for execute the migration. |
||
| 260 | * |
||
| 261 | * @param int $upVersion |
||
| 262 | * @param int $increment Can accept 1 for UP or -1 for down |
||
| 263 | * @param bool $force |
||
| 264 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 265 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 266 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 267 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 268 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 269 | */ |
||
| 270 | protected function migrate($upVersion, $increment, $force) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Run all scripts to up the database version from current up to latest version or the specified version. |
||
| 295 | * |
||
| 296 | * @param int $upVersion |
||
| 297 | * @param bool $force |
||
| 298 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 299 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 300 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 301 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 302 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 303 | */ |
||
| 304 | public function up($upVersion = null, $force = false) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Run all scripts to up or down the database version from current up to latest version or the specified version. |
||
| 311 | * |
||
| 312 | * @param int $upVersion |
||
| 313 | * @param bool $force |
||
| 314 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 315 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 316 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 317 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 318 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 319 | */ |
||
| 320 | public function update($upVersion = null, $force = false) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Run all scripts to down the database version from current version up to the specified version. |
||
| 333 | * |
||
| 334 | * @param int $upVersion |
||
| 335 | * @param bool $force |
||
| 336 | * @throws \ByJG\DbMigration\Exception\DatabaseDoesNotRegistered |
||
| 337 | * @throws \ByJG\DbMigration\Exception\DatabaseIsIncompleteException |
||
| 338 | * @throws \ByJG\DbMigration\Exception\DatabaseNotVersionedException |
||
| 339 | * @throws \ByJG\DbMigration\Exception\InvalidMigrationFile |
||
| 340 | * @throws \ByJG\DbMigration\Exception\OldVersionSchemaException |
||
| 341 | */ |
||
| 342 | public function down($upVersion, $force = false) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @param callable $callable |
||
| 349 | */ |
||
| 350 | public function addCallbackProgress(callable $callable) |
||
| 354 | |||
| 355 | public function isDatabaseVersioned() |
||
| 359 | } |
||
| 360 |