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 |
||
| 20 | class Indexer extends AbstractIndex |
||
| 21 | { |
||
| 22 | /** @var Indexer $instance */ |
||
| 23 | protected static $instance = null; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Get new or existing singleton instance of the Indexer |
||
| 27 | * |
||
| 28 | * @return Indexer |
||
| 29 | */ |
||
| 30 | public static function getInstance() |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Dispatch Indexing request for the page, called by TaskRunner::runIndexer() |
||
| 40 | * |
||
| 41 | * @param string $page name of the page to index |
||
| 42 | * @param bool $verbose print status messages |
||
| 43 | * @param bool $force force reindexing even when the index is up to date |
||
| 44 | * @return bool If the function completed successfully |
||
| 45 | * |
||
| 46 | * @throws IndexLockException |
||
| 47 | * @throws IndexWriteException |
||
| 48 | * @author Satoshi Sahara <[email protected]> |
||
| 49 | * @author Tom N Harris <[email protected]> |
||
| 50 | */ |
||
| 51 | public function dispatch($page, $verbose = false, $force = false) |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Version of the indexer taking into consideration the external tokenizer. |
||
| 64 | * The indexer is only compatible with data written by the same version. |
||
| 65 | * |
||
| 66 | * @triggers INDEXER_VERSION_GET |
||
| 67 | * Plugins that modify what gets indexed should hook this event and |
||
| 68 | * add their version info to the event data like so: |
||
| 69 | * $data[$plugin_name] = $plugin_version; |
||
| 70 | * |
||
| 71 | * @author Tom N Harris <[email protected]> |
||
| 72 | * @author Michael Hamann <[email protected]> |
||
| 73 | * |
||
| 74 | * @return int|string |
||
| 75 | */ |
||
| 76 | public function getVersion() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Adds/updates the search index for the given page |
||
| 97 | * |
||
| 98 | * Locking is handled internally. |
||
| 99 | * |
||
| 100 | * @param string $page name of the page to index |
||
| 101 | * @param bool $verbose print status messages |
||
| 102 | * @param bool $force force reindexing even when the index is up to date |
||
| 103 | * @return bool If the function completed successfully |
||
| 104 | * |
||
| 105 | * @throws IndexLockException |
||
| 106 | * @throws IndexWriteException |
||
| 107 | * @author Satoshi Sahara <[email protected]> |
||
| 108 | * @author Tom N Harris <[email protected]> |
||
| 109 | */ |
||
| 110 | 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 | * @throws IndexLockException |
||
| 198 | * @throws IndexWriteException |
||
| 199 | * @author Satoshi Sahara <[email protected]> |
||
| 200 | * @author Tom N Harris <[email protected]> |
||
| 201 | */ |
||
| 202 | public function deletePage($page, $verbose = false, $force = false) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Rename a page in the search index without changing the indexed content. |
||
| 240 | * This function doesn't check if the old or new name exists in the filesystem. |
||
| 241 | * It returns an error if the old page isn't in the page list of the indexer |
||
| 242 | * and it deletes all previously indexed content of the new page. |
||
| 243 | * |
||
| 244 | * @param string $oldpage The old page name |
||
| 245 | * @param string $newpage The new page name |
||
| 246 | * @return bool If the page was successfully renamed |
||
| 247 | * @throws IndexLockException |
||
| 248 | * @throws IndexWriteException |
||
| 249 | */ |
||
| 250 | public function renamePage($oldpage, $newpage) |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Clear the Page Index |
||
| 279 | * |
||
| 280 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 281 | * @return bool If the index has been cleared successfully |
||
| 282 | * @throws Exception\IndexLockException |
||
| 283 | */ |
||
| 284 | public function clear($requireLock = true) |
||
| 306 | |||
| 307 | } |
||
| 308 |