| Conditions | 12 |
| Paths | 81 |
| Total Lines | 99 |
| Code Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 | $searcher->limit($this->pageSize); |
||
| 38 | $offset=$this->pageSize * ($this->page-1); |
||
| 39 | $searcher->offset($offset); |
||
| 40 | |||
| 41 | $manticoreResult = $searcher->search($q)->highlight( |
||
| 42 | [], |
||
| 43 | ['pre_tags' => '<b>', 'post_tags'=>'</b>'] |
||
| 44 | )->get(); |
||
| 45 | |||
| 46 | $indexes = new Indexes(); |
||
| 47 | $index = $indexes->getIndex($this->indexName); |
||
| 48 | $fields = $index->getFields(); |
||
| 49 | |||
| 50 | $ssResult = new ArrayList(); |
||
| 51 | while ($manticoreResult->valid()) { |
||
| 52 | $hit = $manticoreResult->current(); |
||
| 53 | $source = $hit->getData(); |
||
| 54 | //print_r($source); |
||
| 55 | $ssDataObject = new DataObject(); |
||
| 56 | |||
| 57 | $keys = \array_keys($source); |
||
| 58 | foreach ($keys as $key) { |
||
| 59 | $keyname = $key; |
||
| 60 | foreach ($fields as $field) { |
||
| 61 | if (\strtolower($field) === $key) { |
||
| 62 | $keyname = $field; |
||
| 63 | |||
| 64 | break; |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | // @todo This is a hack as $Title is rendering the ID in the template |
||
| 69 | if ($keyname === 'Title') { |
||
| 70 | $keyname = 'ResultTitle'; |
||
| 71 | } elseif ($keyname === 'link') { |
||
| 72 | $keyname = 'Link'; |
||
| 73 | }; |
||
| 74 | |||
| 75 | /** @phpstan-ignore-next-line */ |
||
| 76 | $ssDataObject->$keyname = $source[$key]; |
||
| 77 | } |
||
| 78 | |||
| 79 | |||
| 80 | // manticore lowercases fields, so as above normalize them back to the SS fieldnames |
||
| 81 | $highlights = $hit->getHighlight(); |
||
| 82 | $highlightsSS = []; |
||
| 83 | |||
| 84 | $keys = \array_keys($highlights); |
||
| 85 | foreach ($keys as $key) { |
||
| 86 | if (!isset($highlights[$key])) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | $keyname = $key; |
||
| 90 | foreach ($fields as $field) { |
||
| 91 | if (\strtolower($field) === $key) { |
||
| 92 | $keyname = $field; |
||
| 93 | |||
| 94 | continue; |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | if ($key === 'link') { |
||
| 99 | $keyname = 'Link'; |
||
| 100 | } |
||
| 101 | |||
| 102 | $highlightsSS[$keyname] = $highlights[$key]; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** @phpstan-ignore-next-line */ |
||
| 106 | $ssDataObject->Highlights = $highlightsSS; |
||
| 107 | |||
| 108 | $ssDataObject->ID = $hit->getId(); |
||
| 109 | $ssResult->push($ssDataObject); |
||
| 110 | $manticoreResult->next(); |
||
| 111 | } |
||
| 112 | |||
| 113 | // we now need to standardize the output returned |
||
| 114 | |||
| 115 | $searchResults = new SearchResults(); |
||
| 116 | $searchResults->setRecords($ssResult); |
||
| 117 | $searchResults->setPage($this->page); |
||
| 118 | $searchResults->setPageSize($this->pageSize); |
||
| 119 | $searchResults->setQuery($q); |
||
|
|
|||
| 120 | $searchResults->setTotalNumberOfResults($manticoreResult->getTotal()); |
||
| 121 | |||
| 122 | $endTime = \microtime(true); |
||
| 123 | $delta = $endTime - $startTime; |
||
| 124 | $delta = \round(1000*$delta)/1000; |
||
| 125 | $searchResults->setTime($delta); |
||
| 126 | |||
| 127 | return $searchResults; |
||
| 128 | } |
||
| 130 |