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 | 74 | public function __construct(Configuration $configuration, $version, $class, SchemaDiffProviderInterface $schemaProvider=null) |
|
106 | { |
||
107 | 74 | $this->configuration = $configuration; |
|
108 | 74 | $this->outputWriter = $configuration->getOutputWriter(); |
|
109 | 74 | $this->class = $class; |
|
110 | 74 | $this->connection = $configuration->getConnection(); |
|
111 | 74 | $this->migration = new $class($this); |
|
112 | 74 | $this->version = $version; |
|
113 | |||
114 | 74 | if ($schemaProvider !== null) { |
|
115 | 1 | $this->schemaProvider = $schemaProvider; |
|
116 | 1 | } |
|
117 | 74 | if($schemaProvider === null) { |
|
118 | 73 | $schemaProvider = new SchemaDiffProvider($this->connection->getSchemaManager(), |
|
119 | 73 | $this->connection->getDatabasePlatform()); |
|
120 | 73 | $this->schemaProvider = new LazySchemaDiffProvider(new LazyLoadingValueHolderFactory(), $schemaProvider); |
|
121 | 73 | } |
|
122 | 74 | } |
|
123 | |||
124 | /** |
||
125 | * Returns the string version in the format YYYYMMDDHHMMSS |
||
126 | * |
||
127 | * @return string $version |
||
128 | */ |
||
129 | 40 | public function getVersion() |
|
130 | { |
||
131 | 40 | return $this->version; |
|
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the Migrations Configuration object instance |
||
136 | * |
||
137 | * @return Configuration $configuration |
||
138 | */ |
||
139 | 71 | public function getConfiguration() |
|
140 | { |
||
141 | 71 | 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() |
|
153 | |||
154 | 26 | public function markMigrated() |
|
155 | { |
||
156 | 26 | $this->markVersion('up'); |
|
158 | |||
159 | 26 | private function markVersion($direction) |
|
172 | |||
173 | 9 | public function markNotMigrated() |
|
177 | |||
178 | /** |
||
179 | * Add some SQL queries to this versions migration |
||
180 | * |
||
181 | * @param array|string $sql |
||
182 | * @param array $params |
||
183 | * @param array $types |
||
184 | */ |
||
185 | 25 | public function addSql($sql, array $params = [], array $types = []) |
|
202 | |||
203 | /** |
||
204 | * @param mixed[] $params Array of prepared statement parameters |
||
205 | * @param string[] $types Array of the types of each statement parameters |
||
206 | */ |
||
207 | 9 | private function addQueryParams($params, $types) |
|
213 | |||
214 | /** |
||
215 | * Write a migration SQL file to the given path |
||
216 | * |
||
217 | * @param string $path The path to write the migration SQL file. |
||
218 | * @param string $direction The direction to execute. |
||
219 | * |
||
220 | * @return boolean $written |
||
221 | */ |
||
222 | 8 | public function writeSqlFile($path, $direction = self::DIRECTION_UP) |
|
242 | |||
243 | /** |
||
244 | * @return AbstractMigration |
||
245 | */ |
||
246 | 2 | public function getMigration() |
|
250 | |||
251 | /** |
||
252 | * Execute this migration version up or down and and return the SQL. |
||
253 | * We are only allowing the addSql call and the schema modification to take effect in the up and down call. |
||
254 | * This is necessary to ensure that the migration is revertable. |
||
255 | * The schema is passed to the pre and post method only to be able to test the presence of some table, And the |
||
256 | * connection that can get used trough it allow for the test of the presence of records. |
||
257 | * |
||
258 | * @param string $direction The direction to execute the migration. |
||
259 | * @param boolean $dryRun Whether to not actually execute the migration SQL and just do a dry run. |
||
260 | * @param boolean $timeAllQueries Measuring or not the execution time of each SQL query. |
||
261 | * |
||
262 | * @return array $sql |
||
263 | * |
||
264 | * @throws \Exception when migration fails |
||
265 | */ |
||
266 | 25 | public function execute($direction, $dryRun = false, $timeAllQueries = false) |
|
362 | |||
363 | 8 | public function getExecutionState() |
|
376 | |||
377 | 18 | private function outputQueryTime($queryStart, $timeAllQueries = false) |
|
386 | |||
387 | /** |
||
388 | * Returns the time this migration version took to execute |
||
389 | * |
||
390 | * @return integer $time The time this migration version took to execute |
||
391 | */ |
||
392 | 12 | public function getTime() |
|
396 | |||
397 | 2 | public function __toString() |
|
401 | |||
402 | 23 | private function executeRegisteredSql($dryRun = false, $timeAllQueries = false) |
|
431 | } |
||
432 |