Conditions | 4 |
Paths | 6 |
Total Lines | 56 |
Code Lines | 26 |
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 |
||
54 | public function sendMailWithPHPMailer($user_email, $from_email, $from_name, $subject, $body) |
||
55 | { |
||
56 | $mail = new PHPMailer; |
||
57 | |||
58 | // you should use UTF-8 to avoid encoding issues |
||
59 | $mail->CharSet = 'UTF-8'; |
||
60 | |||
61 | // if you want to send mail via PHPMailer using SMTP credentials |
||
62 | if (Config::get('EMAIL_USE_SMTP')) { |
||
63 | |||
64 | // set PHPMailer to use SMTP |
||
65 | $mail->IsSMTP(); |
||
66 | |||
67 | // 0 = off, 1 = commands, 2 = commands and data, perfect to see SMTP errors |
||
68 | $mail->SMTPDebug = 0; |
||
69 | |||
70 | // enable SMTP authentication |
||
71 | $mail->SMTPAuth = Config::get('EMAIL_SMTP_AUTH'); |
||
72 | |||
73 | // encryption |
||
74 | if (Config::get('EMAIL_SMTP_ENCRYPTION')) { |
||
75 | $mail->SMTPSecure = Config::get('EMAIL_SMTP_ENCRYPTION'); |
||
76 | } |
||
77 | |||
78 | // set SMTP provider's credentials |
||
79 | $mail->Host = Config::get('EMAIL_SMTP_HOST'); |
||
80 | $mail->Username = Config::get('EMAIL_SMTP_USERNAME'); |
||
81 | $mail->Password = Config::get('EMAIL_SMTP_PASSWORD'); |
||
82 | $mail->Port = Config::get('EMAIL_SMTP_PORT'); |
||
83 | |||
84 | } else { |
||
85 | |||
86 | $mail->IsMail(); |
||
87 | } |
||
88 | |||
89 | // fill mail with data |
||
90 | $mail->From = $from_email; |
||
91 | $mail->FromName = $from_name; |
||
92 | $mail->AddAddress($user_email); |
||
93 | $mail->Subject = $subject; |
||
94 | $mail->Body = $body; |
||
95 | |||
96 | // try to send mail, put result status (true/false into $wasSendingSuccessful) |
||
97 | // I'm unsure if mail->send really returns true or false every time, tis method in PHPMailer is quite complex |
||
98 | $wasSendingSuccessful = $mail->Send(); |
||
99 | |||
100 | if ($wasSendingSuccessful) { |
||
101 | return true; |
||
102 | |||
103 | } else { |
||
104 | |||
105 | // if not successful, copy errors into Mail's error property |
||
106 | $this->error = $mail->ErrorInfo; |
||
107 | return false; |
||
108 | } |
||
109 | } |
||
110 | |||
152 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.