We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 59 |
Total Lines | 379 |
Duplicated Lines | 0 % |
Changes | 25 | ||
Bugs | 0 | Features | 0 |
Complex classes like SearchController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SearchController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | class SearchController extends AbstractController |
||
23 | { |
||
24 | /** |
||
25 | * @var CollectionRepository |
||
26 | */ |
||
27 | protected $collectionRepository; |
||
28 | |||
29 | /** |
||
30 | * @param CollectionRepository $collectionRepository |
||
31 | */ |
||
32 | public function injectCollectionRepository(CollectionRepository $collectionRepository) |
||
33 | { |
||
34 | $this->collectionRepository = $collectionRepository; |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * @var MetadataRepository |
||
39 | */ |
||
40 | protected $metadataRepository; |
||
41 | |||
42 | /** |
||
43 | * @param MetadataRepository $metadataRepository |
||
44 | */ |
||
45 | public function injectMetadataRepository(MetadataRepository $metadataRepository) |
||
46 | { |
||
47 | $this->metadataRepository = $metadataRepository; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @var array $this->searchParams: The current search parameter |
||
52 | * @access protected |
||
53 | */ |
||
54 | protected $searchParams; |
||
55 | |||
56 | /** |
||
57 | * Search Action |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | public function searchAction() |
||
62 | { |
||
63 | // if search was triggered, get search parameters from POST variables |
||
64 | $this->searchParams = $this->getParametersSafely('searchParameter'); |
||
|
|||
65 | |||
66 | // output is done by main action |
||
67 | $this->forward('main', null, null, ['searchParameter' => $this->searchParams]); |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Main action |
||
72 | * |
||
73 | * This shows the search form and optional the facets and extended search form. |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function mainAction() |
||
146 | } |
||
147 | |||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Adds the facets menu to the search form |
||
152 | * |
||
153 | * @access protected |
||
154 | * |
||
155 | * @return string HTML output of facets menu |
||
156 | */ |
||
157 | protected function addFacetsMenu() |
||
158 | { |
||
159 | // Quit without doing anything if no facets are selected. |
||
160 | if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { |
||
161 | return ''; |
||
162 | } |
||
163 | |||
164 | // Get facets from plugin configuration. |
||
165 | $facets = []; |
||
166 | foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { |
||
167 | $facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); |
||
168 | } |
||
169 | |||
170 | $this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * This builds a menu array for HMENU |
||
175 | * |
||
176 | * @access public |
||
177 | * |
||
178 | * @param string $content: The PlugIn content |
||
179 | * @param array $conf: The PlugIn configuration |
||
180 | * |
||
181 | * @return array HMENU array |
||
182 | */ |
||
183 | public function makeFacetsMenuArray($facets) |
||
184 | { |
||
185 | $menuArray = []; |
||
186 | // Set default value for facet search. |
||
187 | $search = [ |
||
188 | 'query' => '*', |
||
189 | 'params' => [ |
||
190 | 'component' => [ |
||
191 | 'facetset' => [ |
||
192 | 'facet' => [] |
||
193 | ] |
||
194 | ] |
||
195 | ] |
||
196 | ]; |
||
197 | |||
198 | // Set needed parameters for facet search. |
||
199 | if (empty($search['params']['filterquery'])) { |
||
200 | $search['params']['filterquery'] = []; |
||
201 | } |
||
202 | |||
203 | $fields = Solr::getFields(); |
||
204 | |||
205 | // Set search query. |
||
206 | $searchParams = $this->searchParams; |
||
207 | if ( |
||
208 | (!empty($searchParams['fulltext'])) |
||
209 | || preg_match('/' . $fields['fulltext'] . ':\((.*)\)/', trim($searchParams['query']), $matches) |
||
210 | ) { |
||
211 | // If the query already is a fulltext query e.g using the facets |
||
212 | $searchParams['query'] = empty($matches[1]) ? $searchParams['query'] : $matches[1]; |
||
213 | // Search in fulltext field if applicable. Query must not be empty! |
||
214 | if (!empty($this->searchParams['query'])) { |
||
215 | $search['query'] = $fields['fulltext'] . ':(' . Solr::escapeQuery(trim($searchParams['query'])) . ')'; |
||
216 | } |
||
217 | } else { |
||
218 | // Retain given search field if valid. |
||
219 | if (!empty($searchParams['query'])) { |
||
220 | $search['query'] = Solr::escapeQueryKeepField(trim($searchParams['query']), $this->settings['storagePid']); |
||
221 | } |
||
222 | } |
||
223 | |||
224 | if (isset($this->searchParams['fq']) && is_array($this->searchParams['fq'])) { |
||
225 | foreach ($this->searchParams['fq'] as $fq) { |
||
226 | $search['params']['filterquery'][]['query'] = $fq; |
||
227 | } |
||
228 | } |
||
229 | // Get applicable facets. |
||
230 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
231 | if (!$solr->ready) { |
||
232 | $this->logger->error('Apache Solr not available'); |
||
233 | return []; |
||
234 | } |
||
235 | |||
236 | foreach (array_keys($facets) as $field) { |
||
237 | $search['params']['component']['facetset']['facet'][] = [ |
||
238 | 'type' => 'field', |
||
239 | 'key' => $field, |
||
240 | 'field' => $field, |
||
241 | 'limit' => $this->settings['limitFacets'], |
||
242 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
||
243 | ]; |
||
244 | } |
||
245 | |||
246 | // Set additional query parameters. |
||
247 | $search['params']['start'] = 0; |
||
248 | $search['params']['rows'] = 0; |
||
249 | // Set query. |
||
250 | $search['params']['query'] = $search['query']; |
||
251 | // Perform search. |
||
252 | $selectQuery = $solr->service->createSelect($search['params']); |
||
253 | $results = $solr->service->select($selectQuery); |
||
254 | $facet = $results->getFacetSet(); |
||
255 | |||
256 | $facetCollectionArray = []; |
||
257 | |||
258 | // replace everything expect numbers and comma |
||
259 | $facetCollections = preg_replace('/[^0-9,]/', '', $this->settings['facetCollections']); |
||
260 | |||
261 | if (!empty($facetCollections)) { |
||
262 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
||
263 | |||
264 | /** @var Collection $collection */ |
||
265 | foreach ($collections as $collection) { |
||
266 | $facetCollectionArray[] = $collection->getIndexName(); |
||
267 | } |
||
268 | } |
||
269 | |||
270 | // Process results. |
||
271 | if ($facet) { |
||
272 | foreach ($facet as $field => $values) { |
||
273 | $entryArray = []; |
||
274 | $entryArray['title'] = htmlspecialchars($facets[$field]); |
||
275 | $entryArray['count'] = 0; |
||
276 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
277 | $entryArray['doNotLinkIt'] = 1; |
||
278 | $entryArray['ITEM_STATE'] = 'NO'; |
||
279 | // Count number of facet values. |
||
280 | $i = 0; |
||
281 | foreach ($values as $value => $count) { |
||
282 | if ($count > 0) { |
||
283 | // check if facet collection configuration exists |
||
284 | if (!empty($this->settings['facetCollections'])) { |
||
285 | if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
||
286 | continue; |
||
287 | } |
||
288 | } |
||
289 | $entryArray['count']++; |
||
290 | if ($entryArray['ITEM_STATE'] == 'NO') { |
||
291 | $entryArray['ITEM_STATE'] = 'IFSUB'; |
||
292 | } |
||
293 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
||
294 | if (++$i == $this->settings['limit']) { |
||
295 | break; |
||
296 | } |
||
297 | } else { |
||
298 | break; |
||
299 | } |
||
300 | } |
||
301 | $menuArray[] = $entryArray; |
||
302 | } |
||
303 | } |
||
304 | return $menuArray; |
||
305 | } |
||
306 | |||
307 | /** |
||
308 | * Creates an array for a HMENU entry of a facet value. |
||
309 | * |
||
310 | * @access protected |
||
311 | * |
||
312 | * @param string $field: The facet's index_name |
||
313 | * @param string $value: The facet's value |
||
314 | * @param int $count: Number of hits for this facet |
||
315 | * @param array $search: The parameters of the current search query |
||
316 | * @param string &$state: The state of the parent item |
||
317 | * |
||
318 | * @return array The array for the facet's menu entry |
||
319 | */ |
||
320 | protected function getFacetsMenuEntry($field, $value, $count, $search, &$state) |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * Returns the extended search form and adds the JS files necessary for extended search. |
||
367 | * |
||
368 | * @access protected |
||
369 | * |
||
370 | * @return string The extended search form or an empty string |
||
371 | */ |
||
372 | protected function addExtendedSearch() |
||
401 | } |
||
402 | } |
||
403 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.