| Conditions | 16 |
| Paths | 2560 |
| Total Lines | 112 |
| 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 |
||
| 105 | public function getForm($action = false) |
||
| 106 | { |
||
| 107 | global $xoopsDB, $xoopsUser; |
||
| 108 | |||
| 109 | if (false === $action) { |
||
| 110 | $action = $_SERVER['REQUEST_URI']; |
||
| 111 | } |
||
| 112 | |||
| 113 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
| 114 | $title = $this->isNew() ? sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_ADD) : sprintf(_MA_XNEWSLETTER_SUBSCRIPTION_EDIT); |
||
| 115 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
| 116 | $form->setExtra('enctype="multipart/form-data"'); |
||
| 117 | |||
| 118 | $form->addElement(new \XoopsFormLabel("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_INFO_PERS . '</span>', '')); |
||
| 119 | $subscr_id = $this->isNew() ? 0 : $this->getVar('subscr_id'); |
||
| 120 | |||
| 121 | // subscr_email |
||
| 122 | if ($subscr_id > 0 || '' != $this->getVar('subscr_email')) { |
||
| 123 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_SUBSCR_EMAIL, $this->getVar('subscr_email'))); |
||
| 124 | $form->addElement(new \XoopsFormHidden('subscr_email', $this->getVar('subscr_email'))); |
||
| 125 | } else { |
||
| 126 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_EMAIL, 'subscr_email', 50, 255, $this->getVar('subscr_email')), true); |
||
| 127 | } |
||
| 128 | |||
| 129 | // subscr_sex |
||
| 130 | if (1 == $this->helper->getConfig('xn_use_salutation')) { |
||
| 131 | $select_subscr_sex = new \XoopsFormSelect(_AM_XNEWSLETTER_SUBSCR_SEX, 'subscr_sex', $this->getVar('subscr_sex')); |
||
| 132 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_EMPTY, _AM_XNEWSLETTER_SUBSCR_SEX_EMPTY); |
||
| 133 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FEMALE, _AM_XNEWSLETTER_SUBSCR_SEX_FEMALE); |
||
| 134 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_MALE, _AM_XNEWSLETTER_SUBSCR_SEX_MALE); |
||
| 135 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_COMP, _AM_XNEWSLETTER_SUBSCR_SEX_COMP); |
||
| 136 | $select_subscr_sex->addOption(_AM_XNEWSLETTER_SUBSCR_SEX_FAMILY, _AM_XNEWSLETTER_SUBSCR_SEX_FAMILY); |
||
| 137 | $form->addElement($select_subscr_sex); |
||
| 138 | } |
||
| 139 | |||
| 140 | // subscr_firstname |
||
| 141 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_FIRSTNAME, 'subscr_firstname', 50, 255, $this->getVar('subscr_firstname')), true); |
||
| 142 | |||
| 143 | // subscr_lastname |
||
| 144 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_SUBSCR_LASTNAME, 'subscr_lastname', 50, 255, $this->getVar('subscr_lastname')), false); |
||
| 145 | |||
| 146 | $form->addElement(new \XoopsFormLabel('<br><br>', '')); |
||
| 147 | |||
| 148 | // get newsletters available for current user |
||
| 149 | $opt_cat = []; |
||
|
|
|||
| 150 | $opt_tray = new \XoopsFormElementTray("<span style='text-decoration:underline'>" . _MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL . '</span>', '<br>'); |
||
| 151 | $opt_tray->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC); |
||
| 152 | /** @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 153 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
| 154 | $uid = (is_object($xoopsUser) && isset($xoopsUser)) ? $xoopsUser->uid() : 0; |
||
| 155 | $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [0 => XOOPS_GROUP_ANONYMOUS]; |
||
| 156 | |||
| 157 | // cats[], existing_catsubcr_id_{$cat_id}, existing_catsubscr_quited_{$cat_id} |
||
| 158 | $catCriteria = new \CriteriaCompo(); |
||
| 159 | $catCriteria->setSort('cat_id'); |
||
| 160 | $catCriteria->setOrder('ASC'); |
||
| 161 | $catObjs = $this->helper->getHandler('Cat')->getAll($catCriteria); |
||
| 162 | // $cat_checkbox = new \XoopsFormCheckBox(_MA_XNEWSLETTER_SUBSCRIPTION_SELECT_CATS, 'cats', null, '<br>'); |
||
| 163 | // $cat_checkbox->setDescription(_MA_XNEWSLETTER_SUBSCRIPTION_CATS_AVAIL_DESC); |
||
| 164 | // |
||
| 165 | // |
||
| 166 | // $cat_tray = new \XoopsFormElementTray(_MA_XNEWSLETTER_SUBSCRIPTION_SELECT_CATS, '<br>'); |
||
| 167 | $values = []; |
||
| 168 | foreach ($catObjs as $cat_id => $catObj) { |
||
| 169 | // if anonymous user or Xoops user can read cat... |
||
| 170 | if ($grouppermHandler->checkRight('newsletter_read_cat', $cat_id, XOOPS_GROUP_ANONYMOUS, $this->helper->getModule()->mid()) |
||
| 171 | || $grouppermHandler->checkRight('newsletter_read_cat', $cat_id, $groups, $this->helper->getModule()->mid())) { |
||
| 172 | // get existing catsubscr |
||
| 173 | $catsubscrCriteria = new \CriteriaCompo(); |
||
| 174 | $catsubscrCriteria->add(new \Criteria('catsubscr_catid', $cat_id)); |
||
| 175 | $catsubscrCriteria->add(new \Criteria('catsubscr_subscrid', $subscr_id)); |
||
| 176 | $catsubscrCriteria->setLimit(1); |
||
| 177 | $catsubscrObjs = $this->helper->getHandler('Catsubscr')->getObjects($catsubscrCriteria); |
||
| 178 | if (isset($catsubscrObjs[0])) { |
||
| 179 | $values[] = $cat_id; |
||
| 180 | $catsubscr_quited = $catsubscrObjs[0]->getVar('catsubscr_quited'); |
||
| 181 | $catsubscr_id = $catsubscrObjs[0]->getVar('catsubscr_id'); |
||
| 182 | } else { |
||
| 183 | $catsubscr_quited = 0; |
||
| 184 | $catsubscr_id = 0; |
||
| 185 | } |
||
| 186 | |||
| 187 | $cat_checkbox[$cat_id] = new \XoopsFormCheckBox('', 'cats[]', null, ''); |
||
| 188 | $name = $catObj->getVar('cat_name'); |
||
| 189 | if ('' !== $catObj->getVar('cat_info')) { |
||
| 190 | $name .= '<br><span class="xnewsletter-cat_info">' . $catObj->getVar('cat_info', 's') . '</span>'; |
||
| 191 | } |
||
| 192 | |||
| 193 | if (0 == $catsubscr_quited) { |
||
| 194 | // NOP |
||
| 195 | } else { |
||
| 196 | $name .= '<span>'; |
||
| 197 | $name .= str_replace('%q', formatTimestamp($catsubscr_quited, $this->helper->getConfig('dateformat')), _MA_XNEWSLETTER_SUBSCRIPTION_QUITED_DETAIL); |
||
| 198 | $name .= '</span>'; |
||
| 199 | } |
||
| 200 | // $name .= "<div style='clear:both'></div>"; |
||
| 201 | $cat_checkbox[$cat_id]->addOption($cat_id, $name); |
||
| 202 | $form->addElement(new \XoopsFormHidden("existing_catsubcr_id_{$cat_id}", $catsubscr_id)); |
||
| 203 | $form->addElement(new \XoopsFormHidden("existing_catsubscr_quited_{$cat_id}", $catsubscr_quited)); |
||
| 204 | $cat_checkbox[$cat_id]->setValue($values); |
||
| 205 | $opt_tray->addElement($cat_checkbox[$cat_id]); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | $form->addElement($opt_tray); |
||
| 209 | |||
| 210 | // op |
||
| 211 | $form->addElement(new \XoopsFormHidden('op', 'save_subscription')); |
||
| 212 | // button |
||
| 213 | $form->addElement(new \XoopsFormButtonTray('', _SUBMIT, 'submit', '', false)); |
||
| 214 | |||
| 215 | return $form; |
||
| 216 | } |
||
| 217 | |||
| 302 |
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.