Complex classes like Version 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 Version, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 38 | class Version |
||
| 39 | { |
||
| 40 | const STATE_NONE = 0; |
||
| 41 | const STATE_PRE = 1; |
||
| 42 | const STATE_EXEC = 2; |
||
| 43 | const STATE_POST = 3; |
||
| 44 | |||
| 45 | const DIRECTION_UP = 'up'; |
||
| 46 | const DIRECTION_DOWN = 'down'; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The Migrations Configuration instance for this migration |
||
| 50 | * |
||
| 51 | * @var Configuration |
||
| 52 | */ |
||
| 53 | private $configuration; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The OutputWriter object instance used for outputting information |
||
| 57 | * |
||
| 58 | * @var OutputWriter |
||
| 59 | */ |
||
| 60 | private $outputWriter; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The version in timestamp format (YYYYMMDDHHMMSS) |
||
| 64 | * |
||
| 65 | * @param int |
||
| 66 | */ |
||
| 67 | private $version; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The migration instance for this version |
||
| 71 | * |
||
| 72 | * @var AbstractMigration |
||
| 73 | */ |
||
| 74 | private $migration; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var \Doctrine\DBAL\Connection |
||
| 78 | */ |
||
| 79 | private $connection; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | private $class; |
||
| 85 | |||
| 86 | /** The array of collected SQL statements for this version */ |
||
| 87 | private $sql = []; |
||
| 88 | |||
| 89 | /** The array of collected parameters for SQL statements for this version */ |
||
| 90 | private $params = []; |
||
| 91 | |||
| 92 | /** The array of collected types for SQL statements for this version */ |
||
| 93 | private $types = []; |
||
| 94 | |||
| 95 | /** The time in seconds that this migration version took to execute */ |
||
| 96 | private $time; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var int |
||
| 100 | */ |
||
| 101 | private $state = self::STATE_NONE; |
||
| 102 | |||
| 103 | /** @var SchemaDiffProviderInterface */ |
||
| 104 | private $schemaProvider; |
||
| 105 | |||
| 106 | 112 | public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider=null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Returns the string version in the format YYYYMMDDHHMMSS |
||
| 127 | * |
||
| 128 | * @return string $version |
||
| 129 | */ |
||
| 130 | 72 | public function getVersion() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * Returns the Migrations Configuration object instance |
||
| 137 | * |
||
| 138 | * @return Configuration $configuration |
||
| 139 | */ |
||
| 140 | 108 | public function getConfiguration() |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Check if this version has been migrated or not. |
||
| 147 | * |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | 15 | public function isMigrated() |
|
| 154 | |||
| 155 | 39 | public function markMigrated() |
|
| 159 | |||
| 160 | 39 | private function markVersion($direction) |
|
| 170 | |||
| 171 | 14 | public function markNotMigrated() |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Add some SQL queries to this versions migration |
||
| 178 | * |
||
| 179 | * @param array|string $sql |
||
| 180 | * @param array $params |
||
| 181 | * @param array $types |
||
| 182 | */ |
||
| 183 | 42 | public function addSql($sql, array $params = [], array $types = []) |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @param mixed[] $params Array of prepared statement parameters |
||
| 203 | * @param string[] $types Array of the types of each statement parameters |
||
| 204 | */ |
||
| 205 | 13 | private function addQueryParams($params, $types) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Write a migration SQL file to the given path |
||
| 214 | * |
||
| 215 | * @param string $path The path to write the migration SQL file. |
||
| 216 | * @param string $direction The direction to execute. |
||
| 217 | * |
||
| 218 | * @return boolean $written |
||
| 219 | */ |
||
| 220 | 9 | public function writeSqlFile($path, $direction = self::DIRECTION_UP) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return AbstractMigration |
||
| 243 | */ |
||
| 244 | 4 | public function getMigration() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Execute this migration version up or down and and return the SQL. |
||
| 251 | * We are only allowing the addSql call and the schema modification to take effect in the up and down call. |
||
| 252 | * This is necessary to ensure that the migration is revertable. |
||
| 253 | * The schema is passed to the pre and post method only to be able to test the presence of some table, And the |
||
| 254 | * connection that can get used trough it allow for the test of the presence of records. |
||
| 255 | * |
||
| 256 | * @param string $direction The direction to execute the migration. |
||
| 257 | * @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. |
||
| 258 | * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
||
| 259 | * |
||
| 260 | * @return array $sql |
||
| 261 | * |
||
| 262 | * @throws \Exception when migration fails |
||
| 263 | */ |
||
| 264 | 43 | public function execute($direction, $dryRun = false, $timeAllQueries = false) |
|
| 367 | |||
| 368 | 8 | public function getExecutionState() |
|
| 381 | |||
| 382 | 19 | private function outputQueryTime($queryStart, $timeAllQueries = false) |
|
| 391 | |||
| 392 | /** |
||
| 393 | * Returns the time this migration version took to execute |
||
| 394 | * |
||
| 395 | * @return integer $time The time this migration version took to execute |
||
| 396 | */ |
||
| 397 | 25 | public function getTime() |
|
| 401 | |||
| 402 | 2 | public function __toString() |
|
| 406 | |||
| 407 | 40 | private function executeRegisteredSql($dryRun = false, $timeAllQueries = false) |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Outputs a SQL query via the `OutputWriter`. |
||
| 439 | * |
||
| 440 | * @param int $idx The SQL query index. Used to look up params. |
||
| 441 | * @param string $query the query to output |
||
| 442 | * @return void |
||
| 443 | */ |
||
| 444 | 12 | private function outputSqlQuery($idx, $query) |
|
| 457 | |||
| 458 | /** |
||
| 459 | * Formats a set of sql parameters for output with dry run. |
||
| 460 | * |
||
| 461 | * @param $params The query parameters |
||
| 462 | * @param $types The types of the query params. Default type is a string |
||
| 463 | * @return string|null a string of the parameters present. |
||
| 464 | */ |
||
| 465 | 12 | private function formatParamsForOutput(array $params, array $types) |
|
| 481 | |||
| 482 | 43 | private function dispatchEvent($eventName, $direction, $dryRun) |
|
| 491 | } |
||
| 492 |