| Conditions | 15 |
| Paths | 324 |
| Total Lines | 122 |
| Code Lines | 71 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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); |
||
| 29 | public function search(?string $q): SearchResults |
||
| 30 | { |
||
| 31 | $startTime = \microtime(true); |
||
| 32 | $client = new Client(); |
||
| 33 | $manticoreClient = $client->getConnection(); |
||
| 34 | |||
| 35 | $searcher = new Search($manticoreClient); |
||
| 36 | $searcher->setIndex($this->indexName); |
||
| 37 | |||
| 38 | $searcher->limit($this->pageSize); |
||
| 39 | $offset=$this->pageSize * ($this->page-1); |
||
| 40 | $searcher->offset($offset); |
||
| 41 | |||
| 42 | $indexes = new Indexes(); |
||
| 43 | $index = $indexes->getIndex($this->indexName); |
||
| 44 | |||
| 45 | $searcher->highlight( |
||
| 46 | [], |
||
| 47 | ['pre_tags' => '<b>', 'post_tags'=>'</b>'] |
||
| 48 | ); |
||
| 49 | |||
| 50 | $q = \is_null($q) |
||
| 51 | ? '' |
||
| 52 | : $q; |
||
| 53 | |||
| 54 | $manticoreResult = $searcher->search($q)->get(); |
||
| 55 | |||
| 56 | $allFields = \array_merge( |
||
| 57 | $index->getFields(), |
||
| 58 | $index->getTokens(), |
||
| 59 | //$index->getHasManyFields(), |
||
| 60 | $index->getHasOneFields(), |
||
| 61 | $index->getStoredFields() |
||
| 62 | ); |
||
| 63 | |||
| 64 | |||
| 65 | $hasManyFields = $index->getHasManyFields(); |
||
| 66 | foreach (\array_keys($hasManyFields) as $key) { |
||
| 67 | $allFields[] = $key; |
||
| 68 | } |
||
| 69 | |||
| 70 | $ssResult = new ArrayList(); |
||
| 71 | while ($manticoreResult->valid()) { |
||
| 72 | $hit = $manticoreResult->current(); |
||
| 73 | $source = $hit->getData(); |
||
| 74 | //print_r($source); |
||
| 75 | $ssDataObject = new DataObject(); |
||
| 76 | |||
| 77 | $keys = \array_keys($source); |
||
| 78 | |||
| 79 | foreach ($keys as $key) { |
||
| 80 | $keyname = $key; |
||
| 81 | foreach ($allFields as $field) { |
||
| 82 | if (\strtolower($field) === $key) { |
||
| 83 | $keyname = $field; |
||
| 84 | |||
| 85 | break; |
||
| 86 | } |
||
| 87 | } |
||
| 88 | |||
| 89 | // @todo This is a hack as $Title is rendering the ID in the template |
||
| 90 | if ($keyname === 'Title') { |
||
| 91 | $keyname = 'ResultTitle'; |
||
| 92 | } elseif ($keyname === 'link') { |
||
| 93 | $keyname = 'Link'; |
||
| 94 | }; |
||
| 95 | |||
| 96 | /** @phpstan-ignore-next-line */ |
||
| 97 | $ssDataObject->$keyname = $source[$key]; |
||
| 98 | } |
||
| 99 | |||
| 100 | |||
| 101 | // manticore lowercases fields, so as above normalize them back to the SS fieldnames |
||
| 102 | $highlights = $hit->getHighlight(); |
||
| 103 | $highlightsSS = []; |
||
| 104 | |||
| 105 | $fieldsToHighlight = $index->getHighlightedFields(); |
||
| 106 | |||
| 107 | $keys = \array_keys($highlights); |
||
| 108 | foreach ($keys as $key) { |
||
| 109 | if (!isset($highlights[$key]) || !\in_array($key, $fieldsToHighlight, true)) { |
||
| 110 | continue; |
||
| 111 | } |
||
| 112 | $keyname = $key; |
||
| 113 | foreach ($allFields as $field) { |
||
| 114 | if (\strtolower($field) === $key) { |
||
| 115 | $keyname = $field; |
||
| 116 | |||
| 117 | continue; |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | if ($key === 'link') { |
||
| 122 | $keyname = 'Link'; |
||
| 123 | } |
||
| 124 | |||
| 125 | $highlightsSS[$keyname] = $highlights[$key]; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** @phpstan-ignore-next-line */ |
||
| 129 | $ssDataObject->Highlights = $highlightsSS; |
||
| 130 | |||
| 131 | $ssDataObject->ID = $hit->getId(); |
||
| 132 | $ssResult->push($ssDataObject); |
||
| 133 | $manticoreResult->next(); |
||
| 134 | } |
||
| 135 | |||
| 136 | // we now need to standardize the output returned |
||
| 137 | |||
| 138 | $searchResults = new SearchResults(); |
||
| 139 | $searchResults->setRecords($ssResult); |
||
| 140 | $searchResults->setPage($this->page); |
||
| 141 | $searchResults->setPageSize($this->pageSize); |
||
| 142 | $searchResults->setQuery($q); |
||
| 143 | $searchResults->setTotalNumberOfResults($manticoreResult->getTotal()); |
||
| 144 | |||
| 145 | $endTime = \microtime(true); |
||
| 146 | $delta = $endTime - $startTime; |
||
| 147 | $delta = \round(1000*$delta)/1000; |
||
| 148 | $searchResults->setTime($delta); |
||
| 149 | |||
| 150 | return $searchResults; |
||
| 151 | } |
||
| 153 |