Conditions | 24 |
Paths | > 20000 |
Total Lines | 156 |
Lines | 15 |
Ratio | 9.62 % |
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 |
||
69 | public function getForm($action = false) |
||
70 | { |
||
71 | global $xoopsDB; |
||
72 | |||
73 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
74 | |||
75 | if (false === $action) { |
||
76 | $action = $_SERVER['REQUEST_URI']; |
||
77 | } |
||
78 | |||
79 | $title = $this->isNew() ? sprintf(_AM_XNEWSLETTER_CAT_ADD) : sprintf(_AM_XNEWSLETTER_CAT_EDIT); |
||
80 | |||
81 | require_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
82 | $form = new \XoopsThemeForm($title, 'form', $action, 'post', true); |
||
83 | $form->setExtra('enctype="multipart/form-data"'); |
||
84 | |||
85 | // cat_name |
||
86 | $form->addElement(new \XoopsFormText(_AM_XNEWSLETTER_CAT_NAME, 'cat_name', 50, 255, $this->getVar('cat_name', 'e')), true); |
||
87 | |||
88 | // cat_info |
||
89 | $cat_info_dhtemtextarea = new \XoopsFormDhtmlTextArea(_AM_XNEWSLETTER_CAT_INFO, 'cat_info', $this->getVar('cat_info', 'e'), 10, 50); |
||
90 | $cat_info_dhtemtextarea->setDescription(_AM_XNEWSLETTER_CAT_INFO_DESC); |
||
91 | $form->addElement($cat_info_dhtemtextarea, false); |
||
92 | |||
93 | // category: dohtml, dosmiley, doxcode, doimage, dobr |
||
94 | $options_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_TEXTOPTIONS, ' '); |
||
95 | $options_tray->setDescription(_AM_XNEWSLETTER_TEXTOPTIONS_DESC); |
||
96 | $html_checkbox = new \XoopsFormCheckBox('', 'dohtml', $this->getVar('dohtml')); |
||
97 | $html_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWHTML); |
||
98 | $options_tray->addElement($html_checkbox); |
||
99 | $smiley_checkbox = new \XoopsFormCheckBox('', 'dosmiley', $this->getVar('dosmiley')); |
||
100 | $smiley_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWSMILEY); |
||
101 | $options_tray->addElement($smiley_checkbox); |
||
102 | $xcodes_checkbox = new \XoopsFormCheckBox('', 'doxcode', $this->getVar('doxcode')); |
||
103 | $xcodes_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWXCODE); |
||
104 | $options_tray->addElement($xcodes_checkbox); |
||
105 | $noimages_checkbox = new \XoopsFormCheckBox('', 'doimage', $this->getVar('doimage')); |
||
106 | $noimages_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWIMAGES); |
||
107 | $options_tray->addElement($noimages_checkbox); |
||
108 | $breaks_checkbox = new \XoopsFormCheckBox('', 'dobr', $this->getVar('dobr')); |
||
109 | $breaks_checkbox->addOption(1, _AM_XNEWSLETTER_ALLOWBREAK); |
||
110 | $options_tray->addElement($breaks_checkbox); |
||
111 | $form->addElement($options_tray); |
||
112 | |||
113 | // cat_gperms... |
||
114 | $memberHandler = xoops_getHandler('member'); |
||
115 | $userGroups = $memberHandler->getGroupList(); |
||
116 | // create admin checkbox |
||
117 | foreach ($userGroups as $group_id => $group_name) { |
||
118 | if (XOOPS_GROUP_ADMIN == $group_id) { |
||
119 | $group_id_admin = $group_id; |
||
120 | $group_name_admin = $group_name; |
||
121 | } |
||
122 | } |
||
123 | $select_perm_admin = new \XoopsFormCheckBox('', 'admin', XOOPS_GROUP_ADMIN); |
||
124 | $select_perm_admin->addOption($group_id_admin, $group_name_admin); |
||
125 | $select_perm_admin->setExtra("disabled='disabled'"); |
||
126 | |||
127 | // permission read cat |
||
128 | $cat_gperms_read = $grouppermHandler->getGroupIds('newsletter_read_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid()); |
||
129 | $arr_cat_gperms_read = $this->isNew() ? '0' : $cat_gperms_read; |
||
130 | $perms_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_READ, ''); |
||
131 | // checkbox webmaster |
||
132 | $perms_tray->addElement($select_perm_admin, false); |
||
133 | // checkboxes other groups |
||
134 | $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_read', $arr_cat_gperms_read); |
||
135 | foreach ($userGroups as $group_id => $group_name) { |
||
136 | if (XOOPS_GROUP_ADMIN != $group_id) { |
||
137 | $select_perm->addOption($group_id, $group_name); |
||
138 | } |
||
139 | } |
||
140 | $perms_tray->addElement($select_perm, false); |
||
141 | $form->addElement($perms_tray, false); |
||
142 | unset($perms_tray); |
||
143 | unset($select_perm); |
||
144 | |||
145 | // permission create cat |
||
146 | $cat_gperms_create = $grouppermHandler->getGroupIds('newsletter_create_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid()); |
||
147 | $arr_cat_gperms_create = $this->isNew() ? '0' : $cat_gperms_create; |
||
148 | $perms_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_CREATE . _AM_XNEWSLETTER_CAT_GPERMS_CREATE_DESC, ''); |
||
149 | // checkbox webmaster |
||
150 | $perms_tray->addElement($select_perm_admin, false); |
||
151 | // checkboxes other groups |
||
152 | $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_create', $arr_cat_gperms_create); |
||
153 | View Code Duplication | foreach ($userGroups as $group_id => $group_name) { |
|
154 | if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) { |
||
155 | $select_perm->addOption($group_id, $group_name); |
||
156 | } |
||
157 | } |
||
158 | $perms_tray->addElement($select_perm, false); |
||
159 | $form->addElement($perms_tray, false); |
||
160 | unset($perms_tray); |
||
161 | unset($select_perm); |
||
162 | |||
163 | // permission admin cat |
||
164 | $cat_gperms_admin = $grouppermHandler->getGroupIds('newsletter_admin_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid()); |
||
165 | $arr_cat_gperms_admin = $this->isNew() ? '0' : $cat_gperms_admin; |
||
166 | $perms_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_ADMIN . _AM_XNEWSLETTER_CAT_GPERMS_ADMIN_DESC, ''); |
||
167 | // checkbox webmaster |
||
168 | $perms_tray->addElement($select_perm_admin, false); |
||
169 | // checkboxes other groups |
||
170 | $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_admin', $arr_cat_gperms_admin); |
||
171 | View Code Duplication | foreach ($userGroups as $group_id => $group_name) { |
|
172 | if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) { |
||
173 | $select_perm->addOption($group_id, $group_name); |
||
174 | } |
||
175 | } |
||
176 | $perms_tray->addElement($select_perm, false); |
||
177 | $form->addElement($perms_tray, false); |
||
178 | unset($perms_tray); |
||
179 | unset($select_perm); |
||
180 | |||
181 | // permission list subscriber of this cat |
||
182 | $cat_gperms_list = $grouppermHandler->getGroupIds('newsletter_list_cat', $this->getVar('cat_id'), $this->helper->getModule()->mid()); |
||
183 | $arr_cat_gperms_admin = $this->isNew() ? '0' : $cat_gperms_list; |
||
184 | |||
185 | $perms_tray = new \XoopsFormElementTray(_AM_XNEWSLETTER_CAT_GPERMS_LIST, ''); |
||
186 | // checkbox webmaster |
||
187 | $perms_tray->addElement($select_perm_admin, false); |
||
188 | // checkboxes other groups |
||
189 | $select_perm = new \XoopsFormCheckBox('', 'cat_gperms_list', $arr_cat_gperms_admin); |
||
190 | View Code Duplication | foreach ($userGroups as $group_id => $group_name) { |
|
191 | if (XOOPS_GROUP_ADMIN != $group_id && XOOPS_GROUP_ANONYMOUS != $group_id) { |
||
192 | $select_perm->addOption($group_id, $group_name); |
||
193 | } |
||
194 | } |
||
195 | $perms_tray->addElement($select_perm, false); |
||
196 | $form->addElement($perms_tray, false); |
||
197 | unset($perms_tray); |
||
198 | unset($select_perm); |
||
199 | |||
200 | // cat_mailinglist |
||
201 | $cat_mailinglist = $this->isNew() ? '0' : $this->getVar('cat_mailinglist'); |
||
202 | $mailinglistCriteria = new \CriteriaCompo(); |
||
203 | $mailinglistCriteria->setSort('mailinglist_id'); |
||
204 | $mailinglistCriteria->setOrder('ASC'); |
||
205 | $numrows_mailinglist = $this->helper->getHandler('Mailinglist')->getCount(); |
||
206 | if ($numrows_mailinglist > 0) { |
||
207 | $opt_mailinglist = new \XoopsFormRadio(_AM_XNEWSLETTER_LETTER_MAILINGLIST, 'cat_mailinglist', $cat_mailinglist); |
||
208 | $opt_mailinglist->addOption('0', _AM_XNEWSLETTER_LETTER_MAILINGLIST_NO); |
||
209 | $mailinglistObjs = $this->helper->getHandler('Mailinglist')->getAll($mailinglistCriteria); |
||
210 | foreach ($mailinglistObjs as $mailinglist_id => $mailinglistObj) { |
||
211 | $opt_mailinglist->addOption($mailinglist_id, $mailinglistObj->getVar('mailinglist_name')); |
||
212 | } |
||
213 | $form->addElement($opt_mailinglist); |
||
214 | } |
||
215 | |||
216 | $time = $this->isNew() ? time() : $this->getVar('cat_created'); |
||
217 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_SUBMITTER, $GLOBALS['xoopsUser']->uname())); |
||
218 | $form->addElement(new \XoopsFormLabel(_AM_XNEWSLETTER_ACCOUNTS_CREATED, formatTimestamp($time, 's'))); |
||
219 | |||
220 | $form->addElement(new \XoopsFormHidden('op', 'save_cat')); |
||
221 | $form->addElement(new \XoopsFormButton('', 'submit', _SUBMIT, 'submit')); |
||
222 | |||
223 | return $form; |
||
224 | } |
||
225 | } |
||
226 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: