Conditions | 41 |
Paths | 576 |
Total Lines | 117 |
Code Lines | 80 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
18 | private function recreateTable(TableEntity $tableAttrs, string $table = '') |
||
19 | { |
||
20 | $alter = []; |
||
21 | $originals = []; |
||
22 | $indexes = []; |
||
23 | foreach ($tableAttrs->fields as $field) { |
||
24 | if ($field[1]) { |
||
25 | $alter[] = (\is_string($field[1]) ? $field[1] : 'ADD ' . implode($field[1])); |
||
26 | if ($field[0] != '') { |
||
27 | $originals[$field[0]] = $field[1][0]; |
||
28 | } |
||
29 | } |
||
30 | } |
||
31 | |||
32 | if ($table != '') { |
||
33 | if (empty($fields)) { |
||
|
|||
34 | foreach ($this->driver->fields($table) as $key => $field) { |
||
35 | if (!empty($indexes)) { |
||
36 | $field->autoIncrement = 0; |
||
37 | } |
||
38 | $fields[] = $this->util->processField($field, $field); |
||
39 | $originals[$key] = $this->driver->escapeId($key); |
||
40 | } |
||
41 | } |
||
42 | $primary_key = false; |
||
43 | foreach ($fields as $field) { |
||
44 | if ($field[6]) { |
||
45 | $primary_key = true; |
||
46 | } |
||
47 | } |
||
48 | $drop_indexes = []; |
||
49 | foreach ($indexes as $key => $val) { |
||
50 | if ($val[2] == 'DROP') { |
||
51 | $drop_indexes[$val[1]] = true; |
||
52 | unset($indexes[$key]); |
||
53 | } |
||
54 | } |
||
55 | foreach ($this->driver->indexes($table) as $key_name => $index) { |
||
56 | $columns = []; |
||
57 | foreach ($index->columns as $key => $column) { |
||
58 | if (!$originals[$column]) { |
||
59 | continue 2; |
||
60 | } |
||
61 | $columns[] = $originals[$column] . ($index->descs[$key] ? ' DESC' : ''); |
||
62 | } |
||
63 | if (!$drop_indexes[$key_name]) { |
||
64 | if ($index->type != 'PRIMARY' || !$primary_key) { |
||
65 | $indexes[] = [$index->type, $key_name, $columns]; |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | foreach ($indexes as $key => $val) { |
||
70 | if ($val[0] == 'PRIMARY') { |
||
71 | unset($indexes[$key]); |
||
72 | $foreign[] = ' PRIMARY KEY (' . implode(', ', $val[2]) . ')'; |
||
73 | } |
||
74 | } |
||
75 | foreach ($this->driver->foreignKeys($table) as $key_name => $foreignKey) { |
||
76 | foreach ($foreignKey->source as $key => $column) { |
||
77 | if (!$originals[$column]) { |
||
78 | continue 2; |
||
79 | } |
||
80 | $foreignKey->source[$key] = $this->driver->unescapeId($originals[$column]); |
||
81 | } |
||
82 | if (!isset($foreign[" $key_name"])) { |
||
83 | $foreign[] = ' ' . $this->driver->formatForeignKey($foreignKey); |
||
84 | } |
||
85 | } |
||
86 | $this->driver->execute('BEGIN'); |
||
87 | } |
||
88 | foreach ($fields as $key => $field) { |
||
89 | $fields[$key] = ' ' . implode($field); |
||
90 | } |
||
91 | $fields = array_merge($fields, array_filter($foreign)); |
||
92 | $tempName = ($table == $name ? "adminer_$name" : $name); |
||
93 | if (!$this->driver->execute('CREATE TABLE ' . $this->driver->table($tempName) . |
||
94 | " (\n" . implode(",\n", $fields) . "\n)")) { |
||
95 | // implicit ROLLBACK to not overwrite $this->driver->error() |
||
96 | return false; |
||
97 | } |
||
98 | if ($table != '') { |
||
99 | if ($originals && !$this->driver->execute('INSERT INTO ' . $this->driver->table($tempName) . |
||
100 | ' (' . implode(', ', $originals) . ') SELECT ' . implode( |
||
101 | ', ', |
||
102 | array_map(function ($key) { |
||
103 | return $this->driver->escapeId($key); |
||
104 | }, array_keys($originals)) |
||
105 | ) . ' FROM ' . $this->driver->table($table))) { |
||
106 | return false; |
||
107 | } |
||
108 | $triggers = []; |
||
109 | foreach ($this->driver->triggers($table) as $trigger_name => $timing_event) { |
||
110 | $trigger = $this->driver->trigger($trigger_name); |
||
111 | $triggers[] = 'CREATE TRIGGER ' . $this->driver->escapeId($trigger_name) . ' ' . |
||
112 | implode(' ', $timing_event) . ' ON ' . $this->driver->table($name) . "\n$trigger[Statement]"; |
||
113 | } |
||
114 | $autoIncrement = $autoIncrement ? 0 : |
||
115 | $this->connection->result('SELECT seq FROM sqlite_sequence WHERE name = ' . |
||
116 | $this->driver->quote($table)); // if $autoIncrement is set then it will be updated later |
||
117 | // drop before creating indexes and triggers to allow using old names |
||
118 | if (!$this->driver->execute('DROP TABLE ' . $this->driver->table($table)) || |
||
119 | ($table == $name && !$this->driver->execute('ALTER TABLE ' . $this->driver->table($tempName) . |
||
120 | ' RENAME TO ' . $this->driver->table($name))) || !$this->alterIndexes($name, $indexes) |
||
121 | ) { |
||
122 | return false; |
||
123 | } |
||
124 | if ($autoIncrement) { |
||
125 | $this->driver->execute('UPDATE sqlite_sequence SET seq = $autoIncrement WHERE name = ' . $this->driver->quote($name)); // ignores error |
||
126 | } |
||
127 | foreach ($triggers as $trigger) { |
||
128 | if (!$this->driver->execute($trigger)) { |
||
129 | return false; |
||
130 | } |
||
131 | } |
||
132 | $this->driver->execute('COMMIT'); |
||
133 | } |
||
134 | return true; |
||
135 | } |
||
286 |