Conditions | 16 |
Paths | 320 |
Total Lines | 132 |
Code Lines | 77 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types = 1); |
||
33 | public function search(?string $q): SearchResults |
||
34 | { |
||
35 | $q = \is_null($q) |
||
36 | ? '' |
||
37 | : $q; |
||
38 | if ($this->searchType === SearchParamTypes::OR) { |
||
39 | $q = $this->makeQueryOr($q); |
||
40 | } |
||
41 | $startTime = \microtime(true); |
||
42 | $client = new Client(); |
||
43 | $manticoreClient = $client->getConnection(); |
||
44 | |||
45 | $searcher = new Search($manticoreClient); |
||
46 | $searcher->setIndex($this->indexName); |
||
47 | |||
48 | $searcher->limit($this->pageSize); |
||
49 | $offset=$this->pageSize * ($this->page-1); |
||
50 | $searcher->offset($offset); |
||
51 | |||
52 | $indexes = new Indexes(); |
||
53 | $index = $indexes->getIndex($this->indexName); |
||
54 | $hasManyFieldsDetails = $index->getHasManyFields(); |
||
55 | $hasManyFieldsNames = \array_keys($hasManyFieldsDetails); |
||
56 | $hasOneFieldsDetails = $index->getHasOneFields(); |
||
57 | $hasOneFieldsNames = \array_keys($hasOneFieldsDetails); |
||
58 | |||
59 | $searcher->highlight( |
||
60 | [], |
||
61 | ['pre_tags' => '<b>', 'post_tags'=>'</b>'] |
||
62 | ); |
||
63 | |||
64 | |||
65 | $fieldHelper = new FieldHelper(); |
||
66 | foreach ($this->filters as $key => $value) { |
||
67 | if ($key === 'q' || $key === 'start') { |
||
68 | continue; |
||
69 | } |
||
70 | $typedValue = $fieldHelper->getFieldValueCorrectlyTyped($index, $key, $value); |
||
71 | |||
72 | if (\in_array($key, $hasManyFieldsNames, true)) { |
||
73 | $searcher->filter($key, 'in', $typedValue); |
||
74 | } elseif (\in_array($key, $hasOneFieldsNames, true)) { |
||
75 | $searcher->filter($key, 'equals', ($typedValue)); |
||
76 | } else { |
||
77 | $searcher->filter($key, 'equals', $typedValue); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | // @todo Deal with subsequent params |
||
82 | foreach ($this->facettedTokens as $facetName) { |
||
83 | // manticore errors out with no error message if the facet name is not lowercase. The second param is an |
||
84 | // alias, use the correctly capitalized version of the fact |
||
85 | $searcher->facet(\strtolower($facetName), $facetName, 1000); |
||
86 | } |
||
87 | |||
88 | // add has many |
||
89 | foreach ($this->hasManyTokens as $facetName) { |
||
90 | // manticore errors out with no error message if the facet name is not lowercase. The second param is an |
||
91 | // alias, use the correctly capitalized version of the fact |
||
92 | $searcher->facet(\strtolower($facetName), $facetName, 1000); |
||
93 | } |
||
94 | |||
95 | $manticoreResult = $searcher->search($q)->get(); |
||
96 | $allFields = $this->getAllFields($index); |
||
97 | |||
98 | $ssResult = new ArrayList(); |
||
99 | while ($manticoreResult->valid()) { |
||
100 | $hit = $manticoreResult->current(); |
||
101 | $source = $hit->getData(); |
||
102 | $ssDataObject = new DataObject(); |
||
103 | |||
104 | $this->populateSearchResult($ssDataObject, $allFields, $source); |
||
105 | |||
106 | // manticore lowercases fields, so as above normalize them back to the SS fieldnames |
||
107 | $highlights = $hit->getHighlight(); |
||
108 | $fieldsToHighlight = $index->getHighlightedFields(); |
||
109 | $this->addHighlights($ssDataObject, $allFields, $highlights, $fieldsToHighlight); |
||
110 | |||
111 | $ssDataObject->ID = $hit->getId(); |
||
112 | $ssResult->push($ssDataObject); |
||
113 | $manticoreResult->next(); |
||
114 | } |
||
115 | |||
116 | // we now need to standardize the output returned |
||
117 | |||
118 | $searchResults = new SearchResults(); |
||
119 | $searchResults->setRecords($ssResult); |
||
120 | $searchResults->setPage($this->page); |
||
121 | $searchResults->setPageSize($this->pageSize); |
||
122 | $searchResults->setQuery($q); |
||
123 | $searchResults->setTotalNumberOfResults($manticoreResult->getTotal()); |
||
124 | |||
125 | // create facet result objects |
||
126 | $manticoreFacets = $manticoreResult->getFacets(); |
||
127 | |||
128 | $hasManyFields = $index->getHasManyFields(); |
||
129 | |||
130 | if (!\is_null($manticoreFacets)) { |
||
131 | $facetTitles = \array_keys($manticoreFacets); |
||
132 | |||
133 | /** @var string $facetTitle */ |
||
134 | foreach ($facetTitles as $facetTitle) { |
||
135 | $facet = new Facet($facetTitle); |
||
136 | |||
137 | // the BY functionality of facets has not yet been implemented, as such database calls required |
||
138 | if (\in_array($facetTitle, $this->hasManyTokens, true)) { |
||
139 | $field = $hasManyFields[$facetTitle]['field']; |
||
140 | $clazz = $hasManyFields[$facetTitle]['class']; |
||
141 | |||
142 | foreach ($manticoreFacets[$facetTitle]['buckets'] as $count) { |
||
143 | $facetClassInstance = DataObject::get_by_id($clazz, $count['key']); |
||
144 | // @phpstan-ignore-next-line |
||
145 | $facet->addFacetCount($facetClassInstance->$field, $count['doc_count']); |
||
146 | } |
||
147 | } else { |
||
148 | // use values as is |
||
149 | foreach ($manticoreFacets[$facetTitle]['buckets'] as $count) { |
||
150 | $facet->addFacetCount($count['key'], $count['doc_count']); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | |||
155 | $searchResults->addFacet($facet); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | $endTime = \microtime(true); |
||
160 | $delta = $endTime - $startTime; |
||
161 | $delta = \round(1000*$delta)/1000; |
||
162 | $searchResults->setTime($delta); |
||
163 | |||
164 | return $searchResults; |
||
165 | } |
||
381 |