| Conditions | 20 |
| Paths | > 20000 |
| Total Lines | 66 |
| Code Lines | 45 |
| Lines | 66 |
| Ratio | 100 % |
| Changes | 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 |
||
| 145 | public function create_table($table, $definition, $index=array()) |
||
| 146 | { |
||
| 147 | $create_sql = "CREATE TABLE $table ("; |
||
| 148 | if (!is_array($definition)) { |
||
| 149 | throw new KumbiaException("Definición inválida para crear la tabla '$table'"); |
||
| 150 | } |
||
| 151 | $create_lines = array(); |
||
| 152 | $index = array(); |
||
| 153 | $unique_index = array(); |
||
| 154 | $primary = array(); |
||
| 155 | //$not_null = ""; |
||
| 156 | //$size = ""; |
||
| 157 | foreach ($definition as $field => $field_def) { |
||
| 158 | if (isset($field_def['not_null'])) { |
||
| 159 | $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; |
||
| 160 | } else { |
||
| 161 | $not_null = ""; |
||
| 162 | } |
||
| 163 | if (isset($field_def['size'])) { |
||
| 164 | $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : ''; |
||
| 165 | } else { |
||
| 166 | $size = ""; |
||
| 167 | } |
||
| 168 | if (isset($field_def['index'])) { |
||
| 169 | if ($field_def['index']) { |
||
| 170 | $index[] = "INDEX($field)"; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | if (isset($field_def['unique_index'])) { |
||
| 174 | if ($field_def['unique_index']) { |
||
| 175 | $index[] = "UNIQUE($field)"; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | if (isset($field_def['primary'])) { |
||
| 179 | if ($field_def['primary']) { |
||
| 180 | $primary[] = "$field"; |
||
| 181 | } |
||
| 182 | } |
||
| 183 | if (isset($field_def['auto'])) { |
||
| 184 | if ($field_def['auto']) { |
||
| 185 | $this->query("CREATE SEQUENCE {$table}_{$field}_seq START WITH 1"); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | if (isset($field_def['extra'])) { |
||
| 189 | $extra = $field_def['extra']; |
||
| 190 | } else { |
||
| 191 | $extra = ""; |
||
| 192 | } |
||
| 193 | $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra; |
||
| 194 | } |
||
| 195 | $create_sql.= join(',', $create_lines); |
||
| 196 | $last_lines = array(); |
||
| 197 | if (count($primary)) { |
||
| 198 | $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')'; |
||
| 199 | } |
||
| 200 | if (count($index)) { |
||
| 201 | $last_lines[] = join(',', $index); |
||
| 202 | } |
||
| 203 | if (count($unique_index)) { |
||
| 204 | $last_lines[] = join(',', $unique_index); |
||
| 205 | } |
||
| 206 | if (count($last_lines)) { |
||
| 207 | $create_sql.= ',' . join(',', $last_lines) . ')'; |
||
| 208 | } |
||
| 209 | return $this->query($create_sql); |
||
| 210 | } |
||
| 211 | |||
| 277 |