Conditions | 9 |
Paths | 16 |
Total Lines | 138 |
Code Lines | 34 |
Lines | 0 |
Ratio | 0 % |
Changes | 16 | ||
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); |
||
41 | public function index(): \SilverStripe\View\ViewableData_Customised |
||
42 | { |
||
43 | // @todo search indexes addition |
||
44 | $q = $this->getRequest()->getVar('q'); |
||
45 | |||
46 | /** @var array $selected */ |
||
47 | $selected = $this->getRequest()->getVars(); |
||
48 | |||
49 | /** @var \Suilven\FreeTextSearch\Page\SearchPage $model */ |
||
50 | $model = SearchPage::get_by_id(SearchPage::class, $this->ID); |
||
51 | |||
52 | |||
53 | unset($selected['start']); |
||
54 | |||
55 | $results = new SearchResults(); |
||
56 | |||
57 | |||
58 | //unset($selected['q']); |
||
59 | |||
60 | if (isset($q) || $model->ShowAllIfEmptyQuery || isset($selected)) { |
||
61 | $results = $this->performSearchIncludingFacets($selected, $model, $q); |
||
62 | } |
||
63 | |||
64 | |||
65 | $results->setQuery($q); |
||
66 | |||
67 | |||
68 | // @todo In the case of facets and no search term this fails |
||
69 | // This is intended for a search where a search term has been provided, but no results |
||
70 | if (isset($q) && $results->getNumberOfResults() === 0) { |
||
71 | // get suggestions |
||
72 | $factory = new SuggesterFactory(); |
||
73 | |||
74 | /** @var \Suilven\FreeTextSearch\Factory\Suggester $suggester */ |
||
75 | $suggester = $factory->getSuggester(); |
||
76 | |||
77 | // @todo this is returning blank |
||
78 | $suggester->setIndex($model->IndexToSearch); |
||
79 | $suggestions = $suggester->suggest($q); |
||
80 | |||
81 | $results['Suggestions'] = new ArrayList($suggestions); |
||
82 | |||
83 | // @todo FIX - only one result returned for now |
||
84 | } |
||
85 | |||
86 | |||
87 | /* |
||
88 | $facetted = isset($results['AllFacets']); |
||
89 | |||
90 | |||
91 | |||
92 | $targetFacet = new ArrayList(); |
||
93 | if (isset($model->ShowTagCloudFor)) { |
||
94 | // get the tag cloud from calculated facets, but if not calculated, ie the arrive on the page case, |
||
95 | // calculate them |
||
96 | if ($facetted) { |
||
97 | $facets = $results['AllFacets']; |
||
98 | } else { |
||
99 | $proxyResults = $this->performSearchIncludingFacets($selected, $model, $q); |
||
100 | $facets = $proxyResults['AllFacets']; |
||
101 | } |
||
102 | |||
103 | foreach ($facets as $facet) { |
||
104 | $name = $facet->getField('Name'); |
||
105 | if ($name === $model->ShowTagCloudFor) { |
||
106 | $targetFacet = $facet->getField('Facets'); |
||
107 | |||
108 | break; |
||
109 | } |
||
110 | } |
||
111 | |||
112 | $facetArray = $targetFacet->toArray(); |
||
113 | $minSize = 10; |
||
114 | $maxSize = 40; |
||
115 | $maxCount = 0; |
||
116 | foreach ($facetArray as $tag) { |
||
117 | $count = $tag['Count']; |
||
118 | $maxCount = $count > $maxCount |
||
119 | ? $count |
||
120 | : $maxCount; |
||
121 | } |
||
122 | |||
123 | $tagCloud = new ArrayList(); |
||
124 | foreach ($facetArray as $tag) { |
||
125 | $size = $minSize + ($maxSize - $minSize) * $tag['Count'] / $maxCount; |
||
126 | $size = \round($size); |
||
127 | $row = new ArrayData([ |
||
128 | 'Name' => $tag['Value'], |
||
129 | 'Size' => $size, |
||
130 | 'Params' => $tag['Params'], |
||
131 | ]); |
||
132 | $tagCloud->push($row); |
||
133 | } |
||
134 | |||
135 | $results['TagCloud'] = $tagCloud; |
||
136 | } |
||
137 | |||
138 | |||
139 | //for($i = 3; $i < 40; $i++) { |
||
140 | // echo "li.tag{$i} { font-size: {$i}px;};\n"; |
||
141 | //} |
||
142 | |||
143 | */ |
||
144 | |||
145 | |||
146 | // defer showing to the template level, still get facets, as this allows optionally for likes of a tag cloud |
||
147 | // $results['ShowAllIfEmptyQuery'] = $model->ShowAllIfEmptyQuery; |
||
148 | // $results['CleanedLink'] = $this->Link(); |
||
149 | |||
150 | $records = $results->getRecords(); |
||
151 | $newRecords = new ArrayList(); |
||
152 | foreach ($records as $record) { |
||
153 | $highsList = new ArrayList(); |
||
154 | $highlightsArray = $record->Highlights; |
||
155 | |||
156 | $keys = \array_keys($highlightsArray); |
||
157 | foreach ($keys as $highlightedField) { |
||
158 | foreach ($highlightsArray[$highlightedField] as $highlightsForField) { |
||
159 | $do = new DataObject(); |
||
160 | $do->Snippet = '...' . $highlightsForField . '...'; |
||
161 | |||
162 | $highsList->push($do); |
||
163 | } |
||
164 | } |
||
165 | |||
166 | |||
167 | |||
168 | $record->Highlights = $highsList; |
||
169 | $newRecords->push($record); |
||
170 | } |
||
171 | |||
172 | return $this->customise(new ArrayData([ |
||
173 | 'NumberOfResults' => $results->getNumberOfResults(), |
||
174 | 'Query' => $results->getQuery(), |
||
175 | 'Records' => $newRecords, |
||
176 | 'Page' => $results->getPage(), |
||
177 | 'PageSize' => $results->getPageSize(), |
||
178 | 'Time' => $results->getTime(), |
||
179 | ])); |
||
217 |