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 | * @author Tom N Harris <[email protected]> |
||
| 46 | * @author Satoshi Sahara <[email protected]> |
||
| 47 | */ |
||
| 48 | public function dispatch($page, $verbose = false, $force = false) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Version of the indexer taking into consideration the external tokenizer. |
||
| 61 | * The indexer is only compatible with data written by the same version. |
||
| 62 | * |
||
| 63 | * @triggers INDEXER_VERSION_GET |
||
| 64 | * Plugins that modify what gets indexed should hook this event and |
||
| 65 | * add their version info to the event data like so: |
||
| 66 | * $data[$plugin_name] = $plugin_version; |
||
| 67 | * |
||
| 68 | * @author Tom N Harris <[email protected]> |
||
| 69 | * @author Michael Hamann <[email protected]> |
||
| 70 | * |
||
| 71 | * @return int|string |
||
| 72 | */ |
||
| 73 | public function getVersion() |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Adds/updates the search index for the given page |
||
| 94 | * |
||
| 95 | * Locking is handled internally. |
||
| 96 | * |
||
| 97 | * @param string $page name of the page to index |
||
| 98 | * @param bool $verbose print status messages |
||
| 99 | * @param bool $force force reindexing even when the index is up to date |
||
| 100 | * @return bool If the function completed successfully |
||
| 101 | * |
||
| 102 | * @author Tom N Harris <[email protected]> |
||
| 103 | * @author Satoshi Sahara <[email protected]> |
||
| 104 | */ |
||
| 105 | public function addPage($page, $verbose = false, $force = false) |
||
| 186 | |||
| 187 | /** |
||
| 188 | * Remove a page from the index |
||
| 189 | * |
||
| 190 | * Erases entries in all known indexes. Locking is handled internally. |
||
| 191 | * |
||
| 192 | * @param string $page name of the page to index |
||
| 193 | * @param bool $verbose print status messages |
||
| 194 | * @param bool $force force reindexing even when the index is up to date |
||
| 195 | * @return bool If the function completed successfully |
||
| 196 | * |
||
| 197 | * @author Tom N Harris <[email protected]> |
||
| 198 | * @author Satoshi Sahara <[email protected]> |
||
| 199 | */ |
||
| 200 | public function deletePage($page, $verbose = false, $force = false) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Rename a page in the search index without changing the indexed content. |
||
| 243 | * This function doesn't check if the old or new name exists in the filesystem. |
||
| 244 | * It returns an error if the old page isn't in the page list of the indexer |
||
| 245 | * and it deletes all previously indexed content of the new page. |
||
| 246 | * |
||
| 247 | * @param string $oldpage The old page name |
||
| 248 | * @param string $newpage The new page name |
||
| 249 | * @return bool If the page was successfully renamed |
||
| 250 | */ |
||
| 251 | public function renamePage($oldpage, $newpage) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Clear the Page Index |
||
| 280 | * |
||
| 281 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 282 | * @return bool If the index has been cleared successfully |
||
| 283 | */ |
||
| 284 | public function clear($requireLock = true) |
||
| 306 | |||
| 307 | } |
||
| 308 |