We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Total Complexity | 90 |
Total Lines | 554 |
Duplicated Lines | 0 % |
Changes | 6 | ||
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 |
||
38 | class SearchController extends AbstractController |
||
39 | { |
||
40 | /** |
||
41 | * @access protected |
||
42 | * @var CollectionRepository |
||
43 | */ |
||
44 | protected CollectionRepository $collectionRepository; |
||
45 | |||
46 | /** |
||
47 | * @access public |
||
48 | * |
||
49 | * @param CollectionRepository $collectionRepository |
||
50 | * |
||
51 | * @return void |
||
52 | */ |
||
53 | public function injectCollectionRepository(CollectionRepository $collectionRepository): void |
||
54 | { |
||
55 | $this->collectionRepository = $collectionRepository; |
||
56 | } |
||
57 | |||
58 | /** |
||
59 | * @access protected |
||
60 | * @var MetadataRepository |
||
61 | */ |
||
62 | protected MetadataRepository $metadataRepository; |
||
63 | |||
64 | /** |
||
65 | * @access public |
||
66 | * |
||
67 | * @param MetadataRepository $metadataRepository |
||
68 | * |
||
69 | * @return void |
||
70 | */ |
||
71 | public function injectMetadataRepository(MetadataRepository $metadataRepository): void |
||
72 | { |
||
73 | $this->metadataRepository = $metadataRepository; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * @access protected |
||
78 | * @var array The current search parameter |
||
79 | */ |
||
80 | protected ?array $searchParams; |
||
81 | |||
82 | /** |
||
83 | * Search Action |
||
84 | * |
||
85 | * @access public |
||
86 | * |
||
87 | * @return ResponseInterface the response |
||
88 | */ |
||
89 | public function searchAction(): ResponseInterface |
||
90 | { |
||
91 | // if search was triggered, get search parameters from POST variables |
||
92 | $this->searchParams = $this->getParametersSafely('searchParameter'); |
||
|
|||
93 | |||
94 | // output is done by main action |
||
95 | return $this->redirect('main', null, null, ['searchParameter' => $this->searchParams]); |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Main action |
||
100 | * |
||
101 | * This shows the search form and optional the facets and extended search form. |
||
102 | * |
||
103 | * @access public |
||
104 | * |
||
105 | * @return ResponseInterface the response |
||
106 | */ |
||
107 | public function mainAction(): ResponseInterface |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Adds the facets menu to the search form |
||
231 | * |
||
232 | * @access protected |
||
233 | */ |
||
234 | protected function addFacetsMenu(): void |
||
235 | { |
||
236 | // Quit without doing anything if no facets are configured. |
||
237 | if (empty($this->settings['facets']) && empty($this->settings['facetCollections'])) { |
||
238 | return; |
||
239 | } |
||
240 | |||
241 | // Get facets from plugin configuration. |
||
242 | $facets = []; |
||
243 | foreach (GeneralUtility::trimExplode(',', $this->settings['facets'], true) as $facet) { |
||
244 | $facets[$facet . '_faceting'] = Helper::translate($facet, 'tx_dlf_metadata', $this->settings['storagePid']); |
||
245 | } |
||
246 | |||
247 | $this->view->assign('facetsMenu', $this->makeFacetsMenuArray($facets)); |
||
248 | } |
||
249 | |||
250 | /** |
||
251 | * This builds a menu array for HMENU |
||
252 | * |
||
253 | * @access public |
||
254 | * |
||
255 | * @param array $facets |
||
256 | * |
||
257 | * @return array HMENU array |
||
258 | */ |
||
259 | public function makeFacetsMenuArray(array $facets): array |
||
260 | { |
||
261 | // Set default value for facet search. |
||
262 | $search = [ |
||
263 | 'query' => '*:*', |
||
264 | 'params' => [ |
||
265 | 'component' => [ |
||
266 | 'facetset' => [ |
||
267 | 'facet' => [] |
||
268 | ] |
||
269 | ], |
||
270 | 'filterquery' => [] |
||
271 | ] |
||
272 | ]; |
||
273 | |||
274 | $fields = Solr::getFields(); |
||
275 | |||
276 | // Set search query. |
||
277 | $searchParams = $this->searchParams; |
||
278 | if ( |
||
279 | (!empty($searchParams['fulltext'])) |
||
280 | || preg_match('/' . $fields['fulltext'] . ':\((.*)\)/', trim($searchParams['query']), $matches) |
||
281 | ) { |
||
282 | // If the query already is a fulltext query e.g using the facets |
||
283 | $searchParams['query'] = empty($matches[1]) ? $searchParams['query'] : $matches[1]; |
||
284 | // Search in fulltext field if applicable. Query must not be empty! |
||
285 | if (!empty($searchParams['query'])) { |
||
286 | $search['query'] = $fields['fulltext'] . ':(' . Solr::escapeQuery(trim($searchParams['query'])) . ')'; |
||
287 | } |
||
288 | } else { |
||
289 | // Retain given search field if valid. |
||
290 | if (!empty($searchParams['query'])) { |
||
291 | $search['query'] = Solr::escapeQueryKeepField(trim($searchParams['query']), $this->settings['storagePid']); |
||
292 | } |
||
293 | } |
||
294 | |||
295 | $collectionsQuery = $this->addCollectionsQuery($search['query']); |
||
296 | if (!empty($collectionsQuery)) { |
||
297 | $search['params']['filterquery'][]['query'] = $collectionsQuery; |
||
298 | } |
||
299 | |||
300 | // add filter query for date search |
||
301 | if (!empty($this->searchParams['dateFrom']) && !empty($this->searchParams['dateTo'])) { |
||
302 | // combine dateFrom and dateTo into filterquery as range search |
||
303 | $search['params']['filterquery'][]['query'] = '{!join from=' . $fields['uid'] . ' to=' . $fields['uid'] . '}' . $fields['date'] . ':[' . $this->searchParams['dateFrom'] . ' TO ' . $this->searchParams['dateTo'] . ']'; |
||
304 | } |
||
305 | |||
306 | // Add extended search query. |
||
307 | if ( |
||
308 | !empty($searchParams['extQuery']) |
||
309 | && is_array($searchParams['extQuery']) |
||
310 | ) { |
||
311 | // If the search query is already set by the simple search field, we have to reset it. |
||
312 | $search['query'] = ''; |
||
313 | $allowedOperators = ['AND', 'OR', 'NOT']; |
||
314 | $numberOfExtQueries = count($searchParams['extQuery']); |
||
315 | for ($i = 0; $i < $numberOfExtQueries; $i++) { |
||
316 | if (!empty($searchParams['extQuery'][$i])) { |
||
317 | if ( |
||
318 | in_array($searchParams['extOperator'][$i], $allowedOperators) |
||
319 | ) { |
||
320 | if (!empty($search['query'])) { |
||
321 | $search['query'] .= ' ' . $searchParams['extOperator'][$i] . ' '; |
||
322 | } |
||
323 | $search['query'] .= Indexer::getIndexFieldName($searchParams['extField'][$i], $this->settings['storagePid']) . ':(' . Solr::escapeQuery($searchParams['extQuery'][$i]) . ')'; |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 | |||
329 | if (isset($this->searchParams['fq']) && is_array($this->searchParams['fq'])) { |
||
330 | foreach ($this->searchParams['fq'] as $fq) { |
||
331 | $search['params']['filterquery'][]['query'] = $fq; |
||
332 | } |
||
333 | } |
||
334 | |||
335 | // Get applicable facets. |
||
336 | $solr = Solr::getInstance($this->settings['solrcore']); |
||
337 | if (!$solr->ready) { |
||
338 | $this->logger->error('Apache Solr not available'); |
||
339 | return []; |
||
340 | } |
||
341 | |||
342 | foreach (array_keys($facets) as $field) { |
||
343 | $search['params']['component']['facetset']['facet'][$field] = [ |
||
344 | 'type' => 'field', |
||
345 | 'mincount' => '1', |
||
346 | 'key' => $field, |
||
347 | 'field' => $field, |
||
348 | 'limit' => $this->settings['limitFacets'], |
||
349 | 'sort' => isset($this->settings['sortingFacets']) ? $this->settings['sortingFacets'] : 'count' |
||
350 | ]; |
||
351 | } |
||
352 | |||
353 | // Set additional query parameters. |
||
354 | $search['params']['start'] = 0; |
||
355 | $search['params']['rows'] = 0; |
||
356 | // Set query. |
||
357 | $search['params']['query'] = $search['query']; |
||
358 | // Perform search. |
||
359 | $selectQuery = $solr->service->createSelect($search['params']); |
||
360 | // check for solr response |
||
361 | $solrRequest = $solr->service->createRequest($selectQuery); |
||
362 | $response = $solr->service->executeRequest($solrRequest); |
||
363 | // return empty facet on solr error |
||
364 | if ($response->getStatusCode() == 400) { |
||
365 | return []; |
||
366 | } |
||
367 | $results = $solr->service->select($selectQuery); |
||
368 | $facet = $results->getFacetSet(); |
||
369 | |||
370 | $facetCollectionArray = []; |
||
371 | |||
372 | // replace everything expect numbers and comma |
||
373 | $facetCollections = preg_replace('/[^\d,]/', '', $this->settings['facetCollections']); |
||
374 | |||
375 | if (!empty($facetCollections)) { |
||
376 | $collections = $this->collectionRepository->findCollectionsBySettings(['collections' => $facetCollections]); |
||
377 | |||
378 | /** @var Collection $collection */ |
||
379 | foreach ($collections as $collection) { |
||
380 | $facetCollectionArray[] = $collection->getIndexName(); |
||
381 | } |
||
382 | } |
||
383 | |||
384 | return $this->processResults($facet, $facetCollectionArray, $search); |
||
385 | } |
||
386 | |||
387 | /** |
||
388 | * Add the collection query string, if the collections are given. |
||
389 | * |
||
390 | * @access private |
||
391 | * |
||
392 | * @param string $query The current query |
||
393 | * |
||
394 | * @return string The collection query string |
||
395 | */ |
||
396 | private function addCollectionsQuery(string $query): string |
||
397 | { |
||
398 | // if collections are given, we prepare the collections query string |
||
399 | // extract collections from collection parameter |
||
400 | $collections = null; |
||
401 | if (array_key_exists('collection', $this->searchParams)) { |
||
402 | foreach (explode(',', $this->searchParams['collection']) as $collectionEntry) { |
||
403 | if (!empty($collectionEntry)) { |
||
404 | $collections[] = $this->collectionRepository->findByUid((int) $collectionEntry); |
||
405 | } |
||
406 | } |
||
407 | } |
||
408 | if ($collections) { |
||
409 | $collectionsQueryString = ''; |
||
410 | $virtualCollectionsQueryString = ''; |
||
411 | foreach ($collections as $collectionEntry) { |
||
412 | // check for virtual collections query string |
||
413 | if ($collectionEntry->getIndexSearch()) { |
||
414 | $virtualCollectionsQueryString .= empty($virtualCollectionsQueryString) ? '(' . $collectionEntry->getIndexSearch() . ')' : ' OR (' . $collectionEntry->getIndexSearch() . ')'; |
||
415 | } else { |
||
416 | $collectionsQueryString .= empty($collectionsQueryString) ? '"' . $collectionEntry->getIndexName() . '"' : ' OR "' . $collectionEntry->getIndexName() . '"'; |
||
417 | } |
||
418 | } |
||
419 | |||
420 | // distinguish between simple collection browsing and actual searching within the collection(s) |
||
421 | if (!empty($collectionsQueryString)) { |
||
422 | if (empty($query)) { |
||
423 | $collectionsQueryString = '(collection_faceting:(' . $collectionsQueryString . ') AND toplevel:true AND partof:0)'; |
||
424 | } else { |
||
425 | $collectionsQueryString = '(collection_faceting:(' . $collectionsQueryString . '))'; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | // virtual collections might query documents that are neither toplevel:true nor partof:0 and need to be searched separately |
||
430 | if (!empty($virtualCollectionsQueryString)) { |
||
431 | $virtualCollectionsQueryString = '(' . $virtualCollectionsQueryString . ')'; |
||
432 | } |
||
433 | |||
434 | // combine both querystrings into a single filterquery via OR if both are given, otherwise pass either of those |
||
435 | return implode(" OR ", array_filter([$collectionsQueryString, $virtualCollectionsQueryString])); |
||
436 | } |
||
437 | return ""; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Creates an array for a HMENU entry of a facet value. |
||
442 | * |
||
443 | * @access private |
||
444 | * |
||
445 | * @param string $field The facet's index_name |
||
446 | * @param string $value The facet's value |
||
447 | * @param int $count Number of hits for this facet |
||
448 | * @param array $search The parameters of the current search query |
||
449 | * @param string &$state The state of the parent item |
||
450 | * |
||
451 | * @return array The array for the facet's menu entry |
||
452 | */ |
||
453 | private function getFacetsMenuEntry(string $field, string $value, int $count, array $search, string &$state): array |
||
481 | } |
||
482 | |||
483 | /** |
||
484 | * Process results. |
||
485 | * |
||
486 | * @access private |
||
487 | * |
||
488 | * @param FacetSet|null $facet |
||
489 | * @param array $facetCollectionArray |
||
490 | * @param array $search |
||
491 | * |
||
492 | * @return array menu array |
||
493 | */ |
||
494 | private function processResults($facet, array $facetCollectionArray, array $search): array |
||
495 | { |
||
496 | $menuArray = []; |
||
497 | |||
498 | if ($facet) { |
||
499 | foreach ($facet as $field => $values) { |
||
500 | $entryArray = []; |
||
501 | $entryArray['field'] = substr($field, 0, strpos($field, '_faceting')); |
||
502 | $entryArray['count'] = 0; |
||
503 | $entryArray['_OVERRIDE_HREF'] = ''; |
||
504 | $entryArray['ITEM_STATE'] = 'NO'; |
||
505 | // Count number of facet values. |
||
506 | $i = 0; |
||
507 | foreach ($values as $value => $count) { |
||
508 | if ($count > 0) { |
||
509 | // check if facet collection configuration exists |
||
510 | if (!empty($this->settings['facetCollections'])) { |
||
511 | if ($field == "collection_faceting" && !in_array($value, $facetCollectionArray)) { |
||
512 | continue; |
||
513 | } |
||
514 | } |
||
515 | $entryArray['count']++; |
||
516 | if ($entryArray['ITEM_STATE'] == 'NO') { |
||
517 | $entryArray['ITEM_STATE'] = 'IFSUB'; |
||
518 | } |
||
519 | $entryArray['_SUB_MENU'][] = $this->getFacetsMenuEntry($field, $value, $count, $search, $entryArray['ITEM_STATE']); |
||
520 | if (++$i == $this->settings['limit']) { |
||
521 | break; |
||
522 | } |
||
523 | } else { |
||
524 | break; |
||
525 | } |
||
526 | } |
||
527 | $menuArray[] = $entryArray; |
||
528 | } |
||
529 | } |
||
530 | return $menuArray; |
||
531 | } |
||
532 | |||
533 | /** |
||
534 | * Translates value depending on the index name. |
||
535 | * |
||
536 | * @access private |
||
537 | * |
||
538 | * @param string $field The facet's index_name |
||
539 | * @param string $value The facet's value |
||
540 | * |
||
541 | * @return string |
||
542 | */ |
||
543 | private function translateValue(string $field, string $value): string |
||
560 | } |
||
561 | } |
||
562 | |||
563 | /** |
||
564 | * Returns the extended search form and adds the JS files necessary for extended search. |
||
565 | * |
||
566 | * @access private |
||
567 | * |
||
568 | * @return void |
||
569 | */ |
||
570 | private function addExtendedSearch(): void |
||
592 | } |
||
593 | } |
||
594 |
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.