| Conditions | 18 |
| Paths | 3552 |
| Total Lines | 219 |
| Code Lines | 135 |
| Lines | 9 |
| Ratio | 4.11 % |
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 |
||
| 346 | function buildForm() { |
||
| 347 | $context = $this->mContext; |
||
| 348 | $user = $context->getUser(); |
||
| 349 | $output = $context->getOutput(); |
||
| 350 | $lang = $context->getLanguage(); |
||
| 351 | $cascadingRestrictionLevels = $context->getConfig()->get( 'CascadingRestrictionLevels' ); |
||
| 352 | $out = ''; |
||
| 353 | if ( !$this->disabled ) { |
||
| 354 | $output->addModules( 'mediawiki.legacy.protect' ); |
||
| 355 | $output->addJsConfigVars( 'wgCascadeableLevels', $cascadingRestrictionLevels ); |
||
| 356 | $out .= Xml::openElement( 'form', [ 'method' => 'post', |
||
| 357 | 'action' => $this->mTitle->getLocalURL( 'action=protect' ), |
||
| 358 | 'id' => 'mw-Protect-Form' ] ); |
||
| 359 | } |
||
| 360 | |||
| 361 | $out .= Xml::openElement( 'fieldset' ) . |
||
| 362 | Xml::element( 'legend', null, $context->msg( 'protect-legend' )->text() ) . |
||
| 363 | Xml::openElement( 'table', [ 'id' => 'mwProtectSet' ] ) . |
||
| 364 | Xml::openElement( 'tbody' ); |
||
| 365 | |||
| 366 | $scExpiryOptions = wfMessage( 'protect-expiry-options' )->inContentLanguage()->text(); |
||
| 367 | $showProtectOptions = $scExpiryOptions !== '-' && !$this->disabled; |
||
| 368 | |||
| 369 | // Not all languages have V_x <-> N_x relation |
||
| 370 | foreach ( $this->mRestrictions as $action => $selected ) { |
||
| 371 | // Messages: |
||
| 372 | // restriction-edit, restriction-move, restriction-create, restriction-upload |
||
| 373 | $msg = $context->msg( 'restriction-' . $action ); |
||
| 374 | $out .= "<tr><td>" . |
||
| 375 | Xml::openElement( 'fieldset' ) . |
||
| 376 | Xml::element( 'legend', null, $msg->exists() ? $msg->text() : $action ) . |
||
| 377 | Xml::openElement( 'table', [ 'id' => "mw-protect-table-$action" ] ) . |
||
| 378 | "<tr><td>" . $this->buildSelector( $action, $selected ) . "</td></tr><tr><td>"; |
||
| 379 | |||
| 380 | $mProtectexpiry = Xml::label( |
||
| 381 | $context->msg( 'protectexpiry' )->text(), |
||
| 382 | "mwProtectExpirySelection-$action" |
||
| 383 | ); |
||
| 384 | $mProtectother = Xml::label( |
||
| 385 | $context->msg( 'protect-othertime' )->text(), |
||
| 386 | "mwProtect-$action-expires" |
||
| 387 | ); |
||
| 388 | |||
| 389 | $expiryFormOptions = new XmlSelect( |
||
| 390 | "wpProtectExpirySelection-$action", |
||
| 391 | "mwProtectExpirySelection-$action", |
||
| 392 | $this->mExpirySelection[$action] |
||
| 393 | ); |
||
| 394 | $expiryFormOptions->setAttribute( 'tabindex', '2' ); |
||
| 395 | if ( $this->disabled ) { |
||
| 396 | $expiryFormOptions->setAttribute( 'disabled', 'disabled' ); |
||
| 397 | } |
||
| 398 | |||
| 399 | if ( $this->mExistingExpiry[$action] ) { |
||
| 400 | if ( $this->mExistingExpiry[$action] == 'infinity' ) { |
||
| 401 | $existingExpiryMessage = $context->msg( 'protect-existing-expiry-infinity' ); |
||
| 402 | } else { |
||
| 403 | $timestamp = $lang->userTimeAndDate( $this->mExistingExpiry[$action], $user ); |
||
| 404 | $d = $lang->userDate( $this->mExistingExpiry[$action], $user ); |
||
| 405 | $t = $lang->userTime( $this->mExistingExpiry[$action], $user ); |
||
| 406 | $existingExpiryMessage = $context->msg( |
||
| 407 | 'protect-existing-expiry', |
||
| 408 | $timestamp, |
||
| 409 | $d, |
||
| 410 | $t |
||
| 411 | ); |
||
| 412 | } |
||
| 413 | $expiryFormOptions->addOption( $existingExpiryMessage->text(), 'existing' ); |
||
| 414 | } |
||
| 415 | |||
| 416 | $expiryFormOptions->addOption( |
||
| 417 | $context->msg( 'protect-othertime-op' )->text(), |
||
| 418 | 'othertime' |
||
| 419 | ); |
||
| 420 | foreach ( explode( ',', $scExpiryOptions ) as $option ) { |
||
| 421 | if ( strpos( $option, ":" ) === false ) { |
||
| 422 | $show = $value = $option; |
||
| 423 | } else { |
||
| 424 | list( $show, $value ) = explode( ":", $option ); |
||
| 425 | } |
||
| 426 | $expiryFormOptions->addOption( $show, htmlspecialchars( $value ) ); |
||
| 427 | } |
||
| 428 | # Add expiry dropdown |
||
| 429 | if ( $showProtectOptions && !$this->disabled ) { |
||
| 430 | $out .= " |
||
| 431 | <table><tr> |
||
| 432 | <td class='mw-label'> |
||
| 433 | {$mProtectexpiry} |
||
| 434 | </td> |
||
| 435 | <td class='mw-input'>" . |
||
| 436 | $expiryFormOptions->getHTML() . |
||
| 437 | "</td> |
||
| 438 | </tr></table>"; |
||
| 439 | } |
||
| 440 | # Add custom expiry field |
||
| 441 | $attribs = [ 'id' => "mwProtect-$action-expires" ] + $this->disabledAttrib; |
||
| 442 | $out .= "<table><tr> |
||
| 443 | <td class='mw-label'>" . |
||
| 444 | $mProtectother . |
||
| 445 | '</td> |
||
| 446 | <td class="mw-input">' . |
||
| 447 | Xml::input( "mwProtect-expiry-$action", 50, $this->mExpiry[$action], $attribs ) . |
||
| 448 | '</td> |
||
| 449 | </tr></table>'; |
||
| 450 | $out .= "</td></tr>" . |
||
| 451 | Xml::closeElement( 'table' ) . |
||
| 452 | Xml::closeElement( 'fieldset' ) . |
||
| 453 | "</td></tr>"; |
||
| 454 | } |
||
| 455 | # Give extensions a chance to add items to the form |
||
| 456 | Hooks::run( 'ProtectionForm::buildForm', [ $this->mArticle, &$out ] ); |
||
| 457 | |||
| 458 | $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); |
||
| 459 | |||
| 460 | // JavaScript will add another row with a value-chaining checkbox |
||
| 461 | if ( $this->mTitle->exists() ) { |
||
| 462 | $out .= Xml::openElement( 'table', [ 'id' => 'mw-protect-table2' ] ) . |
||
| 463 | Xml::openElement( 'tbody' ); |
||
| 464 | $out .= '<tr> |
||
| 465 | <td></td> |
||
| 466 | <td class="mw-input">' . |
||
| 467 | Xml::checkLabel( |
||
| 468 | $context->msg( 'protect-cascade' )->text(), |
||
| 469 | 'mwProtect-cascade', |
||
| 470 | 'mwProtect-cascade', |
||
| 471 | $this->mCascade, $this->disabledAttrib |
||
| 472 | ) . |
||
| 473 | "</td> |
||
| 474 | </tr>\n"; |
||
| 475 | $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); |
||
| 476 | } |
||
| 477 | |||
| 478 | # Add manual and custom reason field/selects as well as submit |
||
| 479 | if ( !$this->disabled ) { |
||
| 480 | $mProtectreasonother = Xml::label( |
||
| 481 | $context->msg( 'protectcomment' )->text(), |
||
| 482 | 'wpProtectReasonSelection' |
||
| 483 | ); |
||
| 484 | |||
| 485 | $mProtectreason = Xml::label( |
||
| 486 | $context->msg( 'protect-otherreason' )->text(), |
||
| 487 | 'mwProtect-reason' |
||
| 488 | ); |
||
| 489 | |||
| 490 | $reasonDropDown = Xml::listDropDown( 'wpProtectReasonSelection', |
||
| 491 | wfMessage( 'protect-dropdown' )->inContentLanguage()->text(), |
||
| 492 | wfMessage( 'protect-otherreason-op' )->inContentLanguage()->text(), |
||
| 493 | $this->mReasonSelection, |
||
| 494 | 'mwProtect-reason', 4 ); |
||
| 495 | |||
| 496 | $out .= Xml::openElement( 'table', [ 'id' => 'mw-protect-table3' ] ) . |
||
| 497 | Xml::openElement( 'tbody' ); |
||
| 498 | $out .= " |
||
| 499 | <tr> |
||
| 500 | <td class='mw-label'> |
||
| 501 | {$mProtectreasonother} |
||
| 502 | </td> |
||
| 503 | <td class='mw-input'> |
||
| 504 | {$reasonDropDown} |
||
| 505 | </td> |
||
| 506 | </tr> |
||
| 507 | <tr> |
||
| 508 | <td class='mw-label'> |
||
| 509 | {$mProtectreason} |
||
| 510 | </td> |
||
| 511 | <td class='mw-input'>" . |
||
| 512 | Xml::input( 'mwProtect-reason', 60, $this->mReason, [ 'type' => 'text', |
||
| 513 | 'id' => 'mwProtect-reason', 'maxlength' => 180 ] ) . |
||
| 514 | // Limited maxlength as the database trims at 255 bytes and other texts |
||
| 515 | // chosen by dropdown menus on this page are also included in this database field. |
||
| 516 | // The byte limit of 180 bytes is enforced in javascript |
||
| 517 | "</td> |
||
| 518 | </tr>"; |
||
| 519 | # Disallow watching is user is not logged in |
||
| 520 | if ( $user->isLoggedIn() ) { |
||
| 521 | $out .= " |
||
| 522 | <tr> |
||
| 523 | <td></td> |
||
| 524 | <td class='mw-input'>" . |
||
| 525 | Xml::checkLabel( $context->msg( 'watchthis' )->text(), |
||
| 526 | 'mwProtectWatch', 'mwProtectWatch', |
||
| 527 | $user->isWatched( $this->mTitle ) || $user->getOption( 'watchdefault' ) ) . |
||
| 528 | "</td> |
||
| 529 | </tr>"; |
||
| 530 | } |
||
| 531 | $out .= " |
||
| 532 | <tr> |
||
| 533 | <td></td> |
||
| 534 | <td class='mw-submit'>" . |
||
| 535 | Xml::submitButton( |
||
| 536 | $context->msg( 'confirm' )->text(), |
||
| 537 | [ 'id' => 'mw-Protect-submit' ] |
||
| 538 | ) . |
||
| 539 | "</td> |
||
| 540 | </tr>\n"; |
||
| 541 | $out .= Xml::closeElement( 'tbody' ) . Xml::closeElement( 'table' ); |
||
| 542 | } |
||
| 543 | $out .= Xml::closeElement( 'fieldset' ); |
||
| 544 | |||
| 545 | View Code Duplication | if ( $user->isAllowed( 'editinterface' ) ) { |
|
| 546 | $link = Linker::linkKnown( |
||
| 547 | $context->msg( 'protect-dropdown' )->inContentLanguage()->getTitle(), |
||
| 548 | $context->msg( 'protect-edit-reasonlist' )->escaped(), |
||
| 549 | [], |
||
| 550 | [ 'action' => 'edit' ] |
||
| 551 | ); |
||
| 552 | $out .= '<p class="mw-protect-editreasons">' . $link . '</p>'; |
||
| 553 | } |
||
| 554 | |||
| 555 | if ( !$this->disabled ) { |
||
| 556 | $out .= Html::hidden( |
||
| 557 | 'wpEditToken', |
||
| 558 | $user->getEditToken( [ 'protect', $this->mTitle->getPrefixedDBkey() ] ) |
||
| 559 | ); |
||
| 560 | $out .= Xml::closeElement( 'form' ); |
||
| 561 | } |
||
| 562 | |||
| 563 | return $out; |
||
| 564 | } |
||
| 565 | |||
| 629 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: