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 = '') { |
||
181 | |||
182 | |||
183 | /** |
||
184 | * @param Query $query |
||
185 | */ |
||
186 | private function addExtractedQueryTermsForMoreLikeThis($query, &$highlights) { |
||
200 | |||
201 | |||
202 | /** |
||
203 | * @param Search $search |
||
204 | * @param Query $query |
||
205 | */ |
||
206 | private function addTypesToSearch(&$search, $types, $query) { |
||
217 | |||
218 | |||
219 | private function getHighlightingConfig() { |
||
258 | |||
259 | |||
260 | private function checkIfTestMode(&$search) { |
||
265 | |||
266 | |||
267 | private function checkForTermsMoreLikeThis($elasticaQuery, $search) { |
||
301 | |||
302 | |||
303 | /** |
||
304 | * Ensure that the index is present |
||
305 | */ |
||
306 | protected function ensureIndex() { |
||
312 | |||
313 | |||
314 | /** |
||
315 | * Ensure that there is a mapping present |
||
316 | * |
||
317 | * @param \Elastica\Type Type object |
||
318 | * @param SilverStripe\Elastica\Searchable DataObject that implements Searchable |
||
319 | * @return \Elastica\Mapping Mapping object |
||
320 | */ |
||
321 | protected function ensureMapping(\Elastica\Type $type, \DataObject $record) { |
||
331 | |||
332 | |||
333 | /** |
||
334 | * Either creates or updates a record in the index. |
||
335 | * |
||
336 | * @param Searchable $record |
||
337 | */ |
||
338 | public function index($record) { |
||
359 | |||
360 | |||
361 | /** |
||
362 | * Begins a bulk indexing operation where documents are buffered rather than |
||
363 | * indexed immediately. |
||
364 | */ |
||
365 | public function startBulkIndex() { |
||
368 | |||
369 | |||
370 | public function listIndexes($trace) { |
||
378 | |||
379 | |||
380 | /** |
||
381 | * Ends the current bulk index operation and indexes the buffered documents. |
||
382 | */ |
||
383 | public function endBulkIndex() { |
||
412 | |||
413 | |||
414 | /** |
||
415 | * Deletes a record from the index. |
||
416 | * |
||
417 | * @param Searchable $record |
||
418 | */ |
||
419 | public function remove($record) { |
||
425 | |||
426 | |||
427 | /** |
||
428 | * Creates the index and the type mappings. |
||
429 | */ |
||
430 | public function define() { |
||
446 | |||
447 | |||
448 | /** |
||
449 | * Refresh an array of records in the index |
||
450 | * |
||
451 | * @param array $records |
||
452 | */ |
||
453 | protected function refreshRecords($records) { |
||
460 | |||
461 | |||
462 | /** |
||
463 | * Get a List of all records by class. Get the "Live data" If the class has the "Versioned" extension |
||
464 | * |
||
465 | * @param string $class Class Name |
||
466 | * @param int $pageSize Optional page size, only a max of this number of records returned |
||
467 | * @param int $page Page number to return |
||
468 | * @return \DataList $records |
||
469 | */ |
||
470 | protected function recordsByClassConsiderVersioned($class, $pageSize = 0, $page = 0) { |
||
489 | |||
490 | |||
491 | /** |
||
492 | * Refresh the records of a given class within the search index |
||
493 | * |
||
494 | * @param string $class Class Name |
||
495 | */ |
||
496 | protected function refreshClass($class) { |
||
510 | |||
511 | |||
512 | /** |
||
513 | * Re-indexes each record in the index. |
||
514 | */ |
||
515 | public function refresh() { |
||
556 | |||
557 | |||
558 | /** |
||
559 | * Reset the current index |
||
560 | */ |
||
561 | public function reset() { |
||
566 | |||
567 | |||
568 | private function createIndex() { |
||
573 | |||
574 | |||
575 | /** |
||
576 | * Get the index settings for the current locale |
||
577 | * @return IndexSettings index settings for the current locale |
||
578 | */ |
||
579 | public function getIndexSettingsForCurrentLocale() { |
||
591 | |||
592 | |||
593 | /** |
||
594 | * Gets the classes which are indexed (i.e. have the extension applied). |
||
595 | * |
||
596 | * @return array |
||
597 | */ |
||
598 | public function getIndexedClasses() { |
||
627 | |||
628 | |||
629 | /** |
||
630 | * Get the number of indexing requests made. Used for testing bulk indexing |
||
631 | * @return integer indexing request counter |
||
632 | */ |
||
633 | public function getIndexingRequestCtr() { |
||
636 | |||
637 | |||
638 | /** |
||
639 | * Get the term vectors in the index for the provided Searchable is_object |
||
640 | * @param Searchable $searchable An object that implements Searchable |
||
641 | * @return array array of field name to terms indexed |
||
642 | */ |
||
643 | public function getTermVectors($searchable) { |
||
684 | } |
||
685 |
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: