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 ProtectedPagesPager 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 ProtectedPagesPager, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 281 | class ProtectedPagesPager extends TablePager { |
||
| 282 | public $mForm, $mConds; |
||
|
|
|||
| 283 | private $type, $level, $namespace, $sizetype, $size, $indefonly, $cascadeonly, $noredirect; |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @var LinkRenderer |
||
| 287 | */ |
||
| 288 | private $linkRenderer; |
||
| 289 | |||
| 290 | /** |
||
| 291 | * @param SpecialProtectedpages $form |
||
| 292 | * @param array $conds |
||
| 293 | * @param $type |
||
| 294 | * @param $level |
||
| 295 | * @param $namespace |
||
| 296 | * @param string $sizetype |
||
| 297 | * @param int $size |
||
| 298 | * @param bool $indefonly |
||
| 299 | * @param bool $cascadeonly |
||
| 300 | * @param bool $noredirect |
||
| 301 | * @param LinkRenderer $linkRenderer |
||
| 302 | */ |
||
| 303 | function __construct( $form, $conds = [], $type, $level, $namespace, |
||
| 304 | $sizetype = '', $size = 0, $indefonly = false, $cascadeonly = false, $noredirect = false, |
||
| 305 | LinkRenderer $linkRenderer |
||
| 306 | ) { |
||
| 307 | $this->mForm = $form; |
||
| 308 | $this->mConds = $conds; |
||
| 309 | $this->type = ( $type ) ? $type : 'edit'; |
||
| 310 | $this->level = $level; |
||
| 311 | $this->namespace = $namespace; |
||
| 312 | $this->sizetype = $sizetype; |
||
| 313 | $this->size = intval( $size ); |
||
| 314 | $this->indefonly = (bool)$indefonly; |
||
| 315 | $this->cascadeonly = (bool)$cascadeonly; |
||
| 316 | $this->noredirect = (bool)$noredirect; |
||
| 317 | $this->linkRenderer = $linkRenderer; |
||
| 318 | parent::__construct( $form->getContext() ); |
||
| 319 | } |
||
| 320 | |||
| 321 | function preprocessResults( $result ) { |
||
| 349 | |||
| 350 | function getFieldNames() { |
||
| 351 | static $headers = null; |
||
| 352 | |||
| 353 | if ( $headers == [] ) { |
||
| 354 | $headers = [ |
||
| 355 | 'log_timestamp' => 'protectedpages-timestamp', |
||
| 356 | 'pr_page' => 'protectedpages-page', |
||
| 357 | 'pr_expiry' => 'protectedpages-expiry', |
||
| 358 | 'log_user' => 'protectedpages-performer', |
||
| 359 | 'pr_params' => 'protectedpages-params', |
||
| 360 | 'log_comment' => 'protectedpages-reason', |
||
| 361 | ]; |
||
| 362 | foreach ( $headers as $key => $val ) { |
||
| 363 | $headers[$key] = $this->msg( $val )->text(); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | return $headers; |
||
| 368 | } |
||
| 369 | |||
| 370 | /** |
||
| 371 | * @param string $field |
||
| 372 | * @param string $value |
||
| 373 | * @return string HTML |
||
| 374 | * @throws MWException |
||
| 375 | */ |
||
| 376 | function formatValue( $field, $value ) { |
||
| 377 | /** @var $row object */ |
||
| 378 | $row = $this->mCurrentRow; |
||
| 379 | |||
| 380 | switch ( $field ) { |
||
| 381 | case 'log_timestamp': |
||
| 382 | // when timestamp is null, this is a old protection row |
||
| 383 | if ( $value === null ) { |
||
| 384 | $formatted = Html::rawElement( |
||
| 385 | 'span', |
||
| 386 | [ 'class' => 'mw-protectedpages-unknown' ], |
||
| 387 | $this->msg( 'protectedpages-unknown-timestamp' )->escaped() |
||
| 388 | ); |
||
| 389 | } else { |
||
| 390 | $formatted = htmlspecialchars( $this->getLanguage()->userTimeAndDate( |
||
| 391 | $value, $this->getUser() ) ); |
||
| 392 | } |
||
| 393 | break; |
||
| 394 | |||
| 395 | case 'pr_page': |
||
| 396 | $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
||
| 397 | if ( !$title ) { |
||
| 398 | $formatted = Html::element( |
||
| 399 | 'span', |
||
| 400 | [ 'class' => 'mw-invalidtitle' ], |
||
| 401 | Linker::getInvalidTitleDescription( |
||
| 402 | $this->getContext(), |
||
| 403 | $row->page_namespace, |
||
| 404 | $row->page_title |
||
| 405 | ) |
||
| 406 | ); |
||
| 407 | } else { |
||
| 408 | $formatted = $this->linkRenderer->makeLink( $title ); |
||
| 409 | } |
||
| 410 | if ( !is_null( $row->page_len ) ) { |
||
| 411 | $formatted .= $this->getLanguage()->getDirMark() . |
||
| 412 | ' ' . Html::rawElement( |
||
| 413 | 'span', |
||
| 414 | [ 'class' => 'mw-protectedpages-length' ], |
||
| 415 | Linker::formatRevisionSize( $row->page_len ) |
||
| 416 | ); |
||
| 417 | } |
||
| 418 | break; |
||
| 419 | |||
| 420 | case 'pr_expiry': |
||
| 421 | $formatted = htmlspecialchars( $this->getLanguage()->formatExpiry( |
||
| 422 | $value, /* User preference timezone */true ) ); |
||
| 423 | $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); |
||
| 424 | if ( $this->getUser()->isAllowed( 'protect' ) && $title ) { |
||
| 425 | $changeProtection = $this->linkRenderer->makeKnownLink( |
||
| 426 | $title, |
||
| 427 | $this->msg( 'protect_change' )->text(), |
||
| 428 | [], |
||
| 429 | [ 'action' => 'unprotect' ] |
||
| 430 | ); |
||
| 431 | $formatted .= ' ' . Html::rawElement( |
||
| 432 | 'span', |
||
| 433 | [ 'class' => 'mw-protectedpages-actions' ], |
||
| 434 | $this->msg( 'parentheses' )->rawParams( $changeProtection )->escaped() |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | break; |
||
| 438 | |||
| 439 | case 'log_user': |
||
| 440 | // when timestamp is null, this is a old protection row |
||
| 441 | if ( $row->log_timestamp === null ) { |
||
| 442 | $formatted = Html::rawElement( |
||
| 443 | 'span', |
||
| 444 | [ 'class' => 'mw-protectedpages-unknown' ], |
||
| 445 | $this->msg( 'protectedpages-unknown-performer' )->escaped() |
||
| 446 | ); |
||
| 447 | } else { |
||
| 448 | $username = UserCache::singleton()->getProp( $value, 'name' ); |
||
| 449 | if ( LogEventsList::userCanBitfield( |
||
| 450 | $row->log_deleted, |
||
| 451 | LogPage::DELETED_USER, |
||
| 452 | $this->getUser() |
||
| 453 | ) ) { |
||
| 454 | if ( $username === false ) { |
||
| 455 | $formatted = htmlspecialchars( $value ); |
||
| 456 | } else { |
||
| 457 | $formatted = Linker::userLink( $value, $username ) |
||
| 458 | . Linker::userToolLinks( $value, $username ); |
||
| 459 | } |
||
| 460 | } else { |
||
| 461 | $formatted = $this->msg( 'rev-deleted-user' )->escaped(); |
||
| 462 | } |
||
| 463 | if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) { |
||
| 464 | $formatted = '<span class="history-deleted">' . $formatted . '</span>'; |
||
| 465 | } |
||
| 466 | } |
||
| 467 | break; |
||
| 468 | |||
| 469 | case 'pr_params': |
||
| 470 | $params = []; |
||
| 471 | // Messages: restriction-level-sysop, restriction-level-autoconfirmed |
||
| 472 | $params[] = $this->msg( 'restriction-level-' . $row->pr_level )->escaped(); |
||
| 473 | if ( $row->pr_cascade ) { |
||
| 474 | $params[] = $this->msg( 'protect-summary-cascade' )->escaped(); |
||
| 475 | } |
||
| 476 | $formatted = $this->getLanguage()->commaList( $params ); |
||
| 477 | break; |
||
| 478 | |||
| 479 | case 'log_comment': |
||
| 480 | // when timestamp is null, this is an old protection row |
||
| 481 | if ( $row->log_timestamp === null ) { |
||
| 482 | $formatted = Html::rawElement( |
||
| 483 | 'span', |
||
| 484 | [ 'class' => 'mw-protectedpages-unknown' ], |
||
| 485 | $this->msg( 'protectedpages-unknown-reason' )->escaped() |
||
| 486 | ); |
||
| 487 | } else { |
||
| 488 | if ( LogEventsList::userCanBitfield( |
||
| 489 | $row->log_deleted, |
||
| 490 | LogPage::DELETED_COMMENT, |
||
| 491 | $this->getUser() |
||
| 492 | ) ) { |
||
| 493 | $formatted = Linker::formatComment( $value !== null ? $value : '' ); |
||
| 494 | } else { |
||
| 495 | $formatted = $this->msg( 'rev-deleted-comment' )->escaped(); |
||
| 496 | } |
||
| 497 | if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) { |
||
| 498 | $formatted = '<span class="history-deleted">' . $formatted . '</span>'; |
||
| 499 | } |
||
| 500 | } |
||
| 501 | break; |
||
| 502 | |||
| 503 | default: |
||
| 504 | throw new MWException( "Unknown field '$field'" ); |
||
| 505 | } |
||
| 506 | |||
| 507 | return $formatted; |
||
| 508 | } |
||
| 509 | |||
| 510 | function getQueryInfo() { |
||
| 572 | |||
| 573 | protected function getTableClass() { |
||
| 576 | |||
| 577 | function getIndexField() { |
||
| 580 | |||
| 581 | function getDefaultSort() { |
||
| 584 | |||
| 585 | function isFieldSortable( $field ) { |
||
| 589 | } |
||
| 590 |
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.