| Conditions | 11 |
| Paths | 256 |
| Total Lines | 39 |
| Code Lines | 19 |
| 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 |
||
| 7 | public function mail($params) { |
||
| 8 | |||
| 9 | $to_name = $params["to_name"]; |
||
| 10 | $to_address = $params["to_address"]; |
||
| 11 | $subject = $params["subject"]; |
||
| 12 | $message = $params["message"]; |
||
| 13 | $from_name = $params["from_name"] ? $params["from_name"] : SMTP_FROM_NAME; |
||
|
|
|||
| 14 | $from_address = $params["from_address"] ? $params["from_address"] : SMTP_FROM_ADDRESS; |
||
| 15 | |||
| 16 | $additional_headers = $params["headers"] ? $params["headers"] : []; |
||
| 17 | |||
| 18 | $from_combined = $from_name ? "$from_name <$from_address>" : $from_address; |
||
| 19 | $to_combined = $to_name ? "$to_name <$to_address>" : $to_address; |
||
| 20 | |||
| 21 | if (defined('_LOG_SENT_MAIL') && _LOG_SENT_MAIL) { |
||
| 22 | Logger::get()->log("Sending mail from $from_combined to $to_combined [$subject]: $message"); |
||
| 23 | } |
||
| 24 | |||
| 25 | // HOOK_SEND_MAIL plugin instructions: |
||
| 26 | // 1. return 1 or true if mail is handled |
||
| 27 | // 2. return -1 if there's been a fatal error and no further action is allowed |
||
| 28 | // 3. any other return value will allow cycling to the next handler and, eventually, to default mail() function |
||
| 29 | // 4. set error message if needed via passed Mailer instance function set_error() |
||
| 30 | |||
| 31 | foreach (PluginHost::getInstance()->get_hooks(PluginHost::HOOK_SEND_MAIL) as $p) { |
||
| 32 | $rc = $p->hook_send_mail($this, $params); |
||
| 33 | |||
| 34 | if ($rc == 1) { |
||
| 35 | return $rc; |
||
| 36 | } |
||
| 37 | |||
| 38 | if ($rc == -1) { |
||
| 39 | return 0; |
||
| 40 | } |
||
| 41 | } |
||
| 42 | |||
| 43 | $headers = ["From: $from_combined", "Content-Type: text/plain; charset=UTF-8"]; |
||
| 44 | |||
| 45 | return mail($to_combined, $subject, $message, implode("\r\n", array_merge($headers, $additional_headers))); |
||
| 46 | } |
||
| 56 |