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