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 | // page to be indexed |
||
| 23 | protected $page; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Indexer constructor |
||
| 27 | * |
||
| 28 | * @param string $page name of the page to index |
||
| 29 | */ |
||
| 30 | public function __construct($page = null) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Dispatch Indexing request for the page, called by TaskRunner::runIndexer() |
||
| 37 | * |
||
| 38 | * @param bool $verbose print status messages |
||
| 39 | * @param bool $force force reindexing even when the index is up to date |
||
| 40 | * @return bool If the function completed successfully |
||
| 41 | * |
||
| 42 | * @throws IndexAccessException |
||
| 43 | * @throws IndexLockException |
||
| 44 | * @throws IndexWriteException |
||
| 45 | * @author Satoshi Sahara <[email protected]> |
||
| 46 | * @author Tom N Harris <[email protected]> |
||
| 47 | */ |
||
| 48 | public function dispatch($verbose = false, $force = false) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Version of the indexer taking into consideration the external tokenizer. |
||
| 65 | * The indexer is only compatible with data written by the same version. |
||
| 66 | * |
||
| 67 | * @triggers INDEXER_VERSION_GET |
||
| 68 | * Plugins that modify what gets indexed should hook this event and |
||
| 69 | * add their version info to the event data like so: |
||
| 70 | * $data[$plugin_name] = $plugin_version; |
||
| 71 | * |
||
| 72 | * @author Tom N Harris <[email protected]> |
||
| 73 | * @author Michael Hamann <[email protected]> |
||
| 74 | * |
||
| 75 | * @return int|string |
||
| 76 | */ |
||
| 77 | public function getVersion() |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Adds/updates the search index for the given page |
||
| 98 | * |
||
| 99 | * Locking is handled internally. |
||
| 100 | * |
||
| 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 IndexAccessException |
||
| 106 | * @throws IndexLockException |
||
| 107 | * @throws IndexWriteException |
||
| 108 | * @author Satoshi Sahara <[email protected]> |
||
| 109 | * @author Tom N Harris <[email protected]> |
||
| 110 | */ |
||
| 111 | public function addPage($verbose = false, $force = false) |
||
| 191 | |||
| 192 | /** |
||
| 193 | * Remove a page from the index |
||
| 194 | * |
||
| 195 | * Erases entries in all known indexes. Locking is handled internally. |
||
| 196 | * |
||
| 197 | * @param bool $verbose print status messages |
||
| 198 | * @param bool $force force reindexing even when the index is up to date |
||
| 199 | * @return bool If the function completed successfully |
||
| 200 | * |
||
| 201 | * @throws IndexAccessException |
||
| 202 | * @throws IndexLockException |
||
| 203 | * @throws IndexWriteException |
||
| 204 | * @author Satoshi Sahara <[email protected]> |
||
| 205 | * @author Tom N Harris <[email protected]> |
||
| 206 | */ |
||
| 207 | public function deletePage($verbose = false, $force = false) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Rename a page in the search index without changing the indexed content. |
||
| 251 | * This function doesn't check if the old or new name exists in the filesystem. |
||
| 252 | * It returns an error if the old page isn't in the page list of the indexer |
||
| 253 | * and it deletes all previously indexed content of the new page. |
||
| 254 | * |
||
| 255 | * @param string $oldpage The old page name |
||
| 256 | * @param string $newpage The new page name |
||
| 257 | * @return bool If the page was successfully renamed |
||
| 258 | * @throws IndexLockException |
||
| 259 | * @throws IndexWriteException |
||
| 260 | */ |
||
| 261 | public function renamePage($oldpage, $newpage) |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Clear the Page Index |
||
| 290 | * |
||
| 291 | * @param bool $requireLock should be false only if the caller is resposible for index lock |
||
| 292 | * @return bool If the index has been cleared successfully |
||
| 293 | * @throws Exception\IndexLockException |
||
| 294 | */ |
||
| 295 | public function clear($requireLock = true) |
||
| 315 | |||
| 316 | } |
||
| 317 |