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 |
||
16 | class WatchedItemStore implements StatsdAwareInterface { |
||
17 | |||
18 | const SORT_DESC = 'DESC'; |
||
19 | const SORT_ASC = 'ASC'; |
||
20 | |||
21 | /** |
||
22 | * @var LoadBalancer |
||
23 | */ |
||
24 | private $loadBalancer; |
||
25 | |||
26 | /** |
||
27 | * @var HashBagOStuff |
||
28 | */ |
||
29 | private $cache; |
||
30 | |||
31 | /** |
||
32 | * @var array[] Looks like $cacheIndex[Namespace ID][Target DB Key][User Id] => 'key' |
||
33 | * The index is needed so that on mass changes all relevant items can be un-cached. |
||
34 | * For example: Clearing a users watchlist of all items or updating notification timestamps |
||
35 | * for all users watching a single target. |
||
36 | */ |
||
37 | private $cacheIndex = []; |
||
38 | |||
39 | /** |
||
40 | * @var callable|null |
||
41 | */ |
||
42 | private $deferredUpdatesAddCallableUpdateCallback; |
||
43 | |||
44 | /** |
||
45 | * @var callable|null |
||
46 | */ |
||
47 | private $revisionGetTimestampFromIdCallback; |
||
48 | |||
49 | /** |
||
50 | * @var StatsdDataFactoryInterface |
||
51 | */ |
||
52 | private $stats; |
||
53 | |||
54 | /** |
||
55 | * @param LoadBalancer $loadBalancer |
||
56 | * @param HashBagOStuff $cache |
||
57 | */ |
||
58 | public function __construct( |
||
68 | |||
69 | public function setStatsdDataFactory( StatsdDataFactoryInterface $stats ) { |
||
72 | |||
73 | /** |
||
74 | * Overrides the DeferredUpdates::addCallableUpdate callback |
||
75 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
76 | * |
||
77 | * @param callable $callback |
||
78 | * |
||
79 | * @see DeferredUpdates::addCallableUpdate for callback signiture |
||
80 | * |
||
81 | * @return ScopedCallback to reset the overridden value |
||
82 | * @throws MWException |
||
83 | */ |
||
84 | View Code Duplication | public function overrideDeferredUpdatesAddCallableUpdateCallback( callable $callback ) { |
|
96 | |||
97 | /** |
||
98 | * Overrides the Revision::getTimestampFromId callback |
||
99 | * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined. |
||
100 | * |
||
101 | * @param callable $callback |
||
102 | * @see Revision::getTimestampFromId for callback signiture |
||
103 | * |
||
104 | * @return ScopedCallback to reset the overridden value |
||
105 | * @throws MWException |
||
106 | */ |
||
107 | View Code Duplication | public function overrideRevisionGetTimestampFromIdCallback( callable $callback ) { |
|
119 | |||
120 | private function getCacheKey( User $user, LinkTarget $target ) { |
||
127 | |||
128 | private function cache( WatchedItem $item ) { |
||
136 | |||
137 | private function uncache( User $user, LinkTarget $target ) { |
||
142 | |||
143 | private function uncacheLinkTarget( LinkTarget $target ) { |
||
153 | |||
154 | private function uncacheUser( User $user ) { |
||
165 | |||
166 | /** |
||
167 | * @param User $user |
||
168 | * @param LinkTarget $target |
||
169 | * |
||
170 | * @return WatchedItem|false |
||
171 | */ |
||
172 | private function getCached( User $user, LinkTarget $target ) { |
||
175 | |||
176 | /** |
||
177 | * Return an array of conditions to select or update the appropriate database |
||
178 | * row. |
||
179 | * |
||
180 | * @param User $user |
||
181 | * @param LinkTarget $target |
||
182 | * |
||
183 | * @return array |
||
184 | */ |
||
185 | private function dbCond( User $user, LinkTarget $target ) { |
||
192 | |||
193 | /** |
||
194 | * @param int $dbIndex DB_MASTER or DB_REPLICA |
||
195 | * |
||
196 | * @return IDatabase |
||
197 | * @throws MWException |
||
198 | */ |
||
199 | private function getConnectionRef( $dbIndex ) { |
||
202 | |||
203 | /** |
||
204 | * Count the number of individual items that are watched by the user. |
||
205 | * If a subject and corresponding talk page are watched this will return 2. |
||
206 | * |
||
207 | * @param User $user |
||
208 | * |
||
209 | * @return int |
||
210 | */ |
||
211 | public function countWatchedItems( User $user ) { |
||
224 | |||
225 | /** |
||
226 | * @param LinkTarget $target |
||
227 | * |
||
228 | * @return int |
||
229 | */ |
||
230 | public function countWatchers( LinkTarget $target ) { |
||
244 | |||
245 | /** |
||
246 | * Number of page watchers who also visited a "recent" edit |
||
247 | * |
||
248 | * @param LinkTarget $target |
||
249 | * @param mixed $threshold timestamp accepted by wfTimestamp |
||
250 | * |
||
251 | * @return int |
||
252 | * @throws DBUnexpectedError |
||
253 | * @throws MWException |
||
254 | */ |
||
255 | public function countVisitingWatchers( LinkTarget $target, $threshold ) { |
||
272 | |||
273 | /** |
||
274 | * @param LinkTarget[] $targets |
||
275 | * @param array $options Allowed keys: |
||
276 | * 'minimumWatchers' => int |
||
277 | * |
||
278 | * @return array multi dimensional like $return[$namespaceId][$titleString] = int $watchers |
||
279 | * All targets will be present in the result. 0 either means no watchers or the number |
||
280 | * of watchers was below the minimumWatchers option if passed. |
||
281 | */ |
||
282 | public function countWatchersMultiple( array $targets, array $options = [] ) { |
||
311 | |||
312 | /** |
||
313 | * Number of watchers of each page who have visited recent edits to that page |
||
314 | * |
||
315 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget $target, mixed $threshold), |
||
316 | * $threshold is: |
||
317 | * - a timestamp of the recent edit if $target exists (format accepted by wfTimestamp) |
||
318 | * - null if $target doesn't exist |
||
319 | * @param int|null $minimumWatchers |
||
320 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $watchers, |
||
321 | * where $watchers is an int: |
||
322 | * - if the page exists, number of users watching who have visited the page recently |
||
323 | * - if the page doesn't exist, number of users that have the page on their watchlist |
||
324 | * - 0 means there are no visiting watchers or their number is below the minimumWatchers |
||
325 | * option (if passed). |
||
326 | */ |
||
327 | public function countVisitingWatchersMultiple( |
||
359 | |||
360 | /** |
||
361 | * Generates condition for the query used in a batch count visiting watchers. |
||
362 | * |
||
363 | * @param IDatabase $db |
||
364 | * @param array $targetsWithVisitThresholds array of pairs (LinkTarget, last visit threshold) |
||
365 | * @return string |
||
366 | */ |
||
367 | private function getVisitingWatchersCondition( |
||
403 | |||
404 | /** |
||
405 | * Get an item (may be cached) |
||
406 | * |
||
407 | * @param User $user |
||
408 | * @param LinkTarget $target |
||
409 | * |
||
410 | * @return WatchedItem|false |
||
411 | */ |
||
412 | public function getWatchedItem( User $user, LinkTarget $target ) { |
||
425 | |||
426 | /** |
||
427 | * Loads an item from the db |
||
428 | * |
||
429 | * @param User $user |
||
430 | * @param LinkTarget $target |
||
431 | * |
||
432 | * @return WatchedItem|false |
||
433 | */ |
||
434 | public function loadWatchedItem( User $user, LinkTarget $target ) { |
||
461 | |||
462 | /** |
||
463 | * @param User $user |
||
464 | * @param array $options Allowed keys: |
||
465 | * 'forWrite' => bool defaults to false |
||
466 | * 'sort' => string optional sorting by namespace ID and title |
||
467 | * one of the self::SORT_* constants |
||
468 | * |
||
469 | * @return WatchedItem[] |
||
470 | */ |
||
471 | public function getWatchedItemsForUser( User $user, array $options = [] ) { |
||
508 | |||
509 | /** |
||
510 | * Must be called separately for Subject & Talk namespaces |
||
511 | * |
||
512 | * @param User $user |
||
513 | * @param LinkTarget $target |
||
514 | * |
||
515 | * @return bool |
||
516 | */ |
||
517 | public function isWatched( User $user, LinkTarget $target ) { |
||
520 | |||
521 | /** |
||
522 | * @param User $user |
||
523 | * @param LinkTarget[] $targets |
||
524 | * |
||
525 | * @return array multi-dimensional like $return[$namespaceId][$titleString] = $timestamp, |
||
526 | * where $timestamp is: |
||
527 | * - string|null value of wl_notificationtimestamp, |
||
528 | * - false if $target is not watched by $user. |
||
529 | */ |
||
530 | public function getNotificationTimestampsBatch( User $user, array $targets ) { |
||
574 | |||
575 | /** |
||
576 | * Must be called separately for Subject & Talk namespaces |
||
577 | * |
||
578 | * @param User $user |
||
579 | * @param LinkTarget $target |
||
580 | */ |
||
581 | public function addWatch( User $user, LinkTarget $target ) { |
||
584 | |||
585 | /** |
||
586 | * @param User $user |
||
587 | * @param LinkTarget[] $targets |
||
588 | * |
||
589 | * @return bool success |
||
590 | */ |
||
591 | public function addWatchBatchForUser( User $user, array $targets ) { |
||
636 | |||
637 | /** |
||
638 | * Removes the an entry for the User watching the LinkTarget |
||
639 | * Must be called separately for Subject & Talk namespaces |
||
640 | * |
||
641 | * @param User $user |
||
642 | * @param LinkTarget $target |
||
643 | * |
||
644 | * @return bool success |
||
645 | * @throws DBUnexpectedError |
||
646 | * @throws MWException |
||
647 | */ |
||
648 | public function removeWatch( User $user, LinkTarget $target ) { |
||
668 | |||
669 | /** |
||
670 | * @param User $user The user to set the timestamp for |
||
671 | * @param string $timestamp Set the update timestamp to this value |
||
672 | * @param LinkTarget[] $targets List of targets to update. Default to all targets |
||
673 | * |
||
674 | * @return bool success |
||
675 | */ |
||
676 | public function setNotificationTimestampsForUser( User $user, $timestamp, array $targets = [] ) { |
||
701 | |||
702 | /** |
||
703 | * @param User $editor The editor that triggered the update. Their notification |
||
704 | * timestamp will not be updated(they have already seen it) |
||
705 | * @param LinkTarget $target The target to update timestamps for |
||
706 | * @param string $timestamp Set the update timestamp to this value |
||
707 | * |
||
708 | * @return int[] Array of user IDs the timestamp has been updated for |
||
709 | */ |
||
710 | public function updateNotificationTimestamp( User $editor, LinkTarget $target, $timestamp ) { |
||
762 | |||
763 | /** |
||
764 | * Reset the notification timestamp of this entry |
||
765 | * |
||
766 | * @param User $user |
||
767 | * @param Title $title |
||
768 | * @param string $force Whether to force the write query to be executed even if the |
||
769 | * page is not watched or the notification timestamp is already NULL. |
||
770 | * 'force' in order to force |
||
771 | * @param int $oldid The revision id being viewed. If not given or 0, latest revision is assumed. |
||
772 | * |
||
773 | * @return bool success |
||
774 | */ |
||
775 | public function resetNotificationTimestamp( User $user, Title $title, $force = '', $oldid = 0 ) { |
||
813 | |||
814 | private function getNotificationTimestamp( User $user, Title $title, $item, $force, $oldid ) { |
||
860 | |||
861 | /** |
||
862 | * @param User $user |
||
863 | * @param int $unreadLimit |
||
864 | * |
||
865 | * @return int|bool The number of unread notifications |
||
866 | * true if greater than or equal to $unreadLimit |
||
867 | */ |
||
868 | public function countUnreadNotifications( User $user, $unreadLimit = null ) { |
||
897 | |||
898 | /** |
||
899 | * Check if the given title already is watched by the user, and if so |
||
900 | * add a watch for the new title. |
||
901 | * |
||
902 | * To be used for page renames and such. |
||
903 | * |
||
904 | * @param LinkTarget $oldTarget |
||
905 | * @param LinkTarget $newTarget |
||
906 | */ |
||
907 | public function duplicateAllAssociatedEntries( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
914 | |||
915 | /** |
||
916 | * Check if the given title already is watched by the user, and if so |
||
917 | * add a watch for the new title. |
||
918 | * |
||
919 | * To be used for page renames and such. |
||
920 | * This must be called separately for Subject and Talk pages |
||
921 | * |
||
922 | * @param LinkTarget $oldTarget |
||
923 | * @param LinkTarget $newTarget |
||
924 | */ |
||
925 | public function duplicateEntry( LinkTarget $oldTarget, LinkTarget $newTarget ) { |
||
965 | |||
966 | } |
||
967 |
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: