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 namespace Nwidart\DbExporter; |
||
8 | class DbMigrations extends DbExporter |
||
9 | { |
||
10 | protected $database; |
||
11 | |||
12 | protected $selects = array( |
||
13 | 'column_name as Field', |
||
14 | 'column_type as Type', |
||
15 | 'is_nullable as Null', |
||
16 | 'column_key as Key', |
||
17 | 'column_default as Default', |
||
18 | 'extra as Extra', |
||
19 | 'data_type as Data_Type' |
||
20 | ); |
||
21 | |||
22 | protected $schema; |
||
23 | |||
24 | protected $customDb = false; |
||
25 | |||
26 | public static $filePath; |
||
27 | |||
28 | /** |
||
29 | * Set the database name |
||
30 | * @param String $database |
||
31 | * @throw InvalidDatabaseException |
||
32 | */ |
||
33 | function __construct($database) |
||
|
|||
34 | { |
||
35 | if (empty($database)) { |
||
36 | throw new InvalidDatabaseException('No database set in app/config/database.php'); |
||
37 | } |
||
38 | |||
39 | $this->database = $database; |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Write the prepared migration to a file |
||
44 | */ |
||
45 | public function write() |
||
46 | { |
||
47 | // Check if convert method was called before |
||
48 | // If not, call it on default DB |
||
49 | if (!$this->customDb) { |
||
50 | $this->convert(); |
||
51 | } |
||
52 | |||
53 | $schema = $this->compile(); |
||
54 | $filename = date('Y_m_d_His') . "_create_" . $this->database . "_database.php"; |
||
55 | self::$filePath = config('db-exporter.export_path.migrations')."{$filename}"; |
||
56 | |||
57 | file_put_contents(self::$filePath, $schema); |
||
58 | |||
59 | return self::$filePath; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Convert the database to migrations |
||
64 | * If none is given, use de DB from condig/database.php |
||
65 | * @param null $database |
||
66 | * @return $this |
||
67 | */ |
||
68 | public function convert($database = null) |
||
187 | |||
188 | /** |
||
189 | * Compile the migration into the base migration file |
||
190 | * TODO use a template with seacrh&replace |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function compile() |
||
228 | |||
229 | } |
||
230 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.