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 WatchedItemStore 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 WatchedItemStore, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class WatchedItemStore implements StatsdAwareInterface { |
||
| 16 | |||
| 17 | const SORT_DESC = 'DESC'; |
||
| 18 | const SORT_ASC = 'ASC'; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var LoadBalancer |
||
| 22 | */ |
||
| 23 | private $loadBalancer; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var HashBagOStuff |
||
| 27 | */ |
||
| 28 | private $cache; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key' |
||
| 32 | * The index is needed so that on mass changes all relevant items can be un-cached. |
||
| 33 | * For example: Clearing a users watchlist of all items or updating notification timestamps |
||
| 34 | * for all users watching a single target. |
||
| 35 | */ |
||
| 36 | private $cacheIndex = []; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var callable|null |
||
| 40 | */ |
||
| 41 | private $deferredUpdatesAddCallableUpdateCallback; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var callable|null |
||
| 45 | */ |
||
| 46 | private $revisionGetTimestampFromIdCallback; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var StatsdDataFactoryInterface |
||
| 50 | */ |
||
| 51 | private $stats; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param LoadBalancer $loadBalancer |
||
| 55 | * @param HashBagOStuff $cache |
||
| 56 | */ |
||
| 57 | public function __construct( |
||
| 67 | |||
| 68 | public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) { |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Overrides the DeferredUpdates::addCallableUpdate callback |
||
| 74 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 75 | * |
||
| 76 | * @param callable $callback |
||
| 77 | * |
||
| 78 | * @see DeferredUpdates::addCallableUpdate for callback signiture |
||
| 79 | * |
||
| 80 | * @return ScopedCallback to reset the overridden value |
||
| 81 | * @throws MWException |
||
| 82 | */ |
||
| 83 | View Code Duplication | public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) { |
|
| 84 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
||
| 85 | throw new MWException( |
||
| 86 | 'Cannot override DeferredUpdates::addCallableUpdate callback in operation.' |
||
| 87 | ); |
||
| 88 | } |
||
| 89 | $previousValue = $this->deferredUpdatesAddCallableUpdateCallback; |
||
| 90 | $this->deferredUpdatesAddCallableUpdateCallback = $callback; |
||
| 91 | return new ScopedCallback( function() use ( $previousValue ) { |
||
| 92 | $this->deferredUpdatesAddCallableUpdateCallback = $previousValue; |
||
| 93 | } ); |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Overrides the Revision::getTimestampFromId callback |
||
| 98 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 99 | * |
||
| 100 | * @param callable $callback |
||
| 101 | * @see Revision::getTimestampFromId for callback signiture |
||
| 102 | * |
||
| 103 | * @return ScopedCallback to reset the overridden value |
||
| 104 | * @throws MWException |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) { |
|
| 107 | if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
||
| 108 | throw new MWException( |
||
| 109 | 'Cannot override Revision::getTimestampFromId callback in operation.' |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | $previousValue = $this->revisionGetTimestampFromIdCallback; |
||
| 113 | $this->revisionGetTimestampFromIdCallback = $callback; |
||
| 114 | return new ScopedCallback( function() use ( $previousValue ) { |
||
| 115 | $this->revisionGetTimestampFromIdCallback = $previousValue; |
||
| 116 | } ); |
||
| 117 | } |
||
| 118 | |||
| 119 | private function getCacheKey( User $user, LinkTarget $target ) { |
||
| 126 | |||
| 127 | private function cache( WatchedItem $item ) { |
||
| 135 | |||
| 136 | private function uncache( User $user, LinkTarget $target ) { |
||
| 141 | |||
| 142 | private function uncacheLinkTarget( LinkTarget $target ) { |
||
| 152 | |||
| 153 | private function uncacheUser( User $user ) { |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param User $user |
||
| 167 | * @param LinkTarget $target |
||
| 168 | * |
||
| 169 | * @return WatchedItem|null |
||
| 170 | */ |
||
| 171 | private function getCached( User $user, LinkTarget $target ) { |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Return an array of conditions to select or update the appropriate database |
||
| 177 | * row. |
||
| 178 | * |
||
| 179 | * @param User $user |
||
| 180 | * @param LinkTarget $target |
||
| 181 | * |
||
| 182 | * @return array |
||
| 183 | */ |
||
| 184 | private function dbCond( User $user, LinkTarget $target ) { |
||
| 191 | |||
| 192 | /** |
||
| 193 | * @param int $dbIndex DB_MASTER or DB_REPLICA |
||
| 194 | * |
||
| 195 | * @return IDatabase |
||
| 196 | * @throws MWException |
||
| 197 | */ |
||
| 198 | private function getConnectionRef( $dbIndex ) { |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Count the number of individual items that are watched by the user. |
||
| 204 | * If a subject and corresponding talk page are watched this will return 2. |
||
| 205 | * |
||
| 206 | * @param User $user |
||
| 207 | * |
||
| 208 | * @return int |
||
| 209 | */ |
||
| 210 | public function countWatchedItems( User $user ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * @param LinkTarget $target |
||
| 226 | * |
||
| 227 | * @return int |
||
| 228 | */ |
||
| 229 | public function countWatchers( LinkTarget $target ) { |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Number of page watchers who also visited a "recent" edit |
||
| 246 | * |
||
| 247 | * @param LinkTarget $target |
||
| 248 | * @param mixed $threshold timestamp accepted by wfTimestamp |
||
| 249 | * |
||
| 250 | * @return int |
||
| 251 | * @throws DBUnexpectedError |
||
| 252 | * @throws MWException |
||
| 253 | */ |
||
| 254 | public function countVisitingWatchers( LinkTarget $target, $threshold ) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param LinkTarget[] $targets |
||
| 274 | * @param array $options Allowed keys: |
||
| 275 | * 'minimumWatchers' => int |
||
| 276 | * |
||
| 277 | * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers |
||
| 278 | * All targets will be present in the result. 0 either means no watchers or the number |
||
| 279 | * of watchers was below the minimumWatchers option if passed. |
||
| 280 | */ |
||
| 281 | public function countWatchersMultiple( array $targets, array $options = [] ) { |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Number of watchers of each page who have visited recent edits to that page |
||
| 313 | * |
||
| 314 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold), |
||
| 315 | * $threshold is: |
||
| 316 | * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp) |
||
| 317 | * - null if $target doesn't exist |
||
| 318 | * @param int|null $minimumWatchers |
||
| 319 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers, |
||
| 320 | * where $watchers is an int: |
||
| 321 | * - if the page exists, number of users watching who have visited the page recently |
||
| 322 | * - if the page doesn't exist, number of users that have the page on their watchlist |
||
| 323 | * - 0 means there are no visiting watchers or their number is below the minimumWatchers |
||
| 324 | * option (if passed). |
||
| 325 | */ |
||
| 326 | public function countVisitingWatchersMultiple( |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Generates condition for the query used in a batch count visiting watchers. |
||
| 361 | * |
||
| 362 | * @param IDatabase $db |
||
| 363 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold) |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | private function getVisitingWatchersCondition( |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Get an item (may be cached) |
||
| 405 | * |
||
| 406 | * @param User $user |
||
| 407 | * @param LinkTarget $target |
||
| 408 | * |
||
| 409 | * @return WatchedItem|false |
||
| 410 | */ |
||
| 411 | public function getWatchedItem( User $user, LinkTarget $target ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Loads an item from the db |
||
| 427 | * |
||
| 428 | * @param User $user |
||
| 429 | * @param LinkTarget $target |
||
| 430 | * |
||
| 431 | * @return WatchedItem|false |
||
| 432 | */ |
||
| 433 | public function loadWatchedItem( User $user, LinkTarget $target ) { |
||
| 460 | |||
| 461 | /** |
||
| 462 | * @param User $user |
||
| 463 | * @param array $options Allowed keys: |
||
| 464 | * 'forWrite' => bool defaults to false |
||
| 465 | * 'sort' => string optional sorting by namespace ID and title |
||
| 466 | * one of the self::SORT_* constants |
||
| 467 | * |
||
| 468 | * @return WatchedItem[] |
||
| 469 | */ |
||
| 470 | public function getWatchedItemsForUser( User $user, array $options = [] ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Must be called separately for Subject & Talk namespaces |
||
| 510 | * |
||
| 511 | * @param User $user |
||
| 512 | * @param LinkTarget $target |
||
| 513 | * |
||
| 514 | * @return bool |
||
| 515 | */ |
||
| 516 | public function isWatched( User $user, LinkTarget $target ) { |
||
| 519 | |||
| 520 | /** |
||
| 521 | * @param User $user |
||
| 522 | * @param LinkTarget[] $targets |
||
| 523 | * |
||
| 524 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp, |
||
| 525 | * where $timestamp is: |
||
| 526 | * - string|null value of wl_notificationtimestamp, |
||
| 527 | * - false if $target is not watched by $user. |
||
| 528 | */ |
||
| 529 | public function getNotificationTimestampsBatch( User $user, array $targets ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Must be called separately for Subject & Talk namespaces |
||
| 576 | * |
||
| 577 | * @param User $user |
||
| 578 | * @param LinkTarget $target |
||
| 579 | */ |
||
| 580 | public function addWatch( User $user, LinkTarget $target ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * @param User $user |
||
| 586 | * @param LinkTarget[] $targets |
||
| 587 | * |
||
| 588 | * @return bool success |
||
| 589 | */ |
||
| 590 | public function addWatchBatchForUser( User $user, array $targets ) { |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Removes the an entry for the User watching the LinkTarget |
||
| 626 | * Must be called separately for Subject & Talk namespaces |
||
| 627 | * |
||
| 628 | * @param User $user |
||
| 629 | * @param LinkTarget $target |
||
| 630 | * |
||
| 631 | * @return bool success |
||
| 632 | * @throws DBUnexpectedError |
||
| 633 | * @throws MWException |
||
| 634 | */ |
||
| 635 | public function removeWatch( User $user, LinkTarget $target ) { |
||
| 655 | |||
| 656 | /** |
||
| 657 | * @param User $user The user to set the timestamp for |
||
| 658 | * @param string $timestamp Set the update timestamp to this value |
||
| 659 | * @param LinkTarget[] $targets List of targets to update. Default to all targets |
||
| 660 | * |
||
| 661 | * @return bool success |
||
| 662 | */ |
||
| 663 | public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) { |
||
| 688 | |||
| 689 | /** |
||
| 690 | * @param User $editor The editor that triggered the update. Their notification |
||
| 691 | * timestamp will not be updated(they have already seen it) |
||
| 692 | * @param LinkTarget $target The target to update timestamps for |
||
| 693 | * @param string $timestamp Set the update timestamp to this value |
||
| 694 | * |
||
| 695 | * @return int[] Array of user IDs the timestamp has been updated for |
||
| 696 | */ |
||
| 697 | public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { |
||
| 749 | |||
| 750 | /** |
||
| 751 | * Reset the notification timestamp of this entry |
||
| 752 | * |
||
| 753 | * @param User $user |
||
| 754 | * @param Title $title |
||
| 755 | * @param string $force Whether to force the write query to be executed even if the |
||
| 756 | * page is not watched or the notification timestamp is already NULL. |
||
| 757 | * 'force' in order to force |
||
| 758 | * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed. |
||
| 759 | * |
||
| 760 | * @return bool success |
||
| 761 | */ |
||
| 762 | public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) { |
||
| 800 | |||
| 801 | private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) { |
||
| 847 | |||
| 848 | /** |
||
| 849 | * @param User $user |
||
| 850 | * @param int $unreadLimit |
||
| 851 | * |
||
| 852 | * @return int|bool The number of unread notifications |
||
| 853 | * true if greater than or equal to $unreadLimit |
||
| 854 | */ |
||
| 855 | public function countUnreadNotifications( User $user, $unreadLimit = null ) { |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Check if the given title already is watched by the user, and if so |
||
| 887 | * add a watch for the new title. |
||
| 888 | * |
||
| 889 | * To be used for page renames and such. |
||
| 890 | * |
||
| 891 | * @param LinkTarget $oldTarget |
||
| 892 | * @param LinkTarget $newTarget |
||
| 893 | */ |
||
| 894 | public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 901 | |||
| 902 | /** |
||
| 903 | * Check if the given title already is watched by the user, and if so |
||
| 904 | * add a watch for the new title. |
||
| 905 | * |
||
| 906 | * To be used for page renames and such. |
||
| 907 | * This must be called separately for Subject and Talk pages |
||
| 908 | * |
||
| 909 | * @param LinkTarget $oldTarget |
||
| 910 | * @param LinkTarget $newTarget |
||
| 911 | */ |
||
| 912 | public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 952 | |||
| 953 | } |
||
| 954 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.