Conditions | 36 |
Paths | > 20000 |
Total Lines | 223 |
Code Lines | 165 |
Lines | 0 |
Ratio | 0 % |
Changes | 2 | ||
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 declare(strict_types=1); |
||
97 | public function execute() |
||
98 | { |
||
99 | $helper = Helper::getInstance(); |
||
100 | $accountHandler = $helper->getHandler('Account'); |
||
101 | $useLogs = $helper->getConfig('use_logs') > Constants::LOG_NONE; |
||
102 | if ($useLogs) { |
||
103 | $logHandler = $helper->getHandler('Log'); |
||
104 | } |
||
105 | $logInfo = '<br>Task ID: ' . $this->mailParams['taskId']; |
||
106 | |||
107 | $errorCode = 0; |
||
108 | |||
109 | $eventLink = ''; |
||
110 | $eventUrl = \WGEVENTS_URL . '/event.php?op=show&id=' . $this->mailParams['evId']; |
||
111 | $eventName = $this->getCleanParam('evName'); |
||
112 | $eventDate = \formatTimestamp($this->mailParams['evDatefrom'], 'm'); |
||
113 | $eventLocation = $this->getCleanParam('evLocation'); |
||
114 | $senderMail = $this->getCleanParam('evRegister_sendermail'); |
||
115 | $senderName = $this->getCleanParam('evRegister_sendername'); |
||
116 | $senderSignatur = $this->getCleanParam('evRegister_signature'); |
||
117 | $firstname = $this->getCleanParam('regFirstname'); |
||
118 | $lastname = $this->getCleanParam('regLastname'); |
||
119 | $infotext = $this->getCleanParam('infotext'); |
||
120 | $mailBody = $this->getCleanParam('mailBody'); |
||
121 | $recipients = $this->mailParams['recipients']; |
||
122 | $userName = $GLOBALS['xoopsConfig']['anonymous']; |
||
123 | if (\is_object($GLOBALS['xoopsUser'])) { |
||
124 | $userName = ('' !== (string)$GLOBALS['xoopsUser']->name()) ? $GLOBALS['xoopsUser']->name() : $GLOBALS['xoopsUser']->uname(); |
||
125 | } |
||
126 | |||
127 | switch ($this->type) { |
||
128 | case 0: |
||
129 | default: |
||
130 | return 0; |
||
131 | case Constants::MAIL_REG_CONFIRM_OUT: |
||
132 | $template = 'mail_reg_confirm_out.tpl'; |
||
133 | $subject = \_MA_WGEVENTS_MAIL_REG_OUT_SUBJECT; |
||
134 | break; |
||
135 | case Constants::MAIL_REG_NOTIFY_OUT: |
||
136 | $template = 'mail_reg_notify_out.tpl'; |
||
137 | $subject = \_MA_WGEVENTS_MAIL_REG_OUT_SUBJECT; |
||
138 | break; |
||
139 | case Constants::MAIL_REG_CONFIRM_IN: |
||
140 | $template = 'mail_reg_confirm_in.tpl'; |
||
141 | $subject = \_MA_WGEVENTS_MAIL_REG_IN_SUBJECT; |
||
142 | break; |
||
143 | case Constants::MAIL_REG_CONFIRM_MODIFY: |
||
144 | $template = 'mail_reg_confirm_modify.tpl'; |
||
145 | $subject = \_MA_WGEVENTS_MAIL_REG_MODIFY_SUBJECT; |
||
146 | break; |
||
147 | case Constants::MAIL_REG_NOTIFY_IN: |
||
148 | $template = 'mail_reg_notify_in.tpl'; |
||
149 | $subject = \_MA_WGEVENTS_MAIL_REG_IN_SUBJECT; |
||
150 | break; |
||
151 | case Constants::MAIL_REG_NOTIFY_MODIFY: |
||
152 | $template = 'mail_reg_notify_modify.tpl'; |
||
153 | $subject = \_MA_WGEVENTS_MAIL_REG_MODIFY_SUBJECT; |
||
154 | break; |
||
155 | case Constants::MAIL_EVENT_NOTIFY_MODIFY: |
||
156 | $template = 'mail_event_notify_modify.tpl'; |
||
157 | $subject = \_MA_WGEVENTS_MAIL_EVENT_NOTIFY_MODIFY_SUBJECT; |
||
158 | break; |
||
159 | case Constants::MAIL_EVENT_NOTIFY_ALL: |
||
160 | $template = 'mail_event_notify_all.tpl'; |
||
161 | $subject = $this->mailParams['mailSubject']; |
||
162 | break; |
||
163 | case Constants::MAIL_REG_NOTIFY_CANCEL: |
||
164 | $template = 'mail_reg_notify_cancel.tpl'; |
||
165 | $subject = \_MA_WGEVENTS_MAIL_EVENT_CANCEL_SUBJECT; |
||
166 | break; |
||
167 | case Constants::MAIL_REG_CONFIRM_CANCEL: |
||
168 | $template = 'mail_reg_confirm_cancel.tpl'; |
||
169 | $subject = \_MA_WGEVENTS_MAIL_EVENT_CANCEL_SUBJECT; |
||
170 | break; |
||
171 | } |
||
172 | |||
173 | // get settings of primary account |
||
174 | $primaryAcc = $accountHandler->getPrimary(); |
||
175 | $account_type = (int)$primaryAcc['type']; |
||
176 | //$account_yourname = (string)$primaryAcc['yourname']; |
||
177 | $account_username = (string)$primaryAcc['yourmail']; |
||
178 | $account_password = (string)$primaryAcc['password']; |
||
179 | $account_server_out = (string)$primaryAcc['server_out']; |
||
180 | $account_port_out = (int)$primaryAcc['port_out']; |
||
181 | $account_securetype_out = (string)$primaryAcc['securetype_out']; |
||
182 | |||
183 | // create info for log |
||
184 | if ($useLogs) { |
||
185 | switch ($account_type) { |
||
186 | case '': |
||
187 | default: |
||
188 | $accountTypeDesc = 'XOOPS system'; |
||
189 | break; |
||
190 | case Constants::ACCOUNT_TYPE_VAL_GMAIL: |
||
191 | $accountTypeDesc = 'gmail'; |
||
192 | break; |
||
193 | case Constants::ACCOUNT_TYPE_VAL_PHP_MAIL: |
||
194 | $accountTypeDesc = 'php mail'; |
||
195 | break; |
||
196 | case Constants::ACCOUNT_TYPE_VAL_PHP_SENDMAIL: |
||
197 | $accountTypeDesc = 'sendmail'; |
||
198 | break; |
||
199 | case Constants::ACCOUNT_TYPE_VAL_POP3: |
||
200 | $accountTypeDesc = 'pop3'; |
||
201 | break; |
||
202 | case Constants::ACCOUNT_TYPE_VAL_SMTP: |
||
203 | $accountTypeDesc = 'smtp'; |
||
204 | break; |
||
205 | } |
||
206 | $logInfo .= '<br>Mailer: ' . $accountTypeDesc; |
||
207 | $logInfo .= '<br>Template: ' . $template; |
||
208 | } |
||
209 | |||
210 | try { |
||
211 | /* |
||
212 | if ($account_type == Constants::ACCOUNT_TYPE_VAL_PHP_SENDMAIL) { |
||
213 | $pop = new POP3(); |
||
214 | $pop->authorise($account_server_out, $account_port_out, 30, $account_username, $account_password, 1); |
||
215 | } |
||
216 | */ |
||
217 | $xoopsMailer = xoops_getMailer(); |
||
218 | $xoopsMailer->useMail(); |
||
219 | // check whether mail body contains any html tags |
||
220 | if (\preg_match('/<\s?[^\>]*\/?\s?>/i', $mailBody)) { |
||
221 | $this->isHtml = true; |
||
222 | $logInfo .= '<br>Check isHtml: true'; |
||
223 | $eventLink = "<a href='" . $eventUrl . "' title='" . $eventUrl . "'>" . $eventName . '</a>'; |
||
224 | } |
||
225 | $xoopsMailer->setHTML($this->isHtml); |
||
226 | //set template path |
||
227 | if (\file_exists(\WGEVENTS_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/')) { |
||
228 | $xoopsMailer->setTemplateDir(\WGEVENTS_PATH . '/language/' . $GLOBALS['xoopsConfig']['language'] . '/mail_template/'); |
||
229 | } else { |
||
230 | $xoopsMailer->setTemplateDir(\WGEVENTS_PATH . '/language/english/mail_template/'); |
||
231 | } |
||
232 | //set template name |
||
233 | $xoopsMailer->setTemplate($template); |
||
234 | $xoopsMailer->CharSet = _CHARSET; //use xoops default character set |
||
235 | //set account settings |
||
236 | if (Constants::ACCOUNT_TYPE_VAL_SMTP == $account_type |
||
237 | || Constants::ACCOUNT_TYPE_VAL_GMAIL == $account_type) { |
||
238 | |||
239 | $xoopsMailer->multimailer->isSMTP(); |
||
240 | $xoopsMailer->multimailer->Port = $account_port_out; // set the SMTP port |
||
241 | $xoopsMailer->multimailer->Host = $account_server_out; //sometimes necessary to repeat |
||
242 | $xoopsMailer->multimailer->SMTPAuth = true; |
||
243 | $xoopsMailer->multimailer->SMTPSecure = $account_securetype_out; |
||
244 | $xoopsMailer->multimailer->Username = $account_username; // SMTP account username |
||
245 | $xoopsMailer->multimailer->Password = $account_password; // SMTP account password |
||
246 | $xoopsMailer->multimailer->SMTPDebug = 0; |
||
247 | } |
||
248 | /* old version: |
||
249 | if ('' != $account_securetype_out) { |
||
250 | $xoopsMailer->SMTPAuth = true; |
||
251 | $xoopsMailer->SMTPSecure = $account_securetype_out; // sets the prefix to the server |
||
252 | } |
||
253 | */ |
||
254 | |||
255 | if (Constants::ACCOUNT_TYPE_VAL_SMTP == $account_type |
||
256 | || Constants::ACCOUNT_TYPE_VAL_GMAIL == $account_type) { |
||
257 | $xoopsMailer->Port = $account_port_out; // set the SMTP port |
||
258 | $xoopsMailer->Host = $account_server_out; //sometimes necessary to repeat |
||
259 | } |
||
260 | |||
261 | if ('' !== $account_securetype_out) { |
||
262 | $xoopsMailer->SMTPAuth = true; |
||
263 | $xoopsMailer->SMTPSecure = $account_securetype_out; // sets the prefix to the server |
||
264 | } |
||
265 | |||
266 | //set sender |
||
267 | $xoopsMailer->setFromEmail($senderMail); |
||
268 | //set sender name |
||
269 | $xoopsMailer->setFromName($senderName); |
||
270 | //set subject |
||
271 | $xoopsMailer->setSubject($subject); |
||
272 | //assign vars |
||
273 | $xoopsMailer->assign('UNAME', $userName); |
||
274 | $xoopsMailer->assign('NAME', \trim($firstname . ' ' . $lastname)); |
||
275 | $xoopsMailer->assign('EVENTNAME', $eventName); |
||
276 | $xoopsMailer->assign('EVENTDATEFROM', $eventDate); |
||
277 | $xoopsMailer->assign('EVENTLOCATION', $eventLocation); |
||
278 | $xoopsMailer->assign('INFOTEXT', $infotext); |
||
279 | if ('' !== $eventLink) { |
||
280 | $xoopsMailer->assign('EVENTURL', $eventLink); |
||
281 | } else { |
||
282 | $xoopsMailer->assign('EVENTURL', $eventUrl); |
||
283 | } |
||
284 | $xoopsMailer->assign('BODY', $mailBody); |
||
285 | $xoopsMailer->assign('SIGNATURE', $senderSignatur); |
||
286 | //set recipient |
||
287 | $xoopsMailer->setToEmails($recipients); |
||
288 | //execute sending |
||
289 | if ($xoopsMailer->send()) { |
||
290 | if ($useLogs) { |
||
291 | $logHandler->createLog('Result MailHandler/executeReg: success' . $logInfo); |
||
292 | } |
||
293 | } else { |
||
294 | if ($useLogs) { |
||
295 | $logHandler->createLog('Result MailHandler/executeReg: failed' .$xoopsMailer->getErrors() . $logInfo); |
||
296 | } |
||
297 | $errMsg = $xoopsMailer->getErrors(); |
||
298 | // check for SMTP error 554 (maximum number of mails exceeded) |
||
299 | $errIds = ['SMTP','554', 'error']; |
||
300 | $arrMsg = \explode(' ', \preg_replace('/[^a-z0-9]/i',' ',$errMsg)); |
||
301 | $countMatches = \count(\array_intersect($arrMsg, $errIds)); |
||
302 | if ($countMatches > 0) { |
||
303 | $errorCode = 554; |
||
304 | } else { |
||
305 | |||
306 | $errorCode = 900; // wgevents internal code for misc error |
||
307 | } |
||
308 | } |
||
309 | $xoopsMailer->reset(); |
||
310 | unset($xoopsMailer); |
||
311 | } |
||
312 | catch (\Exception $e) { |
||
313 | $errorCode = 999; // wgevents internal code for misc error |
||
314 | if ($useLogs) { |
||
315 | $logHandler->createLog('MailHandler/executeReg failed: Exception - ' . $e->getMessage()); |
||
316 | } |
||
317 | } |
||
318 | |||
319 | return $errorCode; |
||
320 | } |
||
399 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: