Total Complexity | 52 |
Total Lines | 342 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like anonymous//bin/migrate.php$0 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.
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 anonymous//bin/migrate.php$0, and based on these observations, apply Extract Interface, too.
1 | #!/usr/bin/php |
||
21 | (new class ($argv, $opt) { |
||
22 | |||
23 | private array $argv; |
||
24 | |||
25 | private array $opt; |
||
26 | |||
27 | private DB $db; |
||
28 | |||
29 | private Schema $schema; |
||
30 | |||
31 | public function __construct(array $argv, array $opt) |
||
32 | { |
||
33 | $this->argv = $argv; |
||
34 | $opt['connection'] ??= 'default'; |
||
35 | $opt['config'] ??= 'db.config.php'; |
||
36 | $this->opt = $opt; |
||
37 | $this->db = DB::fromConfig($opt['connection'], $opt['config']); |
||
38 | $realLogger = $this->db->getLogger(); |
||
39 | $this->db->setLogger(fn($sql) => $this->_stdout($sql) or $realLogger($sql)); |
||
40 | $this->schema = $this->db->getSchema(); |
||
41 | } |
||
42 | |||
43 | private function _stderr(string $text): void |
||
44 | { |
||
45 | fputs(STDERR, "{$text}\n\n"); |
||
46 | } |
||
47 | |||
48 | private function _stdout(string $text): void |
||
49 | { |
||
50 | echo "{$text}\n\n"; |
||
51 | } |
||
52 | |||
53 | private function _usage_exit(): void |
||
54 | { |
||
55 | $this->_stderr(<<< USAGE |
||
56 | |||
57 | $ php {$this->argv[0]} [OPTIONS] ACTION |
||
58 | |||
59 | OPTIONS: |
||
60 | |||
61 | --config=db.config.php |
||
62 | |||
63 | Chooses the configuration file. |
||
64 | |||
65 | --connection=default |
||
66 | |||
67 | Chooses the connection from the configuration file. |
||
68 | |||
69 | ACTIONS: |
||
70 | |||
71 | -h |
||
72 | --help |
||
73 | |||
74 | Prints this usage information to STDERR and calls exit(1) |
||
75 | |||
76 | --status |
||
77 | |||
78 | Outputs the current migration sequence. |
||
79 | |||
80 | --up= |
||
81 | --down= |
||
82 | |||
83 | Migrates up or down, optionally to a target sequence. |
||
84 | For upgrades, the default target is all the way. |
||
85 | For downgrades, the default target is the previous sequence. |
||
86 | |||
87 | --record=<CLASS> |
||
88 | --junction=<INTERFACE> |
||
89 | |||
90 | The FQN of an annotated class or interface, |
||
91 | which the DB instance can use to return Record or Junction access. |
||
92 | |||
93 | The access object's tables are inspected against the database, |
||
94 | and appropriate migration is then generated into the migrations |
||
95 | directory. Statically generated migrations preserve history. |
||
96 | |||
97 | To make CLI execution easier, forward-slashes in the FQN are |
||
98 | converted to namespace separators. |
||
99 | USAGE |
||
100 | ); |
||
101 | exit(1); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @uses h() |
||
106 | * @uses help() |
||
107 | * @uses status() |
||
108 | * @uses up() |
||
109 | * @uses down() |
||
110 | * @uses record() |
||
111 | * @uses junction() |
||
112 | */ |
||
113 | public function _exec(): void |
||
114 | { |
||
115 | foreach (['h', 'help', 'status', 'up', 'down', 'record', 'junction'] as $action) { |
||
116 | if (isset($this->opt[$action])) { |
||
117 | $this->{$action}($this->opt[$action] ?: null); |
||
118 | return; |
||
119 | } |
||
120 | } |
||
121 | $this->_usage_exit(); |
||
122 | } |
||
123 | |||
124 | private function h(): void |
||
125 | { |
||
126 | $this->_usage_exit(); |
||
127 | } |
||
128 | |||
129 | private function help(): void |
||
130 | { |
||
131 | $this->_usage_exit(); |
||
132 | } |
||
133 | |||
134 | private function status(): void |
||
141 | } |
||
142 | |||
143 | private function up(?string $to): void |
||
144 | { |
||
145 | $migrator = $this->db->getMigrator(); |
||
146 | $transaction = $this->db->newTransaction(); |
||
147 | $current = $migrator->getCurrent(); |
||
148 | $currentString = $current ?: 'NONE'; |
||
149 | if ($to) { |
||
150 | $this->_stdout("-- Upgrading from \"{$currentString}\" to \"{$to}\" ..."); |
||
151 | } else { |
||
152 | $this->_stdout("-- Upgrading ALL starting from \"{$currentString}\" ..."); |
||
153 | } |
||
154 | sleep(3); // time to cancel |
||
155 | if ($current === $migrator->up($to ?: null)) { |
||
156 | $this->_stdout("-- Nothing to do."); |
||
157 | } else { |
||
158 | $transaction->commit(); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | private function down(?string $to): void |
||
163 | { |
||
164 | $migrator = $this->db->getMigrator(); |
||
165 | $transaction = $this->db->newTransaction(); |
||
166 | $current = $migrator->getCurrent(); |
||
167 | $currentString = $current ?: 'NONE'; |
||
168 | if ($to) { |
||
169 | $this->_stdout("-- Downgrading from \"{$currentString}\" to \"{$to}\" ..."); |
||
170 | } else { |
||
171 | $this->_stdout("-- Downgrading once from \"{$currentString}\" ..."); |
||
172 | } |
||
173 | sleep(3); // time to cancel |
||
174 | if ($current === $migrator->down($to ?: null)) { |
||
175 | $this->_stdout("-- Nothing to do."); |
||
176 | } else { |
||
177 | $transaction->commit(); |
||
178 | } |
||
179 | } |
||
180 | |||
181 | private function _toClass(string $path): string |
||
182 | { |
||
183 | return str_replace('/', '\\', $path); |
||
184 | } |
||
185 | |||
186 | private function record(string $class): void |
||
187 | { |
||
188 | $class = $this->_toClass($class) or $this->_usage_exit(); |
||
189 | $record = $this->db->getRecord($class); |
||
190 | $up = []; |
||
191 | $down = []; |
||
192 | if (!$this->schema->getTable($record)) { |
||
193 | $this->record_create($record, $up, $down); |
||
194 | } else { |
||
195 | $this->record_add_columns($record, $up, $down); |
||
196 | $this->record_drop_columns($record, $up, $down); |
||
197 | } |
||
198 | $this->record_create_eav($record, $up, $down); |
||
199 | $this->write($class, $up, $down); |
||
200 | } |
||
201 | |||
202 | private function record_create_eav(Record $record, &$up, &$down) |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | |||
220 | private function record_add_columns(Record $record, &$up, &$down) |
||
221 | { |
||
222 | $columns = $this->schema->getColumnInfo($record); |
||
223 | $multiUnique = []; |
||
224 | foreach ($record->getTypes() as $property => $type) { |
||
225 | if (!isset($columns[$property])) { |
||
226 | $T_CONST = Schema::T_CONST_NAMES[$type]; |
||
227 | if ($record->isNullable($property)) { |
||
228 | $T_CONST .= '_NULL'; |
||
229 | } |
||
230 | $up[] = "\$schema->addColumn('{$record}', '{$property}', Schema::{$T_CONST});"; |
||
231 | $down[] = "\$schema->dropColumn('{$record}', '{$property}');"; |
||
232 | } |
||
233 | if ($record->isUnique($property)) { |
||
234 | $up[] = "\$schema->addUniqueKeyConstraint('{$record}', ['{$property}']);"; |
||
235 | $down[] = "\$schema->dropUniqueKeyConstraint('{$record}', ['{$property}']);"; |
||
236 | } elseif ($uniqueGroup = $record->getUniqueGroup($property)) { |
||
237 | $multiUnique[$uniqueGroup][] = $property; |
||
238 | } |
||
239 | } |
||
240 | foreach ($multiUnique as $properties) { |
||
241 | $properties = "'" . implode("','", $properties) . "'"; |
||
242 | $up[] = "\$schema->addUniqueKeyConstraint('{$record}', [{$properties}]);"; |
||
243 | $down[] = "\$schema->dropUniqueKeyConstraint('{$record}', [{$properties}]);"; |
||
244 | } |
||
245 | } |
||
246 | |||
247 | private function record_drop_columns(Record $record, &$up, &$down) |
||
248 | { |
||
249 | $columns = $this->schema->getColumnInfo($record); |
||
250 | foreach ($columns as $column => $info) { |
||
251 | if (!$record[$column]) { |
||
252 | $T_CONST = Schema::T_CONST_NAMES[$info['type']]; |
||
253 | if ($info['nullable']) { |
||
254 | $T_CONST .= '_NULL'; |
||
255 | } |
||
256 | $up[] = "\$schema->dropColumn('{$record}', '{$column}');"; |
||
257 | $down[] = "\$schema->addColumn('{$record}', '{$column}', Schema::{$T_CONST});"; |
||
258 | } |
||
259 | } |
||
260 | } |
||
261 | |||
262 | private function record_create(Record $record, &$up, &$down) |
||
284 | } |
||
285 | } |
||
286 | |||
287 | private function junction(string $class): void |
||
288 | { |
||
289 | $class = $this->_toClass($class) or $this->_usage_exit(); |
||
290 | $junction = $this->db->getJunction($class); |
||
291 | $up = []; |
||
292 | $down = []; |
||
293 | |||
294 | if (!$this->schema->getTable($junction)) { |
||
295 | $records = $junction->getRecords(); |
||
296 | $columns = array_map( |
||
297 | fn(string $column) => "'{$column}' => Schema::T_INT | Schema::I_PRIMARY", |
||
298 | array_keys($records) |
||
299 | ); |
||
300 | $columns = "[\n\t\t\t" . implode(",\n\t\t\t", $columns) . "\n\t\t]"; |
||
301 | $foreign = array_map( |
||
302 | fn(string $column, Record $record) => "'{$column}' => \$schema['{$record}']['id']", |
||
303 | array_keys($records), |
||
304 | $records |
||
305 | ); |
||
306 | $foreign = "[\n\t\t\t" . implode(",\n\t\t\t", $foreign) . "\n\t\t]"; |
||
307 | $up[] = "\$schema->createTable('{$junction}', {$columns}, {$foreign});"; |
||
308 | $down[] = "\$schema->dropTable('{$junction}');"; |
||
309 | } |
||
310 | |||
311 | $this->write($class, $up, $down); |
||
312 | } |
||
313 | |||
314 | private function write(string $class, array $up, array $down): void |
||
363 | } |
||
364 | |||
365 | }; |
||
366 | |||
367 | MIGRATION |
||
368 | ); |
||
369 | } |
||
370 | |||
371 | })->_exec(); |
||
372 |