| Conditions | 16 |
| Paths | 350 |
| Total Lines | 145 |
| Code Lines | 85 |
| 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); |
||
| 200 | private function renderSearchResults( |
||
| 201 | SearchPage $model, |
||
| 202 | SearchResults $results |
||
| 203 | ): \SilverStripe\View\ViewableData_Customised { |
||
| 204 | $indexes = new Indexes(); |
||
| 205 | $index = $indexes->getIndex($model->IndexToSearch); |
||
| 206 | |||
| 207 | $hasManyFieldsDetails = $index->getHasManyFields(); |
||
| 208 | $hasManyFieldsNames = \array_keys($hasManyFieldsDetails); |
||
| 209 | |||
| 210 | /** @var string $clazz */ |
||
| 211 | $clazz = $index->getClass(); |
||
| 212 | |||
| 213 | $templateName = 'Suilven/FreeTextSearch/' . \str_replace('\\', '/', $clazz); |
||
| 214 | $splits = \explode('/', $templateName); |
||
| 215 | $last = \array_pop($splits); |
||
| 216 | $templateName = \implode('/', $splits) . '/Includes/' . $last; |
||
| 217 | |||
| 218 | |||
| 219 | $records = $results->getRecords(); |
||
| 220 | |||
| 221 | $newRecords = new ArrayList(); |
||
| 222 | foreach ($records as $record) { |
||
| 223 | $highsList = new ArrayList(); |
||
| 224 | $highlightsArray = $record->Highlights; |
||
| 225 | |||
| 226 | if (isset($highlightsArray['Title'])) { |
||
| 227 | $record->ResultTitle = $highlightsArray['Title'][0]; |
||
| 228 | unset($highlightsArray['Title']); |
||
| 229 | } |
||
| 230 | |||
| 231 | $record->HighlightedLink = $record->Link; |
||
| 232 | if (isset($highlightsArray['Link']) && \count($highlightsArray['Link']) > 0) { |
||
| 233 | $record->HighlightedLink = $highlightsArray['Link'][0]; |
||
| 234 | unset($highlightsArray['Link']); |
||
| 235 | } |
||
| 236 | |||
| 237 | // this simply repeats the title most times |
||
| 238 | unset($highlightsArray['MenuTitle']); |
||
| 239 | |||
| 240 | $keys = \is_null($highlightsArray) |
||
| 241 | ? [] |
||
| 242 | : \array_keys($highlightsArray); |
||
| 243 | foreach ($keys as $highlightedField) { |
||
| 244 | foreach ($highlightsArray[$highlightedField] as $highlightsForField) { |
||
| 245 | $do = new DataObject(); |
||
| 246 | // @phpstan-ignore-next-line |
||
| 247 | $do->Snippet = '...' . $highlightsForField . '...'; |
||
| 248 | |||
| 249 | $highsList->push($do); |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | $record->Highlights = $highsList; |
||
| 254 | |||
| 255 | $html = $this->renderWith( |
||
| 256 | [ |
||
| 257 | $templateName, |
||
| 258 | 'Suilven/FreeTextSearch/SilverStripe/CMS/Model/Includes/SiteTree', |
||
| 259 | ], |
||
| 260 | [ |
||
| 261 | 'Record' => $record, |
||
| 262 | 'SimilarLink' => $this->Link('similar') . '/' . $record->ID, |
||
| 263 | ] |
||
| 264 | ); |
||
| 265 | $record->HTML = $html; |
||
| 266 | $newRecords->push($record); |
||
| 267 | } |
||
| 268 | |||
| 269 | $paginatedList = new PaginatedList($records); |
||
| 270 | $paginatedList->setLimitItems(false); |
||
| 271 | $paginatedList->setPageLength($results->getPageSize()); |
||
| 272 | $paginatedList->setTotalItems($results->getTotaNumberOfResults()); |
||
| 273 | $paginatedList->setCurrentPage($results->getPage()); |
||
| 274 | |||
| 275 | |||
| 276 | $facets = $results->getFacets(); |
||
| 277 | $selectedFacetNames = \array_keys($this->selected); |
||
| 278 | |||
| 279 | |||
| 280 | $displayFacets = new ArrayList(); |
||
| 281 | |||
| 282 | $helper = new FacetLinkHelper(); |
||
| 283 | |||
| 284 | /** @var \Suilven\FreeTextSearch\Container\Facet $facet */ |
||
| 285 | foreach ($facets as $facet) { |
||
| 286 | $displayFacet = new DataObject(); |
||
| 287 | $facetName= $facet->getName(); |
||
| 288 | /** @phpstan-ignore-next-line */ |
||
| 289 | $displayFacet->Name = $facetName; |
||
| 290 | |||
| 291 | $helper->setFacetInContext($facetName); |
||
| 292 | $isHasManyFacet = \in_array($facetName, $hasManyFieldsNames, true); |
||
| 293 | $isSelectedFacet = \in_array($facetName, $selectedFacetNames, true); |
||
| 294 | |||
| 295 | $counts = new ArrayList(); |
||
| 296 | /** @var \Suilven\FreeTextSearch\Container\FacetCount $facetCount */ |
||
| 297 | foreach ($facet->getFacetCounts() as $facetCount) { |
||
| 298 | // @todo Make this an object |
||
| 299 | $count = new DataObject(); |
||
| 300 | $key = $facetCount->getKey(); |
||
| 301 | /** @phpstan-ignore-next-line */ |
||
| 302 | $count->Key = $key; |
||
| 303 | /** @phpstan-ignore-next-line */ |
||
| 304 | $count->Count = $facetCount->getCount(); |
||
| 305 | $link = $helper->isSelectedFacet($key) ? null: $helper->getDrillDownFacetLink( |
||
| 306 | $model->Link(), |
||
| 307 | $count->Key |
||
| 308 | ); |
||
| 309 | $clearFacetLink = $helper->isSelectedFacet($key) |
||
| 310 | ? $helper->getClearFacetLink($model->Link(), $facet->getName()) |
||
| 311 | : null; |
||
| 312 | |||
| 313 | // @phpstan-ignore-next-line |
||
| 314 | $count->Link = $link; |
||
| 315 | |||
| 316 | // @phpstan-ignore-next-line |
||
| 317 | $count->ClearFacetLink = $clearFacetLink; |
||
| 318 | |||
| 319 | if ($isHasManyFacet && !$isSelectedFacet) { |
||
| 320 | $counts->push($count); |
||
| 321 | } elseif ($isSelectedFacet && $helper->isSelectedFacet($key)) { |
||
| 322 | $counts->push($count); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | |||
| 326 | // @phpstan-ignore-next-line |
||
| 327 | $displayFacet->FacetCounts = $counts; |
||
| 328 | |||
| 329 | |||
| 330 | $displayFacets->push($displayFacet); |
||
| 331 | } |
||
| 332 | |||
| 333 | return $this->customise(new ArrayData([ |
||
| 334 | 'NumberOfResults' => $results->getTotaNumberOfResults(), |
||
| 335 | 'Query' => $results->getQuery(), |
||
| 336 | 'Records' => $newRecords, |
||
| 337 | 'Page' => $results->getPage(), |
||
| 338 | 'PageSize' => $results->getPageSize(), |
||
| 339 | 'Pages' => $results->getTotalPages(), |
||
| 340 | 'Suggestions' => new ArrayList($results->getSuggestions()), |
||
| 341 | 'Time' => $results->getTime(), |
||
| 342 | 'Pagination' => $paginatedList, |
||
| 343 | 'SimilarTo' => $results->getSimilarTo(), |
||
| 344 | 'Facets' => $displayFacets, |
||
| 345 | ])); |
||
| 364 |