| Conditions | 18 |
| Paths | 4 |
| Total Lines | 100 |
| Code Lines | 54 |
| 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 |
||
| 122 | function getRecipientList($action, $recipient_history) { |
||
| 123 | if(!empty($action["query"]) && !empty($recipient_history) && !empty($recipient_history['recipients'])) { |
||
| 124 | // Setup result array with match levels |
||
| 125 | $l_aResult = Array( |
||
| 126 | 0 => Array(), // Matches on whole string |
||
| 127 | 1 => Array() // Matches on part of string |
||
| 128 | ); |
||
| 129 | |||
| 130 | // Loop through all the recipients |
||
| 131 | |||
| 132 | for($i = 0, $len = count($recipient_history['recipients']); $i < $len; $i++) { |
||
| 133 | // Prepare strings for case sensitive search |
||
| 134 | $l_sName = strtolower($recipient_history['recipients'][$i]['display_name']); |
||
| 135 | $l_sEmail = strtolower($recipient_history['recipients'][$i]['smtp_address']); |
||
| 136 | $l_sSearchString = strtolower($action["query"]); |
||
| 137 | |||
| 138 | // Check for the presence of the search string |
||
| 139 | $l_ibPosName = strpos($l_sName, $l_sSearchString); |
||
| 140 | $l_ibPosEmail = strpos($l_sEmail, $l_sSearchString); |
||
| 141 | |||
| 142 | // Check if the string is present in name or email fields |
||
| 143 | if($l_ibPosName !== false || $l_ibPosEmail !== false){ |
||
| 144 | // Check if the found string matches from the start of the word |
||
| 145 | if($l_ibPosName === 0 || substr($l_sName, ($l_ibPosName-1), 1) == ' ' || $l_ibPosEmail === 0 || substr($l_sEmail, ($l_ibPosEmail-1), 1) == ' '){ |
||
| 146 | array_push($l_aResult[0], Array( |
||
| 147 | 'display_name' => $recipient_history['recipients'][$i]['display_name'], |
||
| 148 | 'smtp_address' => $recipient_history['recipients'][$i]['smtp_address'], |
||
| 149 | 'email_address' => $recipient_history['recipients'][$i]['email_address'], |
||
| 150 | 'address_type' => $recipient_history['recipients'][$i]['address_type'], |
||
| 151 | 'count' => $recipient_history['recipients'][$i]['count'], |
||
| 152 | 'last_used' => $recipient_history['recipients'][$i]['last_used'], |
||
| 153 | 'object_type' => $recipient_history['recipients'][$i]['object_type'], |
||
| 154 | )); |
||
| 155 | // Does not match from start of a word, but start in the middle |
||
| 156 | } else { |
||
| 157 | array_push($l_aResult[1], Array( |
||
| 158 | 'display_name' => $recipient_history['recipients'][$i]['display_name'], |
||
| 159 | 'smtp_address' => $recipient_history['recipients'][$i]['smtp_address'], |
||
| 160 | 'email_address' => $recipient_history['recipients'][$i]['email_address'], |
||
| 161 | 'address_type' => $recipient_history['recipients'][$i]['address_type'], |
||
| 162 | 'count' => $recipient_history['recipients'][$i]['count'], |
||
| 163 | 'last_used' => $recipient_history['recipients'][$i]['last_used'], |
||
| 164 | 'object_type' => $recipient_history['recipients'][$i]['object_type'], |
||
| 165 | )); |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | // Prevent the displaying of the exact match of the whole email address when only one item is found. |
||
| 171 | if(count($l_aResult[0]) == 1 && empty($l_aResult[1]) && $l_sSearchString == strtolower($l_aResult[0][0]['smtp_address'])){ |
||
| 172 | $recipientList = Array(); |
||
| 173 | } else { |
||
| 174 | /** |
||
| 175 | * Sort lists |
||
| 176 | * |
||
| 177 | * This block of code sorts the two lists and creates one final list. |
||
| 178 | * The first list holds the matches based on whole words or words |
||
| 179 | * beginning with the search string and the second list contains the |
||
| 180 | * partial matches that start in the middle of the words. |
||
| 181 | * The first list is sorted on count (the number of emails sent to this |
||
| 182 | * email address), name and finally on the email address. This is done |
||
| 183 | * by a natural sort. When this first list already contains the maximum |
||
| 184 | * number of returned items the second list needs no sorting. If it has |
||
| 185 | * less, then the second list is sorted and included in the first list |
||
| 186 | * as well. At the end the final list is sorted on name and email again. |
||
| 187 | * |
||
| 188 | */ |
||
| 189 | $l_iMaxNumListItems = 10; |
||
| 190 | $l_aSortedList = Array(); |
||
| 191 | usort($l_aResult[0], Array($this, 'cmpSortResultList')); |
||
| 192 | for($i = 0, $len = min($l_iMaxNumListItems, count($l_aResult[0])); $i < $len; $i++){ |
||
| 193 | $l_aSortedList[] = $l_aResult[0][$i]; |
||
| 194 | } |
||
| 195 | if(count($l_aSortedList) < $l_iMaxNumListItems){ |
||
| 196 | $l_iMaxNumRemainingListItems = $l_iMaxNumListItems - count($l_aSortedList); |
||
| 197 | usort($l_aResult[1], Array($this, 'cmpSortResultList')); |
||
| 198 | for($i = 0, $len = min($l_iMaxNumRemainingListItems, count($l_aResult[1])); $i < $len; $i++){ |
||
| 199 | $l_aSortedList[] = $l_aResult[1][$i]; |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | $recipientList = Array(); |
||
| 204 | foreach($l_aSortedList as $index => $recipient) { |
||
| 205 | $recipient['id'] = count($recipientList) + 1; |
||
| 206 | $recipientList[] = $recipient; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | |||
| 210 | $data = Array( |
||
| 211 | 'query' => $action["query"], |
||
| 212 | 'results' => $recipientList |
||
| 213 | ); |
||
| 214 | } else { |
||
| 215 | $data = Array( |
||
| 216 | 'query' => $action["query"], |
||
| 217 | 'results' => Array() |
||
| 218 | ); |
||
| 219 | } |
||
| 220 | |||
| 221 | return $data; |
||
| 222 | } |
||
| 225 |