Complex classes like MigrateGenerateCommand 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 MigrateGenerateCommand, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace Xethron\MigrationsGenerator; |
||
| 22 | class MigrateGenerateCommand extends GeneratorCommand { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * The console command name. |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | protected $name = 'migrate:generate'; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * The console command description. |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $description = 'Generate a migration from an existing table structure.'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var \Way\Generators\Filesystem\Filesystem |
||
| 38 | */ |
||
| 39 | protected $file; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var \Way\Generators\Compilers\TemplateCompiler |
||
| 43 | */ |
||
| 44 | protected $compiler; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
| 48 | */ |
||
| 49 | protected $repository; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var \Illuminate\Config\Repository $config |
||
| 53 | */ |
||
| 54 | protected $config; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var \Xethron\MigrationsGenerator\Generators\SchemaGenerator |
||
| 58 | */ |
||
| 59 | protected $schemaGenerator; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Array of Fields to create in a new Migration |
||
| 63 | * Namely: Columns, Indexes and Foreign Keys |
||
| 64 | * @var array |
||
| 65 | */ |
||
| 66 | protected $fields = array(); |
||
| 67 | |||
| 68 | /** |
||
| 69 | * List of Migrations that has been done |
||
| 70 | * @var array |
||
| 71 | */ |
||
| 72 | protected $migrations = array(); |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $log = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | protected $batch; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Filename date prefix (Y_m_d_His) |
||
| 86 | * @var string |
||
| 87 | */ |
||
| 88 | protected $datePrefix; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @var string |
||
| 92 | */ |
||
| 93 | protected $migrationName; |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @var string |
||
| 97 | */ |
||
| 98 | protected $method; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $table; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var string|null |
||
| 107 | */ |
||
| 108 | protected $connection = null; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * For create migrations so they all do not have the same date |
||
| 112 | * @var int |
||
| 113 | */ |
||
| 114 | protected $secondCount = 1; |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Array of ForeignKeys to create in a new Migration |
||
| 118 | * Namely: Foreign Keys |
||
| 119 | * @var array |
||
| 120 | */ |
||
| 121 | protected $foreignKeys = array(); |
||
| 122 | |||
| 123 | /** @var bool // separate or combine the foreign keys to the table create */ |
||
| 124 | protected $separateForeignKeysCreation = true; |
||
| 125 | |||
| 126 | /** @var bool // empty the existing migrations */ |
||
| 127 | protected $clearAllExistingMigrations = false; |
||
| 128 | |||
| 129 | /** @var bool // should we check for table dependencies via foreign keys */ |
||
| 130 | protected $checkForTableDependencies = false; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param \Way\Generators\Generator $generator |
||
| 134 | * @param \Way\Generators\Filesystem\Filesystem $file |
||
| 135 | * @param \Way\Generators\Compilers\TemplateCompiler $compiler |
||
| 136 | * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
| 137 | * @param \Illuminate\Config\Repository $config |
||
| 138 | */ |
||
| 139 | public function __construct( |
||
| 154 | |||
| 155 | /** |
||
| 156 | * Execute the console command. Added for Laravel 5.5 |
||
| 157 | * |
||
| 158 | * @return void |
||
| 159 | */ |
||
| 160 | public function handle() |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Execute the console command. |
||
| 167 | * |
||
| 168 | * @return void |
||
| 169 | */ |
||
| 170 | public function fire() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Make sure the migration date string is always different and sequential |
||
| 225 | * @return false|string |
||
| 226 | */ |
||
| 227 | protected function getDatePrefix() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Check if we should clear all existing migrations, and if so, remove them |
||
| 235 | */ |
||
| 236 | protected function clearAllMigrations() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Ask for user input: Yes/No |
||
| 246 | * @param string $question Question to ask |
||
| 247 | * @return boolean Answer from user |
||
| 248 | */ |
||
| 249 | protected function askYn( $question ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Ask user for a Numeric Value, or blank for default |
||
| 259 | * @param string $question Question to ask |
||
| 260 | * @param int|float $default Default Value (optional) |
||
| 261 | * @return int|float Answer |
||
| 262 | */ |
||
| 263 | protected function askNumeric( $question, $default = null ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Generate tables and index migrations. |
||
| 284 | * |
||
| 285 | * @param array $tables List of tables to create migrations for |
||
| 286 | * @return void |
||
| 287 | */ |
||
| 288 | protected function generateTablesAndIndices( array $tables ) |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Generate tables and index migrations while checking for table dependencies via foreign keys |
||
| 307 | * |
||
| 308 | * @param array $tables |
||
| 309 | */ |
||
| 310 | protected function generateTablesAndIndicesLookingForTableDependencies( array $tables ) |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param $table |
||
| 336 | * @param Collection $tableWithFields |
||
| 337 | */ |
||
| 338 | protected function getDependencyTable($table, Collection &$tableWithFields) |
||
| 339 | { |
||
| 340 | if (!$tableWithFields->has($table)) { |
||
| 341 | $fields = $this->schemaGenerator->getFields($table) ?: []; |
||
| 342 | |||
| 343 | foreach ($fields as $field => $info) { |
||
| 344 | if (substr($field, -3) === '_id') { |
||
| 345 | $tableName = str_plural(substr($field, 0, -3)); |
||
| 346 | $this->getDependencyTable($tableName, $tableWithFields); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | |||
| 350 | $tableWithFields->put($table, $fields); |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Generate foreign key migrations. |
||
| 356 | * |
||
| 357 | * @param array $tables List of tables to create migrations for |
||
| 358 | * @return void |
||
| 359 | */ |
||
| 360 | protected function generateForeignKeys( array $tables ) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Generate Migration for the current table. |
||
| 375 | * |
||
| 376 | * @return void |
||
| 377 | */ |
||
| 378 | protected function generate() |
||
| 389 | |||
| 390 | /** |
||
| 391 | * The path where the file will be created |
||
| 392 | * |
||
| 393 | * @return string |
||
| 394 | */ |
||
| 395 | protected function getFileGenerationPath() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Fetch the template data |
||
| 406 | * |
||
| 407 | * @return array |
||
| 408 | */ |
||
| 409 | protected function getTemplateData() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Separate the foreign keys from create migrations |
||
| 420 | * |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | protected function separateTableAndForeignKeys() |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Combine the table migrations and foreign key creations |
||
| 447 | * |
||
| 448 | * @return array |
||
| 449 | */ |
||
| 450 | protected function combineTableAndForeignKeys() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Get path to template for generator |
||
| 471 | * |
||
| 472 | * @return string |
||
| 473 | */ |
||
| 474 | protected function getTemplatePath() |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Get the console command arguments. |
||
| 481 | * |
||
| 482 | * @return array |
||
| 483 | */ |
||
| 484 | protected function getArguments() |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the console command options. |
||
| 493 | * |
||
| 494 | * @return array |
||
| 495 | */ |
||
| 496 | protected function getOptions() |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Remove all the tables to exclude from the array of tables |
||
| 511 | * |
||
| 512 | * @param array $tables |
||
| 513 | * |
||
| 514 | * @return array |
||
| 515 | */ |
||
| 516 | protected function removeExcludedTables( array $tables ) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Get a list of tables to exclude |
||
| 526 | * |
||
| 527 | * @return array |
||
| 528 | */ |
||
| 529 | protected function getExcludedTables() |
||
| 539 | |||
| 540 | } |
||
| 541 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..