Complex classes like Indexer 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 Indexer, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 19 | class Indexer extends AbstractIndex |
||
| 20 | { |
||
| 21 | /** @var Indexer $instance */ |
||
| 22 | protected static $instance = null; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Get new or existing singleton instance of the Indexer |
||
| 26 | * |
||
| 27 | * @return Indexer |
||
| 28 | */ |
||
| 29 | public static function getInstance() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Dispatch Indexing request for the page, called by TaskRunner::runIndexer() |
||
| 39 | * |
||
| 40 | * @param string $page name of the page to index |
||
| 41 | * @param bool $verbose print status messages |
||
| 42 | * @param bool $force force reindexing even when the index is up to date |
||
| 43 | * @return bool If the function completed successfully |
||
| 44 | * |
||
| 45 | * @throws IndexAccessException |
||
| 46 | * @throws SearchException |
||
| 47 | * @author Satoshi Sahara <[email protected]> |
||
| 48 | * @author Tom N Harris <[email protected]> |
||
| 49 | */ |
||
| 50 | public function dispatch($page, $verbose = false, $force = false) |
||
| 51 | { |
||
| 52 | // check if page was deleted but is still in the index |
||
| 53 | if (!page_exists($page)) { |
||
| 54 | return $this->deletePage($page, $verbose, $force); |
||
| 55 | } |
||
| 56 | |||
| 57 | // update search index |
||
| 58 | return $this->addPage($page, $verbose, $force); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Version of the indexer taking into consideration the external tokenizer. |
||
| 63 | * The indexer is only compatible with data written by the same version. |
||
| 64 | * |
||
| 65 | * @triggers INDEXER_VERSION_GET |
||
| 66 | * Plugins that modify what gets indexed should hook this event and |
||
| 67 | * add their version info to the event data like so: |
||
| 68 | * $data[$plugin_name] = $plugin_version; |
||
| 69 | * |
||
| 70 | * @author Tom N Harris <[email protected]> |
||
| 71 | * @author Michael Hamann <[email protected]> |
||
| 72 | * |
||
| 73 | * @return int|string |
||
| 74 | */ |
||
| 75 | public function getVersion() |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Adds/updates the search index for the given page |
||
| 96 | * |
||
| 97 | * Locking is handled internally. |
||
| 98 | * |
||
| 99 | * @param string $page name of the page to index |
||
| 100 | * @param bool $verbose print status messages |
||
| 101 | * @param bool $force force reindexing even when the index is up to date |
||
| 102 | * @return bool If the function completed successfully |
||
| 103 | * |
||
| 104 | * @throws SearchException |
||
| 105 | * @author Satoshi Sahara <[email protected]> |
||
| 106 | * @author Tom N Harris <[email protected]> |
||
| 107 | */ |
||
| 108 | public function addPage($page, $verbose = false, $force = false) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Remove a page from the index |
||
| 191 | * |
||
| 192 | * Erases entries in all known indexes. Locking is handled internally. |
||
| 193 | * |
||
| 194 | * @param string $page name of the page to index |
||
| 195 | * @param bool $verbose print status messages |
||
| 196 | * @param bool $force force reindexing even when the index is up to date |
||
| 197 | * @return bool If the function completed successfully |
||
| 198 | * |
||
| 199 | * @throws Exception\IndexLockException |
||
| 200 | * @author Satoshi Sahara <[email protected]> |
||
| 201 | * @author Tom N Harris <[email protected]> |
||
| 202 | */ |
||
| 203 | public function deletePage($page, $verbose = false, $force = false) |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Rename a page in the search index without changing the indexed content. |
||
| 246 | * This function doesn't check if the old or new name exists in the filesystem. |
||
| 247 | * It returns an error if the old page isn't in the page list of the indexer |
||
| 248 | * and it deletes all previously indexed content of the new page. |
||
| 249 | * |
||
| 250 | * @param string $oldpage The old page name |
||
| 251 | * @param string $newpage The new page name |
||
| 252 | * @return bool If the page was successfully renamed |
||
| 253 | * @throws Exception\IndexLockException |
||
| 254 | */ |
||
| 255 | public function renamePage($oldpage, $newpage) |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Clear the Page Index |
||
| 284 | * |
||
| 285 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 286 | * @return bool If the index has been cleared successfully |
||
| 287 | * @throws Exception\IndexLockException |
||
| 288 | */ |
||
| 289 | public function clear($requireLock = true) |
||
| 311 | |||
| 312 | } |
||
| 313 |