| Conditions | 8 |
| Paths | 14 |
| Total Lines | 155 |
| Lines | 4 |
| Ratio | 2.58 % |
| 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 |
||
| 52 | public function sendMails($fromUserId, $toUserId, $event, $link, array $data) |
||
| 53 | { |
||
| 54 | $date = date('m-d-Y H:i:s', time()); |
||
| 55 | $mail = new \XoopsMultiMailer(); |
||
| 56 | $wall = new WallUpdates(); |
||
|
|
|||
| 57 | $tpl = new \XoopsTpl(); |
||
| 58 | $message = ''; |
||
| 59 | /** |
||
| 60 | * \XoopsModules\Smallworld\Helper $helper |
||
| 61 | * \XoopsModules\Smallworld\SwUserHandler $swUserHandler |
||
| 62 | */ |
||
| 63 | $helper = Helper::getInstance(); |
||
| 64 | $swUserHandler = $helper->getHandler('SwUser'); |
||
| 65 | |||
| 66 | // From and To user ids |
||
| 67 | $fromXuser = new \XoopsUser($fromUserId); |
||
| 68 | $fromAvatar = $swUserHandler->gravatar($fromUserId); |
||
| 69 | $fromAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($fromUserId, $fromAvatar) . "' height='90px' width='90px'>"; |
||
| 70 | $toXuser = new \XoopsUser($toUserId); |
||
| 71 | $toAvatar = $swUserHandler->gravatar($toUserId); |
||
| 72 | $toAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($toUserId, $toAvatar) . "' height='90px' width='90px'>"; |
||
| 73 | // Sender's XOOPS username |
||
| 74 | $sendName = $fromXuser->getVar('uname'); |
||
| 75 | $sendNameUrl = "<a href='" . $helper->url("userprofile.php?username={$sendName}") . "'>{$sendName}</a>"; |
||
| 76 | // Receiver's XOOPS username and email |
||
| 77 | $receiveName = $toXuser->getVar('uname'); |
||
| 78 | $receiveNameUrl = "<a href='" . $helper->url("userprofile.php?username={$receiveName}") . "'>{$receiveName}</a>"; |
||
| 79 | |||
| 80 | // Checking content of 'event' to send right message |
||
| 81 | switch ($event) { |
||
| 82 | case ('register'): |
||
| 83 | $subject = _SMALLWORLD_MAIL_REGISTERSUBJECT . $GLOBALS['xoopsConfig']['sitename']; |
||
| 84 | |||
| 85 | $registername = $sendName; |
||
| 86 | $toAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($fromUserId, $toAvatar) . "' height='90px' width='90px'>"; |
||
| 87 | |||
| 88 | $tpl = new \XoopsTpl(); |
||
| 89 | $tpl->assign([ |
||
| 90 | 'registername' => $registername, |
||
| 91 | 'sitename' => $GLOBALS['xoopsConfig']['sitename'], |
||
| 92 | 'registerurl' => $sendNameUrl, |
||
| 93 | 'registerlink' => $toAvatarlink |
||
| 94 | ]); |
||
| 95 | |||
| 96 | $lnk = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_register.tpl'); |
||
| 97 | $message = $tpl->fetch($lnk); |
||
| 98 | $mail->Body = $message; |
||
| 99 | $toMail = $GLOBALS['xoopsConfig']['adminmail']; |
||
| 100 | break; |
||
| 101 | // Send email to admin if red/yellow card has been pressed indicating a "bad" thread has been found. |
||
| 102 | case ('complaint'): |
||
| 103 | $subject = _SMALLWORLD_MAIL_COMPLAINT . $GLOBALS['xoopsConfig']['sitename']; |
||
| 104 | |||
| 105 | $senders_id = $fromUserId; |
||
| 106 | $sendersName = stripslashes($data['byuser']); |
||
| 107 | $againstUser = stripslashes($data['a_user']); |
||
| 108 | $time = date('d-m-Y H:i:s', $data['time']); |
||
| 109 | $link = stripslashes($data['link']); |
||
| 110 | |||
| 111 | $tpl = new \XoopsTpl(); |
||
| 112 | $tpl->assign('sendername', $sendersName); |
||
| 113 | $tpl->assign('against', $againstUser); |
||
| 114 | $tpl->assign('time', $time); |
||
| 115 | $tpl->assign('link', $link); |
||
| 116 | $tpl->assign('sitename', $GLOBALS['xoopsConfig']['sitename']); |
||
| 117 | |||
| 118 | $lnk = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_complaint.tpl'); |
||
| 119 | $message = $tpl->fetch($lnk); |
||
| 120 | $mail->Body = $message; |
||
| 121 | $toMail = $GLOBALS['xoopsConfig']['adminmail']; |
||
| 122 | break; |
||
| 123 | case ('commentToWM'): |
||
| 124 | $subject = _SMALLWORLD_MAIL_NEWCOMMENT . $GLOBALS['xoopsConfig']['sitename']; |
||
| 125 | |||
| 126 | $ownermessage = stripslashes($this->getOwnerUpdateFromMsgID($data['msg_id_fk'])); |
||
| 127 | View Code Duplication | if (preg_match('/UPLIMAGE/', $ownermessage)) { |
|
| 128 | $ownmsg = str_replace('UPLIMAGE ', '', $ownermessage); |
||
| 129 | $ownermessage = "<img width='300px' src='" . $ownmsg . "' style='margin: 5px 0px;' >"; |
||
| 130 | } |
||
| 131 | |||
| 132 | $owner = smallworld_getOwnerFromComment($data['msg_id_fk']); |
||
| 133 | $ownerXuser = new \XoopsUser($owner); |
||
| 134 | $ownerAvatar = $swUserHandler->gravatar($owner); |
||
| 135 | $ownerAvatarlink = "<img class='left' src='" . $swUserHandler->getAvatarLink($owner, $ownerAvatar) . "' height='90px' width='90px'>"; |
||
| 136 | $ownerXname = $ownerXuser->uname(); |
||
| 137 | $ownerXnameUrl = "<a href='" . $helper->url("userprofile.php?username='{$ownerXname}") . "'>{$ownerXname}</a>"; |
||
| 138 | $replylink = "<a href='" . $helper->url("permalink.php?ownerid={$owner}&updid={$data['msg_id_fk']}") . "'>" . _SMALLWORLD_SEEANDREPLYHERE . '</a>'; |
||
| 139 | |||
| 140 | $tpl = new \XoopsTpl(); |
||
| 141 | $tpl->assign([ |
||
| 142 | 'receivename' => $receiveName, |
||
| 143 | 'ownername' => $ownerXname, |
||
| 144 | 'ownernameurl' => $ownerXnameUrl, |
||
| 145 | 'sendname' => $sendName, |
||
| 146 | 'sendnameurl' => $sendNameUrl, |
||
| 147 | 'sitename' => $GLOBALS['xoopsConfig']['sitename'], |
||
| 148 | 'ownermessage' => $ownermessage, |
||
| 149 | 'from_avatarlink' => $fromAvatarlink, |
||
| 150 | 'to_avatarlink' => $ownerAvatarlink, |
||
| 151 | 'itemtext' => stripslashes($data['comment']), |
||
| 152 | 'itemtextdate' => $date, |
||
| 153 | 'replylink' => $replylink |
||
| 154 | ]); |
||
| 155 | $lnk = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_newcomment.tpl'); |
||
| 156 | $message = $tpl->fetch($lnk); |
||
| 157 | $mail->Body = $message; |
||
| 158 | $toMail = $toXuser->getVar('email'); |
||
| 159 | break; |
||
| 160 | case ('friendshipfollow'): |
||
| 161 | $subject = _SMALLWORLD_MAIL_NEWFRIENDFOLLOWER . $GLOBALS['xoopsConfig']['sitename']; |
||
| 162 | $link = "<a href='" . $helper->url('index.php') . "'>" . _SMALLWORLD_GOTOSMALLWORLDHERE . "</a>"; |
||
| 163 | |||
| 164 | $tpl = new \XoopsTpl(); |
||
| 165 | $tpl->assign([ |
||
| 166 | 'toUser' => $receiveName, |
||
| 167 | 'date' => $date, |
||
| 168 | 'link' => $link, |
||
| 169 | 'sitename' => $GLOBALS['xoopsConfig']['sitename'] |
||
| 170 | ]); |
||
| 171 | |||
| 172 | $lnk = $helper->url('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_attencionneeded.tpl'); |
||
| 173 | $message = $tpl->fetch($lnk); |
||
| 174 | $mail->Body = $message; |
||
| 175 | $toMail = $toXuser->getVar('email'); |
||
| 176 | break; |
||
| 177 | case ('tag'): |
||
| 178 | $subject = _SMALLWORLD_MAIL_FRIENDTAGGEDYOU . $GLOBALS['xoopsConfig']['sitename']; |
||
| 179 | $tpl = new \XoopsTpl(); |
||
| 180 | $tpl->assign([ |
||
| 181 | 'toUser' => $receiveName, |
||
| 182 | 'fromUser' => $sendName, |
||
| 183 | 'date' => $date, |
||
| 184 | 'link' => $link, |
||
| 185 | 'sitename' => $GLOBALS['xoopsConfig']['sitename'] |
||
| 186 | ]); |
||
| 187 | |||
| 188 | $lnk = $helper->path('language/' . $GLOBALS['xoopsConfig']['language'] . '/mailTpl/mail_tag.tpl'); |
||
| 189 | $message = $tpl->fetch($lnk); |
||
| 190 | $mail->Body = $message; |
||
| 191 | $toMail = $toXuser->getVar('email'); |
||
| 192 | break; |
||
| 193 | } |
||
| 194 | |||
| 195 | $mail->isMail(); |
||
| 196 | $mail->isHTML(true); |
||
| 197 | $mail->addAddress($toMail); |
||
| 198 | $mail->Subject = $subject; |
||
| 199 | |||
| 200 | $retVal = true; |
||
| 201 | if (!$mail->send()) { |
||
| 202 | //@todo figure out what to do if failure, if anything |
||
| 203 | $retVal = false; |
||
| 204 | } |
||
| 205 | return $retVal; |
||
| 206 | } |
||
| 207 | |||
| 243 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.