| 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 |
||
| 156 | public function create_table($table, $definition, $index=array()) |
||
| 157 | { |
||
| 158 | $create_sql = "CREATE TABLE $table ("; |
||
| 159 | if (!is_array($definition)) { |
||
| 160 | throw new KumbiaException("Definición inválida para crear la tabla '$table'"); |
||
| 161 | } |
||
| 162 | $create_lines = array(); |
||
| 163 | $index = array(); |
||
| 164 | $unique_index = array(); |
||
| 165 | $primary = array(); |
||
| 166 | //$not_null = ""; |
||
| 167 | //$size = ""; |
||
| 168 | foreach ($definition as $field => $field_def) { |
||
| 169 | if (isset($field_def['not_null'])) { |
||
| 170 | $not_null = $field_def['not_null'] ? 'NOT NULL' : ''; |
||
| 171 | } else { |
||
| 172 | $not_null = ""; |
||
| 173 | } |
||
| 174 | if (isset($field_def['size'])) { |
||
| 175 | $size = $field_def['size'] ? '(' . $field_def['size'] . ')' : ''; |
||
| 176 | } else { |
||
| 177 | $size = ""; |
||
| 178 | } |
||
| 179 | if (isset($field_def['index'])) { |
||
| 180 | if ($field_def['index']) { |
||
| 181 | $index[] = "INDEX($field)"; |
||
| 182 | } |
||
| 183 | } |
||
| 184 | if (isset($field_def['unique_index'])) { |
||
| 185 | if ($field_def['unique_index']) { |
||
| 186 | $index[] = "UNIQUE($field)"; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | if (isset($field_def['primary'])) { |
||
| 190 | if ($field_def['primary']) { |
||
| 191 | $primary[] = "$field"; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | if (isset($field_def['auto'])) { |
||
| 195 | if ($field_def['auto']) { |
||
| 196 | $field_def['type'] = "SERIAL"; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | if (isset($field_def['extra'])) { |
||
| 200 | $extra = $field_def['extra']; |
||
| 201 | } else { |
||
| 202 | $extra = ""; |
||
| 203 | } |
||
| 204 | $create_lines[] = "$field " . $field_def['type'] . $size . ' ' . $not_null . ' ' . $extra; |
||
| 205 | } |
||
| 206 | $create_sql.= join(',', $create_lines); |
||
| 207 | $last_lines = array(); |
||
| 208 | if (count($primary)) { |
||
| 209 | $last_lines[] = 'PRIMARY KEY(' . join(",", $primary) . ')'; |
||
| 210 | } |
||
| 211 | if (count($index)) { |
||
| 212 | $last_lines[] = join(',', $index); |
||
| 213 | } |
||
| 214 | if (count($unique_index)) { |
||
| 215 | $last_lines[] = join(',', $unique_index); |
||
| 216 | } |
||
| 217 | if (count($last_lines)) { |
||
| 218 | $create_sql.= ',' . join(',', $last_lines) . ')'; |
||
| 219 | } |
||
| 220 | return $this->query($create_sql); |
||
| 221 | } |
||
| 222 | |||
| 286 |