| Total Complexity | 74 |
| Total Lines | 599 |
| Duplicated Lines | 7.51 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AlldbController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AlldbController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class AlldbController extends BaseController |
||
| 11 | { |
||
| 12 | public $_name = 'AlldbController'; |
||
| 13 | public $table_place = 'alldb-databases'; |
||
| 14 | |||
| 15 | public function render() |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Show default list of databases in the server |
||
| 77 | */ |
||
| 78 | public function doDefault($msg = '') |
||
| 79 | { |
||
| 80 | $conf = $this->conf; |
||
| 81 | $misc = $this->misc; |
||
| 82 | $lang = $this->lang; |
||
| 83 | |||
| 84 | $this->printTrail('server'); |
||
| 85 | $this->printTabs('server', 'databases'); |
||
| 86 | $this->printMsg($msg); |
||
| 87 | $data = $misc->getDatabaseAccessor(); |
||
| 88 | $databases = $data->getDatabases(); |
||
| 89 | |||
| 90 | $columns = [ |
||
| 91 | 'database' => [ |
||
| 92 | 'title' => $lang['strdatabase'], |
||
| 93 | 'field' => Decorator::field('datname'), |
||
| 94 | 'url' => SUBFOLDER . "/redirect/database?{$misc->href}&", |
||
| 95 | 'vars' => ['database' => 'datname'], |
||
| 96 | ], |
||
| 97 | 'owner' => [ |
||
| 98 | 'title' => $lang['strowner'], |
||
| 99 | 'field' => Decorator::field('datowner'), |
||
| 100 | ], |
||
| 101 | 'encoding' => [ |
||
| 102 | 'title' => $lang['strencoding'], |
||
| 103 | 'field' => Decorator::field('datencoding'), |
||
| 104 | ], |
||
| 105 | 'lc_collate' => [ |
||
| 106 | 'title' => $lang['strcollation'], |
||
| 107 | 'field' => Decorator::field('datcollate'), |
||
| 108 | ], |
||
| 109 | 'lc_ctype' => [ |
||
| 110 | 'title' => $lang['strctype'], |
||
| 111 | 'field' => Decorator::field('datctype'), |
||
| 112 | ], |
||
| 113 | 'tablespace' => [ |
||
| 114 | 'title' => $lang['strtablespace'], |
||
| 115 | 'field' => Decorator::field('tablespace'), |
||
| 116 | ], |
||
| 117 | 'dbsize' => [ |
||
| 118 | 'title' => $lang['strsize'], |
||
| 119 | 'field' => Decorator::field('dbsize'), |
||
| 120 | 'type' => 'prettysize', |
||
| 121 | ], |
||
| 122 | 'actions' => [ |
||
| 123 | 'title' => $lang['stractions'], |
||
| 124 | ], |
||
| 125 | 'comment' => [ |
||
| 126 | 'title' => $lang['strcomment'], |
||
| 127 | 'field' => Decorator::field('datcomment'), |
||
| 128 | ], |
||
| 129 | ]; |
||
| 130 | |||
| 131 | $actions = [ |
||
| 132 | 'multiactions' => [ |
||
| 133 | 'keycols' => ['database' => 'datname'], |
||
| 134 | 'url' => 'alldb.php', |
||
| 135 | 'default' => null, |
||
| 136 | ], |
||
| 137 | 'drop' => [ |
||
| 138 | 'content' => $lang['strdrop'], |
||
| 139 | 'attr' => [ |
||
| 140 | 'href' => [ |
||
| 141 | 'url' => 'alldb.php', |
||
| 142 | 'urlvars' => [ |
||
| 143 | 'subject' => 'database', |
||
| 144 | 'action' => 'confirm_drop', |
||
| 145 | 'dropdatabase' => Decorator::field('datname'), |
||
| 146 | ], |
||
| 147 | ], |
||
| 148 | ], |
||
| 149 | 'multiaction' => 'confirm_drop', |
||
| 150 | ], |
||
| 151 | 'privileges' => [ |
||
| 152 | 'content' => $lang['strprivileges'], |
||
| 153 | 'attr' => [ |
||
| 154 | 'href' => [ |
||
| 155 | 'url' => 'privileges.php', |
||
| 156 | 'urlvars' => [ |
||
| 157 | 'subject' => 'database', |
||
| 158 | 'database' => Decorator::field('datname'), |
||
| 159 | ], |
||
| 160 | ], |
||
| 161 | ], |
||
| 162 | ], |
||
| 163 | ]; |
||
| 164 | if ($data->hasAlterDatabase()) { |
||
| 165 | $actions['alter'] = [ |
||
| 166 | 'content' => $lang['stralter'], |
||
| 167 | 'attr' => [ |
||
| 168 | 'href' => [ |
||
| 169 | 'url' => 'alldb.php', |
||
| 170 | 'urlvars' => [ |
||
| 171 | 'subject' => 'database', |
||
| 172 | 'action' => 'confirm_alter', |
||
| 173 | 'alterdatabase' => Decorator::field('datname'), |
||
| 174 | ], |
||
| 175 | ], |
||
| 176 | ], |
||
| 177 | ]; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (!$data->hasTablespaces()) { |
||
| 181 | unset($columns['tablespace']); |
||
| 182 | } |
||
| 183 | |||
| 184 | if (!$data->hasServerAdminFuncs()) { |
||
| 185 | unset($columns['dbsize']); |
||
| 186 | } |
||
| 187 | |||
| 188 | if (!$data->hasDatabaseCollation()) { |
||
| 189 | unset($columns['lc_collate'], $columns['lc_ctype']); |
||
| 190 | } |
||
| 191 | |||
| 192 | if (!isset($data->privlist['database'])) { |
||
| 193 | unset($actions['privileges']); |
||
| 194 | } |
||
| 195 | |||
| 196 | echo $this->printTable($databases, $columns, $actions, $this->table_place, $lang['strnodatabases']); |
||
| 197 | |||
| 198 | $navlinks = [ |
||
| 199 | 'create' => [ |
||
| 200 | 'attr' => [ |
||
| 201 | 'href' => [ |
||
| 202 | 'url' => 'alldb.php', |
||
| 203 | 'urlvars' => [ |
||
| 204 | 'action' => 'create', |
||
| 205 | 'server' => $_REQUEST['server'], |
||
| 206 | ], |
||
| 207 | ], |
||
| 208 | ], |
||
| 209 | 'content' => $lang['strcreatedatabase'], |
||
| 210 | ], |
||
| 211 | ]; |
||
| 212 | $this->printNavLinks($navlinks, $this->table_place, get_defined_vars()); |
||
| 213 | } |
||
| 214 | |||
| 215 | View Code Duplication | public function doTree() |
|
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Display a form for alter and perform actual alter |
||
| 242 | */ |
||
| 243 | public function doAlter($confirm) |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * Show confirmation of drop and perform actual drop |
||
| 314 | */ |
||
| 315 | public function doDrop($confirm) |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } //END DROP |
||
| 375 | } |
||
| 376 | |||
| 377 | // END FUNCTION |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Displays a screen where they can enter a new database |
||
| 381 | */ |
||
| 382 | public function doCreate($msg = '') |
||
| 383 | { |
||
| 384 | $conf = $this->conf; |
||
| 385 | $misc = $this->misc; |
||
| 386 | $lang = $this->lang; |
||
| 387 | $data = $misc->getDatabaseAccessor(); |
||
| 388 | |||
| 389 | $this->printTrail('server'); |
||
| 390 | $this->printTitle($lang['strcreatedatabase'], 'pg.database.create'); |
||
| 391 | $this->printMsg($msg); |
||
| 392 | |||
| 393 | if (!isset($_POST['formName'])) { |
||
| 394 | $_POST['formName'] = ''; |
||
| 395 | } |
||
| 396 | |||
| 397 | // Default encoding is that in language file |
||
| 398 | if (!isset($_POST['formEncoding'])) { |
||
| 399 | $_POST['formEncoding'] = ''; |
||
| 400 | } |
||
| 401 | if (!isset($_POST['formTemplate'])) { |
||
| 402 | $_POST['formTemplate'] = 'template1'; |
||
| 403 | } |
||
| 404 | |||
| 405 | if (!isset($_POST['formSpc'])) { |
||
| 406 | $_POST['formSpc'] = ''; |
||
| 407 | } |
||
| 408 | |||
| 409 | if (!isset($_POST['formComment'])) { |
||
| 410 | $_POST['formComment'] = ''; |
||
| 411 | } |
||
| 412 | |||
| 413 | // Fetch a list of databases in the cluster |
||
| 414 | $templatedbs = $data->getDatabases(false); |
||
| 415 | |||
| 416 | // Fetch all tablespaces from the database |
||
| 417 | if ($data->hasTablespaces()) { |
||
| 418 | $tablespaces = $data->getTablespaces(); |
||
| 419 | } |
||
| 420 | |||
| 421 | echo '<form action="' . SUBFOLDER . "/src/views/alldb.php\" method=\"post\">\n"; |
||
| 422 | echo "<table>\n"; |
||
| 423 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strname']}</th>\n"; |
||
| 424 | echo "\t\t<td class=\"data1\"><input name=\"formName\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" value=\"", |
||
| 425 | htmlspecialchars($_POST['formName']), "\" /></td>\n\t</tr>\n"; |
||
| 426 | |||
| 427 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strtemplatedb']}</th>\n"; |
||
| 428 | echo "\t\t<td class=\"data1\">\n"; |
||
| 429 | echo "\t\t\t<select name=\"formTemplate\">\n"; |
||
| 430 | // Always offer template0 and template1 |
||
| 431 | echo "\t\t\t\t<option value=\"template0\"", |
||
| 432 | ($_POST['formTemplate'] == 'template0') ? ' selected="selected"' : '', ">template0</option>\n"; |
||
| 433 | echo "\t\t\t\t<option value=\"template1\"", |
||
| 434 | ($_POST['formTemplate'] == 'template1') ? ' selected="selected"' : '', ">template1</option>\n"; |
||
| 435 | while (!$templatedbs->EOF) { |
||
| 436 | $dbname = htmlspecialchars($templatedbs->fields['datname']); |
||
| 437 | if ($dbname != 'template1') { |
||
| 438 | // filter out for $conf[show_system] users so we dont get duplicates |
||
| 439 | echo "\t\t\t\t<option value=\"{$dbname}\"", |
||
| 440 | ($dbname == $_POST['formTemplate']) ? ' selected="selected"' : '', ">{$dbname}</option>\n"; |
||
| 441 | } |
||
| 442 | $templatedbs->moveNext(); |
||
| 443 | } |
||
| 444 | echo "\t\t\t</select>\n"; |
||
| 445 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 446 | |||
| 447 | // ENCODING |
||
| 448 | echo "\t<tr>\n\t\t<th class=\"data left required\">{$lang['strencoding']}</th>\n"; |
||
| 449 | echo "\t\t<td class=\"data1\">\n"; |
||
| 450 | echo "\t\t\t<select name=\"formEncoding\">\n"; |
||
| 451 | echo "\t\t\t\t<option value=\"\"></option>\n"; |
||
| 452 | while (list($key) = each($data->codemap)) { |
||
| 453 | echo "\t\t\t\t<option value=\"", htmlspecialchars($key), '"', |
||
| 454 | ($key == $_POST['formEncoding']) ? ' selected="selected"' : '', '>', |
||
| 455 | $misc->printVal($key), "</option>\n"; |
||
| 456 | } |
||
| 457 | echo "\t\t\t</select>\n"; |
||
| 458 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 459 | |||
| 460 | if ($data->hasDatabaseCollation()) { |
||
| 461 | if (!isset($_POST['formCollate'])) { |
||
| 462 | $_POST['formCollate'] = ''; |
||
| 463 | } |
||
| 464 | |||
| 465 | if (!isset($_POST['formCType'])) { |
||
| 466 | $_POST['formCType'] = ''; |
||
| 467 | } |
||
| 468 | |||
| 469 | // LC_COLLATE |
||
| 470 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcollation']}</th>\n"; |
||
| 471 | echo "\t\t<td class=\"data1\">\n"; |
||
| 472 | echo "\t\t\t<input name=\"formCollate\" value=\"", htmlspecialchars($_POST['formCollate']), "\" />\n"; |
||
| 473 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 474 | |||
| 475 | // LC_CTYPE |
||
| 476 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strctype']}</th>\n"; |
||
| 477 | echo "\t\t<td class=\"data1\">\n"; |
||
| 478 | echo "\t\t\t<input name=\"formCType\" value=\"", htmlspecialchars($_POST['formCType']), "\" />\n"; |
||
| 479 | echo "\t\t</td>\n\t</tr>\n"; |
||
| 480 | } |
||
| 481 | |||
| 482 | // Tablespace (if there are any) |
||
| 483 | View Code Duplication | if ($data->hasTablespaces() && $tablespaces->recordCount() > 0) { |
|
| 484 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strtablespace']}</th>\n"; |
||
| 485 | echo "\t\t<td class=\"data1\">\n\t\t\t<select name=\"formSpc\">\n"; |
||
| 486 | // Always offer the default (empty) option |
||
| 487 | echo "\t\t\t\t<option value=\"\"", |
||
| 488 | ($_POST['formSpc'] == '') ? ' selected="selected"' : '', "></option>\n"; |
||
| 489 | // Display all other tablespaces |
||
| 490 | while (!$tablespaces->EOF) { |
||
| 491 | $spcname = htmlspecialchars($tablespaces->fields['spcname']); |
||
| 492 | echo "\t\t\t\t<option value=\"{$spcname}\"", |
||
| 493 | ($spcname == $_POST['formSpc']) ? ' selected="selected"' : '', ">{$spcname}</option>\n"; |
||
| 494 | $tablespaces->moveNext(); |
||
| 495 | } |
||
| 496 | echo "\t\t\t</select>\n\t\t</td>\n\t</tr>\n"; |
||
| 497 | } |
||
| 498 | |||
| 499 | // Comments (if available) |
||
| 500 | View Code Duplication | if ($data->hasSharedComments()) { |
|
| 501 | echo "\t<tr>\n\t\t<th class=\"data left\">{$lang['strcomment']}</th>\n"; |
||
| 502 | echo "\t\t<td><textarea name=\"formComment\" rows=\"3\" cols=\"32\">", |
||
| 503 | htmlspecialchars($_POST['formComment']), "</textarea></td>\n\t</tr>\n"; |
||
| 504 | } |
||
| 505 | |||
| 506 | echo "</table>\n"; |
||
| 507 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_create\" />\n"; |
||
| 508 | echo $misc->form; |
||
| 509 | echo "<input type=\"submit\" value=\"{$lang['strcreate']}\" />\n"; |
||
| 510 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 511 | echo "</form>\n"; |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Actually creates the new view in the database |
||
| 516 | */ |
||
| 517 | public function doSaveCreate() |
||
| 518 | { |
||
| 519 | $conf = $this->conf; |
||
| 520 | $misc = $this->misc; |
||
| 521 | $lang = $this->lang; |
||
| 522 | $data = $misc->getDatabaseAccessor(); |
||
| 523 | |||
| 524 | // Default tablespace to null if it isn't set |
||
| 525 | if (!isset($_POST['formSpc'])) { |
||
| 526 | $_POST['formSpc'] = null; |
||
| 527 | } |
||
| 528 | |||
| 529 | // Default comment to blank if it isn't set |
||
| 530 | if (!isset($_POST['formComment'])) { |
||
| 531 | $_POST['formComment'] = null; |
||
| 532 | } |
||
| 533 | |||
| 534 | // Default collate to blank if it isn't set |
||
| 535 | if (!isset($_POST['formCollate'])) { |
||
| 536 | $_POST['formCollate'] = null; |
||
| 537 | } |
||
| 538 | |||
| 539 | // Default ctype to blank if it isn't set |
||
| 540 | if (!isset($_POST['formCType'])) { |
||
| 541 | $_POST['formCType'] = null; |
||
| 542 | } |
||
| 543 | |||
| 544 | // Check that they've given a name and a definition |
||
| 545 | if ($_POST['formName'] == '') { |
||
| 546 | $this->doCreate($lang['strdatabaseneedsname']); |
||
| 547 | } else { |
||
| 548 | $status = $data->createDatabase($_POST['formName'], $_POST['formEncoding'], $_POST['formSpc'], |
||
| 549 | $_POST['formComment'], $_POST['formTemplate'], $_POST['formCollate'], $_POST['formCType']); |
||
| 550 | if ($status == 0) { |
||
| 551 | $this->misc->setReloadBrowser(true); |
||
| 552 | $this->doDefault($lang['strdatabasecreated']); |
||
| 553 | } else { |
||
| 554 | $this->doCreate($lang['strdatabasecreatedbad']); |
||
| 555 | } |
||
| 556 | } |
||
| 557 | } |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Displays options for cluster download |
||
| 561 | */ |
||
| 562 | public function doExport($msg = '') |
||
| 609 | } |
||
| 610 | } |
||
| 611 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: