Conditions | 18 |
Paths | 876 |
Total Lines | 163 |
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 |
||
41 | function subscribingMLHandler($type, $subscr_id, $mailinglist_id) |
||
42 | { |
||
43 | global $xoopsUser, $xoopsConfig; |
||
44 | $helper = XoopsModules\Xnewsletter\Helper::getInstance(); |
||
45 | |||
46 | // get subscriber |
||
47 | $subscrObj = $helper->getHandler('Subscr')->get($subscr_id); |
||
48 | $subscr_email = $subscrObj->getVar('subscr_email'); |
||
49 | |||
50 | // get mailinglist |
||
51 | $mailinglistObj = $helper->getHandler('Mailinglist')->get($mailinglist_id); |
||
52 | $mailinglist_listname = $mailinglistObj->getVar('mailinglist_listname'); |
||
53 | $mailinglist_email = $mailinglistObj->getVar('mailinglist_email'); |
||
54 | $mailinglist_subscribe = $mailinglistObj->getVar('mailinglist_subscribe'); |
||
55 | $mailinglist_unsubscribe = $mailinglistObj->getVar('mailinglist_unsubscribe'); |
||
56 | $mailinglist_system = (int)$mailinglistObj->getVar('mailinglist_system'); |
||
57 | $mailinglist_target = $mailinglistObj->getVar('mailinglist_target'); |
||
58 | $mailinglist_pwd = $mailinglistObj->getVar('mailinglist_pwd'); |
||
59 | $mailinglist_notifyowner = $mailinglistObj->getVar('mailinglist_notifyowner'); |
||
60 | |||
61 | $senderUid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0; |
||
62 | |||
63 | if ($mailinglist_system === _XNEWSLETTER_MAILINGLIST_TYPE_DEFAULT_VAL) { |
||
64 | // this should not happen |
||
65 | $protocolObj = $helper->getHandler('Protocol')->create(); |
||
66 | $protocolObj->setVar('protocol_letter_id', 0); |
||
67 | $protocolObj->setVar('protocol_subscriber_id', $subscr_id); |
||
68 | $protocolObj->setVar('protocol_status', 'Error adding mailing list: invalid mailing list type'); |
||
69 | $protocolObj->setVar('protocol_success', false); |
||
70 | $protocolObj->setVar('protocol_submitter', $senderUid); |
||
71 | $protocolObj->setVar('protocol_created', time()); |
||
72 | |||
73 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
||
74 | return true; |
||
75 | } |
||
76 | return $protocolObj->getHtmlErrors(); |
||
77 | } |
||
78 | if ($mailinglist_system === _XNEWSLETTER_MAILINGLIST_TYPE_MAJORDOMO_VAL) { |
||
79 | |||
80 | if (_XNEWSLETTER_MAILINGLIST_SUBSCRIBE == $type) { |
||
81 | $action_code = $mailinglist_subscribe; |
||
82 | } else { |
||
83 | $action_code = $mailinglist_unsubscribe; |
||
84 | } |
||
85 | $action_code = str_replace('{email}', $subscr_email, $action_code); |
||
86 | $action_code = str_replace('{nameofmylist}', $mailinglist_listname, $action_code); |
||
87 | |||
88 | require_once XOOPS_ROOT_PATH . '/class/mail/phpmailer/class.phpmailer.php'; |
||
89 | require_once XOOPS_ROOT_PATH . '/class/mail/phpmailer/class.pop3.php'; |
||
90 | require_once XOOPS_ROOT_PATH . '/class/mail/phpmailer/class.smtp.php'; |
||
91 | |||
92 | try { |
||
93 | $xoopsMailer = xoops_getMailer(); |
||
94 | $xoopsMailer->reset(); |
||
95 | //$xoopsMailer->setTemplateDir(); |
||
96 | $xoopsMailer->useMail(); |
||
97 | $xoopsMailer->setHTML(false); |
||
98 | //$xoopsMailer->setTemplate('activate.tpl'); |
||
99 | $xoopsMailer->setToEmails($mailinglist_email); |
||
100 | if (isset($xoopsConfig['adminmail'])) { |
||
101 | $xoopsMailer->setFromEmail($xoopsConfig['adminmail']); |
||
102 | } |
||
103 | if (isset($xoopsConfig['sitename'])) { |
||
104 | $xoopsMailer->setFromName($xoopsConfig['sitename']); |
||
105 | } |
||
106 | //$xoopsMailer->setSubject($subject); |
||
107 | $xoopsMailer->setBody($action_code); |
||
108 | $xoopsMailer->send(); |
||
109 | |||
110 | $protocol_status = str_replace('%a', $action_code, _AM_XNEWSLETTER_SEND_SUCCESS_ML_DETAIL); |
||
111 | $xoopsMailer->reset(); |
||
112 | $protocol_success = true; |
||
113 | } catch (\Exception $e) { |
||
114 | $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER . $xoopsMailer->getErrors(); //error messages |
||
115 | $protocol_success = false; |
||
116 | $helper->addLog($e); |
||
117 | } |
||
118 | |||
119 | //create item in protocol for this email |
||
120 | $text_clean = ['<strong>', '</strong>', '<br/>', '<br>']; |
||
121 | $protocol_status = str_replace($text_clean, '', $protocol_status); |
||
122 | |||
123 | $protocolObj = $helper->getHandler('Protocol')->create(); |
||
124 | $protocolObj->setVar('protocol_letter_id', 0); |
||
125 | $protocolObj->setVar('protocol_subscriber_id', $subscr_id); |
||
126 | $protocolObj->setVar('protocol_status', $protocol_status); |
||
127 | $protocolObj->setVar('protocol_success', $protocol_success); |
||
128 | $protocolObj->setVar('protocol_submitter', $senderUid); |
||
129 | $protocolObj->setVar('protocol_created', time()); |
||
130 | |||
131 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
||
132 | return true; |
||
133 | } |
||
134 | return $protocolObj->getHtmlErrors(); |
||
135 | } |
||
136 | |||
137 | if ($mailinglist_system === _XNEWSLETTER_MAILINGLIST_TYPE_MAILMAN_VAL) { |
||
138 | |||
139 | $action_code = $mailinglist_target; |
||
140 | if (substr($action_code, -1) !== '/') { |
||
141 | $action_code .= '/'; |
||
142 | } |
||
143 | $action_code .= 'mailman/admin/'; |
||
144 | $action_code .= $mailinglist_listname; |
||
145 | $action_code .= '/members/'; |
||
146 | |||
147 | if (_XNEWSLETTER_MAILINGLIST_SUBSCRIBE == $type) { |
||
148 | $action_code .= 'add?subscribe_or_invite=0&send_welcome_msg_to_this_batch=0¬ification_to_list_owner=' . $mailinglist_notifyowner; |
||
149 | $action_code .= '&subscribees_upload='; |
||
150 | } else { |
||
151 | $action_code .= 'remove?send_unsub_ack_to_this_batch=0&send_unsub_notifications_to_list_owner=' . $mailinglist_notifyowner; |
||
152 | $action_code .= '&unsubscribees_upload='; |
||
153 | } |
||
154 | $action_code .= $subscr_email; |
||
155 | $action_code .= '&adminpw=' . $mailinglist_pwd; |
||
156 | //echo "action_code:".$action_code; |
||
157 | |||
158 | try { |
||
159 | $req = curl_init(); |
||
160 | curl_setopt($req, CURLOPT_URL, $action_code); |
||
161 | curl_setopt($req, CURLOPT_RETURNTRANSFER, true); |
||
162 | |||
163 | $res_curl = curl_exec($req); |
||
|
|||
164 | |||
165 | if(!curl_errno($req)){ |
||
166 | $status = (int)curl_getinfo($req, CURLINFO_HTTP_CODE); |
||
167 | if ($status === 200) { |
||
168 | $protocol_status = str_replace('%a', $subscr_email, _AM_XNEWSLETTER_SEND_SUCCESS_ML_DETAIL); |
||
169 | $protocol_status = str_replace('%m', $mailinglist_listname, $protocol_status); |
||
170 | $protocol_success = true; |
||
171 | } else { |
||
172 | $protocol_success = false; |
||
173 | $protocol_status = 'Curl error: ' . curl_error($req); |
||
174 | } |
||
175 | } else { |
||
176 | $protocol_success = false; |
||
177 | $protocol_status = 'Curl error: ' . curl_error($req); |
||
178 | } |
||
179 | } catch (\Exception $e) { |
||
180 | $protocol_status = _AM_XNEWSLETTER_SEND_ERROR_PHPMAILER; //error messages |
||
181 | $protocol_success = false; |
||
182 | $helper->addLog($e); |
||
183 | } |
||
184 | curl_close($req); |
||
185 | //create item in protocol for this email |
||
186 | $text_clean = ['<strong>', '</strong>', '<br/>', '<br>']; |
||
187 | $protocol_status = str_replace($text_clean, '', $protocol_status); |
||
188 | |||
189 | $protocolObj = $helper->getHandler('Protocol')->create(); |
||
190 | $protocolObj->setVar('protocol_letter_id', 0); |
||
191 | $protocolObj->setVar('protocol_subscriber_id', $subscr_id); |
||
192 | $protocolObj->setVar('protocol_status', $protocol_status); |
||
193 | $protocolObj->setVar('protocol_success', $protocol_success); |
||
194 | $protocolObj->setVar('protocol_submitter', $senderUid); |
||
195 | $protocolObj->setVar('protocol_created', time()); |
||
196 | |||
197 | if ($helper->getHandler('Protocol')->insert($protocolObj)) { |
||
198 | return true; |
||
199 | } |
||
200 | return $protocolObj->getHtmlErrors(); |
||
201 | } |
||
202 | return null; |
||
203 | } |
||
204 | |||
233 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.