Complex classes like DbMigrations 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 DbMigrations, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class DbMigrations extends DbExporter |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * Column data types. |
||
| 14 | * |
||
| 15 | * @var string |
||
| 16 | **/ |
||
| 17 | protected $columns = [ |
||
| 18 | 'int' => 'integer', |
||
| 19 | 'smallint' => 'smallInteger', |
||
| 20 | 'bigint' => 'bigInteger', |
||
| 21 | 'char ' => 'string', |
||
| 22 | 'varchar' => 'string', |
||
| 23 | 'float' => 'float', |
||
| 24 | 'double' => 'double', |
||
| 25 | 'decimal' => 'decimal', |
||
| 26 | 'tinyint' => 'tinyInteger', |
||
| 27 | 'date' => 'date', |
||
| 28 | 'timestamp' => 'timestamp', |
||
| 29 | 'datetime' => 'dateTime', |
||
| 30 | 'longtext' => 'longText', |
||
| 31 | 'mediumtext' => 'mediumText', |
||
| 32 | 'text' => 'text', |
||
| 33 | 'longblob' => 'binary', |
||
| 34 | 'blob' => 'binary', |
||
| 35 | 'enum' => 'enum', |
||
| 36 | 'char' => 'char ', |
||
| 37 | 'geometry' => 'geometry', |
||
| 38 | 'time' => 'time', |
||
| 39 | 'point' => 'point', |
||
| 40 | 'polygon' => 'polygon', |
||
| 41 | 'multipolygon' => 'muliPolygon', |
||
| 42 | 'multilinestring' => 'multiLineString', |
||
| 43 | 'mulitpoint' => 'multiPoint', |
||
| 44 | 'mediumint' => 'mediumInteger', |
||
| 45 | 'mac' => 'macAddress', |
||
| 46 | 'json' => 'json', |
||
| 47 | 'linestring' => 'lineString', |
||
| 48 | 'geometrycollection' => 'geometryCollection', |
||
| 49 | 'bool' => 'boolean', |
||
| 50 | 'year' => 'year', |
||
| 51 | ]; |
||
| 52 | /** |
||
| 53 | * Primary key column types. |
||
| 54 | * |
||
| 55 | * @var array |
||
| 56 | **/ |
||
| 57 | protected $primaryKeys = [ |
||
| 58 | 'bigint' => 'bigIncrements', |
||
| 59 | 'int' => 'increments', |
||
| 60 | ]; |
||
| 61 | |||
| 62 | protected $schema; |
||
| 63 | |||
| 64 | protected $customDb = false; |
||
| 65 | |||
| 66 | public static $filePath; |
||
| 67 | |||
| 68 | protected $primaryKey; |
||
| 69 | |||
| 70 | protected $defaultLength; |
||
| 71 | |||
| 72 | protected $methodName; |
||
| 73 | /** |
||
| 74 | * File name for migration file. |
||
| 75 | * |
||
| 76 | * @var string |
||
| 77 | */ |
||
| 78 | public $filename; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the database name. |
||
| 82 | * |
||
| 83 | * @param string $database |
||
| 84 | * @throw InvalidDatabaseException |
||
| 85 | */ |
||
| 86 | public function __construct($database) |
||
| 94 | |||
| 95 | //end __construct() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Write the prepared migration to a file. |
||
| 99 | */ |
||
| 100 | public function write() |
||
| 117 | |||
| 118 | //end write() |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Convert the database to migrations |
||
| 122 | * If none is given, use de DB from condig/database.php. |
||
| 123 | * |
||
| 124 | * @param null $database |
||
| 125 | * |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function convert($database = null) |
||
| 211 | |||
| 212 | //end convert() |
||
| 213 | |||
| 214 | public function columnType($type, $columns = 'columns', $method = '') |
||
| 218 | |||
| 219 | //end columnType() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Compile the migration into the base migration file |
||
| 223 | * TODO use a template with seacrh&replace. |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | protected function compile($null = null) |
||
| 228 | { |
||
| 229 | $upSchema = ''; |
||
| 230 | $downSchema = ''; |
||
| 231 | $upConstraint = ''; |
||
| 232 | $downConstraint = ''; |
||
| 233 | |||
| 234 | // prevent of failure when no table |
||
| 235 | if (!is_null($this->schema) && is_array($this->schema)) { |
||
| 236 | foreach ($this->schema as $name => $values) { |
||
| 237 | // check again for ignored tables |
||
| 238 | if (in_array($name, self::$ignore)) { |
||
| 239 | continue; |
||
| 240 | } |
||
| 241 | |||
| 242 | $upSchema .= " |
||
| 243 | /** |
||
| 244 | * Migration schema for table {$name} |
||
| 245 | * |
||
| 246 | */ |
||
| 247 | {$values['up']}"; |
||
| 248 | $upConstraint .= " |
||
| 249 | {$values['constraint']}"; |
||
| 250 | $downConstraint .= " |
||
| 251 | {$values['constraint_down']}"; |
||
| 252 | |||
| 253 | $downSchema .= " |
||
| 254 | {$values['down']}"; |
||
| 255 | } |
||
| 256 | }//end if |
||
| 257 | |||
| 258 | // Grab the template |
||
| 259 | $template = File::get(__DIR__.'/stubs/migration.stub'); |
||
| 260 | |||
| 261 | // Replace the classname |
||
| 262 | $template = str_replace('{{name}}', 'Create'.ucfirst(Str::camel($this->database)).'Database', $template); |
||
| 263 | |||
| 264 | // Replace the up and down values |
||
| 265 | $template = str_replace('{{up}}', $upSchema, $template); |
||
| 266 | $template = str_replace('{{down}}', $downSchema, $template); |
||
| 267 | $template = str_replace('{{upCon}}', $upConstraint, $template); |
||
| 268 | $template = str_replace('{{downCon}}', $downConstraint, $template); |
||
| 269 | |||
| 270 | return $template; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * summary. |
||
| 275 | * |
||
| 276 | * @author |
||
| 277 | */ |
||
| 278 | public function hasDefaults($type, $column) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * summary. |
||
| 304 | * |
||
| 305 | * @author |
||
| 306 | */ |
||
| 307 | protected function unsetData() |
||
| 313 | |||
| 314 | //end compile() |
||
| 315 | }//end class |
||
| 316 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.