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 = '') { |
||
199 | |||
200 | |||
201 | private function addTypesToSearch(&$search, $type) { |
||
212 | |||
213 | |||
214 | private function getHighlightingConfig() { |
||
253 | |||
254 | |||
255 | private function checkForTermsMoreLikeThis($elasticaQuery, $search) { |
||
291 | |||
292 | |||
293 | /** |
||
294 | * Ensure that the index is present |
||
295 | */ |
||
296 | protected function ensureIndex() { |
||
302 | |||
303 | |||
304 | /** |
||
305 | * Ensure that there is a mapping present |
||
306 | * |
||
307 | * @param \Elastica\Type Type object |
||
308 | * @param SilverStripe\Elastica\Searchable DataObject that implements Searchable |
||
309 | * @return \Elastica\Mapping Mapping object |
||
310 | */ |
||
311 | protected function ensureMapping(\Elastica\Type $type, \DataObject $record) { |
||
321 | |||
322 | |||
323 | /** |
||
324 | * Either creates or updates a record in the index. |
||
325 | * |
||
326 | * @param Searchable $record |
||
327 | */ |
||
328 | public function index($record) { |
||
349 | |||
350 | |||
351 | /** |
||
352 | * Begins a bulk indexing operation where documents are buffered rather than |
||
353 | * indexed immediately. |
||
354 | */ |
||
355 | public function startBulkIndex() { |
||
358 | |||
359 | |||
360 | public function listIndexes($trace) { |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Ends the current bulk index operation and indexes the buffered documents. |
||
372 | */ |
||
373 | public function endBulkIndex() { |
||
402 | |||
403 | |||
404 | /** |
||
405 | * Deletes a record from the index. |
||
406 | * |
||
407 | * @param Searchable $record |
||
408 | */ |
||
409 | public function remove($record) { |
||
415 | |||
416 | |||
417 | /** |
||
418 | * Creates the index and the type mappings. |
||
419 | */ |
||
420 | public function define() { |
||
436 | |||
437 | |||
438 | /** |
||
439 | * Refresh an array of records in the index |
||
440 | * |
||
441 | * @param array $records |
||
442 | */ |
||
443 | protected function refreshRecords($records) { |
||
450 | |||
451 | |||
452 | /** |
||
453 | * Get a List of all records by class. Get the "Live data" If the class has the "Versioned" extension |
||
454 | * |
||
455 | * @param string $class Class Name |
||
456 | * @param int $pageSize Optional page size, only a max of this number of records returned |
||
457 | * @param int $page Page number to return |
||
458 | * @return \DataList $records |
||
459 | */ |
||
460 | protected function recordsByClassConsiderVersioned($class, $pageSize = 0, $page = 0) { |
||
479 | |||
480 | |||
481 | /** |
||
482 | * Refresh the records of a given class within the search index |
||
483 | * |
||
484 | * @param string $class Class Name |
||
485 | */ |
||
486 | protected function refreshClass($class) { |
||
500 | |||
501 | |||
502 | /** |
||
503 | * Re-indexes each record in the index. |
||
504 | */ |
||
505 | public function refresh() { |
||
546 | |||
547 | |||
548 | /** |
||
549 | * Reset the current index |
||
550 | */ |
||
551 | public function reset() { |
||
556 | |||
557 | |||
558 | private function createIndex() { |
||
563 | |||
564 | |||
565 | /** |
||
566 | * Get the index settings for the current locale |
||
567 | * @return IndexSettings index settings for the current locale |
||
568 | */ |
||
569 | public function getIndexSettingsForCurrentLocale() { |
||
581 | |||
582 | |||
583 | /** |
||
584 | * Gets the classes which are indexed (i.e. have the extension applied). |
||
585 | * |
||
586 | * @return array |
||
587 | */ |
||
588 | public function getIndexedClasses() { |
||
617 | |||
618 | |||
619 | /** |
||
620 | * Get the number of indexing requests made. Used for testing bulk indexing |
||
621 | * @return integer indexing request counter |
||
622 | */ |
||
623 | public function getIndexingRequestCtr() { |
||
626 | |||
627 | |||
628 | /** |
||
629 | * Get the term vectors in the index for the provided Searchable is_object |
||
630 | * @param Searchable $searchable An object that implements Searchable |
||
631 | * @return array array of field name to terms indexed |
||
632 | */ |
||
633 | public function getTermVectors($searchable) { |
||
674 | } |
||
675 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.