Complex classes like ElasticaService 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 ElasticaService, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class ElasticaService { |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var \Elastica\Document[] |
||
| 17 | */ |
||
| 18 | protected $buffer = array(); |
||
| 19 | |||
| 20 | |||
| 21 | /** |
||
| 22 | * @var bool controls whether indexing operations are buffered or not |
||
| 23 | */ |
||
| 24 | protected $buffered = false; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @var \Elastica\Client Elastica Client object |
||
| 29 | */ |
||
| 30 | private $client; |
||
| 31 | |||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string index name |
||
| 35 | */ |
||
| 36 | private $indexName; |
||
| 37 | |||
| 38 | |||
| 39 | /** |
||
| 40 | * The code of the locale being indexed or searched |
||
| 41 | * @var string e.g. th_TH, en_US |
||
| 42 | */ |
||
| 43 | private $locale; |
||
| 44 | |||
| 45 | |||
| 46 | /** |
||
| 47 | * Mapping of DataObject ClassName and whether it is in the SiteTree or not |
||
| 48 | * @var array $site_tree_classes; |
||
| 49 | */ |
||
| 50 | private static $site_tree_classes = array(); |
||
| 51 | |||
| 52 | |||
| 53 | /** |
||
| 54 | * Counter used to for testing, records indexing requests |
||
| 55 | * @var integer |
||
| 56 | */ |
||
| 57 | public static $indexing_request_ctr = 0; |
||
| 58 | |||
| 59 | |||
| 60 | /** |
||
| 61 | * Array of highlighted fields, e.g. Title, Title.standard. If this is empty then the |
||
| 62 | * ShowHighlight field of SearchableField is used to determine which fields to highlight |
||
| 63 | * @var array |
||
| 64 | */ |
||
| 65 | private $highlightedFields = array(); |
||
| 66 | |||
| 67 | |||
| 68 | /** |
||
| 69 | * The number of documents to index currently for this locale |
||
| 70 | * @var integer The number of documents left to index |
||
| 71 | */ |
||
| 72 | private $nDocumentsToIndexForLocale = 0; |
||
| 73 | |||
| 74 | |||
| 75 | /* |
||
| 76 | Set the highlight fields for subsequent searches |
||
| 77 | */ |
||
| 78 | public function setHighlightedFields($newHighlightedFields) { |
||
| 79 | $this->highlightedFields = $newHighlightedFields; |
||
| 80 | } |
||
| 81 | |||
| 82 | |||
| 83 | /* |
||
| 84 | Enable this to allow test classes not to be ignored when indexing |
||
| 85 | */ |
||
| 86 | public $test_mode = false; |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * @param \Elastica\Client $client |
||
| 91 | * @param string $newIndexName Name of the new index |
||
| 92 | */ |
||
| 93 | public function __construct(Client $client, $newIndexName) { |
||
| 98 | |||
| 99 | |||
| 100 | public function setTestMode($newTestMode) { |
||
| 101 | $this->test_mode = $newTestMode; |
||
| 102 | } |
||
| 103 | |||
| 104 | |||
| 105 | /** |
||
| 106 | * @return \Elastica\Client |
||
| 107 | */ |
||
| 108 | public function getClient() { |
||
| 111 | |||
| 112 | |||
| 113 | /** |
||
| 114 | * @return \Elastica\Index |
||
| 115 | */ |
||
| 116 | public function getIndex() { |
||
| 120 | |||
| 121 | |||
| 122 | public function setLocale($newLocale) { |
||
| 125 | |||
| 126 | public function getIndexName() { |
||
| 127 | return $this->indexName; |
||
| 128 | } |
||
| 129 | |||
| 130 | private function getLocaleIndexName() { |
||
| 136 | |||
| 137 | |||
| 138 | /** |
||
| 139 | * Performs a search query and returns a result list. |
||
| 140 | * |
||
| 141 | * @param \Elastica\Query|string|array $query |
||
| 142 | * @param string|array $types List of comma separated SilverStripe classes to search, or blank for all |
||
| 143 | * @return \Elastica\ResultList |
||
| 144 | */ |
||
| 145 | public function search($query, $types = '') { |
||
| 187 | |||
| 188 | |||
| 189 | private function addExtractedQueryTermsForMoreLikeThis($query, &$highlights) { |
||
| 203 | |||
| 204 | |||
| 205 | private function addTypesToSearch(&$search, $type) { |
||
| 216 | |||
| 217 | |||
| 218 | private function getHighlightingConfig() { |
||
| 257 | |||
| 258 | |||
| 259 | private function checkForTermsMoreLikeThis($elasticaQuery, $search) { |
||
| 295 | |||
| 296 | |||
| 297 | /** |
||
| 298 | * Ensure that the index is present |
||
| 299 | */ |
||
| 300 | protected function ensureIndex() { |
||
| 306 | |||
| 307 | |||
| 308 | /** |
||
| 309 | * Ensure that there is a mapping present |
||
| 310 | * |
||
| 311 | * @param \Elastica\Type Type object |
||
| 312 | * @param SilverStripe\Elastica\Searchable DataObject that implements Searchable |
||
| 313 | * @return \Elastica\Mapping Mapping object |
||
| 314 | */ |
||
| 315 | protected function ensureMapping(\Elastica\Type $type, \DataObject $record) { |
||
| 325 | |||
| 326 | |||
| 327 | /** |
||
| 328 | * Either creates or updates a record in the index. |
||
| 329 | * |
||
| 330 | * @param Searchable $record |
||
| 331 | */ |
||
| 332 | public function index($record) { |
||
| 353 | |||
| 354 | |||
| 355 | /** |
||
| 356 | * Begins a bulk indexing operation where documents are buffered rather than |
||
| 357 | * indexed immediately. |
||
| 358 | */ |
||
| 359 | public function startBulkIndex() { |
||
| 362 | |||
| 363 | |||
| 364 | public function listIndexes($trace) { |
||
| 372 | |||
| 373 | |||
| 374 | /** |
||
| 375 | * Ends the current bulk index operation and indexes the buffered documents. |
||
| 376 | */ |
||
| 377 | public function endBulkIndex() { |
||
| 406 | |||
| 407 | |||
| 408 | /** |
||
| 409 | * Deletes a record from the index. |
||
| 410 | * |
||
| 411 | * @param Searchable $record |
||
| 412 | */ |
||
| 413 | public function remove($record) { |
||
| 419 | |||
| 420 | |||
| 421 | /** |
||
| 422 | * Creates the index and the type mappings. |
||
| 423 | */ |
||
| 424 | public function define() { |
||
| 440 | |||
| 441 | |||
| 442 | /** |
||
| 443 | * Refresh an array of records in the index |
||
| 444 | * |
||
| 445 | * @param array $records |
||
| 446 | */ |
||
| 447 | protected function refreshRecords($records) { |
||
| 454 | |||
| 455 | |||
| 456 | /** |
||
| 457 | * Get a List of all records by class. Get the "Live data" If the class has the "Versioned" extension |
||
| 458 | * |
||
| 459 | * @param string $class Class Name |
||
| 460 | * @param int $pageSize Optional page size, only a max of this number of records returned |
||
| 461 | * @param int $page Page number to return |
||
| 462 | * @return \DataList $records |
||
| 463 | */ |
||
| 464 | protected function recordsByClassConsiderVersioned($class, $pageSize = 0, $page = 0) { |
||
| 483 | |||
| 484 | |||
| 485 | /** |
||
| 486 | * Refresh the records of a given class within the search index |
||
| 487 | * |
||
| 488 | * @param string $class Class Name |
||
| 489 | */ |
||
| 490 | protected function refreshClass($class) { |
||
| 504 | |||
| 505 | |||
| 506 | /** |
||
| 507 | * Re-indexes each record in the index. |
||
| 508 | */ |
||
| 509 | public function refresh() { |
||
| 550 | |||
| 551 | |||
| 552 | /** |
||
| 553 | * Reset the current index |
||
| 554 | */ |
||
| 555 | public function reset() { |
||
| 560 | |||
| 561 | |||
| 562 | private function createIndex() { |
||
| 567 | |||
| 568 | |||
| 569 | /** |
||
| 570 | * Get the index settings for the current locale |
||
| 571 | * @return IndexSettings index settings for the current locale |
||
| 572 | */ |
||
| 573 | public function getIndexSettingsForCurrentLocale() { |
||
| 585 | |||
| 586 | |||
| 587 | /** |
||
| 588 | * Gets the classes which are indexed (i.e. have the extension applied). |
||
| 589 | * |
||
| 590 | * @return array |
||
| 591 | */ |
||
| 592 | public function getIndexedClasses() { |
||
| 621 | |||
| 622 | |||
| 623 | /** |
||
| 624 | * Get the number of indexing requests made. Used for testing bulk indexing |
||
| 625 | * @return integer indexing request counter |
||
| 626 | */ |
||
| 627 | public function getIndexingRequestCtr() { |
||
| 630 | |||
| 631 | |||
| 632 | /** |
||
| 633 | * Get the term vectors in the index for the provided Searchable is_object |
||
| 634 | * @param Searchable $searchable An object that implements Searchable |
||
| 635 | * @return array array of field name to terms indexed |
||
| 636 | */ |
||
| 637 | public function getTermVectors($searchable) { |
||
| 678 | } |
||
| 679 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.