Conditions | 25 |
Paths | 1348 |
Total Lines | 141 |
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:
Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.
There are several approaches to avoid long parameter lists:
1 | <?php |
||
115 | function phpmailer_send($to, $to_name, $subject, $body, array $bcc = NULL, $html = true, array $files = NULL, array $params = NULL, $image = NULL) { |
||
116 | |||
117 | require_once elgg_get_plugins_path() . '/phpmailer/vendors/class.phpmailer.php'; |
||
118 | $phpmailer = new PHPMailer(true); //defaults to using php "mail()"; the true param means it will throw exceptions on errors, which we need to catch |
||
119 | |||
120 | // CONFIGURE SERVER INFORMATION defaults |
||
121 | $phpmailer->IsSMTP(); // telling the class to use SMTP |
||
122 | $phpmailer->Host = elgg_get_plugin_setting('phpmailer_host', 'phpmailer'); // SMTP server |
||
123 | $phpmailer->Port = elgg_get_plugin_setting('ep_phpmailer_port', 'phpmailer'); // SMTP server port |
||
124 | $phpmailer->SMTPAuth = elgg_get_plugin_setting('phpmailer_smtp_auth', 'phpmailer'); |
||
125 | $phpmailer->SMTPSecure = ""; // set below |
||
126 | $phpmailer->Username = elgg_get_plugin_setting('phpmailer_username', 'phpmailer'); |
||
127 | $phpmailer->Password = elgg_get_plugin_setting('phpmailer_password', 'phpmailer'); |
||
128 | $phpmailer->SetFrom( elgg_get_plugin_setting('phpmailer_from_email', 'phpmailer'), elgg_get_plugin_setting('phpmailer_from_name', 'phpmailer') ); |
||
129 | |||
130 | /*if (!$from) { |
||
131 | throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'from')); |
||
132 | }*/ |
||
133 | |||
134 | if (!$to && !$bcc) { |
||
135 | throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'to')); |
||
136 | } |
||
137 | |||
138 | if (!$subject) { |
||
139 | throw new NotificationException(sprintf(elgg_echo('NotificationException:MissingParameter'), 'subject')); |
||
140 | } |
||
141 | |||
142 | // set line ending if admin selected \n (if admin did not change setting, null is returned) |
||
143 | if (elgg_get_plugin_setting('nonstd_mta', 'phpmailer')) |
||
144 | $phpmailer->LE = "\n"; |
||
145 | else |
||
146 | $phpmailer->LE = "\r\n"; |
||
147 | |||
148 | //////////////////////////////////// |
||
149 | // Format message |
||
150 | $phpmailer->SMTPDebug = 0; // Set to 1 for debugging information |
||
151 | |||
152 | $phpmailer->ClearAllRecipients(); |
||
153 | $phpmailer->ClearAttachments(); |
||
154 | |||
155 | // Set the from name and email |
||
156 | //$phpmailer->From = $from; |
||
157 | //$phpmailer->FromName = $from_name; |
||
158 | |||
159 | if (!filter_var($to, FILTER_VALIDATE_EMAIL)) { |
||
160 | return; |
||
161 | } |
||
162 | |||
163 | // Set destination address |
||
164 | if (isset($to)) { |
||
165 | $phpmailer->AddAddress($to, $to_name); |
||
166 | } |
||
167 | |||
168 | // set bccs if exists |
||
169 | if ($bcc && is_array($bcc)) { |
||
170 | foreach ($bcc as $address) |
||
171 | $phpmailer->AddBCC($address); |
||
172 | } |
||
173 | |||
174 | $phpmailer->Subject = $subject; |
||
175 | |||
176 | if ($image) |
||
177 | $phpmailer->AddEmbeddedImage($image, 'wire_image'); |
||
178 | |||
179 | if (!$html) { |
||
180 | $phpmailer->CharSet = 'utf-8'; |
||
181 | $phpmailer->IsHTML(false); |
||
182 | if ($params && array_key_exists('altbody', $params)) { |
||
183 | $phpmailer->AltBody = $params['altbody']; |
||
184 | } |
||
185 | |||
186 | $trans_tbl = get_html_translation_table(HTML_ENTITIES); |
||
187 | $trans_tbl[chr(146)] = '’'; |
||
188 | foreach ($trans_tbl as $k => $v) { |
||
189 | $ttr[$v] = utf8_encode($k); |
||
190 | } |
||
191 | $source = strtr($body, $ttr); |
||
192 | $body = strip_tags($source); |
||
193 | |||
194 | $phpmailer->Body = $body; |
||
195 | |||
196 | } else { |
||
197 | |||
198 | //$phpmailer->IsHTML(true); |
||
199 | $phpmailer->CharSet = 'utf-8'; |
||
200 | $phpmailer->MsgHTML($body); |
||
201 | } |
||
202 | |||
203 | |||
204 | if ($files && is_array($files)) { |
||
205 | foreach ($files as $file) { |
||
206 | if (isset($file['path'])) |
||
207 | $phpmailer->AddAttachment($file['path'], $file['name']); |
||
208 | } |
||
209 | } |
||
210 | |||
211 | $is_smtp = elgg_get_plugin_setting('phpmailer_smtp', 'phpmailer'); |
||
212 | $smtp_host = elgg_get_plugin_setting('phpmailer_host', 'phpmailer'); |
||
213 | $smtp_auth = elgg_get_plugin_setting('phpmailer_smtp_auth', 'phpmailer'); |
||
214 | |||
215 | $is_ssl = elgg_get_plugin_setting('ep_phpmailer_ssl', 'phpmailer'); |
||
216 | $smtp_port = elgg_get_plugin_setting('ep_phpmailer_port', 'phpmailer'); |
||
217 | |||
218 | try { |
||
219 | |||
220 | if ($is_smtp && isset($smtp_host)) { |
||
221 | |||
222 | if ($smtp_auth && $is_ssl) { |
||
223 | |||
224 | $phpmailer->SMTPSecure = "tls"; |
||
225 | |||
226 | } |
||
227 | } else { |
||
228 | // use php's mail |
||
229 | $phpmailer->IsMail(); |
||
230 | } |
||
231 | |||
232 | $phpmailer->Port = $smtp_port; |
||
233 | |||
234 | $return = $phpmailer->Send(); |
||
235 | |||
236 | } catch (phpmailerException $e) { |
||
237 | |||
238 | $errMess = $e->getMessage(); |
||
239 | $errStack = $e->getTraceAsString(); |
||
240 | $errType = $e->getCode(); |
||
241 | phpmailer_logging($errMess, $errStack, 'PHPMailer', $errType); |
||
242 | } |
||
243 | |||
244 | |||
245 | |||
246 | if (!$return) { |
||
247 | |||
248 | $errMess = $phpmailer->ErrorInfo; |
||
249 | $errStack = ""; |
||
250 | $errType = "custom"; |
||
251 | phpmailer_logging($errMess, $errStack, 'PHPMailer', $errType); |
||
252 | } |
||
253 | |||
254 | return $return; |
||
255 | } |
||
256 | |||
265 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: