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 |
||
| 37 | class Version |
||
| 38 | { |
||
| 39 | const STATE_NONE = 0; |
||
| 40 | const STATE_PRE = 1; |
||
| 41 | const STATE_EXEC = 2; |
||
| 42 | const STATE_POST = 3; |
||
| 43 | |||
| 44 | const DIRECTION_UP = 'up'; |
||
| 45 | const DIRECTION_DOWN = 'down'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The Migrations Configuration instance for this migration |
||
| 49 | * |
||
| 50 | * @var Configuration |
||
| 51 | */ |
||
| 52 | private $configuration; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The OutputWriter object instance used for outputting information |
||
| 56 | * |
||
| 57 | * @var OutputWriter |
||
| 58 | */ |
||
| 59 | private $outputWriter; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The version in timestamp format (YYYYMMDDHHMMSS) |
||
| 63 | * |
||
| 64 | * @param int |
||
| 65 | */ |
||
| 66 | private $version; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * The migration instance for this version |
||
| 70 | * |
||
| 71 | * @var AbstractMigration |
||
| 72 | */ |
||
| 73 | private $migration; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var \Doctrine\DBAL\Connection |
||
| 77 | */ |
||
| 78 | private $connection; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | private $class; |
||
| 84 | |||
| 85 | /** The array of collected SQL statements for this version */ |
||
| 86 | private $sql = []; |
||
| 87 | |||
| 88 | /** The array of collected parameters for SQL statements for this version */ |
||
| 89 | private $params = []; |
||
| 90 | |||
| 91 | /** The array of collected types for SQL statements for this version */ |
||
| 92 | private $types = []; |
||
| 93 | |||
| 94 | /** The time in seconds that this migration version took to execute */ |
||
| 95 | private $time; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $state = self::STATE_NONE; |
||
| 101 | |||
| 102 | /** @var SchemaDiffProviderInterface */ |
||
| 103 | private $schemaProvider; |
||
| 104 | |||
| 105 | 106 | public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider=null) |
|
| 106 | { |
||
| 107 | 106 | $this->configuration = $configuration; |
|
| 108 | 106 | $this->outputWriter = $configuration->getOutputWriter(); |
|
| 109 | 106 | $this->class = $class; |
|
| 110 | 106 | $this->connection = $configuration->getConnection(); |
|
| 111 | 106 | $this->migration = new $class($this); |
|
| 112 | 106 | $this->version = $version; |
|
| 113 | |||
| 114 | 106 | if ($schemaProvider !== null) { |
|
| 115 | 1 | $this->schemaProvider = $schemaProvider; |
|
| 116 | 1 | } |
|
| 117 | 106 | if($schemaProvider === null) { |
|
| 118 | 105 | $schemaProvider = new SchemaDiffProvider($this->connection->getSchemaManager(), |
|
| 119 | 105 | $this->connection->getDatabasePlatform()); |
|
| 120 | 105 | $this->schemaProvider = LazySchemaDiffProvider::fromDefaultProxyFacyoryConfiguration($schemaProvider); |
|
| 121 | 105 | } |
|
| 122 | 106 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Returns the string version in the format YYYYMMDDHHMMSS |
||
| 126 | * |
||
| 127 | * @return string $version |
||
| 128 | */ |
||
| 129 | 66 | public function getVersion() |
|
| 130 | { |
||
| 131 | 66 | return $this->version; |
|
| 132 | } |
||
| 133 | |||
| 134 | /** |
||
| 135 | * Returns the Migrations Configuration object instance |
||
| 136 | * |
||
| 137 | * @return Configuration $configuration |
||
| 138 | */ |
||
| 139 | 102 | public function getConfiguration() |
|
| 140 | { |
||
| 141 | 102 | return $this->configuration; |
|
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Check if this version has been migrated or not. |
||
| 146 | * |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 15 | public function isMigrated() |
|
| 150 | { |
||
| 151 | 15 | return $this->configuration->hasVersionMigrated($this); |
|
| 152 | } |
||
| 153 | |||
| 154 | 36 | public function markMigrated() |
|
| 158 | |||
| 159 | 36 | private function markVersion($direction) |
|
| 169 | |||
| 170 | 9 | public function markNotMigrated() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Add some SQL queries to this versions migration |
||
| 177 | * |
||
| 178 | * @param array|string $sql |
||
| 179 | * @param array $params |
||
| 180 | * @param array $types |
||
| 181 | */ |
||
| 182 | 40 | public function addSql($sql, array $params = [], array $types = []) |
|
| 183 | { |
||
| 184 | 40 | if (is_array($sql)) { |
|
| 185 | 39 | foreach ($sql as $key => $query) { |
|
| 186 | 13 | $this->sql[] = $query; |
|
| 187 | 13 | if (!empty($params[$key])) { |
|
| 188 | 1 | $queryTypes = isset($types[$key]) ? $types[$key] : []; |
|
| 189 | 1 | $this->addQueryParams($params[$key], $queryTypes); |
|
| 190 | 1 | } |
|
| 191 | 39 | } |
|
| 192 | 39 | } else { |
|
| 193 | 22 | $this->sql[] = $sql; |
|
| 194 | 22 | if (!empty($params)) { |
|
| 195 | 12 | $this->addQueryParams($params, $types); |
|
| 196 | 12 | } |
|
| 197 | } |
||
| 198 | 40 | } |
|
| 199 | |||
| 200 | /** |
||
| 201 | * @param mixed[] $params Array of prepared statement parameters |
||
| 202 | * @param string[] $types Array of the types of each statement parameters |
||
| 203 | */ |
||
| 204 | 13 | private function addQueryParams($params, $types) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Write a migration SQL file to the given path |
||
| 213 | * |
||
| 214 | * @param string $path The path to write the migration SQL file. |
||
| 215 | * @param string $direction The direction to execute. |
||
| 216 | * |
||
| 217 | * @return boolean $written |
||
| 218 | */ |
||
| 219 | 9 | public function writeSqlFile($path, $direction = self::DIRECTION_UP) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * @return AbstractMigration |
||
| 242 | */ |
||
| 243 | 4 | public function getMigration() |
|
| 247 | |||
| 248 | /** |
||
| 249 | * Execute this migration version up or down and and return the SQL. |
||
| 250 | * We are only allowing the addSql call and the schema modification to take effect in the up and down call. |
||
| 251 | * This is necessary to ensure that the migration is revertable. |
||
| 252 | * The schema is passed to the pre and post method only to be able to test the presence of some table, And the |
||
| 253 | * connection that can get used trough it allow for the test of the presence of records. |
||
| 254 | * |
||
| 255 | * @param string $direction The direction to execute the migration. |
||
| 256 | * @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. |
||
| 257 | * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
||
| 258 | * |
||
| 259 | * @return array $sql |
||
| 260 | * |
||
| 261 | * @throws \Exception when migration fails |
||
| 262 | */ |
||
| 263 | 40 | public function execute($direction, $dryRun = false, $timeAllQueries = false) |
|
| 359 | |||
| 360 | 8 | public function getExecutionState() |
|
| 373 | |||
| 374 | 18 | private function outputQueryTime($queryStart, $timeAllQueries = false) |
|
| 383 | |||
| 384 | /** |
||
| 385 | * Returns the time this migration version took to execute |
||
| 386 | * |
||
| 387 | * @return integer $time The time this migration version took to execute |
||
| 388 | */ |
||
| 389 | 22 | public function getTime() |
|
| 393 | |||
| 394 | 2 | public function __toString() |
|
| 398 | |||
| 399 | 38 | private function executeRegisteredSql($dryRun = false, $timeAllQueries = false) |
|
| 428 | |||
| 429 | /** |
||
| 430 | * Outputs a SQL query via the `OutputWriter`. |
||
| 431 | * |
||
| 432 | * @param int $idx The SQL query index. Used to look up params. |
||
| 433 | * @param string $query the query to output |
||
| 434 | * @return void |
||
| 435 | */ |
||
| 436 | 11 | private function outputSqlQuery($idx, $query) |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Formats a set of sql parameters for output with dry run. |
||
| 452 | * |
||
| 453 | * @param $params The query parameters |
||
| 454 | * @param $types The types of the query params. Default type is a string |
||
| 455 | * @return string|null a string of the parameters present. |
||
| 456 | */ |
||
| 457 | 11 | private function formatParamsForOutput(array $params, array $types) |
|
| 473 | } |
||
| 474 |