Total Complexity | 55 |
Total Lines | 356 |
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 |
||
24 | (new class ($argv, $opt) { |
||
25 | |||
26 | private array $argv; |
||
27 | |||
28 | private array $opt; |
||
29 | |||
30 | private DB $db; |
||
31 | |||
32 | private Schema $schema; |
||
33 | |||
34 | public function __construct(array $argv, array $opt) |
||
35 | { |
||
36 | $this->argv = $argv; |
||
37 | $opt['connection'] ??= 'default'; |
||
38 | $opt['config'] ??= 'db.config.php'; |
||
39 | $this->opt = $opt; |
||
40 | $this->db = DB::fromConfig($opt['connection'], $opt['config']); |
||
41 | $realLogger = $this->db->getLogger(); |
||
42 | $this->db->setLogger(fn($sql) => $this->_stdout($sql) or $realLogger($sql)); |
||
43 | $this->schema = $this->db->getSchema(); |
||
44 | } |
||
45 | |||
46 | private function _stderr(string $text): void |
||
47 | { |
||
48 | fputs(STDERR, "{$text}\n\n"); |
||
49 | } |
||
50 | |||
51 | private function _stdout(string $text): void |
||
52 | { |
||
53 | echo "{$text}\n\n"; |
||
54 | } |
||
55 | |||
56 | private function _usage_exit(): void |
||
57 | { |
||
58 | $this->_stderr(<<< USAGE |
||
59 | |||
60 | $ php {$this->argv[0]} [OPTIONS] ACTION |
||
61 | |||
62 | OPTIONS: |
||
63 | |||
64 | --config=db.config.php |
||
65 | |||
66 | Chooses the configuration file. |
||
67 | |||
68 | --connection=default |
||
69 | |||
70 | Chooses the connection from the configuration file. |
||
71 | |||
72 | ACTIONS: |
||
73 | |||
74 | -h |
||
75 | --help |
||
76 | |||
77 | Prints this usage information to STDERR and calls exit(1) |
||
78 | |||
79 | --status |
||
80 | |||
81 | Outputs the current migration sequence. |
||
82 | |||
83 | --up= |
||
84 | --down= |
||
85 | |||
86 | Migrates up or down, optionally to a target sequence. |
||
87 | For upgrades, the default target is all the way. |
||
88 | For downgrades, the default target is the previous sequence. |
||
89 | |||
90 | --record=<CLASS> |
||
91 | --junction=<INTERFACE> |
||
92 | |||
93 | The FQN of an annotated class or interface, |
||
94 | which the DB instance can use to return Record or Junction access. |
||
95 | |||
96 | The access object's tables are inspected against the database, |
||
97 | and appropriate migration is then generated into the migrations |
||
98 | directory. Statically generated migrations preserve history. |
||
99 | |||
100 | To make CLI execution easier, forward-slashes in the FQN are |
||
101 | converted to namespace separators. |
||
102 | USAGE |
||
103 | ); |
||
104 | exit(1); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @uses h() |
||
109 | * @uses help() |
||
110 | * @uses status() |
||
111 | * @uses up() |
||
112 | * @uses down() |
||
113 | * @uses record() |
||
114 | * @uses junction() |
||
115 | */ |
||
116 | public function _exec(): void |
||
117 | { |
||
118 | foreach (['h', 'help', 'status', 'up', 'down', 'record', 'junction'] as $action) { |
||
119 | if (isset($this->opt[$action])) { |
||
120 | $this->{$action}($this->opt[$action] ?: null); |
||
121 | return; |
||
122 | } |
||
123 | } |
||
124 | $this->_usage_exit(); |
||
125 | } |
||
126 | |||
127 | private function h(): void |
||
128 | { |
||
129 | $this->_usage_exit(); |
||
130 | } |
||
131 | |||
132 | private function help(): void |
||
133 | { |
||
134 | $this->_usage_exit(); |
||
135 | } |
||
136 | |||
137 | private function status(): void |
||
144 | } |
||
145 | |||
146 | private function up(?string $to): void |
||
147 | { |
||
148 | $migrator = $this->db->getMigrator(); |
||
149 | $transaction = $this->db->newTransaction(); |
||
150 | $current = $migrator->getCurrent(); |
||
151 | $currentString = $current ?: 'NONE'; |
||
152 | if ($to) { |
||
153 | $this->_stdout("-- Upgrading from \"{$currentString}\" to \"{$to}\" ..."); |
||
154 | } else { |
||
155 | $this->_stdout("-- Upgrading ALL starting from \"{$currentString}\" ..."); |
||
156 | } |
||
157 | sleep(3); // time to cancel |
||
158 | if ($current === $migrator->up($to ?: null)) { |
||
159 | $this->_stdout("-- Nothing to do."); |
||
160 | } else { |
||
161 | $transaction->commit(); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | private function down(?string $to): void |
||
181 | } |
||
182 | } |
||
183 | |||
184 | private function _toClass(string $path): string |
||
185 | { |
||
186 | return str_replace('/', '\\', $path); |
||
187 | } |
||
188 | |||
189 | private function record(string $class): void |
||
203 | } |
||
204 | |||
205 | private function record_create(Record $record, &$up, &$down) |
||
206 | { |
||
207 | $serializer = $record->getSerializer(); |
||
208 | |||
209 | // define the table |
||
210 | $columns = []; |
||
211 | foreach ($serializer->getStorageTypes() as $column => $type) { |
||
212 | $T_CONST = Schema::T_CONST_NAMES[$type]; |
||
213 | if ($serializer->isNullable($column)) { |
||
214 | $T_CONST .= '_NULL'; |
||
215 | } |
||
216 | $columns[$column] = "'{$column}' => Schema::{$T_CONST}"; |
||
217 | } |
||
218 | $columns['id'] = "'id' => Schema::T_AUTOINCREMENT"; |
||
219 | $columns = "[\n\t\t\t" . implode(",\n\t\t\t", $columns) . "\n\t\t]"; |
||
220 | |||
221 | $foreign = []; |
||
222 | foreach ($serializer->getForeign() as $col => $class) { |
||
223 | $foreign[$col] = "'{$col}' => \$schema['{$this->db->getRecord($class)}']['id']"; |
||
224 | } |
||
225 | if ($foreign) { |
||
226 | $foreign = "[\n\t\t\t" . implode(",\n\t\t\t", $foreign) . "\n\t\t]"; |
||
227 | $up[] = "\$schema->createTable('{$record}', {$columns}, {$foreign});"; |
||
228 | } else { |
||
229 | $up[] = "\$schema->createTable('{$record}', {$columns});"; |
||
230 | } |
||
231 | $down[] = "\$schema->dropTable('{$record}');"; |
||
232 | |||
233 | // add unique constraints |
||
234 | foreach ($serializer->getUnique() as $column) { |
||
235 | $up[] = "\$schema->addUniqueKey('{$record}', ['{$column}']);"; |
||
236 | $down[] = "\$schema->dropUniqueKey('{$record}', ['{$column}']);"; |
||
237 | } |
||
238 | foreach ($serializer->getUniqueGroups() as $columns) { |
||
239 | $columns = implode("', '", $columns); |
||
240 | $up[] = "\$schema->addUniqueKey('{$record}', ['{$columns}']);"; |
||
241 | $down[] = "\$schema->dropUniqueKey('{$record}', ['{$columns}']);"; |
||
242 | } |
||
243 | } |
||
244 | |||
245 | private function record_create_eav(Record $record, &$up, &$down) |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | private function record_add_columns(Record $record, &$up, &$down) |
||
264 | { |
||
265 | $columns = $this->schema->getColumnInfo($record); |
||
266 | $serializer = $record->getSerializer(); |
||
267 | foreach ($serializer->getStorageTypes() as $property => $type) { |
||
268 | if (!isset($columns[$property])) { |
||
269 | $T_CONST = Schema::T_CONST_NAMES[$type]; |
||
270 | if ($serializer->isNullable($property)) { |
||
271 | $T_CONST .= '_NULL'; |
||
272 | } |
||
273 | $up[] = "\$schema->addColumn('{$record}', '{$property}', Schema::{$T_CONST});"; |
||
274 | } |
||
275 | } |
||
276 | foreach ($serializer->getUnique() as $property) { |
||
277 | if (!$this->schema->hasUniqueKey($record, [$property])) { |
||
278 | $up[] = "\$schema->addUniqueKey('{$record}', ['{$property}']);"; |
||
279 | $down[] = "\$schema->dropUniqueKey('{$record}', ['{$property}']);"; |
||
280 | } |
||
281 | } |
||
282 | foreach ($serializer->getUniqueGroups() as $properties) { |
||
283 | if (!$this->schema->hasUniqueKey($record, $properties)) { |
||
284 | $properties = "'" . implode("','", $properties) . "'"; |
||
285 | $up[] = "\$schema->addUniqueKey('{$record}', [{$properties}]);"; |
||
286 | $down[] = "\$schema->dropUniqueKey('{$record}', [{$properties}]);"; |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | |||
291 | private function record_drop_columns(Record $record, &$up, &$down) |
||
299 | } |
||
300 | } |
||
301 | } |
||
302 | } |
||
303 | |||
304 | private function junction(string $class): void |
||
305 | { |
||
306 | $class = $this->_toClass($class) or $this->_usage_exit(); |
||
307 | $junction = $this->db->getJunction($class); |
||
308 | $up = []; |
||
309 | $down = []; |
||
310 | |||
311 | if (!$this->schema->getTable($junction)) { |
||
312 | $records = $junction->getRecords(); |
||
313 | $columns = array_map( |
||
314 | fn(string $column) => "'{$column}' => Schema::T_INT | Schema::I_PRIMARY", |
||
315 | array_keys($records) |
||
316 | ); |
||
317 | $columns = "[\n\t\t\t" . implode(",\n\t\t\t", $columns) . "\n\t\t]"; |
||
318 | $foreign = array_map( |
||
319 | fn(string $column, Record $record) => "'{$column}' => \$schema['{$record}']['id']", |
||
320 | array_keys($records), |
||
321 | $records |
||
322 | ); |
||
323 | $foreign = "[\n\t\t\t" . implode(",\n\t\t\t", $foreign) . "\n\t\t]"; |
||
324 | $up[] = "\$schema->createTable('{$junction}', {$columns}, {$foreign});"; |
||
325 | $down[] = "\$schema->dropTable('{$junction}');"; |
||
326 | } |
||
327 | |||
328 | $this->write($class, $up, $down); |
||
329 | } |
||
330 | |||
331 | private function write(string $class, array $up, array $down): void |
||
380 | } |
||
381 | |||
382 | }; |
||
389 |