Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 51 | abstract class AbstractCommand extends Command |
||
| 52 | { |
||
| 53 | /** |
||
| 54 | * The location of the default migration template. |
||
| 55 | */ |
||
| 56 | const DEFAULT_MIGRATION_TEMPLATE = '/../../Migration/Migration.template.php.dist'; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * The location of the default seed template. |
||
| 60 | */ |
||
| 61 | const DEFAULT_SEED_TEMPLATE = '/../../Seed/Seed.template.php.dist'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var \Phinx\Config\ConfigInterface |
||
| 65 | */ |
||
| 66 | protected $config; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var \Phinx\Db\Adapter\AdapterInterface |
||
| 70 | */ |
||
| 71 | protected $adapter; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var \Phinx\Migration\Manager |
||
| 75 | */ |
||
| 76 | protected $manager; |
||
| 77 | 54 | ||
| 78 | /** |
||
| 79 | 54 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
|
| 80 | 54 | */ |
|
| 81 | 54 | protected $dispatcher; |
|
| 82 | |||
| 83 | /** |
||
| 84 | * {@inheritdoc} |
||
| 85 | */ |
||
| 86 | protected function configure() |
||
| 91 | |||
| 92 | 32 | /** |
|
| 93 | * Bootstrap Phinx. |
||
| 94 | * |
||
| 95 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 96 | 32 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
|
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | 32 | public function bootstrap(InputInterface $input, OutputInterface $output) |
|
| 131 | |||
| 132 | /** |
||
| 133 | * Sets the config. |
||
| 134 | * |
||
| 135 | * @param \Phinx\Config\ConfigInterface $config |
||
| 136 | * @return \Phinx\Console\Command\AbstractCommand |
||
| 137 | 32 | */ |
|
| 138 | public function setConfig(ConfigInterface $config) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Gets the config. |
||
| 147 | * |
||
| 148 | * @return \Phinx\Config\ConfigInterface |
||
| 149 | */ |
||
| 150 | public function getConfig() |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Sets the database adapter. |
||
| 157 | * |
||
| 158 | * @param \Phinx\Db\Adapter\AdapterInterface $adapter |
||
| 159 | * @return \Phinx\Console\Command\AbstractCommand |
||
| 160 | */ |
||
| 161 | public function setAdapter(AdapterInterface $adapter) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Gets the database adapter. |
||
| 170 | 32 | * |
|
| 171 | * @return \Phinx\Db\Adapter\AdapterInterface |
||
| 172 | 32 | */ |
|
| 173 | 32 | public function getAdapter() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * Sets the migration manager. |
||
| 180 | * |
||
| 181 | 32 | * @param \Phinx\Migration\Manager $manager |
|
| 182 | * @return \Phinx\Console\Command\AbstractCommand |
||
| 183 | 32 | */ |
|
| 184 | public function setManager(Manager $manager) |
||
| 190 | |||
| 191 | /** |
||
| 192 | 10 | * Gets the migration manager. |
|
| 193 | * |
||
| 194 | 10 | * @return \Phinx\Migration\Manager|null |
|
| 195 | */ |
||
| 196 | 10 | public function getManager() |
|
| 200 | 4 | ||
| 201 | /** |
||
| 202 | 10 | * Returns config file path |
|
| 203 | * |
||
| 204 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 205 | * @return string |
||
| 206 | 10 | */ |
|
| 207 | protected function locateConfigFile(InputInterface $input) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Parse the config file and load it into the config object |
||
| 243 | * |
||
| 244 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 245 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 246 | * @throws \InvalidArgumentException |
||
| 247 | * @return void |
||
| 248 | */ |
||
| 249 | protected function loadConfig(InputInterface $input, OutputInterface $output) |
||
| 291 | 32 | ||
| 292 | /** |
||
| 293 | 32 | * Load the migrations manager and inject the config |
|
| 294 | * |
||
| 295 | * @param \Symfony\Component\Console\Input\InputInterface $input |
||
| 296 | * @param \Symfony\Component\Console\Output\OutputInterface $output |
||
| 297 | */ |
||
| 298 | protected function loadManager(InputInterface $input, OutputInterface $output) |
||
| 309 | |||
| 310 | /** |
||
| 311 | 13 | * Verify that the migration directory exists and is writable. |
|
| 312 | * |
||
| 313 | * @param string $path |
||
| 314 | * @throws \InvalidArgumentException |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | 13 | View Code Duplication | protected function verifyMigrationDirectory($path) |
| 333 | |||
| 334 | /** |
||
| 335 | 2 | * Verify that the seed directory exists and is writable. |
|
| 336 | * |
||
| 337 | * @param string $path |
||
| 338 | * @throws \InvalidArgumentException |
||
| 339 | * @return void |
||
| 340 | */ |
||
| 341 | 2 | View Code Duplication | protected function verifySeedDirectory($path) |
| 357 | |||
| 358 | 1 | /** |
|
| 359 | * Returns the migration template filename. |
||
| 360 | 1 | * |
|
| 361 | * @return string |
||
| 362 | */ |
||
| 363 | protected function getMigrationTemplateFilename() |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Returns the seed template filename. |
||
| 370 | * |
||
| 371 | * @return string |
||
| 372 | */ |
||
| 373 | protected function getSeedTemplateFilename() |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Load all event subscribers |
||
| 380 | * |
||
| 381 | * @throws \ReflectionException |
||
| 382 | * @return void |
||
| 383 | */ |
||
| 384 | protected function loadEventSubscribers() |
||
| 404 | } |
||
| 405 |
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.