| Conditions | 29 |
| Paths | 51 |
| Total Lines | 103 |
| Code Lines | 79 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 5 | ||
| 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 |
||
| 94 | public function search($search_entryid, $search_patterns, $folder_entryid, $recursive) { |
||
| 95 | $whereFolderids = ''; |
||
| 96 | if (isset($folder_entryid)) { |
||
| 97 | try { |
||
| 98 | $folder = mapi_msgstore_openentry($this->store, $folder_entryid); |
||
| 99 | if (!$folder) { |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | $tmp_props = mapi_getprops($folder, [PR_FOLDER_ID]); |
||
| 103 | if (empty($tmp_props[PR_FOLDER_ID])) { |
||
| 104 | return false; |
||
| 105 | } |
||
| 106 | $folder_id = IndexSqlite::get_gc_value((int) $tmp_props[PR_FOLDER_ID]); |
||
| 107 | $whereFolderids .= "c.folder_id in (" . $folder_id . ", "; |
||
| 108 | if ($recursive) { |
||
| 109 | $this->getWhereFolderids($folder, $whereFolderids); |
||
| 110 | } |
||
| 111 | $whereFolderids = substr($whereFolderids, 0, -2) . ") AND "; |
||
| 112 | } |
||
| 113 | catch (Exception $e) { |
||
| 114 | error_log(sprintf("Index: error getting folder information %s - %s", $this->username, $e)); |
||
| 115 | |||
| 116 | return false; |
||
| 117 | } |
||
| 118 | } |
||
| 119 | $sql_string = "SELECT c.message_id, c.entryid, c.folder_id, " . |
||
| 120 | "c.message_class, c.date, c.readflag, c.attach_indexed " . |
||
| 121 | "FROM msg_content c " . |
||
| 122 | "JOIN messages m ON c.message_id = m.rowid " . |
||
| 123 | "WHERE "; |
||
| 124 | if (!empty($whereFolderids)) { |
||
| 125 | $sql_string .= $whereFolderids; |
||
| 126 | } |
||
| 127 | $sql_string .= "messages MATCH '"; |
||
| 128 | $this->count = 0; |
||
| 129 | // Extract search_patterns into separate variables |
||
| 130 | [ |
||
| 131 | 'sender' => $sender, |
||
| 132 | 'sending' => $sending, |
||
| 133 | 'recipients' => $recipients, |
||
| 134 | 'subject' => $subject, |
||
| 135 | 'content' => $content, |
||
| 136 | 'attachments' => $attachments, |
||
| 137 | 'others' => $others, |
||
| 138 | 'message_classes' => $message_classes, |
||
| 139 | 'date_start' => $date_start, |
||
| 140 | 'date_end' => $date_end, |
||
| 141 | 'unread' => $unread, |
||
| 142 | 'has_attachments' => $has_attachments, |
||
| 143 | 'categories' => $categories, |
||
| 144 | ] = $search_patterns; |
||
| 145 | if (isset($sender) && $sender == $sending && $sending == $recipients && $recipients == $subject && |
||
| 146 | $subject == $content && $content == $attachments && $attachments == $others && empty($categories)) { |
||
| 147 | $sql_string .= SQLite3::escapeString($this->quote_words($sender)) . "'"; |
||
| 148 | } |
||
| 149 | else { |
||
| 150 | $first = true; |
||
| 151 | foreach ($search_patterns as $key => $search_pattern) { |
||
| 152 | switch($key) { |
||
| 153 | case 'message_classes': |
||
| 154 | case 'date_start': |
||
| 155 | case 'date_end': |
||
| 156 | case 'unread': |
||
| 157 | case 'has_attachments': |
||
| 158 | case 'categories': |
||
| 159 | break; |
||
| 160 | default: |
||
| 161 | if (!is_null($search_pattern)) { |
||
| 162 | if ($first === true) { |
||
| 163 | $first = false; |
||
| 164 | } |
||
| 165 | else { |
||
| 166 | $sql_string .= " OR "; |
||
| 167 | } |
||
| 168 | $sql_string .= $key . ':' . SQLite3::escapeString($this->quote_words($search_pattern)); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | if ($first) { |
||
| 173 | return false; |
||
| 174 | } |
||
| 175 | $sql_string .= "'"; |
||
| 176 | if (!empty($categories)) { |
||
| 177 | foreach ($categories as $category) { |
||
| 178 | $sql_string .= " AND messages MATCH 'others:" . SQLite3::escapeString($this->quote_words($category)) . "'"; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } |
||
| 182 | $sql_string .= " ORDER BY c.date DESC LIMIT " . MAX_FTS_RESULT_ITEMS; |
||
| 183 | $results = $this->query($sql_string); |
||
| 184 | while (($row = $results->fetchArray(SQLITE3_ASSOC)) && !$this->result_full()) { |
||
| 185 | $this->try_insert_content( |
||
| 186 | $search_entryid, |
||
| 187 | $row, |
||
| 188 | $message_classes, |
||
| 189 | $date_start, |
||
| 190 | $date_end, |
||
| 191 | $unread, |
||
| 192 | $has_attachments |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | |||
| 196 | return true; |
||
| 197 | } |
||
| 253 |
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.