| Conditions | 8 |
| Paths | 32 |
| Total Lines | 86 |
| Code Lines | 52 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 8 | ||
| 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); |
||
| 32 | public function search(?string $q): SearchResults |
||
| 33 | { |
||
| 34 | $q = \is_null($q) |
||
| 35 | ? '' |
||
| 36 | : $q; |
||
| 37 | if ($this->searchType === SearchParamTypes::OR) { |
||
| 38 | $q = $this->makeQueryOr($q); |
||
| 39 | } |
||
| 40 | $startTime = \microtime(true); |
||
| 41 | $client = new Client(); |
||
| 42 | $manticoreClient = $client->getConnection(); |
||
| 43 | |||
| 44 | $searcher = new Search($manticoreClient); |
||
| 45 | $searcher->setIndex($this->indexName); |
||
| 46 | |||
| 47 | $searcher->limit($this->pageSize); |
||
| 48 | $offset=$this->pageSize * ($this->page-1); |
||
| 49 | $searcher->offset($offset); |
||
| 50 | |||
| 51 | $indexes = new Indexes(); |
||
| 52 | $index = $indexes->getIndex($this->indexName); |
||
| 53 | |||
| 54 | $searcher->highlight( |
||
| 55 | [], |
||
| 56 | ['pre_tags' => '<b>', 'post_tags'=>'</b>'] |
||
| 57 | ); |
||
| 58 | |||
| 59 | // @todo Deal with subsequent params |
||
| 60 | foreach ($this->facettedTokens as $facetName) { |
||
| 61 | // manticore errors out with no error message if the facet name is not lowercase. The second param is an |
||
| 62 | // alias, use the correctly capitalized version of the fact |
||
| 63 | $searcher->facet(\strtolower($facetName), $facetName); |
||
| 64 | } |
||
| 65 | |||
| 66 | $manticoreResult = $searcher->search($q)->get(); |
||
| 67 | $allFields = $this->getAllFields($index); |
||
| 68 | |||
| 69 | $ssResult = new ArrayList(); |
||
| 70 | while ($manticoreResult->valid()) { |
||
| 71 | $hit = $manticoreResult->current(); |
||
| 72 | $source = $hit->getData(); |
||
| 73 | $ssDataObject = new DataObject(); |
||
| 74 | |||
| 75 | $this->populateSearchResult($ssDataObject, $allFields, $source); |
||
| 76 | |||
| 77 | // manticore lowercases fields, so as above normalize them back to the SS fieldnames |
||
| 78 | $highlights = $hit->getHighlight(); |
||
| 79 | $fieldsToHighlight = $index->getHighlightedFields(); |
||
| 80 | $this->addHighlights($ssDataObject, $allFields, $highlights, $fieldsToHighlight); |
||
| 81 | |||
| 82 | $ssDataObject->ID = $hit->getId(); |
||
| 83 | $ssResult->push($ssDataObject); |
||
| 84 | $manticoreResult->next(); |
||
| 85 | } |
||
| 86 | |||
| 87 | // we now need to standardize the output returned |
||
| 88 | |||
| 89 | $searchResults = new SearchResults(); |
||
| 90 | $searchResults->setRecords($ssResult); |
||
| 91 | $searchResults->setPage($this->page); |
||
| 92 | $searchResults->setPageSize($this->pageSize); |
||
| 93 | $searchResults->setQuery($q); |
||
| 94 | $searchResults->setTotalNumberOfResults($manticoreResult->getTotal()); |
||
| 95 | |||
| 96 | // create facet result objects |
||
| 97 | $manticoreFacets = $manticoreResult->getFacets(); |
||
| 98 | |||
| 99 | if (!\is_null($manticoreFacets)) { |
||
| 100 | $facetTitles = \array_keys($manticoreFacets); |
||
| 101 | |||
| 102 | /** @var string $facetTitle */ |
||
| 103 | foreach ($facetTitles as $facetTitle) { |
||
| 104 | $facet = new Facet($facetTitle); |
||
| 105 | foreach ($manticoreFacets[$facetTitle]['buckets'] as $count) { |
||
| 106 | $facet->addFacetCount($count['key'], $count['doc_count']); |
||
| 107 | } |
||
| 108 | $searchResults->addFacet($facet); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | $endTime = \microtime(true); |
||
| 113 | $delta = $endTime - $startTime; |
||
| 114 | $delta = \round(1000*$delta)/1000; |
||
| 115 | $searchResults->setTime($delta); |
||
| 116 | |||
| 117 | return $searchResults; |
||
| 118 | } |
||
| 325 |