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 |
||
| 13 | class WatchedItemStore { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var LoadBalancer |
||
| 17 | */ |
||
| 18 | private $loadBalancer; |
||
| 19 | |||
| 20 | /** |
||
| 21 | * @var BagOStuff |
||
| 22 | */ |
||
| 23 | private $cache; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * @var callable|null |
||
| 27 | */ |
||
| 28 | private $deferredUpdatesAddCallableUpdateCallback; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var callable|null |
||
| 32 | */ |
||
| 33 | private $revisionGetTimestampFromIdCallback; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var self|null |
||
| 37 | */ |
||
| 38 | private static $instance; |
||
| 39 | |||
| 40 | public function __construct( LoadBalancer $loadBalancer, BagOStuff $cache ) { |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Overrides the DeferredUpdates::addCallableUpdate callback |
||
| 49 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 50 | * |
||
| 51 | * @param callable $callback |
||
| 52 | * @see DeferredUpdates::addCallableUpdate for callback signiture |
||
| 53 | * |
||
| 54 | * @throws MWException |
||
| 55 | */ |
||
| 56 | View Code Duplication | public function overrideDeferredUpdatesAddCallableUpdateCallback( $callback ) { |
|
| 65 | |||
| 66 | /** |
||
| 67 | * Overrides the Revision::getTimestampFromId callback |
||
| 68 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 69 | * |
||
| 70 | * @param callable $callback |
||
| 71 | * @see Revision::getTimestampFromId for callback signiture |
||
| 72 | * |
||
| 73 | * @throws MWException |
||
| 74 | */ |
||
| 75 | View Code Duplication | public function overrideRevisionGetTimestampFromIdCallback( $callback ) { |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Overrides the default instance of this class |
||
| 87 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
| 88 | * |
||
| 89 | * @param WatchedItemStore $store |
||
| 90 | * |
||
| 91 | * @throws MWException |
||
| 92 | */ |
||
| 93 | public static function overrideDefaultInstance( WatchedItemStore $store ) { |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | public static function getDefaultInstance() { |
||
| 114 | |||
| 115 | private function getCacheKey( User $user, LinkTarget $target ) { |
||
| 122 | |||
| 123 | private function cache( WatchedItem $item ) { |
||
| 129 | |||
| 130 | private function uncache( User $user, LinkTarget $target ) { |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param User $user |
||
| 136 | * @param LinkTarget $target |
||
| 137 | * |
||
| 138 | * @return WatchedItem|null |
||
| 139 | */ |
||
| 140 | private function getCached( User $user, LinkTarget $target ) { |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Return an array of conditions to select or update the appropriate database |
||
| 146 | * row. |
||
| 147 | * |
||
| 148 | * @param User $user |
||
| 149 | * @param LinkTarget $target |
||
| 150 | * |
||
| 151 | * @return array |
||
| 152 | */ |
||
| 153 | private function dbCond( User $user, LinkTarget $target ) { |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Get an item (may be cached) |
||
| 163 | * |
||
| 164 | * @param User $user |
||
| 165 | * @param LinkTarget $target |
||
| 166 | * |
||
| 167 | * @return WatchedItem|false |
||
| 168 | */ |
||
| 169 | public function getWatchedItem( User $user, LinkTarget $target ) { |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Loads an item from the db |
||
| 179 | * |
||
| 180 | * @param User $user |
||
| 181 | * @param LinkTarget $target |
||
| 182 | * |
||
| 183 | * @return WatchedItem|false |
||
| 184 | */ |
||
| 185 | public function loadWatchedItem( User $user, LinkTarget $target ) { |
||
| 213 | |||
| 214 | /** |
||
| 215 | * Must be called separately for Subject & Talk namespaces |
||
| 216 | * |
||
| 217 | * @param User $user |
||
| 218 | * @param LinkTarget $target |
||
| 219 | * |
||
| 220 | * @return bool |
||
| 221 | */ |
||
| 222 | public function isWatched( User $user, LinkTarget $target ) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Must be called separately for Subject & Talk namespaces |
||
| 228 | * |
||
| 229 | * @param User $user |
||
| 230 | * @param LinkTarget $target |
||
| 231 | */ |
||
| 232 | public function addWatch( User $user, LinkTarget $target ) { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param array[] $userTargetCombinations array of arrays containing [0] => User [1] => LinkTarget |
||
| 238 | * |
||
| 239 | * @return bool success |
||
| 240 | */ |
||
| 241 | public function addWatchBatch( array $userTargetCombinations ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Removes the an entry for the User watching the LinkTarget |
||
| 283 | * Must be called separately for Subject & Talk namespaces |
||
| 284 | * |
||
| 285 | * @param User $user |
||
| 286 | * @param LinkTarget $target |
||
| 287 | * |
||
| 288 | * @return bool success |
||
| 289 | * @throws DBUnexpectedError |
||
| 290 | * @throws MWException |
||
| 291 | */ |
||
| 292 | public function removeWatch( User $user, LinkTarget $target ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * @param User $editor The editor that triggered the update. Their notification |
||
| 316 | * timestamp will not be updated(they have already seen it) |
||
| 317 | * @param LinkTarget $target The target to update timestamps for |
||
| 318 | * @param string $timestamp Set the update timestamp to this value |
||
| 319 | * |
||
| 320 | * @return int[] Array of user IDs the timestamp has been updated for |
||
| 321 | */ |
||
| 322 | public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Reset the notification timestamp of this entry |
||
| 364 | * |
||
| 365 | * @param User $user |
||
| 366 | * @param Title $title |
||
| 367 | * @param string $force Whether to force the write query to be executed even if the |
||
| 368 | * page is not watched or the notification timestamp is already NULL. |
||
| 369 | * 'force' in order to force |
||
| 370 | * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed. |
||
| 371 | * |
||
| 372 | * @return bool success |
||
| 373 | */ |
||
| 374 | public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) { |
||
| 412 | |||
| 413 | private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Check if the given title already is watched by the user, and if so |
||
| 462 | * add a watch for the new title. |
||
| 463 | * |
||
| 464 | * To be used for page renames and such. |
||
| 465 | * |
||
| 466 | * @param LinkTarget $oldTarget |
||
| 467 | * @param LinkTarget $newTarget |
||
| 468 | */ |
||
| 469 | public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Check if the given title already is watched by the user, and if so |
||
| 483 | * add a watch for the new title. |
||
| 484 | * |
||
| 485 | * To be used for page renames and such. |
||
| 486 | * This must be called separately for Subject and Talk pages |
||
| 487 | * |
||
| 488 | * @param LinkTarget $oldTarget |
||
| 489 | * @param LinkTarget $newTarget |
||
| 490 | */ |
||
| 491 | public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
| 533 | |||
| 534 | } |
||
| 535 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.