Complex classes like AbstractCommand 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 AbstractCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class AbstractCommand extends Command |
||
| 29 | { |
||
| 30 | public const FORMAT_JSON = 'json'; |
||
| 31 | public const FORMAT_YML_ALIAS = 'yaml'; |
||
| 32 | public const FORMAT_YML = 'yml'; |
||
| 33 | public const FORMAT_PHP = 'php'; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The location of the default migration template. |
||
| 37 | */ |
||
| 38 | protected const DEFAULT_MIGRATION_TEMPLATE = '/../../Migration/Migration.template.php.dist'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The location of the default seed template. |
||
| 42 | */ |
||
| 43 | protected const DEFAULT_SEED_TEMPLATE = '/../../Seed/Seed.template.php.dist'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var \Phinx\Config\ConfigInterface |
||
| 47 | */ |
||
| 48 | protected $config; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
| 52 | */ |
||
| 53 | protected $adapter; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var \Phinx\Migration\Manager |
||
| 57 | */ |
||
| 58 | protected $manager; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Exit code for when command executes successfully |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | public const CODE_SUCCESS = 0; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Exit code for when command hits a non-recoverable error during execution |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | public const CODE_ERROR = 1; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Exit code for when status command is run and there are missing migrations |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | public const CODE_STATUS_MISSING = 2; |
||
| 77 | 54 | ||
| 78 | /** |
||
| 79 | 54 | * Exit code for when status command is run and there are no missing migations, |
|
| 80 | 54 | * but does have down migrations |
|
| 81 | 54 | * @var int |
|
| 82 | */ |
||
| 83 | public const CODE_STATUS_DOWN = 3; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritDoc} |
||
| 87 | * |
||
| 88 | * @return void |
||
| 89 | */ |
||
| 90 | 32 | protected function configure() |
|
| 95 | |||
| 96 | 32 | /** |
|
| 97 | * Bootstrap Phinx. |
||
| 98 | * |
||
| 99 | 32 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
|
| 100 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
| 101 | 32 | * |
|
| 102 | * @return void |
||
| 103 | 32 | */ |
|
| 104 | 32 | public function bootstrap(InputInterface $input, OutputInterface $output) |
|
| 140 | |||
| 141 | /** |
||
| 142 | * Sets the config. |
||
| 143 | * |
||
| 144 | * @param \Phinx\Config\ConfigInterface $config Config |
||
| 145 | * |
||
| 146 | * @return $this |
||
| 147 | */ |
||
| 148 | public function setConfig(ConfigInterface $config) |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Gets the config. |
||
| 157 | * |
||
| 158 | * @return \Phinx\Config\ConfigInterface |
||
| 159 | */ |
||
| 160 | public function getConfig() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Sets the database adapter. |
||
| 167 | * |
||
| 168 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter Adapter |
||
| 169 | * |
||
| 170 | 32 | * @return $this |
|
| 171 | */ |
||
| 172 | 32 | public function setAdapter(AdapterInterface $adapter) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * Gets the database adapter. |
||
| 181 | 32 | * |
|
| 182 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
| 183 | 32 | */ |
|
| 184 | public function getAdapter() |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Sets the migration manager. |
||
| 191 | * |
||
| 192 | 10 | * @param \Phinx\Migration\Manager $manager Manager |
|
| 193 | * |
||
| 194 | 10 | * @return $this |
|
| 195 | */ |
||
| 196 | 10 | public function setManager(Manager $manager) |
|
| 202 | 10 | ||
| 203 | /** |
||
| 204 | * Gets the migration manager. |
||
| 205 | * |
||
| 206 | 10 | * @return \Phinx\Migration\Manager|null |
|
| 207 | */ |
||
| 208 | 10 | public function getManager() |
|
| 212 | 6 | ||
| 213 | /** |
||
| 214 | * Returns config file path |
||
| 215 | 4 | * |
|
| 216 | 4 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
|
| 217 | * |
||
| 218 | 4 | * @return string |
|
| 219 | 3 | */ |
|
| 220 | 3 | protected function locateConfigFile(InputInterface $input) |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Parse the config file and load it into the config object |
||
| 256 | * |
||
| 257 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
| 258 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
| 259 | * |
||
| 260 | * @throws \InvalidArgumentException |
||
| 261 | * |
||
| 262 | * @return void |
||
| 263 | */ |
||
| 264 | protected function loadConfig(InputInterface $input, OutputInterface $output) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Load the migrations manager and inject the config |
||
| 311 | 13 | * |
|
| 312 | * @param \Symfony\Component\Console\Input\InputInterface $input Input |
||
| 313 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 13 | protected function loadManager(InputInterface $input, OutputInterface $output) |
|
| 332 | |||
| 333 | /** |
||
| 334 | * Verify that the migration directory exists and is writable. |
||
| 335 | 2 | * |
|
| 336 | * @param string $path Path |
||
| 337 | * |
||
| 338 | * @throws \InvalidArgumentException |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | 2 | */ |
|
| 342 | protected function verifyMigrationDirectory($path) |
||
| 358 | 1 | ||
| 359 | /** |
||
| 360 | 1 | * Verify that the seed directory exists and is writable. |
|
| 361 | * |
||
| 362 | * @param string $path Path |
||
| 363 | * |
||
| 364 | * @throws \InvalidArgumentException |
||
| 365 | * |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | protected function verifySeedDirectory($path) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the migration template filename. |
||
| 387 | * |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | protected function getMigrationTemplateFilename() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * Returns the seed template filename. |
||
| 397 | * |
||
| 398 | * @return string |
||
| 399 | */ |
||
| 400 | protected function getSeedTemplateFilename() |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Gets environment to use for command. |
||
| 407 | * |
||
| 408 | * @return string|null name of environment, null if could not be found |
||
| 409 | */ |
||
| 410 | protected function getEnvironment(InputInterface $input, OutputInterface $output): ?string |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Prints out general runtime information about loaded environment. |
||
| 432 | * |
||
| 433 | * @param array|null $envOptions environment options |
||
| 434 | * @param \Symfony\Component\Console\Output\OutputInterface $output Output |
||
| 435 | * @return bool |
||
| 436 | */ |
||
| 437 | protected function checkEnvironmentOptions(?array $envOptions, OutputInterface $output): bool |
||
| 473 | } |
||
| 474 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.