| Conditions | 16 |
| Paths | 80 |
| Total Lines | 105 |
| Code Lines | 84 |
| 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 |
||
| 98 | function insert($obj, $force=true) { |
||
| 99 | if ($obj->isNew()) { |
||
| 100 | $obj->setVar('created', time()); |
||
| 101 | $new = true; |
||
| 102 | $sendmail = true; |
||
| 103 | } else { |
||
| 104 | $obj->setVar('updated', time()); |
||
| 105 | $new = false; |
||
| 106 | if ($obj->vars['songid']['changed']==true) { |
||
| 107 | $songs_handler = xoops_getmodulehandler('songs', 'songlist'); |
||
| 108 | $criteria = new Criteria('songid', $obj->getVar('songid')); |
||
| 109 | $songs = $songs_handler->getObjects($criteria, false); |
||
| 110 | if (is_object($songs[0])) { |
||
| 111 | foreach($songs[0]->getVar('aids') as $aid) |
||
| 112 | $ad[] = $aid; |
||
| 113 | $obj->setVar('sid', $songs[0]->getVar('sid')); |
||
| 114 | $obj->setVar('aid', $ad[0]); |
||
| 115 | $sendmail = true; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | } |
||
| 119 | if ($rid = parent::insert($obj, $force)) { |
||
| 120 | if ($sendmail==true) { |
||
| 121 | if ($new==true) { |
||
| 122 | xoops_loadLanguage('email', 'songlist'); |
||
| 123 | $xoopsMailer =& getMailer(); |
||
| 124 | $xoopsMailer->setHTML(true); |
||
| 125 | $xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('/modules/songlist/language/'.$GLOBALS['xoopsConfig']['language'].'/mail_templates/')); |
||
| 126 | $xoopsMailer->setTemplate('songlist_request_created.html'); |
||
| 127 | $xoopsMailer->setSubject(sprintf(_MN_SONGLIST_SUBJECT_REQUESTMADE, $rid)); |
||
| 128 | |||
| 129 | foreach(explode('|', $GLOBALS['songlistModuleConfig']['email']) as $email) |
||
| 130 | $xoopsMailer->setToEmails($email); |
||
| 131 | |||
| 132 | $xoopsMailer->setToEmails($obj->getVar('email')); |
||
| 133 | |||
| 134 | $xoopsMailer->assign("SITEURL", XOOPS_URL); |
||
| 135 | $xoopsMailer->assign("SITENAME", $GLOBALS['xoopsConfig']['sitename']); |
||
| 136 | $xoopsMailer->assign("RID", $rid); |
||
| 137 | $xoopsMailer->assign("TITLE", $obj->getVar('title')); |
||
| 138 | $xoopsMailer->assign("ALBUM", $obj->getVar('album')); |
||
| 139 | $xoopsMailer->assign("ARTIST", $obj->getVar('artist')); |
||
| 140 | $xoopsMailer->assign("EMAIL", $obj->getVar('email')); |
||
| 141 | $xoopsMailer->assign("NAME", $obj->getVar('name')); |
||
| 142 | |||
| 143 | if(!$xoopsMailer->send() ){ |
||
| 144 | xoops_error($xoopsMailer->getErrors(true), 'Email Send Error'); |
||
| 145 | } |
||
| 146 | } else { |
||
| 147 | xoops_loadLanguage('email', 'songlist'); |
||
| 148 | $songs_handler = xoops_getmodulehandler('songs', 'songlist'); |
||
| 149 | $artists_handler = xoops_getmodulehandler('artists', 'songlist'); |
||
| 150 | $albums_handler = xoops_getmodulehandler('albums', 'songlist'); |
||
| 151 | $genre_handler = xoops_getmodulehandler('genre', 'songlist'); |
||
| 152 | |||
| 153 | $song = $songs_handler->get($obj->getVar('sid')); |
||
| 154 | if (is_object($song)) { |
||
| 155 | $sng = $genre->getVar('title'); |
||
| 156 | } |
||
| 157 | $album = $album_handler->get($song->getVar('abid')); |
||
| 158 | if (is_object($album)) { |
||
| 159 | $alb = $genre->getVar('title'); |
||
| 160 | $alb_img = $genre->getImage(); |
||
| 161 | } |
||
| 162 | $genre = $genre_handler->get($song->getVar('abid')); |
||
| 163 | if (is_object($genre)) { |
||
| 164 | $gen = $genre->getVar('name'); |
||
| 165 | } |
||
| 166 | $artists = $artists_handler->getObjects(new Criteria('aid', '('.implode(',', $song->getVar('aid')).')', 'IN'), false); |
||
| 167 | $art = ''; |
||
| 168 | foreach($artists as $id => $artist) { |
||
| 169 | $art .= $artist->getVar('name') . ($id<sizeof($artists)-1?', ':''); |
||
| 170 | } |
||
| 171 | $xoopsMailer =& getMailer(); |
||
| 172 | $xoopsMailer->setHTML(true); |
||
| 173 | $xoopsMailer->setTemplateDir($GLOBALS['xoops']->path('/modules/songlist/language/'.$GLOBALS['xoopsConfig']['language'].'/mail_templates/')); |
||
| 174 | $xoopsMailer->setTemplate('songlist_request_updated.html'); |
||
| 175 | $xoopsMailer->setSubject(sprintf(_MN_SONGLIST_SUBJECT_REQUESTFOUND, $rid)); |
||
| 176 | |||
| 177 | $xoopsMailer->setToEmails($obj->getVar('email')); |
||
| 178 | |||
| 179 | $xoopsMailer->assign("SITEURL", XOOPS_URL); |
||
| 180 | $xoopsMailer->assign("SITENAME", $GLOBALS['xoopsConfig']['sitename']); |
||
| 181 | $xoopsMailer->assign("RID", $rid); |
||
| 182 | $xoopsMailer->assign("TITLE", $obj->getVar('title')); |
||
| 183 | $xoopsMailer->assign("ALBUM", $obj->getVar('album')); |
||
| 184 | $xoopsMailer->assign("ARTIST", $obj->getVar('artist')); |
||
| 185 | $xoopsMailer->assign("EMAIL", $obj->getVar('email')); |
||
| 186 | $xoopsMailer->assign("NAME", $obj->getVar('name')); |
||
| 187 | $xoopsMailer->assign("SONGID", $song->getVar('songid')); |
||
| 188 | $xoopsMailer->assign("SONGURL", $song->getURL()); |
||
| 189 | $xoopsMailer->assign("FOUNDTITLE", $sng); |
||
| 190 | $xoopsMailer->assign("FOUNDALBUM", $alb); |
||
| 191 | $xoopsMailer->assign("FOUNDARTIST", $art); |
||
| 192 | $xoopsMailer->assign("FOUNDGENRE", $gen); |
||
| 193 | $xoopsMailer->assign("EMAIL", $obj->getVar('email')); |
||
| 194 | $xoopsMailer->assign("NAME", $obj->getVar('name')); |
||
| 195 | |||
| 196 | if(!$xoopsMailer->send() ){ |
||
| 197 | xoops_error($xoopsMailer->getErrors(true), 'Email Send Error'); |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | return $rid; |
||
| 203 | } |
||
| 214 | ?> |
||
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.