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