Complex classes like Environment 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 Environment, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
38 | class Environment |
||
39 | { |
||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $name; |
||
44 | |||
45 | /** |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $options; |
||
49 | |||
50 | /** |
||
51 | * @var \Symfony\Component\Console\Input\InputInterface |
||
52 | */ |
||
53 | protected $input; |
||
54 | |||
55 | /** |
||
56 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
57 | */ |
||
58 | protected $output; |
||
59 | |||
60 | /** |
||
61 | * @var int |
||
62 | */ |
||
63 | protected $currentVersion; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $schemaTableName = 'phinxlog'; |
||
69 | |||
70 | /** |
||
71 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
72 | */ |
||
73 | protected $adapter; |
||
74 | |||
75 | /** |
||
76 | * Class Constructor. |
||
77 | * |
||
78 | * @param string $name Environment Name |
||
79 | * @param array $options Options |
||
80 | * @return \Phinx\Migration\Manager\Environment |
||
|
|||
81 | */ |
||
82 | 404 | public function __construct($name, $options) |
|
87 | |||
88 | /** |
||
89 | * Executes the specified migration on this environment. |
||
90 | * |
||
91 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
92 | * @param string $direction Direction |
||
93 | * @return void |
||
94 | */ |
||
95 | 10 | public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP) |
|
96 | { |
||
97 | 10 | $direction = ($direction === MigrationInterface::UP) ? MigrationInterface::UP : MigrationInterface::DOWN; |
|
98 | 10 | $migration->setMigratingUp($direction === MigrationInterface::UP); |
|
99 | |||
100 | 10 | $startTime = time(); |
|
101 | 10 | $migration->setAdapter($this->getAdapter()); |
|
102 | |||
103 | // begin the transaction if the adapter supports it |
||
104 | 10 | if ($this->getAdapter()->hasTransactions()) { |
|
105 | 6 | $this->getAdapter()->beginTransaction(); |
|
106 | 6 | } |
|
107 | |||
108 | // Run the migration |
||
109 | 10 | if (method_exists($migration, MigrationInterface::CHANGE)) { |
|
110 | 5 | if ($direction === MigrationInterface::DOWN) { |
|
111 | // Create an instance of the ProxyAdapter so we can record all |
||
112 | // of the migration commands for reverse playback |
||
113 | |||
114 | /** @var \Phinx\Db\Adapter\ProxyAdapter $proxyAdapter */ |
||
115 | 4 | $proxyAdapter = AdapterFactory::instance() |
|
116 | 4 | ->getWrapper('proxy', $this->getAdapter()); |
|
117 | 4 | $migration->setAdapter($proxyAdapter); |
|
118 | /** @noinspection PhpUndefinedMethodInspection */ |
||
119 | 4 | $migration->change(); |
|
120 | 4 | $proxyAdapter->executeInvertedCommands(); |
|
121 | 4 | $migration->setAdapter($this->getAdapter()); |
|
122 | 4 | } else { |
|
123 | /** @noinspection PhpUndefinedMethodInspection */ |
||
124 | 4 | $migration->change(); |
|
125 | } |
||
126 | 5 | } else { |
|
127 | 5 | $migration->{$direction}(); |
|
128 | } |
||
129 | |||
130 | // commit the transaction if the adapter supports it |
||
131 | 10 | if ($this->getAdapter()->hasTransactions()) { |
|
132 | 6 | $this->getAdapter()->commitTransaction(); |
|
133 | 6 | } |
|
134 | |||
135 | // Record it in the database |
||
136 | 10 | $this->getAdapter()->migrated($migration, $direction, date('Y-m-d H:i:s', $startTime), date('Y-m-d H:i:s', time())); |
|
137 | 10 | } |
|
138 | |||
139 | /** |
||
140 | * Executes the specified seeder on this environment. |
||
141 | * |
||
142 | * @param \Phinx\Seed\SeedInterface $seed |
||
143 | * @return void |
||
144 | */ |
||
145 | public function executeSeed(SeedInterface $seed) |
||
164 | |||
165 | /** |
||
166 | * Sets the environment's name. |
||
167 | * |
||
168 | * @param string $name Environment Name |
||
169 | * @return \Phinx\Migration\Manager\Environment |
||
170 | */ |
||
171 | 1 | public function setName($name) |
|
177 | |||
178 | /** |
||
179 | * Gets the environment name. |
||
180 | * |
||
181 | * @return string |
||
182 | 3 | */ |
|
183 | public function getName() |
||
187 | |||
188 | /** |
||
189 | * Sets the environment's options. |
||
190 | * |
||
191 | * @param array $options Environment Options |
||
192 | * @return \Phinx\Migration\Manager\Environment |
||
193 | 6 | */ |
|
194 | public function setOptions($options) |
||
200 | |||
201 | /** |
||
202 | * Gets the environment's options. |
||
203 | * |
||
204 | 2 | * @return array |
|
205 | */ |
||
206 | 2 | public function getOptions() |
|
210 | |||
211 | /** |
||
212 | * Parse a database-agnostic DSN into individual options. |
||
213 | * |
||
214 | * @param array $options Options |
||
215 | 7 | * @return array |
|
216 | */ |
||
217 | 7 | protected function parseAgnosticDsn(array $options) |
|
236 | |||
237 | 6 | /** |
|
238 | * Sets the console input. |
||
239 | 6 | * |
|
240 | 6 | * @param \Symfony\Component\Console\Input\InputInterface $input |
|
241 | * @return \Phinx\Migration\Manager\Environment |
||
242 | */ |
||
243 | public function setInput(InputInterface $input) |
||
244 | { |
||
245 | $this->input = $input; |
||
246 | |||
247 | return $this; |
||
248 | 8 | } |
|
249 | |||
250 | 8 | /** |
|
251 | * Gets the console input. |
||
252 | * |
||
253 | * @return \Symfony\Component\Console\Input\InputInterface |
||
254 | */ |
||
255 | public function getInput() |
||
256 | { |
||
257 | return $this->input; |
||
258 | 6 | } |
|
259 | |||
260 | 6 | /** |
|
261 | * Sets the console output. |
||
262 | * |
||
263 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
264 | * @return \Phinx\Migration\Manager\Environment |
||
265 | */ |
||
266 | public function setOutput(OutputInterface $output) |
||
272 | |||
273 | /** |
||
274 | * Gets the console output. |
||
275 | * |
||
276 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
277 | */ |
||
278 | public function getOutput() |
||
282 | 6 | ||
283 | 6 | /** |
|
284 | * Gets all migrated version numbers. |
||
285 | * |
||
286 | * @return array |
||
287 | */ |
||
288 | public function getVersions() |
||
292 | |||
293 | /** |
||
294 | * Get all migration log entries, indexed by version creation time and sorted ascendingly by the configuration's |
||
295 | * version_order option |
||
296 | 6 | * |
|
297 | 6 | * @return array |
|
298 | */ |
||
299 | 6 | public function getVersionLog() |
|
300 | 1 | { |
|
301 | 1 | return $this->getAdapter()->getVersionLog(); |
|
302 | } |
||
303 | 6 | ||
304 | 6 | /** |
|
305 | * Sets the current version of the environment. |
||
306 | * |
||
307 | * @param int $version Environment Version |
||
308 | * @return \Phinx\Migration\Manager\Environment |
||
309 | */ |
||
310 | public function setCurrentVersion($version) |
||
316 | 14 | ||
317 | /** |
||
318 | * Gets the current version of the environment. |
||
319 | * |
||
320 | * @return int |
||
321 | */ |
||
322 | public function getCurrentVersion() |
||
338 | 1 | ||
339 | /** |
||
340 | * Sets the database adapter. |
||
341 | 9 | * |
|
342 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
||
343 | 9 | * @return \Phinx\Migration\Manager\Environment |
|
344 | */ |
||
345 | public function setAdapter(AdapterInterface $adapter) |
||
351 | |||
352 | /** |
||
353 | 8 | * Gets the database adapter. |
|
354 | 5 | * |
|
355 | 5 | * @return \Phinx\Db\Adapter\AdapterInterface |
|
356 | */ |
||
357 | 8 | public function getAdapter() |
|
404 | |||
405 | /** |
||
406 | * Sets the schema table name. |
||
407 | * |
||
408 | * @param string $schemaTableName Schema Table Name |
||
409 | * @return \Phinx\Migration\Manager\Environment |
||
410 | */ |
||
411 | public function setSchemaTableName($schemaTableName) |
||
417 | |||
418 | /** |
||
419 | * Gets the schema table name. |
||
420 | * |
||
421 | * @return string |
||
422 | */ |
||
423 | public function getSchemaTableName() |
||
427 | } |
||
428 |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.