Conditions | 8 |
Paths | 25 |
Total Lines | 83 |
Code Lines | 51 |
Lines | 0 |
Ratio | 0 % |
Changes | 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); |
||
191 | private function renderSearchResults( |
||
192 | SearchPage $model, |
||
193 | SearchResults $results |
||
194 | ): \SilverStripe\View\ViewableData_Customised { |
||
195 | $indexes = new Indexes(); |
||
196 | $index = $indexes->getIndex($model->IndexToSearch); |
||
197 | |||
198 | /** @var string $clazz */ |
||
199 | $clazz = $index->getClass(); |
||
200 | |||
201 | $templateName = 'Suilven/FreeTextSearch/' . \str_replace('\\', '/', $clazz); |
||
202 | $splits = \explode('/', $templateName); |
||
203 | $last = \array_pop($splits); |
||
204 | $templateName = \implode('/', $splits) . '/Includes/' . $last; |
||
205 | |||
206 | |||
207 | $records = $results->getRecords(); |
||
208 | |||
209 | $newRecords = new ArrayList(); |
||
210 | foreach ($records as $record) { |
||
211 | $highsList = new ArrayList(); |
||
212 | $highlightsArray = $record->Highlights; |
||
213 | |||
214 | if (isset($highlightsArray['Title'])) { |
||
215 | $record->ResultTitle = $highlightsArray['Title'][0]; |
||
216 | unset($highlightsArray['Title']); |
||
217 | } |
||
218 | |||
219 | $record->HighlightedLink = $record->Link; |
||
220 | if (isset($highlightsArray['Link']) && \count($highlightsArray['Link']) > 0) { |
||
221 | $record->HighlightedLink = $highlightsArray['Link'][0]; |
||
222 | unset($highlightsArray['Link']); |
||
223 | } |
||
224 | |||
225 | // this simply repeats the title most times |
||
226 | unset($highlightsArray['MenuTitle']); |
||
227 | |||
228 | $keys = \is_null($highlightsArray) |
||
229 | ? [] |
||
230 | : \array_keys($highlightsArray); |
||
231 | foreach ($keys as $highlightedField) { |
||
232 | foreach ($highlightsArray[$highlightedField] as $highlightsForField) { |
||
233 | $do = new DataObject(); |
||
234 | // @phpstan-ignore-next-line |
||
235 | $do->Snippet = '...' . $highlightsForField . '...'; |
||
236 | |||
237 | $highsList->push($do); |
||
238 | } |
||
239 | } |
||
240 | |||
241 | $record->Highlights = $highsList; |
||
242 | |||
243 | $html = $this->renderWith( |
||
244 | [ |
||
245 | $templateName, |
||
246 | 'Suilven/FreeTextSearch/SilverStripe/CMS/Model/Includes/SiteTree', |
||
247 | ], |
||
248 | [ |
||
249 | 'Record' => $record, |
||
250 | 'SimilarLink' => $this->Link('similar') . '/' . $record->ID, |
||
251 | ] |
||
252 | ); |
||
253 | $record->HTML = $html; |
||
254 | $newRecords->push($record); |
||
255 | } |
||
256 | |||
257 | $paginatedList = new PaginatedList($records); |
||
258 | $paginatedList->setLimitItems(false); |
||
259 | $paginatedList->setPageLength($results->getPageSize()); |
||
260 | $paginatedList->setTotalItems($results->getTotaNumberOfResults()); |
||
261 | $paginatedList->setCurrentPage($results->getPage()); |
||
262 | |||
263 | return $this->customise(new ArrayData([ |
||
264 | 'NumberOfResults' => $results->getTotaNumberOfResults(), |
||
265 | 'Query' => $results->getQuery(), |
||
266 | 'Records' => $newRecords, |
||
267 | 'Page' => $results->getPage(), |
||
268 | 'PageSize' => $results->getPageSize(), |
||
269 | 'Pages' => $results->getTotalPages(), |
||
270 | 'Suggestions' => new ArrayList($results->getSuggestions()), |
||
271 | 'Time' => $results->getTime(), |
||
272 | 'Pagination' => $paginatedList, |
||
273 | 'SimilarTo' => $results->getSimilarTo(), |
||
274 | ])); |
||
293 |