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 LinkCache 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 LinkCache, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class LinkCache { |
||
| 32 | /** @var HashBagOStuff */ |
||
| 33 | private $mGoodLinks; |
||
| 34 | /** @var HashBagOStuff */ |
||
| 35 | private $mBadLinks; |
||
| 36 | /** @var WANObjectCache */ |
||
| 37 | private $wanCache; |
||
| 38 | |||
| 39 | /** @var bool */ |
||
| 40 | private $mForUpdate = false; |
||
| 41 | |||
| 42 | /** @var TitleFormatter */ |
||
| 43 | private $titleFormatter; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * How many Titles to store. There are two caches, so the amount actually |
||
| 47 | * stored in memory can be up to twice this. |
||
| 48 | */ |
||
| 49 | const MAX_SIZE = 10000; |
||
| 50 | |||
| 51 | public function __construct( TitleFormatter $titleFormatter, WANObjectCache $cache ) { |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Get an instance of this class. |
||
| 60 | * |
||
| 61 | * @return LinkCache |
||
| 62 | * @deprecated since 1.28, use MediaWikiServices instead |
||
| 63 | */ |
||
| 64 | public static function singleton() { |
||
| 67 | |||
| 68 | /** |
||
| 69 | * General accessor to get/set whether the master DB should be used |
||
| 70 | * |
||
| 71 | * This used to also set the FOR UPDATE option (locking the rows read |
||
| 72 | * in order to avoid link table inconsistency), which was later removed |
||
| 73 | * for performance on wikis with a high edit rate. |
||
| 74 | * |
||
| 75 | * @param bool $update |
||
| 76 | * @return bool |
||
| 77 | */ |
||
| 78 | public function forUpdate( $update = null ) { |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param string $title Prefixed DB key |
||
| 84 | * @return int Page ID or zero |
||
| 85 | */ |
||
| 86 | public function getGoodLinkID( $title ) { |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Get a field of a title object from cache. |
||
| 96 | * If this link is not a cached good title, it will return NULL. |
||
| 97 | * @param LinkTarget $target |
||
| 98 | * @param string $field ('length','redirect','revision','model') |
||
| 99 | * @return string|int|null |
||
| 100 | */ |
||
| 101 | public function getGoodLinkFieldObj( LinkTarget $target, $field ) { |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $title Prefixed DB key |
||
| 112 | * @return bool |
||
| 113 | */ |
||
| 114 | public function isBadLink( $title ) { |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Add a link for the title to the link cache |
||
| 121 | * |
||
| 122 | * @param int $id Page's ID |
||
| 123 | * @param LinkTarget $target |
||
| 124 | * @param int $len Text's length |
||
| 125 | * @param int $redir Whether the page is a redirect |
||
| 126 | * @param int $revision Latest revision's ID |
||
| 127 | * @param string|null $model Latest revision's content model ID |
||
| 128 | * @param string|null $lang Language code of the page, if not the content language |
||
| 129 | */ |
||
| 130 | public function addGoodLinkObj( $id, LinkTarget $target, $len = -1, $redir = null, |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Same as above with better interface. |
||
| 146 | * @since 1.19 |
||
| 147 | * @param LinkTarget $target |
||
| 148 | * @param stdClass $row Object which has the fields page_id, page_is_redirect, |
||
| 149 | * page_latest and page_content_model |
||
| 150 | */ |
||
| 151 | public function addGoodLinkObjFromRow( LinkTarget $target, $row ) { |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param LinkTarget $target |
||
| 165 | */ |
||
| 166 | public function addBadLinkObj( LinkTarget $target ) { |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $title Prefixed DB key |
||
| 175 | */ |
||
| 176 | public function clearBadLink( $title ) { |
||
| 179 | |||
| 180 | /** |
||
| 181 | * @param LinkTarget $target |
||
| 182 | */ |
||
| 183 | public function clearLink( LinkTarget $target ) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Add a title to the link cache, return the page_id or zero if non-existent |
||
| 191 | * |
||
| 192 | * @deprecated since 1.27, unused |
||
| 193 | * @param string $title Prefixed DB key |
||
| 194 | * @return int Page ID or zero |
||
| 195 | */ |
||
| 196 | public function addLink( $title ) { |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Fields that LinkCache needs to select |
||
| 206 | * |
||
| 207 | * @since 1.28 |
||
| 208 | * @return array |
||
| 209 | */ |
||
| 210 | View Code Duplication | public static function getSelectFields() { |
|
| 223 | |||
| 224 | /** |
||
| 225 | * Add a title to the link cache, return the page_id or zero if non-existent |
||
| 226 | * |
||
| 227 | * @param LinkTarget $nt LinkTarget object to add |
||
| 228 | * @return int Page ID or zero |
||
| 229 | */ |
||
| 230 | public function addLinkObj( LinkTarget $nt ) { |
||
| 280 | |||
| 281 | private function isCacheable( LinkTarget $title ) { |
||
| 284 | |||
| 285 | private function fetchPageRow( IDatabase $db, LinkTarget $nt ) { |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Purge the link cache for a title |
||
| 301 | * |
||
| 302 | * @param LinkTarget $title |
||
| 303 | * @since 1.28 |
||
| 304 | */ |
||
| 305 | public function invalidateTitle( LinkTarget $title ) { |
||
| 313 | |||
| 314 | /** |
||
| 315 | * Clears cache |
||
| 316 | */ |
||
| 317 | public function clear() { |
||
| 321 | } |
||
| 322 |