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; |
||
| 20 | class MigrateGenerateCommand extends GeneratorCommand { |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The console command name. |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $name = 'migrate:generate'; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The console command description. |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $description = 'Generate a migration from an existing table structure.'; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var \Way\Generators\Filesystem\Filesystem |
||
| 36 | */ |
||
| 37 | protected $file; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var \Way\Generators\Compilers\TemplateCompiler |
||
| 41 | */ |
||
| 42 | protected $compiler; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
| 46 | */ |
||
| 47 | protected $repository; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var \Illuminate\Config\Repository $config |
||
| 51 | */ |
||
| 52 | protected $config; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var \Xethron\MigrationsGenerator\Generators\SchemaGenerator |
||
| 56 | */ |
||
| 57 | protected $schemaGenerator; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Array of Fields to create in a new Migration |
||
| 61 | * Namely: Columns, Indexes and Foreign Keys |
||
| 62 | * @var array |
||
| 63 | */ |
||
| 64 | protected $fields = array(); |
||
| 65 | |||
| 66 | /** |
||
| 67 | * List of Migrations that has been done |
||
| 68 | * @var array |
||
| 69 | */ |
||
| 70 | protected $migrations = array(); |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | protected $log = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | protected $batch; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Filename date prefix (Y_m_d_His) |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | protected $datePrefix; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $migrationName; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | protected $method; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | protected $table; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string|null |
||
| 105 | */ |
||
| 106 | protected $connection = null; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var bool |
||
| 110 | */ |
||
| 111 | protected $ignoreCreatedMigration = false; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param \Way\Generators\Generator $generator |
||
| 115 | * @param \Way\Generators\Filesystem\Filesystem $file |
||
| 116 | * @param \Way\Generators\Compilers\TemplateCompiler $compiler |
||
| 117 | * @param \Illuminate\Database\Migrations\MigrationRepositoryInterface $repository |
||
| 118 | * @param \Illuminate\Config\Repository $config |
||
| 119 | */ |
||
| 120 | public function __construct( |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Execute the console command. Added for Laravel 5.5 |
||
| 138 | * |
||
| 139 | * @return void |
||
| 140 | */ |
||
| 141 | public function handle() |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Execute the console command. |
||
| 148 | * |
||
| 149 | * @return void |
||
| 150 | */ |
||
| 151 | public function fire() |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Ask for user input: Yes/No |
||
| 203 | * @param string $question Question to ask |
||
| 204 | * @return boolean Answer from user |
||
| 205 | */ |
||
| 206 | protected function askYn( $question ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Ask user for a Numeric Value, or blank for default |
||
| 216 | * @param string $question Question to ask |
||
| 217 | * @param int|float $default Default Value (optional) |
||
| 218 | * @return int|float Answer |
||
| 219 | */ |
||
| 220 | protected function askNumeric( $question, $default = null ) { |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Generate tables and index migrations. |
||
| 241 | * |
||
| 242 | * @param array $tables List of tables to create migrations for |
||
| 243 | * @return void |
||
| 244 | */ |
||
| 245 | protected function generateTablesAndIndices( array $tables ) |
||
| 263 | |||
| 264 | /**Return a substring between 2 strings |
||
| 265 | * @param $string |
||
| 266 | * @param $start |
||
| 267 | * @param $end |
||
| 268 | * @param bool $trim |
||
| 269 | * @return false|string |
||
| 270 | */ |
||
| 271 | public function getStringBetween($string, $start, $end, $trim = true) { |
||
| 280 | |||
| 281 | /**Check if migration for table already exists |
||
| 282 | * @param $table |
||
| 283 | * @param string $prefix |
||
| 284 | * @param string $suffix |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | protected function migrationExist($table, $prefix = 'create_', $suffix = '_table') |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Generate foreign key migrations. |
||
| 306 | * |
||
| 307 | * @param array $tables List of tables to create migrations for |
||
| 308 | * @return void |
||
| 309 | */ |
||
| 310 | protected function generateForeignKeys( array $tables ) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Generate Migration for the current table. |
||
| 334 | * |
||
| 335 | * @return void |
||
| 336 | */ |
||
| 337 | protected function generate() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * The path where the file will be created |
||
| 351 | * |
||
| 352 | * @return string |
||
| 353 | */ |
||
| 354 | protected function getFileGenerationPath() |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get the date prefix for the migration. |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | protected function getDatePrefix() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Fetch the template data |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | protected function getTemplateData() |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Get path to template for generator |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | protected function getTemplatePath() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Get the console command arguments. |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | protected function getArguments() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Get the console command options. |
||
| 421 | * |
||
| 422 | * @return array |
||
| 423 | */ |
||
| 424 | protected function getOptions() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Remove all the tables to exclude from the array of tables |
||
| 440 | * |
||
| 441 | * @param array $tables |
||
| 442 | * |
||
| 443 | * @return array |
||
| 444 | */ |
||
| 445 | protected function removeExcludedTables( array $tables ) |
||
| 452 | |||
| 453 | /** |
||
| 454 | * Get a list of tables to exclude |
||
| 455 | * |
||
| 456 | * @return array |
||
| 457 | */ |
||
| 458 | protected function getExcludedTables() |
||
| 468 | |||
| 469 | } |
||
| 470 |
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..