Total Complexity | 63 |
Total Lines | 522 |
Duplicated Lines | 0 % |
Changes | 6 | ||
Bugs | 1 | Features | 0 |
Complex classes like SchemaDescription 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 SchemaDescription, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class SchemaDescription implements \ArrayAccess |
||
13 | { |
||
14 | |||
15 | /** |
||
16 | * Schema description that this class wraps. |
||
17 | */ |
||
18 | private array $description; |
||
19 | |||
20 | /** |
||
21 | * Create a new instance of the schema description. |
||
22 | * |
||
23 | * @param array $description |
||
24 | * @param AbstractDatabaseManipulator $manipulator |
||
25 | */ |
||
26 | private function __construct($description, $manipulator) |
||
27 | { |
||
28 | $this->description = $description; |
||
29 | $this->description['tables'] = $this->convertColumnTypes( |
||
30 | $this->description['tables'] ?? [], $manipulator |
||
31 | ); |
||
32 | foreach ($this->description['schemata'] as $name => $schema) { |
||
33 | if (is_array($schema['tables'])) { |
||
34 | $this->description['schemata'][$name]['tables'] = $this->convertColumnTypes( |
||
35 | $schema['tables'], $manipulator |
||
36 | ); |
||
37 | } |
||
38 | } |
||
39 | $this->flattenAllColumns(); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * Convert the column types between generic yentu types and native database |
||
44 | * types on the tables |
||
45 | * |
||
46 | * @param array $tables |
||
47 | * @param AbstractDatabaseManipulator $manipulator |
||
48 | * @return array |
||
49 | */ |
||
50 | private function convertColumnTypes($tables, $manipulator) |
||
51 | { |
||
52 | foreach ($tables as $i => $table) { |
||
53 | foreach ($table['columns'] as $j => $column) { |
||
54 | $tables[$i]['columns'][$j]['type'] = $manipulator->convertTypes( |
||
55 | $column['type'], AbstractDatabaseManipulator::CONVERT_TO_YENTU, $column['length'] |
||
56 | ); |
||
57 | } |
||
58 | } |
||
59 | return $tables; |
||
60 | } |
||
61 | |||
62 | #[\Override] |
||
63 | public function offsetExists($offset): bool |
||
64 | { |
||
65 | return isset($this->description[$offset]); |
||
66 | } |
||
67 | |||
68 | #[\Override] |
||
69 | public function offsetGet($offset): mixed |
||
70 | { |
||
71 | $return = $this->description[$offset]; |
||
72 | if(is_array($return)) { |
||
73 | $return = Parameters::wrap($this->description[$offset]); |
||
74 | $this->description[$offset] = $return; |
||
75 | } |
||
76 | return $return; |
||
77 | } |
||
78 | |||
79 | #[\Override] |
||
80 | public function offsetSet(mixed $offset, mixed $value): void |
||
81 | { |
||
82 | |||
83 | } |
||
84 | |||
85 | #[\Override] |
||
86 | public function offsetUnset(mixed $offset): void |
||
87 | { |
||
88 | unset($this->description[$offset]); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Add a schema to the schema description. |
||
93 | * @param $name |
||
94 | */ |
||
95 | public function addSchema($name) |
||
96 | { |
||
97 | $this->description['schemata'][$name] = array( |
||
98 | 'name' => $name, |
||
99 | 'tables' => array() |
||
100 | ); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Drop the schema from the schema description. |
||
105 | * @param string $name |
||
106 | */ |
||
107 | public function dropSchema($name) |
||
108 | { |
||
109 | unset($this->description['schemata'][$name]); |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Add a table to the schema description. |
||
114 | * The table array contains the `columns`, `primary_key`, `unique_keys`, |
||
115 | * `foreign_keys` and `indices`. |
||
116 | * @param array $details |
||
117 | */ |
||
118 | public function addTable($details) |
||
119 | { |
||
120 | $table = array( |
||
121 | 'name' => $details['name'], |
||
122 | 'schema' => $details['schema'], |
||
123 | 'columns' => array(), |
||
124 | 'primary_key' => array(), |
||
125 | 'unique_keys' => array(), |
||
126 | 'foreign_keys' => array(), |
||
127 | 'indices' => array() |
||
128 | ); |
||
129 | $this->setTable(array('schema' => $details['schema'], 'table' => $details['name']), $table); |
||
|
|||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Change the name of a table. |
||
134 | * The details contains two separate |
||
135 | * @param array $details |
||
136 | */ |
||
137 | public function changeTableName($details) |
||
138 | { |
||
139 | // Use the from details to query for the existing table |
||
140 | $query = $details['from']; |
||
141 | $query['table'] = $details['from']['name']; |
||
142 | $table = $this->getTable($query); |
||
143 | |||
144 | // unset the existing table from the description array |
||
145 | if ($details['from']['schema'] == '') { |
||
146 | unset($this->description["tables"][$details['from']['name']]); |
||
147 | } else { |
||
148 | unset($this->description['schemata'][$details['from']['schema']]["tables"][$details['from']['name']]); |
||
149 | } |
||
150 | |||
151 | // reassign the existing table to the description array using the to details |
||
152 | $table['name'] = $details['to']['name']; |
||
153 | $details['to']['table'] = $table['name']; |
||
154 | $this->setTable($details['to'], $table); |
||
155 | } |
||
156 | |||
157 | /** |
||
158 | * Add a view to the schema description. |
||
159 | * |
||
160 | * @param array $details |
||
161 | */ |
||
162 | public function addView($details) |
||
163 | { |
||
164 | $view = array( |
||
165 | 'name' => $details['name'], |
||
166 | 'definition' => $details['definition'] |
||
167 | ); |
||
168 | |||
169 | if ($details['schema'] != '') { |
||
170 | $schemata = $this->description['schemata']; |
||
171 | $schemata[$details['schema']]['views'][$details['name']] = $view; |
||
172 | $this->description['schemata'] = $schemata; |
||
173 | } else { |
||
174 | $this->description['views'][$details['name']] = $view; |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * Drop a view from the description. |
||
180 | * |
||
181 | * @param array $details |
||
182 | */ |
||
183 | public function dropView($details) |
||
184 | { |
||
185 | $this->dropTable($details, 'views'); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Change the definition of a view in the schema description. |
||
190 | * |
||
191 | * @param type $details |
||
192 | */ |
||
193 | public function changeViewDefinition($details) |
||
194 | { |
||
195 | $viewDetails = array( |
||
196 | 'view' => $details['from']['name'], |
||
197 | 'schema' => $details['from']['schema'] |
||
198 | ); |
||
199 | $view = $this->getTable($viewDetails, 'view'); |
||
200 | $view['definition'] = $details['to']['definition']; |
||
201 | $this->setTable($viewDetails, $view, 'view'); |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Drop a table from the schema description. |
||
206 | * |
||
207 | * @param array $details |
||
208 | * @param string $type |
||
209 | */ |
||
210 | public function dropTable($details, $type = 'tables') |
||
211 | { |
||
212 | if ($details['schema'] != '') { |
||
213 | unset($this->description['schemata'][$details['schema']][$type][$details['name']]); |
||
214 | } else { |
||
215 | unset($this->description[$type][$details['name']]); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Get a table from the schema description. |
||
221 | * |
||
222 | * @param array $details |
||
223 | * @param string $type |
||
224 | * @return array |
||
225 | */ |
||
226 | public function getTable($details, $type = 'table') |
||
227 | { |
||
228 | if ($details['schema'] == '') { |
||
229 | return $this->description["{$type}s"][$details[$type]]; |
||
230 | } else { |
||
231 | return $this->description['schemata'][$details['schema']]["{$type}s"][$details[$type]]; |
||
232 | } |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Replace a table on the schema description with a new one. |
||
237 | * |
||
238 | * @param array $details |
||
239 | * @param string $table |
||
240 | * @param type $type |
||
241 | */ |
||
242 | private function setTable($details, $table, $type = 'table') |
||
243 | { |
||
244 | if ($details['schema'] == '') { |
||
245 | $this->description["{$type}s"][$details[$type]] = $table; |
||
246 | } else { |
||
247 | $this->description['schemata'][$details['schema']]["{$type}s"][$details[$type]] = $table; |
||
248 | } |
||
249 | $this->flattenAllColumns(); |
||
250 | } |
||
251 | |||
252 | /** |
||
253 | * Flatten all table constraints to make it easy to address them through |
||
254 | * a single linear array. |
||
255 | */ |
||
256 | private function flattenAllColumns() |
||
257 | { |
||
258 | foreach ($this->description['schemata'] as $schemaName => $schema) { |
||
259 | foreach ($schema['tables'] as $tableName => $table) { |
||
260 | $this->description['schemata'][$schemaName]['tables'][$tableName]['flat_foreign_keys'] = $this->flattenColumns($table['foreign_keys']); |
||
261 | $this->description['schemata'][$schemaName]['tables'][$tableName]['flat_unique_keys'] = $this->flattenColumns($table['unique_keys']); |
||
262 | $this->description['schemata'][$schemaName]['tables'][$tableName]['flat_indices'] = $this->flattenColumns($table['indices']); |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Flatten column keys so they can be accessed linearly in an array. |
||
269 | */ |
||
270 | private function flattenColumns($items): array |
||
271 | { |
||
272 | $flattened = array(); |
||
273 | if (is_array($items)) { |
||
274 | foreach ($items as $name => $item) { |
||
275 | $columns = array_map(fn($x) => $x, $item['columns']); |
||
276 | sort($columns); |
||
277 | $column = implode(':', $columns); |
||
278 | // foreach ($item['columns'] as $column) { |
||
279 | $flattened[$column] = $name; |
||
280 | // } |
||
281 | } |
||
282 | } |
||
283 | return $flattened; |
||
284 | } |
||
285 | |||
286 | /** |
||
287 | * Add a column to a table. |
||
288 | * |
||
289 | * @param array $details |
||
290 | * @throws DatabaseManipulatorException |
||
291 | */ |
||
292 | public function addColumn($details) |
||
293 | { |
||
294 | $table = $this->getTable($details); |
||
295 | if ($details['type'] == '') { |
||
296 | throw new exceptions\DatabaseManipulatorException("Please specify a data type for the '{$details['name']}' column of the '{$table['name']}' table"); |
||
297 | } |
||
298 | $table['columns'][$details['name']] = array( |
||
299 | 'name' => $details['name'], |
||
300 | 'type' => $details['type'], |
||
301 | 'nulls' => $details['nulls'] |
||
302 | ); |
||
303 | $this->setTable($details, $table); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Drop a column from a table in the schema description. |
||
308 | * |
||
309 | * @param array $details |
||
310 | */ |
||
311 | public function dropColumn($details) |
||
312 | { |
||
313 | $table = $this->getTable($details); |
||
314 | unset($table['columns'][$details['name']]); |
||
315 | |||
316 | foreach ($table['foreign_keys'] as $i => $foreignKey) { |
||
317 | if (array_search($details['name'], $foreignKey['columns']) !== false) { |
||
318 | unset($table['foreign_keys'][$i]); |
||
319 | } |
||
320 | } |
||
321 | |||
322 | foreach ($table['unique_keys'] as $i => $uniqueKey) { |
||
323 | if (array_search($details['name'], $uniqueKey['columns']) !== false) { |
||
324 | unset($table['unique_keys'][$i]); |
||
325 | } |
||
326 | } |
||
327 | |||
328 | $this->setTable($details, $table); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Change the null state of a column. |
||
333 | * |
||
334 | * @param array $details |
||
335 | */ |
||
336 | public function changeColumnNulls($details) |
||
337 | { |
||
338 | $table = $this->getTable($details['to']); |
||
339 | $table['columns'][$details['to']['name']]['nulls'] = $details['to']['nulls']; |
||
340 | $this->setTable($details['to'], $table); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Change the name of a column. |
||
345 | * |
||
346 | * @param array $details |
||
347 | */ |
||
348 | public function changeColumnName($details) |
||
349 | { |
||
350 | $table = $this->getTable($details['to']); |
||
351 | $column = $table['columns'][$details['from']['name']]; |
||
352 | $column['name'] = $details['to']['name']; |
||
353 | unset($table['columns'][$details['from']['name']]); |
||
354 | $table['columns'][$details['to']['name']] = $column; |
||
355 | |||
356 | // Rename all indices |
||
357 | foreach ($table['unique_keys'] as $name => $key) { |
||
358 | $position = array_search($details['from']['name'], $key['columns']); |
||
359 | if ($position !== false) { |
||
360 | $table['unique_keys'][$name]['columns'][$position] = $column['name']; |
||
361 | } |
||
362 | } |
||
363 | |||
364 | // Rename all unique keys |
||
365 | // Rename all foreign keys |
||
366 | // rename all primary keys |
||
367 | |||
368 | $this->setTable($details['to'], $table); |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * Change the default value of the column. |
||
373 | * |
||
374 | * @param array $details |
||
375 | */ |
||
376 | public function changeColumnDefault($details) |
||
377 | { |
||
378 | $table = $this->getTable($details['to']); |
||
379 | $table['columns'][$details['to']['name']]['default'] = $details['to']['default']; |
||
380 | $this->setTable($details['to'], $table); |
||
381 | } |
||
382 | |||
383 | /** |
||
384 | * Add a primary key to a table in the schema description. |
||
385 | * |
||
386 | * @param array $details |
||
387 | */ |
||
388 | public function addPrimaryKey($details) |
||
389 | { |
||
390 | $table = $this->getTable($details); |
||
391 | $table['primary_key'] = array( |
||
392 | $details['name'] => array( |
||
393 | 'columns' => $details['columns'] |
||
394 | ) |
||
395 | ); |
||
396 | $this->setTable($details, $table); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Drop a primarykey from a table in the schema description. |
||
401 | * |
||
402 | * @param array $details |
||
403 | */ |
||
404 | public function dropPrimaryKey($details) |
||
405 | { |
||
406 | $table = $this->getTable($details); |
||
407 | unset($table['primary_key']); |
||
408 | $this->setTable($details, $table); |
||
409 | } |
||
410 | |||
411 | /** |
||
412 | * Make the primary key auto increment. |
||
413 | * |
||
414 | * @param array $details |
||
415 | */ |
||
416 | public function addAutoPrimaryKey($details) |
||
417 | { |
||
418 | $table = $this->getTable($details); |
||
419 | $table['auto_increment'] = true; |
||
420 | $this->setTable($details, $table); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Prevent the primary key from auto incrementing. |
||
425 | * |
||
426 | * @param array $details |
||
427 | */ |
||
428 | public function dropAutoPrimaryKey($details) |
||
429 | { |
||
430 | $table = $this->getTable($details); |
||
431 | $table['auto_increment'] = false; |
||
432 | $this->setTable($details, $table); |
||
433 | } |
||
434 | |||
435 | /** |
||
436 | * Add a unique constraint to a set of columns of a given table in the |
||
437 | * schema description. |
||
438 | * |
||
439 | * @param array $details |
||
440 | */ |
||
441 | public function addUniqueKey($details) |
||
442 | { |
||
443 | $table = $this->getTable($details); |
||
444 | $table['unique_keys'][$details['name']]['columns'] = $details['columns']; |
||
445 | $this->setTable($details, $table); |
||
446 | } |
||
447 | |||
448 | /** |
||
449 | * Drop a unique key from a table in the schema description. |
||
450 | * |
||
451 | * @param type $details |
||
452 | */ |
||
453 | public function dropUniqueKey($details) |
||
454 | { |
||
455 | $table = $this->getTable($details); |
||
456 | if (isset($table['unique_keys'][$details['name']])) { |
||
457 | unset($table['unique_keys'][$details['name']]); |
||
458 | } else { |
||
459 | // Deal with special edge cases as in SQLite where unique keys are not named |
||
460 | foreach ($table['unique_keys'] as $i => $key) { |
||
461 | if ($key['columns'] == $details['columns']) { |
||
462 | unset($table['unique_keys'][$i]); |
||
463 | } |
||
464 | } |
||
465 | } |
||
466 | $this->setTable($details, $table); |
||
467 | } |
||
468 | |||
469 | /** |
||
470 | * Add an index to the table. |
||
471 | * |
||
472 | * @param array $details |
||
473 | */ |
||
474 | public function addIndex($details) |
||
475 | { |
||
476 | $table = $this->getTable($details); |
||
477 | $table['indices'][$details['name']]['columns'] = $details['columns']; |
||
478 | $this->setTable($details, $table); |
||
479 | } |
||
480 | |||
481 | public function dropIndex($details) |
||
486 | } |
||
487 | |||
488 | public function addForeignKey($details) |
||
489 | { |
||
490 | $table = $this->getTable($details); |
||
491 | $table['foreign_keys'][$details['name']] = $details; |
||
492 | $this->setTable($details, $table); |
||
493 | } |
||
494 | |||
495 | public function dropForeignKey($details) |
||
496 | { |
||
497 | $table = $this->getTable($details); |
||
498 | unset($table['foreign_keys'][$details['name']]); |
||
499 | $this->setTable($details, $table); |
||
500 | } |
||
501 | |||
502 | public function changeForeignKeyOnDelete($details) |
||
503 | { |
||
504 | $table = $this->getTable($details['from']); |
||
505 | $table['foreign_keys'][$details['from']['name']]['on_delete'] = $details['to']['on_delete']; |
||
506 | $this->setTable($details['from'], $table); |
||
507 | } |
||
508 | |||
509 | public function changeForeignKeyOnUpdate($details) |
||
514 | } |
||
515 | |||
516 | public function executeQuery() |
||
517 | { |
||
518 | |||
519 | } |
||
520 | |||
521 | public function reverseQuery() |
||
522 | { |
||
523 | |||
524 | } |
||
525 | |||
526 | public static function wrap($description, $manipulator) |
||
529 | } |
||
530 | |||
531 | public function getArray() |
||
532 | { |
||
534 | } |
||
535 | |||
536 | } |
||
537 |