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 ManualLogEntry 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 ManualLogEntry, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 394 | class ManualLogEntry extends LogEntryBase { |
||
| 395 | /** @var string Type of log entry */ |
||
| 396 | protected $type; |
||
| 397 | |||
| 398 | /** @var string Sub type of log entry */ |
||
| 399 | protected $subtype; |
||
| 400 | |||
| 401 | /** @var array Parameters for log entry */ |
||
| 402 | protected $parameters = []; |
||
| 403 | |||
| 404 | /** @var array */ |
||
| 405 | protected $relations = []; |
||
| 406 | |||
| 407 | /** @var User Performer of the action for the log entry */ |
||
| 408 | protected $performer; |
||
| 409 | |||
| 410 | /** @var Title Target title for the log entry */ |
||
| 411 | protected $target; |
||
| 412 | |||
| 413 | /** @var string Timestamp of creation of the log entry */ |
||
| 414 | protected $timestamp; |
||
| 415 | |||
| 416 | /** @var string Comment for the log entry */ |
||
| 417 | protected $comment = ''; |
||
| 418 | |||
| 419 | /** @var int A rev id associated to the log entry */ |
||
| 420 | protected $revId = 0; |
||
| 421 | |||
| 422 | /** @var array Change tags add to the log entry */ |
||
| 423 | protected $tags = null; |
||
| 424 | |||
| 425 | /** @var int Deletion state of the log entry */ |
||
| 426 | protected $deleted; |
||
| 427 | |||
| 428 | /** @var int ID of the log entry */ |
||
| 429 | protected $id; |
||
| 430 | |||
| 431 | /** @var Can this log entry be patrolled? */ |
||
| 432 | protected $isPatrollable = false; |
||
| 433 | |||
| 434 | /** @var bool Whether this is a legacy log entry */ |
||
| 435 | protected $legacy = false; |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Constructor. |
||
| 439 | * |
||
| 440 | * @since 1.19 |
||
| 441 | * @param string $type |
||
| 442 | * @param string $subtype |
||
| 443 | */ |
||
| 444 | public function __construct( $type, $subtype ) { |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Set extra log parameters. |
||
| 451 | * |
||
| 452 | * You can pass params to the log action message by prefixing the keys with |
||
| 453 | * a number and optional type, using colons to separate the fields. The |
||
| 454 | * numbering should start with number 4, the first three parameters are |
||
| 455 | * hardcoded for every message. |
||
| 456 | * |
||
| 457 | * If you want to store stuff that should not be available in messages, don't |
||
| 458 | * prefix the array key with a number and don't use the colons. |
||
| 459 | * |
||
| 460 | * Example: |
||
| 461 | * $entry->setParameters( |
||
| 462 | * '4::color' => 'blue', |
||
| 463 | * '5:number:count' => 3000, |
||
| 464 | * 'animal' => 'dog' |
||
| 465 | * ); |
||
| 466 | * |
||
| 467 | * @since 1.19 |
||
| 468 | * @param array $parameters Associative array |
||
| 469 | */ |
||
| 470 | public function setParameters( $parameters ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Declare arbitrary tag/value relations to this log entry. |
||
| 476 | * These can be used to filter log entries later on. |
||
| 477 | * |
||
| 478 | * @param array $relations Map of (tag => (list of values|value)) |
||
| 479 | * @since 1.22 |
||
| 480 | */ |
||
| 481 | public function setRelations( array $relations ) { |
||
| 484 | |||
| 485 | /** |
||
| 486 | * Set the user that performed the action being logged. |
||
| 487 | * |
||
| 488 | * @since 1.19 |
||
| 489 | * @param User $performer |
||
| 490 | */ |
||
| 491 | public function setPerformer( User $performer ) { |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Set the title of the object changed. |
||
| 497 | * |
||
| 498 | * @since 1.19 |
||
| 499 | * @param Title $target |
||
| 500 | */ |
||
| 501 | public function setTarget( Title $target ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Set the timestamp of when the logged action took place. |
||
| 507 | * |
||
| 508 | * @since 1.19 |
||
| 509 | * @param string $timestamp |
||
| 510 | */ |
||
| 511 | public function setTimestamp( $timestamp ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Set a comment associated with the action being logged. |
||
| 517 | * |
||
| 518 | * @since 1.19 |
||
| 519 | * @param string $comment |
||
| 520 | */ |
||
| 521 | public function setComment( $comment ) { |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Set an associated revision id. |
||
| 527 | * |
||
| 528 | * For example, the ID of the revision that was inserted to mark a page move |
||
| 529 | * or protection, file upload, etc. |
||
| 530 | * |
||
| 531 | * @since 1.27 |
||
| 532 | * @param int $revId |
||
| 533 | */ |
||
| 534 | public function setAssociatedRevId( $revId ) { |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Set change tags for the log entry. |
||
| 540 | * |
||
| 541 | * @since 1.27 |
||
| 542 | * @param string|string[] $tags |
||
| 543 | */ |
||
| 544 | public function setTags( $tags ) { |
||
| 550 | |||
| 551 | /** |
||
| 552 | * Set whether this log entry should be made patrollable |
||
| 553 | * This shouldn't depend on config, only on whether there is full support |
||
| 554 | * in the software for patrolling this log entry. |
||
| 555 | * False by default |
||
| 556 | * |
||
| 557 | * @since 1.27 |
||
| 558 | * @param bool $patrollable |
||
| 559 | */ |
||
| 560 | public function setIsPatrollable( $patrollable ) { |
||
| 563 | |||
| 564 | /** |
||
| 565 | * Set the 'legacy' flag |
||
| 566 | * |
||
| 567 | * @since 1.25 |
||
| 568 | * @param bool $legacy |
||
| 569 | */ |
||
| 570 | public function setLegacy( $legacy ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Set the 'deleted' flag. |
||
| 576 | * |
||
| 577 | * @since 1.19 |
||
| 578 | * @param int $deleted One of LogPage::DELETED_* bitfield constants |
||
| 579 | */ |
||
| 580 | public function setDeleted( $deleted ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Insert the entry into the `logging` table. |
||
| 586 | * |
||
| 587 | * @param IDatabase $dbw |
||
| 588 | * @return int ID of the log entry |
||
| 589 | * @throws MWException |
||
| 590 | */ |
||
| 591 | public function insert( IDatabase $dbw = null ) { |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Get a RecentChanges object for the log entry |
||
| 664 | * |
||
| 665 | * @param int $newId |
||
| 666 | * @return RecentChange |
||
| 667 | * @since 1.23 |
||
| 668 | */ |
||
| 669 | public function getRecentChange( $newId = 0 ) { |
||
| 702 | |||
| 703 | /** |
||
| 704 | * Publish the log entry. |
||
| 705 | * |
||
| 706 | * @param int $newId Id of the log entry. |
||
| 707 | * @param string $to One of: rcandudp (default), rc, udp |
||
| 708 | */ |
||
| 709 | public function publish( $newId, $to = 'rcandudp' ) { |
||
| 710 | DeferredUpdates::addCallableUpdate( |
||
| 711 | function () use ( $newId, $to ) { |
||
| 712 | $log = new LogPage( $this->getType() ); |
||
| 713 | if ( !$log->isRestricted() ) { |
||
| 714 | $rc = $this->getRecentChange( $newId ); |
||
| 715 | |||
| 716 | if ( $to === 'rc' || $to === 'rcandudp' ) { |
||
| 717 | // save RC, passing tags so they are applied there |
||
| 718 | $tags = $this->getTags(); |
||
| 719 | if ( is_null( $tags ) ) { |
||
| 720 | $tags = []; |
||
| 721 | } |
||
| 722 | $rc->addTags( $tags ); |
||
| 723 | $rc->save( 'pleasedontudp' ); |
||
| 724 | } |
||
| 725 | |||
| 726 | if ( $to === 'udp' || $to === 'rcandudp' ) { |
||
| 727 | $rc->notifyRCFeeds(); |
||
| 728 | } |
||
| 729 | |||
| 730 | // Log the autopatrol if the log entry is patrollable |
||
| 731 | if ( $this->getIsPatrollable() && |
||
| 732 | $rc->getAttribute( 'rc_patrolled' ) === 1 |
||
| 733 | ) { |
||
| 734 | PatrolLog::record( $rc, true, $this->getPerformer() ); |
||
| 735 | } |
||
| 736 | } |
||
| 737 | }, |
||
| 738 | DeferredUpdates::POSTSEND, |
||
| 739 | wfGetDB( DB_MASTER ) |
||
| 740 | ); |
||
| 741 | } |
||
| 742 | |||
| 743 | public function getType() { |
||
| 746 | |||
| 747 | public function getSubtype() { |
||
| 750 | |||
| 751 | public function getParameters() { |
||
| 754 | |||
| 755 | /** |
||
| 756 | * @return User |
||
| 757 | */ |
||
| 758 | public function getPerformer() { |
||
| 761 | |||
| 762 | /** |
||
| 763 | * @return Title |
||
| 764 | */ |
||
| 765 | public function getTarget() { |
||
| 768 | |||
| 769 | public function getTimestamp() { |
||
| 774 | |||
| 775 | public function getComment() { |
||
| 778 | |||
| 779 | /** |
||
| 780 | * @since 1.27 |
||
| 781 | * @return int |
||
| 782 | */ |
||
| 783 | public function getAssociatedRevId() { |
||
| 786 | |||
| 787 | /** |
||
| 788 | * @since 1.27 |
||
| 789 | * @return array |
||
| 790 | */ |
||
| 791 | public function getTags() { |
||
| 794 | |||
| 795 | /** |
||
| 796 | * Whether this log entry is patrollable |
||
| 797 | * |
||
| 798 | * @since 1.27 |
||
| 799 | * @return bool |
||
| 800 | */ |
||
| 801 | public function getIsPatrollable() { |
||
| 804 | |||
| 805 | /** |
||
| 806 | * @since 1.25 |
||
| 807 | * @return bool |
||
| 808 | */ |
||
| 809 | public function isLegacy() { |
||
| 812 | |||
| 813 | public function getDeleted() { |
||
| 816 | } |
||
| 817 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountIdthat can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theidproperty of an instance of theAccountclass. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.