| Conditions | 23 |
| Paths | 304 |
| Total Lines | 178 |
| Code Lines | 123 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 228 | public function doAlter($msg = '') |
||
| 229 | { |
||
| 230 | $conf = $this->conf; |
||
| 231 | |||
| 232 | $lang = $this->lang; |
||
| 233 | $data = $this->misc->getDatabaseAccessor(); |
||
| 234 | |||
| 235 | if (!isset($_REQUEST['stage'])) { |
||
| 236 | $_REQUEST['stage'] = 1; |
||
| 237 | } |
||
| 238 | |||
| 239 | $this->prtrace('$_REQUEST', $_REQUEST, 'msg', $msg); |
||
| 240 | |||
| 241 | switch ($_REQUEST['stage']) { |
||
| 242 | case 1: |
||
| 243 | $this->printTrail('column'); |
||
| 244 | $this->printTitle($lang['stralter'], 'pg.column.alter'); |
||
| 245 | $this->printMsg($msg); |
||
| 246 | |||
| 247 | echo '<script src="'.\SUBFOLDER.'/js/tables.js" type="text/javascript"></script>'; |
||
| 248 | echo '<form action="'.\SUBFOLDER."/src/views/colproperties.php\" method=\"post\">\n"; |
||
| 249 | |||
| 250 | // Output table header |
||
| 251 | echo "<table>\n"; |
||
| 252 | echo "<tr><th class=\"data required\">{$lang['strname']}</th>\n"; |
||
| 253 | if ($data->hasAlterColumnType()) { |
||
| 254 | echo "<th class=\"data required\" colspan=\"2\">{$lang['strtype']}</th>\n"; |
||
| 255 | echo "<th class=\"data\">{$lang['strlength']}</th>\n"; |
||
| 256 | } else { |
||
| 257 | echo "<th class=\"data required\">{$lang['strtype']}</th>\n"; |
||
| 258 | } |
||
| 259 | echo "<th class=\"data\">{$lang['strnotnull']}</th>\n<th class=\"data\">{$lang['strdefault']}</th>\n<th class=\"data\">{$lang['strcomment']}</th></tr>\n"; |
||
| 260 | |||
| 261 | $column = $data->getTableAttributes($_REQUEST['table'], $_REQUEST['column']); |
||
| 262 | $column->fields['attnotnull'] = $data->phpBool($column->fields['attnotnull']); |
||
| 263 | |||
| 264 | // Upon first drawing the screen, load the existing column information |
||
| 265 | // from the database. |
||
| 266 | if (!isset($_REQUEST['default'])) { |
||
| 267 | $_REQUEST['field'] = $column->fields['attname']; |
||
| 268 | $_REQUEST['type'] = $column->fields['base_type']; |
||
| 269 | // Check to see if its' an array type... |
||
| 270 | // XXX: HACKY |
||
| 271 | if ('[]' == substr($column->fields['base_type'], strlen($column->fields['base_type']) - 2)) { |
||
| 272 | $_REQUEST['type'] = substr($column->fields['base_type'], 0, strlen($column->fields['base_type']) - 2); |
||
| 273 | $_REQUEST['array'] = '[]'; |
||
| 274 | } else { |
||
| 275 | $_REQUEST['type'] = $column->fields['base_type']; |
||
| 276 | $_REQUEST['array'] = ''; |
||
| 277 | } |
||
| 278 | // To figure out the length, look in the brackets :( |
||
| 279 | // XXX: HACKY |
||
| 280 | if ($column->fields['type'] != $column->fields['base_type'] && preg_match('/\\(([0-9, ]*)\\)/', $column->fields['type'], $bits)) { |
||
| 281 | $_REQUEST['length'] = $bits[1]; |
||
| 282 | } else { |
||
| 283 | $_REQUEST['length'] = ''; |
||
| 284 | } |
||
| 285 | |||
| 286 | $_REQUEST['default'] = $_REQUEST['olddefault'] = $column->fields['adsrc']; |
||
| 287 | if ($column->fields['attnotnull']) { |
||
| 288 | $_REQUEST['notnull'] = 'YES'; |
||
| 289 | } |
||
| 290 | |||
| 291 | $_REQUEST['comment'] = $column->fields['comment']; |
||
| 292 | } |
||
| 293 | |||
| 294 | // Column name |
||
| 295 | echo "<tr><td><input name=\"field\" size=\"16\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 296 | htmlspecialchars($_REQUEST['field']), "\" /></td>\n"; |
||
| 297 | |||
| 298 | // Column type |
||
| 299 | $escaped_predef_types = []; // the JS escaped array elements |
||
| 300 | if ($data->hasAlterColumnType()) { |
||
| 301 | // Fetch all available types |
||
| 302 | $types = $data->getTypes(true, false, true); |
||
| 303 | $types_for_js = []; |
||
| 304 | |||
| 305 | echo "<td><select name=\"type\" id=\"type\" class=\"select2\" onchange=\"checkLengths(document.getElementById('type').value,'');\">"."\n"; |
||
| 306 | while (!$types->EOF) { |
||
| 307 | $typname = $types->fields['typname']; |
||
| 308 | $types_for_js[] = $typname; |
||
| 309 | echo "\t<option value=\"", htmlspecialchars($typname), '"', ($typname == $_REQUEST['type']) ? ' selected="selected"' : '', '>', |
||
| 310 | $this->misc->printVal($typname), "</option>\n"; |
||
| 311 | $types->moveNext(); |
||
| 312 | } |
||
| 313 | echo "</select>\n"; |
||
| 314 | echo "</td>\n"; |
||
| 315 | |||
| 316 | // Output array type selector |
||
| 317 | echo "<td><select name=\"array\">\n"; |
||
| 318 | echo "\t<option value=\"\"", ('' == $_REQUEST['array']) ? ' selected="selected"' : '', "></option>\n"; |
||
| 319 | echo "\t<option value=\"[]\"", ('[]' == $_REQUEST['array']) ? ' selected="selected"' : '', ">[ ]</option>\n"; |
||
| 320 | echo "</select></td>\n"; |
||
| 321 | $predefined_size_types = array_intersect($data->predefined_size_types, $types_for_js); |
||
| 322 | foreach ($predefined_size_types as $value) { |
||
| 323 | $escaped_predef_types[] = "'{$value}'"; |
||
| 324 | } |
||
| 325 | |||
| 326 | echo '<td><input name="length" id="lengths" size="8" value="', |
||
| 327 | htmlspecialchars($_REQUEST['length']), "\" /></td>\n"; |
||
| 328 | } else { |
||
| 329 | // Otherwise draw the read-only type name |
||
| 330 | echo '<td>', $this->misc->printVal($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "</td>\n"; |
||
| 331 | } |
||
| 332 | |||
| 333 | echo '<td><input type="checkbox" name="notnull"', (isset($_REQUEST['notnull'])) ? ' checked="checked"' : '', " /></td>\n"; |
||
| 334 | echo '<td><input name="default" size="20" value="', |
||
| 335 | htmlspecialchars($_REQUEST['default']), "\" /></td>\n"; |
||
| 336 | echo '<td><input name="comment" size="40" value="', |
||
| 337 | htmlspecialchars($_REQUEST['comment']), "\" /></td></tr>\n"; |
||
| 338 | echo "</table>\n"; |
||
| 339 | echo "<p><input type=\"hidden\" name=\"action\" value=\"properties\" />\n"; |
||
| 340 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
| 341 | echo $this->misc->form; |
||
| 342 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 343 | echo '<input type="hidden" name="column" value="', htmlspecialchars($_REQUEST['column']), "\" />\n"; |
||
| 344 | echo '<input type="hidden" name="olddefault" value="', htmlspecialchars($_REQUEST['olddefault']), "\" />\n"; |
||
| 345 | if ($column->fields['attnotnull']) { |
||
| 346 | echo "<input type=\"hidden\" name=\"oldnotnull\" value=\"on\" />\n"; |
||
| 347 | } |
||
| 348 | |||
| 349 | echo '<input type="hidden" name="oldtype" value="', htmlspecialchars($data->formatType($column->fields['type'], $column->fields['atttypmod'])), "\" />\n"; |
||
| 350 | // Add hidden variables to suppress error notices if we don't support altering column type |
||
| 351 | if (!$data->hasAlterColumnType()) { |
||
| 352 | echo '<input type="hidden" name="type" value="', htmlspecialchars($_REQUEST['type']), "\" />\n"; |
||
| 353 | echo '<input type="hidden" name="length" value="', htmlspecialchars($_REQUEST['length']), "\" />\n"; |
||
| 354 | echo '<input type="hidden" name="array" value="', htmlspecialchars($_REQUEST['array']), "\" />\n"; |
||
| 355 | } |
||
| 356 | echo "<input type=\"submit\" value=\"{$lang['stralter']}\" />\n"; |
||
| 357 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 358 | echo "</form>\n"; |
||
| 359 | echo '<script type="text/javascript">predefined_lengths = new Array('.implode(',', $escaped_predef_types).");checkLengths(document.getElementById('type').value,'');</script>\n"; |
||
| 360 | |||
| 361 | break; |
||
| 362 | case 2: |
||
| 363 | // Check inputs |
||
| 364 | if ('' == trim($_REQUEST['field'])) { |
||
| 365 | $_REQUEST['stage'] = 1; |
||
| 366 | $this->doAlter($lang['strcolneedsname']); |
||
| 367 | |||
| 368 | return; |
||
| 369 | } |
||
| 370 | if (!isset($_REQUEST['length'])) { |
||
| 371 | $_REQUEST['length'] = ''; |
||
| 372 | } |
||
| 373 | |||
| 374 | list($status, $sql) = $data->alterColumn( |
||
| 375 | $_REQUEST['table'], |
||
| 376 | $_REQUEST['column'], |
||
| 377 | $_REQUEST['field'], |
||
| 378 | isset($_REQUEST['notnull']), |
||
| 379 | isset($_REQUEST['oldnotnull']), |
||
| 380 | $_REQUEST['default'], |
||
| 381 | $_REQUEST['olddefault'], |
||
| 382 | $_REQUEST['type'], |
||
| 383 | $_REQUEST['length'], |
||
| 384 | $_REQUEST['array'], |
||
| 385 | $_REQUEST['oldtype'], |
||
| 386 | $_REQUEST['comment'] |
||
| 387 | ); |
||
| 388 | |||
| 389 | $this->prtrace('status', $status, 'sql', $sql); |
||
| 390 | if (0 == $status) { |
||
| 391 | if ($_REQUEST['column'] != $_REQUEST['field']) { |
||
| 392 | $_REQUEST['column'] = $_REQUEST['field']; |
||
| 393 | $this->misc->setReloadBrowser(true); |
||
| 394 | } |
||
| 395 | $this->doDefault($sql."<br/>{$lang['strcolumnaltered']}"); |
||
| 396 | } else { |
||
| 397 | $_REQUEST['stage'] = 1; |
||
| 398 | $this->doAlter($sql."<br/>{$lang['strcolumnalteredbad']}"); |
||
| 399 | |||
| 400 | return; |
||
| 401 | } |
||
| 402 | |||
| 403 | break; |
||
| 404 | default: |
||
| 405 | echo "<p>{$lang['strinvalidparam']}</p>\n"; |
||
| 406 | } |
||
| 409 |