| Conditions | 12 |
| Paths | 48 |
| Total Lines | 43 |
| Code Lines | 26 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 74 | public function search($q, $firstPage = false, $searchType = 'AND', $searchOrder = 'newest', $limit = -1, $page = 1, array $moduleData = []) |
||
| 75 | { |
||
| 76 | $limit = isset($limit) && !empty($limit) ? $limit : $this->variableApi->get('ZikulaSearchModule', 'itemsperpage', 25); |
||
| 77 | $offset = $limit > 0 ? (($page - 1) * $limit) : 0; |
||
| 78 | |||
| 79 | // obtain and persist search results from searchableModules |
||
| 80 | if ($firstPage) { |
||
| 81 | // Clear current search result for current user - before showing the first page |
||
| 82 | // Clear also older searches from other users. |
||
| 83 | $this->searchResultRepository->clearOldResults($this->session->getId()); |
||
| 84 | // convert query string to an *array* of words |
||
| 85 | $words = ($searchType == 'EXACT') ? [trim($q)] : preg_split('/ /', $q, -1, PREG_SPLIT_NO_EMPTY); |
||
| 86 | $searchableModules = $this->searchableModuleCollector->getAll(); |
||
| 87 | foreach ($searchableModules as $moduleName => $searchableInstance) { |
||
| 88 | if (isset($moduleData['active']) && !$moduleData['active']) { |
||
| 89 | continue; |
||
| 90 | } |
||
| 91 | $moduleFormData = isset($moduleData[$moduleName]) ? $moduleData[$moduleName] : null; |
||
| 92 | $results = $searchableInstance->getResults($words, $searchType, $moduleFormData); |
||
| 93 | foreach ($results as $searchResult) { |
||
| 94 | $this->searchResultRepository->persist($searchResult); |
||
| 95 | } |
||
| 96 | } |
||
| 97 | $this->searchResultRepository->flush(); |
||
| 98 | |||
| 99 | $resultCount = $this->searchResultRepository->countResults($this->session->getId()); |
||
| 100 | $this->session->set('searchResultCount', $resultCount); |
||
| 101 | } else { |
||
| 102 | $resultCount = $this->session->get('searchResultCount'); |
||
| 103 | } |
||
| 104 | |||
| 105 | $results = $this->searchResultRepository->getResults(['sesid' => $this->session->getId()], $this->computeSort($searchOrder), $limit, $offset); |
||
| 106 | |||
| 107 | $result = [ |
||
| 108 | 'count' => $resultCount, |
||
| 109 | 'results' => $results, |
||
| 110 | ]; |
||
| 111 | if (isset($searchableInstance)) { |
||
| 112 | $result['errors'] = $searchableInstance->getErrors(); |
||
| 113 | } |
||
| 114 | |||
| 115 | return $result; |
||
| 116 | } |
||
| 117 | |||
| 150 |