| Conditions | 17 |
| Paths | 3075 |
| Total Lines | 130 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 |
||
| 121 | public function searchQuery($search_words, $excluded_words, &$participants) |
||
| 122 | { |
||
| 123 | global $context, $modSettings; |
||
| 124 | |||
| 125 | // Only request the results if they haven't been cached yet. |
||
| 126 | $cached_results = []; |
||
| 127 | $cache_key = 'manticore_results_' . md5(User::$info->query_see_board . '_' . $context['params']); |
||
| 128 | if (!Cache::instance()->getVar($cached_results, $cache_key)) |
||
| 129 | { |
||
| 130 | // Connect to the manticore searchd and set a few options. |
||
| 131 | $myManticore = $this->manticoreConnect(); |
||
| 132 | |||
| 133 | // Compile different options for our query |
||
| 134 | $index = (empty($modSettings['manticore_index_prefix']) ? 'elkarte' : $modSettings['manticore_index_prefix']) . '_index'; |
||
| 135 | $query = 'SELECT *' . (empty($this->_searchParams->topic) ? ', COUNT(*) num' : '') . ', WEIGHT() relevance FROM ' . $index; |
||
| 136 | |||
| 137 | // Construct the (binary mode & |) query. |
||
| 138 | $where_match = $search_words[0]; |
||
| 139 | |||
| 140 | // Nothing to search, return zero results |
||
| 141 | if (trim($where_match) === '') |
||
| 142 | { |
||
| 143 | return []; |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($this->_searchParams->subject_only) |
||
| 147 | { |
||
| 148 | $where_match = '@subject ' . $where_match; |
||
| 149 | } |
||
| 150 | |||
| 151 | $query .= " WHERE MATCH('" . $where_match . "')"; |
||
| 152 | |||
| 153 | // Set the limits based on the search parameters, board, member, dates, etc |
||
| 154 | $extra_where = $this->buildQueryLimits(); |
||
| 155 | if (!empty($extra_where)) |
||
| 156 | { |
||
| 157 | $query .= ' AND ' . implode(' AND ', $extra_where); |
||
| 158 | } |
||
| 159 | |||
| 160 | // Put together a sort string; besides the main column sort (relevance, id_topic, or num_replies) |
||
| 161 | $this->_searchParams->sort_dir = strtoupper($this->_searchParams->sort_dir); |
||
| 162 | $manticore_sort = $this->_searchParams->sort === 'id_msg' ? 'id_topic' : $this->_searchParams->sort; |
||
| 163 | |||
| 164 | // Add secondary sorting based on relevance(rank) value (if not the main sort method) and age |
||
| 165 | $manticore_sort .= ' ' . $this->_searchParams->sort_dir . ($this->_searchParams->sort === 'relevance' ? '' : ', relevance DESC') . ', poster_time DESC'; |
||
| 166 | |||
| 167 | // Grouping by topic id makes it return only one result per topic, so don't set that for in-topic searches |
||
| 168 | if (empty($this->_searchParams->topic)) |
||
| 169 | { |
||
| 170 | // In the topic group, use the most weighty result for display purposes |
||
| 171 | $query .= ' GROUP BY id_topic WITHIN GROUP ORDER BY relevance DESC'; |
||
| 172 | } |
||
| 173 | |||
| 174 | $query .= ' ORDER BY ' . $manticore_sort; |
||
| 175 | $query .= ' LIMIT ' . min(500, $modSettings['manticore_max_results']); |
||
| 176 | |||
| 177 | // Set any options needed, like field weights. |
||
| 178 | // ranker is a modification of SPH_RANK_SPH04 sum((4*lcs+2*(min_hit_pos==1)+exact_hit)*user_weight)*1000+bm25 |
||
| 179 | // Each term will return a 0-1000 range we include our acprel value for the final total and order. Position |
||
| 180 | // is the relative reply # to a post, so the later a reply in a topic the less overall weight it is given |
||
| 181 | // the calculated value of ranker is returned in WEIGHTS() which we name relevance in the query |
||
| 182 | $subject_weight = empty($modSettings['search_weight_subject']) ? 30 : $modSettings['search_weight_subject']; |
||
| 183 | $query .= ' |
||
| 184 | OPTION |
||
| 185 | field_weights=(subject=' . $subject_weight . ', body=' . (100 - $subject_weight) . '), |
||
| 186 | ranker=expr(\'sum((4*lcs+2*(min_hit_pos==1)+word_count)*user_weight*position) + acprel + bm25 \'), |
||
| 187 | idf=plain, |
||
| 188 | boolean_simplify=1, |
||
| 189 | max_matches=' . min(500, $modSettings['manticore_max_results']); |
||
| 190 | |||
| 191 | // Execute the search query. |
||
| 192 | $request = mysqli_query($myManticore, $query); |
||
| 193 | |||
| 194 | // Bad query, lets log the error and act like it's not our fault |
||
| 195 | if ($request === false) |
||
| 196 | { |
||
| 197 | // Just log the error. |
||
| 198 | if (mysqli_error($myManticore) !== '') |
||
| 199 | { |
||
| 200 | Errors::instance()->log_error(mysqli_error($myManticore)); |
||
| 201 | } |
||
| 202 | |||
| 203 | Errors::instance()->fatal_lang_error('error_invalid_search_daemon'); |
||
| 204 | } |
||
| 205 | |||
| 206 | // Get the relevant information from the search results. |
||
| 207 | $cached_results = [ |
||
| 208 | 'num_results' => 0, |
||
| 209 | 'matches' => [], |
||
| 210 | ]; |
||
| 211 | |||
| 212 | if (mysqli_num_rows($request) !== 0) |
||
| 213 | { |
||
| 214 | while ($match = mysqli_fetch_assoc($request)) |
||
| 215 | { |
||
| 216 | $num = 0; |
||
| 217 | if (empty($this->_searchParams->topic)) |
||
| 218 | { |
||
| 219 | $num = $match['num'] ?? ($match['@count'] ?? 0); |
||
| 220 | } |
||
| 221 | |||
| 222 | $cached_results['matches'][$match['id']] = [ |
||
| 223 | 'id' => $match['id_topic'], |
||
| 224 | 'num_matches' => $num, |
||
| 225 | 'matches' => [], |
||
| 226 | 'relevance' => round($match['relevance']), |
||
| 227 | ]; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | |||
| 231 | mysqli_free_result($request); |
||
| 232 | mysqli_close($myManticore); |
||
| 233 | |||
| 234 | $cached_results['num_results'] = count($cached_results['matches']); |
||
| 235 | |||
| 236 | // Store the search results in the cache. |
||
| 237 | Cache::instance()->put($cache_key, $cached_results, 600); |
||
| 238 | } |
||
| 239 | |||
| 240 | $participants = []; |
||
| 241 | $topics = []; |
||
| 242 | foreach (array_slice(array_keys($cached_results['matches']), $this->_req->getRequest('start', 'intval', 0), $modSettings['search_results_per_page']) as $msgID) |
||
| 243 | { |
||
| 244 | $topics[$msgID] = $cached_results['matches'][$msgID]; |
||
| 245 | $participants[$cached_results['matches'][$msgID]['id']] = false; |
||
| 246 | } |
||
| 247 | |||
| 248 | $this->_num_results = $cached_results['num_results']; |
||
| 249 | |||
| 250 | return $topics; |
||
| 251 | } |
||
| 328 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths