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