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() |
||
272 | |||
273 | /** |
||
274 | * summary. |
||
275 | * |
||
276 | * @author |
||
277 | */ |
||
278 | public function hasDefaults($type, $column) |
||
291 | |||
292 | /** |
||
293 | * summary. |
||
294 | * |
||
295 | * @author |
||
296 | */ |
||
297 | protected function unsetData() |
||
303 | |||
304 | //end compile() |
||
305 | }//end class |
||
306 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.