Complex classes like FulltextIndex 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 FulltextIndex, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class FulltextIndex extends AbstractIndex |
||
| 19 | { |
||
| 20 | // numeric page id to be added to or deleted from the Fulltext index |
||
| 21 | protected $pageID; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * FulltextIndex constructor |
||
| 25 | * |
||
| 26 | * @param string|int $page a page name or numeric page id |
||
| 27 | */ |
||
| 28 | public function __construct($page = null) |
||
| 29 | { |
||
| 30 | if (isset($page)) { |
||
| 31 | $this->pageID = is_int($page) ? $page : $this->getPID($page); |
||
| 32 | } |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Measure the length of a string |
||
| 37 | * Differs from strlen in handling of asian characters. |
||
| 38 | * |
||
| 39 | * @author Tom N Harris <[email protected]> |
||
| 40 | * |
||
| 41 | * @param string $w |
||
| 42 | * @return int |
||
| 43 | */ |
||
| 44 | public function wordlen($w) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Adds the contents of a page to the fulltext index |
||
| 59 | * |
||
| 60 | * The added text replaces previous words for the same page. |
||
| 61 | * An empty value erases the page. |
||
| 62 | * |
||
| 63 | * @param string $text the body of the page |
||
| 64 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 65 | * @return bool if the function completed successfully |
||
| 66 | * |
||
| 67 | * @throws IndexAccessException |
||
| 68 | * @throws IndexLockException |
||
| 69 | * @throws IndexWriteException |
||
| 70 | * @author Andreas Gohr <[email protected]> |
||
| 71 | * @author Tom N Harris <[email protected]> |
||
| 72 | */ |
||
| 73 | public function addWords($text, $requireLock = true) |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Split the words in a page and add them to the index |
||
| 129 | * |
||
| 130 | * @param string $text content of the page |
||
| 131 | * @return array list of word IDs and number of times used, false on errors |
||
| 132 | * |
||
| 133 | * @throws IndexWriteException |
||
| 134 | * @author Andreas Gohr <[email protected]> |
||
| 135 | * @author Christopher Smith <[email protected]> |
||
| 136 | * @author Tom N Harris <[email protected]> |
||
| 137 | */ |
||
| 138 | protected function getWords($text) |
||
| 177 | |||
| 178 | /** |
||
| 179 | * Delete the contents of a page to the fulltext index |
||
| 180 | * |
||
| 181 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 182 | * @return bool If renaming the value has been successful, false on error |
||
| 183 | * |
||
| 184 | * @throws IndexAccessException |
||
| 185 | * @throws IndexLockException |
||
| 186 | * @throws IndexWriteException |
||
| 187 | * @author Satoshi Sahara <[email protected]> |
||
| 188 | * @author Tom N Harris <[email protected]> |
||
| 189 | */ |
||
| 190 | public function deleteWords($requireLock = true) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Find pages in the fulltext index containing the words, |
||
| 230 | * |
||
| 231 | * The search words must be pre-tokenized, meaning only letters and |
||
| 232 | * numbers with an optional wildcard |
||
| 233 | * |
||
| 234 | * The returned array will have the original tokens as key. The values |
||
| 235 | * in the returned list is an array with the page names as keys and the |
||
| 236 | * number of times that token appears on the page as value. |
||
| 237 | * |
||
| 238 | * @param array $tokens list of words to search for |
||
| 239 | * @return array list of page names with usage counts |
||
| 240 | * |
||
| 241 | * @author Tom N Harris <[email protected]> |
||
| 242 | * @author Andreas Gohr <[email protected]> |
||
| 243 | */ |
||
| 244 | public function lookupWords(&$tokens) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Find the index ID of each search term |
||
| 286 | * |
||
| 287 | * The query terms should only contain valid characters, with a '*' at |
||
| 288 | * either the beginning or end of the word (or both). |
||
| 289 | * The $result parameter can be used to merge the index locations with |
||
| 290 | * the appropriate query term. |
||
| 291 | * |
||
| 292 | * @param array $words The query terms. |
||
| 293 | * @param array $result Set to word => array("length*id" ...) |
||
| 294 | * @return array Set to length => array(id ...) |
||
| 295 | * |
||
| 296 | * @author Tom N Harris <[email protected]> |
||
| 297 | */ |
||
| 298 | protected function getIndexWords(&$words, &$result) |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get the word lengths that have been indexed |
||
| 378 | * |
||
| 379 | * Reads the index directory and returns an array of lengths |
||
| 380 | * that there are indices for. |
||
| 381 | * |
||
| 382 | * @author YoBoY <[email protected]> |
||
| 383 | * |
||
| 384 | * @param array|int $filter |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public function getIndexLengths($filter) |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Get the list of lengths indexed in the wiki |
||
| 413 | * |
||
| 414 | * Read the index directory or a cache file and returns |
||
| 415 | * a sorted array of lengths of the words used in the wiki. |
||
| 416 | * |
||
| 417 | * @author YoBoY <[email protected]> |
||
| 418 | * |
||
| 419 | * @return array |
||
| 420 | */ |
||
| 421 | public function listIndexLengths() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Return a list of words sorted by number of times used |
||
| 471 | * |
||
| 472 | * @param int $min bottom frequency threshold |
||
| 473 | * @param int $max upper frequency limit. No limit if $max<$min |
||
| 474 | * @param int $minlen minimum length of words to count |
||
| 475 | * @return array list of words as the keys and frequency as value |
||
| 476 | * |
||
| 477 | * @author Tom N Harris <[email protected]> |
||
| 478 | */ |
||
| 479 | public function histogram($min=1, $max=0, $minlen=3) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * Clear the Fulltext Index |
||
| 486 | * |
||
| 487 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 488 | * @return bool If the index has been cleared successfully |
||
| 489 | * @throws Exception\IndexLockException |
||
| 490 | */ |
||
| 491 | public function clear($requireLock = true) |
||
| 508 | } |
||
| 509 |