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 | */ |
||
| 81 | public function __construct($name, $options) |
||
| 86 | 404 | ||
| 87 | /** |
||
| 88 | * Executes the specified migration on this environment. |
||
| 89 | * |
||
| 90 | * @param \Phinx\Migration\MigrationInterface $migration Migration |
||
| 91 | * @param string $direction Direction |
||
| 92 | * @param bool $fake flag that if true, we just record running the migration, but not actually do the migration |
||
| 93 | * @return void |
||
| 94 | */ |
||
| 95 | 10 | public function executeMigration(MigrationInterface $migration, $direction = MigrationInterface::UP, $fake = false) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Executes the specified seeder on this environment. |
||
| 143 | * |
||
| 144 | * @param \Phinx\Seed\SeedInterface $seed |
||
| 145 | * @return void |
||
| 146 | */ |
||
| 147 | public function executeSeed(SeedInterface $seed) |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Sets the environment's name. |
||
| 169 | * |
||
| 170 | * @param string $name Environment Name |
||
| 171 | 1 | * @return \Phinx\Migration\Manager\Environment |
|
| 172 | */ |
||
| 173 | 1 | public function setName($name) |
|
| 179 | |||
| 180 | /** |
||
| 181 | * Gets the environment name. |
||
| 182 | 3 | * |
|
| 183 | * @return string |
||
| 184 | 3 | */ |
|
| 185 | public function getName() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Sets the environment's options. |
||
| 192 | * |
||
| 193 | 6 | * @param array $options Environment Options |
|
| 194 | * @return \Phinx\Migration\Manager\Environment |
||
| 195 | 6 | */ |
|
| 196 | 6 | public function setOptions($options) |
|
| 202 | |||
| 203 | /** |
||
| 204 | 2 | * Gets the environment's options. |
|
| 205 | * |
||
| 206 | 2 | * @return array |
|
| 207 | */ |
||
| 208 | public function getOptions() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Parse a database-agnostic DSN into individual options. |
||
| 215 | 7 | * |
|
| 216 | * @param array $options Options |
||
| 217 | 7 | * @return array |
|
| 218 | 7 | */ |
|
| 219 | protected function parseAgnosticDsn(array $options) |
||
| 238 | |||
| 239 | 6 | /** |
|
| 240 | 6 | * Sets the console input. |
|
| 241 | * |
||
| 242 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 243 | * @return \Phinx\Migration\Manager\Environment |
||
| 244 | */ |
||
| 245 | public function setInput(InputInterface $input) |
||
| 246 | { |
||
| 247 | $this->input = $input; |
||
| 248 | 8 | ||
| 249 | return $this; |
||
| 250 | 8 | } |
|
| 251 | |||
| 252 | /** |
||
| 253 | * Gets the console input. |
||
| 254 | * |
||
| 255 | * @return \Symfony\Component\Console\Input\InputInterface |
||
| 256 | */ |
||
| 257 | public function getInput() |
||
| 258 | 6 | { |
|
| 259 | return $this->input; |
||
| 260 | 6 | } |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Sets the console output. |
||
| 264 | * |
||
| 265 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
| 266 | * @return \Phinx\Migration\Manager\Environment |
||
| 267 | */ |
||
| 268 | public function setOutput(OutputInterface $output) |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Gets the console output. |
||
| 277 | * |
||
| 278 | * @return \Symfony\Component\Console\Output\OutputInterface |
||
| 279 | */ |
||
| 280 | 6 | public function getOutput() |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Gets all migrated version numbers. |
||
| 287 | * |
||
| 288 | * @return array |
||
| 289 | */ |
||
| 290 | public function getVersions() |
||
| 294 | |||
| 295 | /** |
||
| 296 | 6 | * Get all migration log entries, indexed by version creation time and sorted ascendingly by the configuration's |
|
| 297 | 6 | * version_order option |
|
| 298 | * |
||
| 299 | 6 | * @return array |
|
| 300 | 1 | */ |
|
| 301 | 1 | public function getVersionLog() |
|
| 302 | { |
||
| 303 | 6 | return $this->getAdapter()->getVersionLog(); |
|
| 304 | 6 | } |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Sets the current version of the environment. |
||
| 308 | * |
||
| 309 | * @param int $version Environment Version |
||
| 310 | * @return \Phinx\Migration\Manager\Environment |
||
| 311 | */ |
||
| 312 | public function setCurrentVersion($version) |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Gets the current version of the environment. |
||
| 321 | * |
||
| 322 | * @return int |
||
| 323 | */ |
||
| 324 | 17 | public function getCurrentVersion() |
|
| 340 | |||
| 341 | 9 | /** |
|
| 342 | * Sets the database adapter. |
||
| 343 | 9 | * |
|
| 344 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Database Adapter |
||
| 345 | * @return \Phinx\Migration\Manager\Environment |
||
| 346 | 8 | */ |
|
| 347 | public function setAdapter(AdapterInterface $adapter) |
||
| 353 | 8 | ||
| 354 | 5 | /** |
|
| 355 | 5 | * Gets the database adapter. |
|
| 356 | * |
||
| 357 | 8 | * @return \Phinx\Db\Adapter\AdapterInterface |
|
| 358 | 5 | */ |
|
| 359 | 5 | public function getAdapter() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Sets the schema table name. |
||
| 409 | * |
||
| 410 | * @param string $schemaTableName Schema Table Name |
||
| 411 | * @return \Phinx\Migration\Manager\Environment |
||
| 412 | */ |
||
| 413 | public function setSchemaTableName($schemaTableName) |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Gets the schema table name. |
||
| 422 | * |
||
| 423 | * @return string |
||
| 424 | */ |
||
| 425 | public function getSchemaTableName() |
||
| 429 | } |
||
| 430 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.