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