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