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 |
||
| 17 | class FulltextIndex extends AbstractIndex |
||
| 18 | { |
||
| 19 | /** @var FulltextIndex $instance */ |
||
| 20 | protected static $instance = null; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get new or existing singleton instance of the FulltextIndex |
||
| 24 | * |
||
| 25 | * @return FulltextIndex |
||
| 26 | */ |
||
| 27 | public static function getInstance() |
||
| 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 $page a page name |
||
| 64 | * @param string $text the body of the page |
||
| 65 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 66 | * @return bool if the function completed successfully |
||
| 67 | * |
||
| 68 | * @throws IndexLockException |
||
| 69 | * @throws IndexWriteException |
||
| 70 | * @author Andreas Gohr <[email protected]> |
||
| 71 | * @author Tom N Harris <[email protected]> |
||
| 72 | */ |
||
| 73 | public function addPageWords($page, $text, $requireLock = true) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Split the words in a page and add them to the index |
||
| 125 | * |
||
| 126 | * @param string $text content of the page |
||
| 127 | * @return array list of word IDs and number of times used, false on errors |
||
| 128 | * |
||
| 129 | * @throws IndexWriteException |
||
| 130 | * @author Andreas Gohr <[email protected]> |
||
| 131 | * @author Christopher Smith <[email protected]> |
||
| 132 | * @author Tom N Harris <[email protected]> |
||
| 133 | */ |
||
| 134 | protected function getPageWords($text) |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Delete the contents of a page to the fulltext index |
||
| 177 | * |
||
| 178 | * @param string $page a page name |
||
| 179 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 180 | * @return bool If renaming the value has been successful, false on error |
||
| 181 | * |
||
| 182 | * @throws IndexLockException |
||
| 183 | * @throws IndexWriteException |
||
| 184 | * @author Satoshi Sahara <[email protected]> |
||
| 185 | * @author Tom N Harris <[email protected]> |
||
| 186 | */ |
||
| 187 | public function deletePageWords($page, $requireLock = true) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Find pages in the fulltext index containing the words, |
||
| 223 | * |
||
| 224 | * The search words must be pre-tokenized, meaning only letters and |
||
| 225 | * numbers with an optional wildcard |
||
| 226 | * |
||
| 227 | * The returned array will have the original tokens as key. The values |
||
| 228 | * in the returned list is an array with the page names as keys and the |
||
| 229 | * number of times that token appears on the page as value. |
||
| 230 | * |
||
| 231 | * @param array $tokens list of words to search for |
||
| 232 | * @return array list of page names with usage counts |
||
| 233 | * |
||
| 234 | * @author Tom N Harris <[email protected]> |
||
| 235 | * @author Andreas Gohr <[email protected]> |
||
| 236 | */ |
||
| 237 | public function lookupWords(&$tokens) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Find the index ID of each search term |
||
| 279 | * |
||
| 280 | * The query terms should only contain valid characters, with a '*' at |
||
| 281 | * either the beginning or end of the word (or both). |
||
| 282 | * The $result parameter can be used to merge the index locations with |
||
| 283 | * the appropriate query term. |
||
| 284 | * |
||
| 285 | * @param array $words The query terms. |
||
| 286 | * @param array $result Set to word => array("length*id" ...) |
||
| 287 | * @return array Set to length => array(id ...) |
||
| 288 | * |
||
| 289 | * @author Tom N Harris <[email protected]> |
||
| 290 | */ |
||
| 291 | protected function getIndexWords(&$words, &$result) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Get the word lengths that have been indexed |
||
| 373 | * |
||
| 374 | * Reads the index directory and returns an array of lengths |
||
| 375 | * that there are indices for. |
||
| 376 | * |
||
| 377 | * @author YoBoY <[email protected]> |
||
| 378 | * |
||
| 379 | * @param array|int $filter |
||
| 380 | * @return array |
||
| 381 | */ |
||
| 382 | public function getIndexLengths($filter) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get the list of lengths indexed in the wiki |
||
| 408 | * |
||
| 409 | * Read the index directory or a cache file and returns |
||
| 410 | * a sorted array of lengths of the words used in the wiki. |
||
| 411 | * |
||
| 412 | * @author YoBoY <[email protected]> |
||
| 413 | * |
||
| 414 | * @return array |
||
| 415 | */ |
||
| 416 | public function listIndexLengths() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Return a list of words sorted by number of times used |
||
| 466 | * |
||
| 467 | * @param int $min bottom frequency threshold |
||
| 468 | * @param int $max upper frequency limit. No limit if $max<$min |
||
| 469 | * @param int $minlen minimum length of words to count |
||
| 470 | * @return array list of words as the keys and frequency as value |
||
| 471 | * |
||
| 472 | * @author Tom N Harris <[email protected]> |
||
| 473 | */ |
||
| 474 | public function histogram($min=1, $max=0, $minlen=3) |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Clear the Fulltext Index |
||
| 481 | * |
||
| 482 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 483 | * @return bool If the index has been cleared successfully |
||
| 484 | * @throws Exception\IndexLockException |
||
| 485 | */ |
||
| 486 | public function clear($requireLock = true) |
||
| 503 | } |
||
| 504 |