| Conditions | 17 |
| Paths | 42 |
| Total Lines | 121 |
| Code Lines | 96 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 1 | 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 |
||
| 194 | public function search($search_entryid, $descriptor, $folder_entryid, $recursive) { |
||
| 195 | $startTime = microtime(true); |
||
| 196 | $this->logDebug('Search invoked', [ |
||
| 197 | 'user' => $this->username, |
||
| 198 | 'search_entryid' => self::formatEntryIdForLog($search_entryid), |
||
| 199 | 'folder_entryid' => self::formatEntryIdForLog($folder_entryid), |
||
| 200 | 'recursive' => (bool) $recursive, |
||
| 201 | 'descriptor' => $descriptor, |
||
| 202 | ]); |
||
| 203 | if ($this->openResult) { |
||
| 204 | $this->logDebug('Search aborted: index database unavailable', ['open_result' => $this->openResult]); |
||
| 205 | return false; |
||
| 206 | } |
||
| 207 | $whereFolderids = ''; |
||
| 208 | if (isset($folder_entryid)) { |
||
| 209 | try { |
||
| 210 | $folder = mapi_msgstore_openentry($this->store, $folder_entryid); |
||
| 211 | if (!$folder) { |
||
| 212 | return false; |
||
| 213 | } |
||
| 214 | $tmp_props = mapi_getprops($folder, [PR_FOLDER_ID]); |
||
| 215 | if (empty($tmp_props[PR_FOLDER_ID])) { |
||
| 216 | return false; |
||
| 217 | } |
||
| 218 | $folder_id = IndexSqlite::get_gc_value((int) $tmp_props[PR_FOLDER_ID]); |
||
| 219 | $whereFolderids .= "c.folder_id in (" . $folder_id . ", "; |
||
| 220 | if ($recursive) { |
||
| 221 | $this->getWhereFolderids($folder, $whereFolderids); |
||
| 222 | } |
||
| 223 | $whereFolderids = substr($whereFolderids, 0, -2) . ") AND "; |
||
| 224 | $this->logDebug('Folder scope resolved', [ |
||
| 225 | 'root_folder_gc_id' => $folder_id, |
||
| 226 | 'folder_clause' => rtrim($whereFolderids), |
||
| 227 | ]); |
||
| 228 | } |
||
| 229 | catch (Exception $e) { |
||
| 230 | error_log(sprintf("Index: error getting folder information %s - %s", $this->username, $e)); |
||
| 231 | $this->logDebug('Failed to resolve folder scope', ['error' => $e->getMessage()]); |
||
| 232 | |||
| 233 | return false; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | $sql_string = "SELECT c.message_id, c.entryid, c.folder_id, " . |
||
| 237 | "c.message_class, c.date, c.readflag, c.attach_indexed " . |
||
| 238 | "FROM msg_content c " . |
||
| 239 | "JOIN messages m ON c.message_id = m.rowid " . |
||
| 240 | "WHERE "; |
||
| 241 | if (!empty($whereFolderids)) { |
||
| 242 | $sql_string .= $whereFolderids; |
||
| 243 | } |
||
| 244 | $ftsAst = $descriptor['ast'] ?? null; |
||
| 245 | $message_classes = $descriptor['message_classes'] ?? null; |
||
| 246 | $date_start = $descriptor['date_start'] ?? null; |
||
| 247 | $date_end = $descriptor['date_end'] ?? null; |
||
| 248 | $unread = !empty($descriptor['unread']); |
||
| 249 | $has_attachments = !empty($descriptor['has_attachments']); |
||
| 250 | $this->logDebug('Search filters resolved', [ |
||
| 251 | 'unread' => $unread, |
||
| 252 | 'has_attachments' => $has_attachments, |
||
| 253 | 'date_start' => $date_start, |
||
| 254 | 'date_end' => $date_end, |
||
| 255 | 'message_classes_count' => is_array($message_classes) ? count($message_classes) : null, |
||
| 256 | ]); |
||
| 257 | |||
| 258 | $ftsQuery = $this->compileFtsExpression($ftsAst); |
||
| 259 | if ($ftsQuery === null || $ftsQuery === '') { |
||
| 260 | $this->logDebug('FTS query compilation returned empty expression', [ |
||
| 261 | 'ast' => $ftsAst, |
||
| 262 | ]); |
||
| 263 | return false; |
||
| 264 | } |
||
| 265 | |||
| 266 | $sql_string .= "messages MATCH '" . $ftsQuery . "'"; |
||
| 267 | $this->count = 0; |
||
| 268 | $sql_string .= " ORDER BY c.date DESC LIMIT " . MAX_FTS_RESULT_ITEMS; |
||
| 269 | $this->logDebug('Executing SQLite FTS query', ['sql' => $sql_string]); |
||
| 270 | $results = $this->query($sql_string); |
||
| 271 | if ($results === false) { |
||
| 272 | $this->logDebug('SQLite query execution failed', [ |
||
| 273 | 'error_code' => $this->lastErrorCode(), |
||
| 274 | 'error_message' => $this->lastErrorMsg(), |
||
| 275 | ]); |
||
| 276 | return false; |
||
| 277 | } |
||
| 278 | $matchedRows = 0; |
||
| 279 | $sampleRows = []; |
||
| 280 | while (($row = $results->fetchArray(SQLITE3_ASSOC)) && !$this->result_full()) { |
||
| 281 | ++$matchedRows; |
||
| 282 | $previousCount = $this->count; |
||
| 283 | $this->try_insert_content( |
||
| 284 | $search_entryid, |
||
| 285 | $row, |
||
| 286 | $message_classes, |
||
| 287 | $date_start, |
||
| 288 | $date_end, |
||
| 289 | $unread, |
||
| 290 | $has_attachments |
||
| 291 | ); |
||
| 292 | if ($this->count > $previousCount && count($sampleRows) < self::DEBUG_SAMPLE_LIMIT) { |
||
| 293 | $sampleRows[] = [ |
||
| 294 | 'message_id' => $row['message_id'] ?? null, |
||
| 295 | 'entryid' => self::formatBinaryFieldForLog($row['entryid'] ?? null), |
||
| 296 | 'message_class' => $row['message_class'] ?? null, |
||
| 297 | 'date' => $row['date'] ?? null, |
||
| 298 | 'readflag' => $row['readflag'] ?? null, |
||
| 299 | 'attach_indexed' => $row['attach_indexed'] ?? null, |
||
| 300 | ]; |
||
| 301 | } |
||
| 302 | } |
||
| 303 | $durationMs = (int) round((microtime(true) - $startTime) * 1000); |
||
| 304 | $this->logDebug('Search completed', [ |
||
| 305 | 'fts_query' => $ftsQuery, |
||
| 306 | 'matched_rows' => $matchedRows, |
||
| 307 | 'linked_messages' => $this->count, |
||
| 308 | 'limit_reached' => $this->result_full(), |
||
| 309 | 'folder_clause' => $whereFolderids !== '' ? rtrim($whereFolderids) : null, |
||
| 310 | 'sample_messages' => $sampleRows, |
||
| 311 | 'duration_ms' => $durationMs, |
||
| 312 | ]); |
||
| 313 | |||
| 314 | return true; |
||
| 315 | } |
||
| 424 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.