| Conditions | 10 |
| Paths | 75 |
| Total Lines | 64 |
| Code Lines | 28 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | 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 |
||
| 178 | protected function _sendReports(array $pmData, string $reason): void |
||
| 179 | { |
||
| 180 | global $language, $modSettings; |
||
| 181 | |||
| 182 | // Remove the line breaks... |
||
| 183 | $body = preg_replace('~<br ?/?>~i', "\n", $pmData['body']); |
||
| 184 | |||
| 185 | $recipients = $this->_getPMRecipients($pmData['id_pm'] ?? (int) $this->_req->getPost('pmsg', 'intval', $this->_req->getQuery('pmsg', 'intval', 0))); |
||
| 186 | |||
| 187 | // Now let's get out and loop through the admins. |
||
| 188 | $admins = admins($this->_req->getPost('id_admin', 'intval', 0)); |
||
| 189 | |||
| 190 | // Maybe we shouldn't advertise this? |
||
| 191 | if (empty($admins)) |
||
| 192 | { |
||
| 193 | throw new Exception('no_access', false); |
||
| 194 | } |
||
| 195 | |||
| 196 | $memberFromName = un_htmlspecialchars($pmData['memberFromName']); |
||
| 197 | |||
| 198 | // Prepare the message storage array. |
||
| 199 | $messagesToSend = []; |
||
| 200 | |||
| 201 | // Loop through each admin and add them to the right language pile... |
||
| 202 | foreach ($admins as $id_admin => $admin_info) |
||
| 203 | { |
||
| 204 | // Need to send in the correct language! |
||
| 205 | $cur_language = empty($admin_info['lngfile']) || empty($modSettings['userLanguage']) ? $language : $admin_info['lngfile']; |
||
| 206 | |||
| 207 | if (!isset($messagesToSend[$cur_language])) |
||
| 208 | { |
||
| 209 | $mtxt = []; |
||
| 210 | $lang = new Loader($cur_language, $mtxt, database()); |
||
| 211 | $lang->load('PersonalMessage', false); |
||
| 212 | |||
| 213 | // Make the body. |
||
| 214 | $report_body = str_replace(['{REPORTER}', '{SENDER}'], [un_htmlspecialchars($this->user->name), $memberFromName], $mtxt['pm_report_pm_user_sent']); |
||
|
|
|||
| 215 | $report_body .= "\n" . '[b]' . $reason . '[/b]' . "\n\n"; |
||
| 216 | if (!empty($recipients)) |
||
| 217 | { |
||
| 218 | $report_body .= $mtxt['pm_report_pm_other_recipients'] . ' ' . implode(', ', $recipients) . "\n\n"; |
||
| 219 | } |
||
| 220 | |||
| 221 | $report_body .= $mtxt['pm_report_pm_unedited_below'] . "\n" . '[quote author=' . (empty($pmData['memberFromID']) ? '"' . $memberFromName . '"' : $memberFromName . ' link=action=profile;u=' . $pmData['memberFromID'] . ' date=' . $pmData['time']) . ']' . "\n" . un_htmlspecialchars($body) . '[/quote]'; |
||
| 222 | |||
| 223 | // Plonk it in the array ;) |
||
| 224 | $messagesToSend[$cur_language] = [ |
||
| 225 | 'subject' => (Util::strpos($pmData['subject'], $mtxt['pm_report_pm_subject']) === false ? $mtxt['pm_report_pm_subject'] : '') . un_htmlspecialchars($pmData['subject']), |
||
| 226 | 'body' => $report_body, |
||
| 227 | 'recipients' => [ |
||
| 228 | 'to' => [], |
||
| 229 | 'bcc' => [] |
||
| 230 | ], |
||
| 231 | ]; |
||
| 232 | } |
||
| 233 | |||
| 234 | // Add them to the list. |
||
| 235 | $messagesToSend[$cur_language]['recipients']['to'][$id_admin] = $id_admin; |
||
| 236 | } |
||
| 237 | |||
| 238 | // Send a different email for each language. |
||
| 239 | foreach ($messagesToSend as $message) |
||
| 240 | { |
||
| 241 | sendpm($message['recipients'], $message['subject'], $message['body']); |
||
| 242 | } |
||
| 263 |