Conditions | 41 |
Paths | 8192 |
Total Lines | 115 |
Code Lines | 72 |
Lines | 37 |
Ratio | 32.17 % |
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 |
||
169 | public function indexedWordQuery(array $words, array $search_data) |
||
170 | { |
||
171 | global $modSettings, $smcFunc; |
||
172 | |||
173 | $query_select = array( |
||
174 | 'id_msg' => 'm.id_msg', |
||
175 | ); |
||
176 | $query_where = array(); |
||
177 | $query_params = $search_data['params']; |
||
178 | |||
179 | if( $smcFunc['db_title'] == "PostgreSQL") |
||
180 | $modSettings['search_simple_fulltext'] = true; |
||
181 | |||
182 | if ($query_params['id_search']) |
||
183 | $query_select['id_search'] = '{int:id_search}'; |
||
184 | |||
185 | $count = 0; |
||
186 | if (empty($modSettings['search_simple_fulltext'])) |
||
187 | View Code Duplication | foreach ($words['words'] as $regularWord) |
|
188 | { |
||
189 | $query_where[] = 'm.body' . (in_array($regularWord, $query_params['excluded_words']) ? ' NOT' : '') . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : 'RLIKE') . '{string:complex_body_' . $count . '}'; |
||
190 | $query_params['complex_body_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($regularWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $regularWord), '\\\'') . '[[:>:]]'; |
||
191 | } |
||
192 | |||
193 | if ($query_params['user_query']) |
||
194 | $query_where[] = '{raw:user_query}'; |
||
195 | if ($query_params['board_query']) |
||
196 | $query_where[] = 'm.id_board {raw:board_query}'; |
||
197 | |||
198 | if ($query_params['topic']) |
||
199 | $query_where[] = 'm.id_topic = {int:topic}'; |
||
200 | if ($query_params['min_msg_id']) |
||
201 | $query_where[] = 'm.id_msg >= {int:min_msg_id}'; |
||
202 | if ($query_params['max_msg_id']) |
||
203 | $query_where[] = 'm.id_msg <= {int:max_msg_id}'; |
||
204 | |||
205 | $count = 0; |
||
206 | View Code Duplication | if (!empty($query_params['excluded_phrases']) && empty($modSettings['search_force_index'])) |
|
207 | foreach ($query_params['excluded_phrases'] as $phrase) |
||
208 | { |
||
209 | $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : 'RLIKE') . '{string:exclude_subject_phrase_' . $count . '}'; |
||
210 | $query_params['exclude_subject_phrase_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($phrase, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $phrase), '\\\'') . '[[:>:]]'; |
||
211 | } |
||
212 | $count = 0; |
||
213 | View Code Duplication | if (!empty($query_params['excluded_subject_words']) && empty($modSettings['search_force_index'])) |
|
214 | foreach ($query_params['excluded_subject_words'] as $excludedWord) |
||
215 | { |
||
216 | $query_where[] = 'subject NOT ' . (empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? ' LIKE ' : 'RLIKE') . '{string:exclude_subject_words_' . $count . '}'; |
||
217 | $query_params['exclude_subject_words_' . $count++] = empty($modSettings['search_match_words']) || $search_data['no_regexp'] ? '%' . strtr($excludedWord, array('_' => '\\_', '%' => '\\%')) . '%' : '[[:<:]]' . addcslashes(preg_replace(array('/([\[\]$.+*?|{}()])/'), array('[$1]'), $excludedWord), '\\\'') . '[[:>:]]'; |
||
218 | } |
||
219 | |||
220 | if (!empty($modSettings['search_simple_fulltext'])) |
||
221 | { |
||
222 | View Code Duplication | if ($smcFunc['db_title'] == "PostgreSQL") |
|
223 | { |
||
224 | $language_ftx = $smcFunc['db_search_language'](); |
||
225 | |||
226 | $query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:body_match})'; |
||
227 | $query_params['language_ftx'] = $language_ftx; |
||
228 | } |
||
229 | else |
||
230 | $query_where[] = 'MATCH (body) AGAINST ({string:body_match})'; |
||
231 | $query_params['body_match'] = implode(' ', array_diff($words['indexed_words'], $query_params['excluded_index_words'])); |
||
232 | } |
||
233 | else |
||
234 | { |
||
235 | $query_params['boolean_match'] = ''; |
||
236 | |||
237 | // remove any indexed words that are used in the complex body search terms |
||
238 | $words['indexed_words'] = array_diff($words['indexed_words'], $words['complex_words']); |
||
239 | |||
240 | if ($smcFunc['db_title'] == "PostgreSQL") |
||
241 | { |
||
242 | $row = 0; |
||
243 | foreach ($words['indexed_words'] as $fulltextWord) |
||
244 | { |
||
245 | $query_params['boolean_match'] .= ($row <> 0 ? '&' : ''); |
||
246 | $query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '!' : '') . $fulltextWord . ' '; |
||
247 | $row++; |
||
248 | } |
||
249 | } |
||
250 | else |
||
251 | foreach ($words['indexed_words'] as $fulltextWord) |
||
252 | $query_params['boolean_match'] .= (in_array($fulltextWord, $query_params['excluded_index_words']) ? '-' : '+') . $fulltextWord . ' '; |
||
253 | |||
254 | $query_params['boolean_match'] = substr($query_params['boolean_match'], 0, -1); |
||
255 | |||
256 | // if we have bool terms to search, add them in |
||
257 | View Code Duplication | if ($query_params['boolean_match']) |
|
258 | { |
||
259 | if($smcFunc['db_title'] == "PostgreSQL") |
||
260 | { |
||
261 | $language_ftx = $smcFunc['db_search_language'](); |
||
262 | |||
263 | $query_where[] = 'to_tsvector({string:language_ftx},body) @@ plainto_tsquery({string:language_ftx},{string:boolean_match})'; |
||
264 | $query_params['language_ftx'] = $language_ftx; |
||
265 | } |
||
266 | else |
||
267 | $query_where[] = 'MATCH (body) AGAINST ({string:boolean_match} IN BOOLEAN MODE)'; |
||
268 | } |
||
269 | } |
||
270 | |||
271 | $ignoreRequest = $smcFunc['db_search_query']('insert_into_log_messages_fulltext', ($smcFunc['db_support_ignore'] ? ( ' |
||
272 | INSERT IGNORE INTO {db_prefix}' . $search_data['insert_into'] . ' |
||
273 | (' . implode(', ', array_keys($query_select)) . ')') : '') . ' |
||
274 | SELECT ' . implode(', ', $query_select) . ' |
||
275 | FROM {db_prefix}messages AS m |
||
276 | WHERE ' . implode(' |
||
277 | AND ', $query_where) . (empty($search_data['max_results']) ? '' : ' |
||
278 | LIMIT ' . ($search_data['max_results'] - $search_data['indexed_results'])), |
||
279 | $query_params |
||
280 | ); |
||
281 | |||
282 | return $ignoreRequest; |
||
283 | } |
||
284 | } |
||
286 | ?> |
Instead of relying on
global
state, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state