| Conditions | 47 |
| Paths | 15506 |
| Total Lines | 178 |
| Lines | 6 |
| Ratio | 3.37 % |
| 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 |
||
| 315 | public function main($content, $conf) { |
||
| 316 | $this->init($conf); |
||
| 317 | // Disable caching for this plugin. |
||
| 318 | $this->setCache(FALSE); |
||
| 319 | // Quit without doing anything if required variables are not set. |
||
| 320 | if (empty($this->conf['solrcore'])) { |
||
| 321 | Helper::devLog('Incomplete plugin configuration', DEVLOG_SEVERITY_WARNING); |
||
| 322 | return $content; |
||
| 323 | } |
||
| 324 | if (!isset($this->piVars['query']) |
||
| 325 | && empty($this->piVars['extQuery'])) { |
||
| 326 | // Extract query and filter from last search. |
||
| 327 | $list = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(DocumentList::class); |
||
| 328 | View Code Duplication | if (!empty($list->metadata['searchString'])) { |
|
| 329 | if ($list->metadata['options']['source'] == 'search') { |
||
| 330 | $search['query'] = $list->metadata['searchString']; |
||
| 331 | } |
||
| 332 | $search['params'] = $list->metadata['options']['params']; |
||
| 333 | } |
||
| 334 | // Add javascript for search suggestions if enabled and jQuery autocompletion is available. |
||
| 335 | if (!empty($this->conf['suggest'])) { |
||
| 336 | $this->addAutocompleteJS(); |
||
| 337 | } |
||
| 338 | // Load template file. |
||
| 339 | $this->getTemplate(); |
||
| 340 | // Configure @action URL for form. |
||
| 341 | $linkConf = [ |
||
| 342 | 'parameter' => $GLOBALS['TSFE']->id |
||
| 343 | ]; |
||
| 344 | // Fill markers. |
||
| 345 | $markerArray = [ |
||
| 346 | '###ACTION_URL###' => $this->cObj->typoLink_URL($linkConf), |
||
| 347 | '###LABEL_QUERY###' => (!empty($search['query']) ? htmlspecialchars($search['query']) : $this->pi_getLL('label.query')), |
||
| 348 | '###LABEL_SUBMIT###' => $this->pi_getLL('label.submit'), |
||
| 349 | '###FIELD_QUERY###' => $this->prefixId.'[query]', |
||
| 350 | '###QUERY###' => (!empty($search['query']) ? $search['query'] : ''), |
||
| 351 | '###FULLTEXTSWITCH###' => $this->addFulltextSwitch($list->metadata['fulltextSearch']), |
||
| 352 | '###FIELD_DOC###' => ($this->conf['searchIn'] == 'document' || $this->conf['searchIn'] == 'all' ? $this->addCurrentDocument() : ''), |
||
| 353 | '###FIELD_COLL###' => ($this->conf['searchIn'] == 'collection' || $this->conf['searchIn'] == 'all' ? $this->addCurrentCollection() : ''), |
||
| 354 | '###ADDITIONAL_INPUTS###' => $this->addEncryptedCoreName(), |
||
| 355 | '###FACETS_MENU###' => $this->addFacetsMenu(), |
||
| 356 | '###LOGICAL_PAGE###' => $this->addLogicalPage() |
||
| 357 | ]; |
||
| 358 | // Get additional fields for extended search. |
||
| 359 | $extendedSearch = $this->addExtendedSearch(); |
||
| 360 | // Display search form. |
||
| 361 | $content .= $this->cObj->substituteSubpart($this->cObj->substituteMarkerArray($this->template, $markerArray), '###EXT_SEARCH_ENTRY###', $extendedSearch); |
||
| 362 | return $this->pi_wrapInBaseClass($content); |
||
| 363 | } else { |
||
| 364 | // Instantiate search object. |
||
| 365 | $solr = Solr::getInstance($this->conf['solrcore']); |
||
| 366 | if (!$solr->ready) { |
||
| 367 | Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR); |
||
| 368 | return $content; |
||
| 369 | } |
||
| 370 | // Build label for result list. |
||
| 371 | $label = $this->pi_getLL('search', '', TRUE); |
||
| 372 | if (!empty($this->piVars['query'])) { |
||
| 373 | $label .= htmlspecialchars(sprintf($this->pi_getLL('for', ''), $this->piVars['query'])); |
||
| 374 | } |
||
| 375 | // Prepare query parameters. |
||
| 376 | $params = []; |
||
| 377 | $matches = []; |
||
| 378 | // Set search query. |
||
| 379 | if ((!empty($this->conf['fulltext']) && !empty($this->piVars['fulltext'])) |
||
| 380 | || preg_match('/fulltext:\((.*)\)/', $this->piVars['query'], $matches)) { |
||
| 381 | // If the query already is a fulltext query e.g using the facets |
||
| 382 | $this->piVars['query'] = empty($matches[1]) ? $this->piVars['query'] : $matches[1]; |
||
| 383 | // Search in fulltext field if applicable. query must not be empty! |
||
| 384 | if (!empty($this->piVars['query'])) { |
||
| 385 | $query = 'fulltext:('.Solr::escapeQuery($this->piVars['query']).')'; |
||
| 386 | } |
||
| 387 | } else { |
||
| 388 | // Retain given search field if valid. |
||
| 389 | $query = Solr::escapeQueryKeepField($this->piVars['query'], $this->conf['pages']); |
||
| 390 | } |
||
| 391 | // Add extended search query. |
||
| 392 | if (!empty($this->piVars['extQuery']) |
||
| 393 | && is_array($this->piVars['extQuery'])) { |
||
| 394 | $allowedOperators = ['AND', 'OR', 'NOT']; |
||
| 395 | $allowedFields = \TYPO3\CMS\Core\Utility\GeneralUtility::trimExplode(',', $this->conf['extendedFields'], TRUE); |
||
| 396 | for ($i = 0; $i < count($this->piVars['extQuery']); $i++) { |
||
| 397 | if (!empty($this->piVars['extQuery'][$i])) { |
||
| 398 | if (in_array($this->piVars['extOperator'][$i], $allowedOperators) |
||
| 399 | && in_array($this->piVars['extField'][$i], $allowedFields)) { |
||
| 400 | if (!empty($query)) { |
||
| 401 | $query .= ' '.$this->piVars['extOperator'][$i].' '; |
||
| 402 | } |
||
| 403 | $query .= Indexer::getIndexFieldName($this->piVars['extField'][$i], $this->conf['pages']).':('.Solr::escapeQuery($this->piVars['extQuery'][$i]).')'; |
||
| 404 | } |
||
| 405 | } |
||
| 406 | } |
||
| 407 | } |
||
| 408 | // Add filter query for faceting. |
||
| 409 | if (!empty($this->piVars['fq'])) { |
||
| 410 | foreach ($this->piVars['fq'] as $filterQuery) { |
||
| 411 | $params['filterquery'][]['query'] = $filterQuery; |
||
| 412 | } |
||
| 413 | } |
||
| 414 | // Add filter query for in-document searching. |
||
| 415 | if ($this->conf['searchIn'] == 'document' |
||
| 416 | || $this->conf['searchIn'] == 'all') { |
||
| 417 | if (!empty($this->piVars['id']) |
||
| 418 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['id'])) { |
||
| 419 | $params['filterquery'][]['query'] = 'uid:('.$this->piVars['id'].') OR partof:('.$this->piVars['id'].')'; |
||
| 420 | $label .= htmlspecialchars(sprintf($this->pi_getLL('in', ''), Document::getTitle($this->piVars['id']))); |
||
| 421 | } |
||
| 422 | } |
||
| 423 | // Add filter query for in-collection searching. |
||
| 424 | if ($this->conf['searchIn'] == 'collection' |
||
| 425 | || $this->conf['searchIn'] == 'all') { |
||
| 426 | if (!empty($this->piVars['collection']) |
||
| 427 | && \TYPO3\CMS\Core\Utility\MathUtility::canBeInterpretedAsInteger($this->piVars['collection'])) { |
||
| 428 | $index_name = Helper::getIndexNameFromUid($this->piVars['collection'], 'tx_dlf_collections', $this->conf['pages']); |
||
| 429 | $params['filterquery'][]['query'] = 'collection_faceting:("'.Solr::escapeQuery($index_name).'")'; |
||
| 430 | $label .= sprintf($this->pi_getLL('in', '', TRUE), Helper::translate($index_name, 'tx_dlf_collections', $this->conf['pages'])); |
||
| 431 | } |
||
| 432 | } |
||
| 433 | // Add filter query for collection restrictions. |
||
| 434 | if ($this->conf['collections']) { |
||
| 435 | $collIds = explode(',', $this->conf['collections']); |
||
| 436 | $collIndexNames = []; |
||
| 437 | foreach ($collIds as $collId) { |
||
| 438 | $collIndexNames[] = Solr::escapeQuery(Helper::getIndexNameFromUid(intval($collId), 'tx_dlf_collections', $this->conf['pages'])); |
||
| 439 | } |
||
| 440 | // Last value is fake and used for distinction in $this->addCurrentCollection() |
||
| 441 | $params['filterquery'][]['query'] = 'collection_faceting:("'.implode('" OR "', $collIndexNames).'" OR "FakeValueForDistinction")'; |
||
| 442 | } |
||
| 443 | // Set some query parameters. |
||
| 444 | $params['query'] = $query; |
||
| 445 | $params['start'] = 0; |
||
| 446 | $params['rows'] = 0; |
||
| 447 | $params['sort'] = ['score' => 'desc']; |
||
| 448 | // Instantiate search object. |
||
| 449 | $solr = Solr::getInstance($this->conf['solrcore']); |
||
| 450 | if (!$solr->ready) { |
||
| 451 | Helper::devLog('Apache Solr not available', DEVLOG_SEVERITY_ERROR); |
||
| 452 | return $content; |
||
| 453 | } |
||
| 454 | // Set search parameters. |
||
| 455 | $solr->cPid = $this->conf['pages']; |
||
| 456 | $solr->params = $params; |
||
| 457 | // Perform search. |
||
| 458 | $list = $solr->search(); |
||
| 459 | $list->metadata = [ |
||
| 460 | 'label' => $label, |
||
| 461 | 'thumbnail' => '', |
||
| 462 | 'searchString' => $this->piVars['query'], |
||
| 463 | 'fulltextSearch' => (!empty($this->piVars['fulltext']) ? '1' : '0'), |
||
| 464 | 'options' => $list->metadata['options'] |
||
| 465 | ]; |
||
| 466 | $list->save(); |
||
| 467 | // Clean output buffer. |
||
| 468 | ob_end_clean(); |
||
| 469 | $additionalParams = []; |
||
| 470 | if (!empty($this->piVars['logicalPage'])) { |
||
| 471 | $additionalParams['logicalPage'] = $this->piVars['logicalPage']; |
||
| 472 | } |
||
| 473 | // Jump directly to the page view, if there is only one result and it is configured |
||
| 474 | if ($list->metadata['options']['numberOfHits'] == 1 && !empty($this->conf['showSingleResult'])) { |
||
| 475 | $linkConf['parameter'] = $this->conf['targetPidPageView']; |
||
| 476 | $additionalParams['id'] = $list->current()['uid']; |
||
| 477 | $additionalParams['highlight_word'] = preg_replace('/\s\s+/', ';', $list->metadata['searchString']); |
||
| 478 | $additionalParams['page'] = count($list[0]['subparts']) == 1 ? $list[0]['subparts'][0]['page'] : 1; |
||
| 479 | } else { |
||
| 480 | // Keep some plugin variables. |
||
| 481 | $linkConf['parameter'] = $this->conf['targetPid']; |
||
| 482 | if (!empty($this->piVars['order'])) { |
||
| 483 | $additionalParams['order'] = $this->piVars['order']; |
||
| 484 | $additionalParams['asc'] = !empty($this->piVars['asc']) ? '1' : '0'; |
||
| 485 | } |
||
| 486 | } |
||
| 487 | $linkConf['additionalParams'] = \TYPO3\CMS\Core\Utility\GeneralUtility::implodeArrayForUrl($this->prefixId, $additionalParams, '', TRUE, FALSE); |
||
| 488 | // Send headers. |
||
| 489 | header('Location: '.\TYPO3\CMS\Core\Utility\GeneralUtility::locationHeaderUrl($this->cObj->typoLink_URL($linkConf))); |
||
| 490 | exit; |
||
| 491 | } |
||
| 492 | } |
||
| 493 | |||
| 583 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.