| Conditions | 40 |
| Paths | 564 |
| Total Lines | 233 |
| Code Lines | 158 |
| Lines | 12 |
| Ratio | 5.15 % |
| 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 |
||
| 237 | public function addForeignKey($stage, $msg = '') |
||
| 238 | { |
||
| 239 | $conf = $this->conf; |
||
| 240 | $misc = $this->misc; |
||
| 241 | $lang = $this->lang; |
||
| 242 | $data = $misc->getDatabaseAccessor(); |
||
| 243 | |||
| 244 | if (!isset($_POST['name'])) { |
||
| 245 | $_POST['name'] = ''; |
||
| 246 | } |
||
| 247 | |||
| 248 | if (!isset($_POST['target'])) { |
||
| 249 | $_POST['target'] = ''; |
||
| 250 | } |
||
| 251 | |||
| 252 | switch ($stage) { |
||
| 253 | case 2: |
||
| 254 | // Check that they've given at least one source column |
||
| 255 | if (!isset($_REQUEST['SourceColumnList']) && (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) || sizeof($_POST['IndexColumnList']) == 0)) { |
||
| 256 | $this->addForeignKey(1, $lang['strfkneedscols']); |
||
| 257 | } else { |
||
| 258 | // Copy the IndexColumnList variable from stage 1 |
||
| 259 | if (isset($_REQUEST['IndexColumnList']) && !isset($_REQUEST['SourceColumnList'])) { |
||
| 260 | $_REQUEST['SourceColumnList'] = serialize($_REQUEST['IndexColumnList']); |
||
| 261 | } |
||
| 262 | |||
| 263 | // Initialise variables |
||
| 264 | if (!isset($_POST['upd_action'])) { |
||
| 265 | $_POST['upd_action'] = null; |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!isset($_POST['del_action'])) { |
||
| 269 | $_POST['del_action'] = null; |
||
| 270 | } |
||
| 271 | |||
| 272 | if (!isset($_POST['match'])) { |
||
| 273 | $_POST['match'] = null; |
||
| 274 | } |
||
| 275 | |||
| 276 | if (!isset($_POST['deferrable'])) { |
||
| 277 | $_POST['deferrable'] = null; |
||
| 278 | } |
||
| 279 | |||
| 280 | if (!isset($_POST['initially'])) { |
||
| 281 | $_POST['initially'] = null; |
||
| 282 | } |
||
| 283 | |||
| 284 | $_REQUEST['target'] = unserialize($_REQUEST['target']); |
||
| 285 | |||
| 286 | $this->printTrail('table'); |
||
| 287 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 288 | $this->printMsg($msg); |
||
| 289 | |||
| 290 | // Unserialize target and fetch appropriate table. This is a bit messy |
||
| 291 | // because the table could be in another schema. |
||
| 292 | $data->setSchema($_REQUEST['target']['schemaname']); |
||
| 293 | $attrs = $data->getTableAttributes($_REQUEST['target']['tablename']); |
||
| 294 | $data->setSchema($_REQUEST['schema']); |
||
| 295 | |||
| 296 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 297 | $selColumns->set_style('width: 15em;'); |
||
| 298 | |||
| 299 | View Code Duplication | if ($attrs->recordCount() > 0) { |
|
| 300 | while (!$attrs->EOF) { |
||
| 301 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 302 | $attrs->moveNext(); |
||
| 303 | } |
||
| 304 | } |
||
| 305 | |||
| 306 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 307 | $selIndex->set_style('width: 15em;'); |
||
| 308 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 309 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 310 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 311 | $buttonAdd->set_attribute('type', 'button'); |
||
| 312 | |||
| 313 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 314 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 315 | $buttonRemove->set_attribute('type', 'button'); |
||
| 316 | |||
| 317 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 318 | |||
| 319 | echo "<table>\n"; |
||
| 320 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 321 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=data>{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 322 | echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n"; |
||
| 323 | echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . '</td>'; |
||
| 324 | echo '<td class="data1">' . $selIndex->fetch() . "</td></tr>\n"; |
||
| 325 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['stractions']}</th></tr>"; |
||
| 326 | echo '<tr>'; |
||
| 327 | echo "<td class=\"data1\" colspan=\"3\">\n"; |
||
| 328 | // ON SELECT actions |
||
| 329 | echo "{$lang['stronupdate']} <select name=\"upd_action\">"; |
||
| 330 | foreach ($data->fkactions as $v) { |
||
| 331 | echo "<option value=\"{$v}\"", ($_POST['upd_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 332 | } |
||
| 333 | |||
| 334 | echo "</select><br />\n"; |
||
| 335 | |||
| 336 | // ON DELETE actions |
||
| 337 | echo "{$lang['strondelete']} <select name=\"del_action\">"; |
||
| 338 | foreach ($data->fkactions as $v) { |
||
| 339 | echo "<option value=\"{$v}\"", ($_POST['del_action'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 340 | } |
||
| 341 | |||
| 342 | echo "</select><br />\n"; |
||
| 343 | |||
| 344 | // MATCH options |
||
| 345 | echo '<select name="match">'; |
||
| 346 | foreach ($data->fkmatches as $v) { |
||
| 347 | echo "<option value=\"{$v}\"", ($_POST['match'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 348 | } |
||
| 349 | |||
| 350 | echo "</select><br />\n"; |
||
| 351 | |||
| 352 | // DEFERRABLE options |
||
| 353 | echo '<select name="deferrable">'; |
||
| 354 | foreach ($data->fkdeferrable as $v) { |
||
| 355 | echo "<option value=\"{$v}\"", ($_POST['deferrable'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 356 | } |
||
| 357 | |||
| 358 | echo "</select><br />\n"; |
||
| 359 | |||
| 360 | // INITIALLY options |
||
| 361 | echo '<select name="initially">'; |
||
| 362 | foreach ($data->fkinitial as $v) { |
||
| 363 | echo "<option value=\"{$v}\"", ($_POST['initially'] == $v) ? ' selected="selected"' : '', ">{$v}</option>\n"; |
||
| 364 | } |
||
| 365 | |||
| 366 | echo "</select>\n"; |
||
| 367 | echo "</td></tr>\n"; |
||
| 368 | echo "</table>\n"; |
||
| 369 | |||
| 370 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 371 | echo $misc->form; |
||
| 372 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 373 | echo '<input type="hidden" name="name" value="', htmlspecialchars($_REQUEST['name']), "\" />\n"; |
||
| 374 | echo '<input type="hidden" name="target" value="', htmlspecialchars(serialize($_REQUEST['target'])), "\" />\n"; |
||
| 375 | echo '<input type="hidden" name="SourceColumnList" value="', htmlspecialchars($_REQUEST['SourceColumnList']), "\" />\n"; |
||
| 376 | echo "<input type=\"hidden\" name=\"stage\" value=\"3\" />\n"; |
||
| 377 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 378 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 379 | echo "</form>\n"; |
||
| 380 | } |
||
| 381 | break; |
||
| 382 | case 3: |
||
| 383 | // Unserialize target |
||
| 384 | $_POST['target'] = unserialize($_POST['target']); |
||
| 385 | |||
| 386 | // Check that they've given at least one column |
||
| 387 | if (isset($_POST['SourceColumnList'])) { |
||
| 388 | $temp = unserialize($_POST['SourceColumnList']); |
||
| 389 | } |
||
| 390 | |||
| 391 | if (!isset($_POST['IndexColumnList']) || !is_array($_POST['IndexColumnList']) |
||
| 392 | || sizeof($_POST['IndexColumnList']) == 0 || !isset($temp) |
||
| 393 | || !is_array($temp) || sizeof($temp) == 0) { |
||
| 394 | $this->addForeignKey(2, $lang['strfkneedscols']); |
||
| 395 | } else { |
||
| 396 | $status = $data->addForeignKey($_POST['table'], $_POST['target']['schemaname'], $_POST['target']['tablename'], |
||
| 397 | unserialize($_POST['SourceColumnList']), $_POST['IndexColumnList'], $_POST['upd_action'], $_POST['del_action'], |
||
| 398 | $_POST['match'], $_POST['deferrable'], $_POST['initially'], $_POST['name']); |
||
| 399 | if ($status == 0) { |
||
| 400 | $this->doDefault($lang['strfkadded']); |
||
| 401 | } else { |
||
| 402 | $this->addForeignKey(2, $lang['strfkaddedbad']); |
||
| 403 | } |
||
| 404 | } |
||
| 405 | break; |
||
| 406 | default: |
||
| 407 | $this->printTrail('table'); |
||
| 408 | $this->printTitle($lang['straddfk'], 'pg.constraint.foreign_key'); |
||
| 409 | $this->printMsg($msg); |
||
| 410 | |||
| 411 | $attrs = $data->getTableAttributes($_REQUEST['table']); |
||
| 412 | $tables = $data->getTables(true); |
||
| 413 | |||
| 414 | $selColumns = new \PHPPgAdmin\XHtml\XHtmlSelect('TableColumnList', true, 10); |
||
| 415 | $selColumns->set_style('width: 15em;'); |
||
| 416 | |||
| 417 | View Code Duplication | if ($attrs->recordCount() > 0) { |
|
| 418 | while (!$attrs->EOF) { |
||
| 419 | $selColumns->add(new \PHPPgAdmin\XHtml\XHtmlOption($attrs->fields['attname'])); |
||
| 420 | $attrs->moveNext(); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | |||
| 424 | $selIndex = new \PHPPgAdmin\XHtml\XHtmlSelect('IndexColumnList[]', true, 10); |
||
| 425 | $selIndex->set_style('width: 15em;'); |
||
| 426 | $selIndex->set_attribute('id', 'IndexColumnList'); |
||
| 427 | $buttonAdd = new \PHPPgAdmin\XHtml\XHtmlButton('add', '>>'); |
||
| 428 | $buttonAdd->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 429 | $buttonAdd->set_attribute('type', 'button'); |
||
| 430 | |||
| 431 | $buttonRemove = new \PHPPgAdmin\XHtml\XHtmlButton('remove', '<<'); |
||
| 432 | $buttonRemove->set_attribute('onclick', 'buttonPressed(this);'); |
||
| 433 | $buttonRemove->set_attribute('type', 'button'); |
||
| 434 | |||
| 435 | echo "<form onsubmit=\"doSelectAll();\" name=\"formIndex\" action=\"constraints.php\" method=\"post\">\n"; |
||
| 436 | |||
| 437 | echo "<table>\n"; |
||
| 438 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strname']}</th></tr>\n"; |
||
| 439 | echo "<tr><td class=\"data1\" colspan=\"3\"><input type=\"text\" name=\"name\" size=\"32\" maxlength=\"{$data->_maxNameLen}\" /></td></tr>\n"; |
||
| 440 | echo "<tr><th class=\"data\">{$lang['strtablecolumnlist']}</th><th class=\"data\"> </th><th class=\"data required\">{$lang['strfkcolumnlist']}</th></tr>\n"; |
||
| 441 | echo '<tr><td class="data1">' . $selColumns->fetch() . "</td>\n"; |
||
| 442 | echo '<td class="data1" style="text-align: center">' . $buttonRemove->fetch() . $buttonAdd->fetch() . "</td>\n"; |
||
| 443 | echo '<td class=data1>' . $selIndex->fetch() . "</td></tr>\n"; |
||
| 444 | echo "<tr><th class=\"data\" colspan=\"3\">{$lang['strfktarget']}</th></tr>"; |
||
| 445 | echo '<tr>'; |
||
| 446 | echo '<td class="data1" colspan="3"><select class="select2" name="target">'; |
||
| 447 | while (!$tables->EOF) { |
||
| 448 | $key = ['schemaname' => $tables->fields['nspname'], 'tablename' => $tables->fields['relname']]; |
||
| 449 | $key = serialize($key); |
||
| 450 | echo '<option value="', htmlspecialchars($key), '">'; |
||
| 451 | if ($tables->fields['nspname'] != $_REQUEST['schema']) { |
||
| 452 | echo htmlspecialchars($tables->fields['nspname']), '.'; |
||
| 453 | } |
||
| 454 | echo htmlspecialchars($tables->fields['relname']), "</option>\n"; |
||
| 455 | $tables->moveNext(); |
||
| 456 | } |
||
| 457 | echo "</select>\n"; |
||
| 458 | echo '</td></tr>'; |
||
| 459 | echo "</table>\n"; |
||
| 460 | |||
| 461 | echo "<p><input type=\"hidden\" name=\"action\" value=\"save_add_foreign_key\" />\n"; |
||
| 462 | echo $misc->form; |
||
| 463 | echo '<input type="hidden" name="table" value="', htmlspecialchars($_REQUEST['table']), "\" />\n"; |
||
| 464 | echo "<input type=\"hidden\" name=\"stage\" value=\"2\" />\n"; |
||
| 465 | echo "<input type=\"submit\" value=\"{$lang['stradd']}\" />\n"; |
||
| 466 | echo "<input type=\"submit\" name=\"cancel\" value=\"{$lang['strcancel']}\" /></p>\n"; |
||
| 467 | echo "</form>\n"; |
||
| 468 | //echo "<script>jQuery('select[name=\"target\"]').select2()</script>"; |
||
| 469 | break; |
||
| 470 | } |
||
| 709 |
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: