Conditions | 14 |
Paths | 2160 |
Total Lines | 127 |
Code Lines | 86 |
Lines | 16 |
Ratio | 12.6 % |
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 |
||
146 | function manageStatus() |
||
147 | { |
||
148 | global $aSortBy, $aOrderBy, $aLimitBy, $order, $limit, $start, $sort; |
||
149 | $hStatus =& xhelpGetHandler('status'); |
||
150 | |||
151 | if(isset($_POST['changeDefaultStatus'])){ |
||
152 | xhelpSetMeta("default_status", $_POST['default']); |
||
153 | } |
||
154 | |||
155 | if(isset($_POST['newStatus'])){ |
||
156 | View Code Duplication | if($_POST['desc'] == ''){ // If no description supplied |
|
157 | $message = _AM_XHELP_MESSAGE_NO_DESC; |
||
158 | redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message); |
||
159 | } |
||
160 | $newStatus =& $hStatus->create(); |
||
161 | |||
162 | $newStatus->setVar('state', intval($_POST['state'])); |
||
163 | $newStatus->setVar('description', $_POST['desc']); |
||
164 | if($hStatus->insert($newStatus)){ |
||
165 | header("Location: ".XHELP_ADMIN_URL."/status.php?op=manageStatus"); |
||
166 | } else { |
||
167 | $message = _AM_MESSAGE_ADD_STATUS_ERR; |
||
168 | redirect_header(XHELP_ADMIN_URL."/status.php?op=manageStatus", 3, $message); |
||
169 | } |
||
170 | } |
||
171 | xoops_cp_header(); |
||
172 | //echo $oAdminButton->renderButtons('manStatus'); |
||
173 | $indexAdmin = new ModuleAdmin(); |
||
174 | echo $indexAdmin->addNavigation('status.php?op=manageStatus'); |
||
175 | |||
176 | echo "<form method='post' action='".XHELP_ADMIN_URL."/status.php?op=manageStatus'>"; |
||
177 | echo "<table width='100%' cellspacing='1' class='outer'> |
||
178 | <tr><th colspan='2'><label>"._AM_XHELP_TEXT_ADD_STATUS."</label></th></tr>"; |
||
179 | echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_DESCRIPTION."</td> |
||
180 | <td class='even'> |
||
181 | <input type='text' name='desc' value='' class='formButton' /> |
||
182 | </td> |
||
183 | </tr>"; |
||
184 | echo "<tr><td class='head' width='20%'>"._AM_XHELP_TEXT_STATE."</td><td class='even'> |
||
185 | <select name='state'> |
||
186 | <option value='1'>".xhelpGetState(1)."</option> |
||
187 | <option value='2'>".xhelpGetState(2)."</option> |
||
188 | </select></td></tr>"; |
||
189 | echo "<tr><td class='foot' colspan='2'><input type='submit' name='newStatus' value='"._AM_XHELP_TEXT_ADD_STATUS."' class='formButton' /></td></tr>"; |
||
190 | echo "</table></form>"; |
||
191 | |||
192 | // Get list of existing statuses |
||
193 | $crit = new Criteria('', ''); |
||
194 | $crit->setOrder($order); |
||
195 | $crit->setSort($sort); |
||
196 | $crit->setLimit($limit); |
||
197 | $crit->setStart($start); |
||
198 | $statuses =& $hStatus->getObjects($crit); |
||
199 | $total = $hStatus->getCount($crit); |
||
200 | |||
201 | $aStatuses = array(); |
||
202 | foreach($statuses as $status){ |
||
203 | $aStatuses[$status->getVar('id')] = $status->getVar('description'); |
||
204 | } |
||
205 | |||
206 | if(!$default_status = xhelpGetMeta("default_status")){ |
||
207 | xhelpSetMeta("default_status", "1"); |
||
208 | $default_status = 1; |
||
209 | } |
||
210 | $form = new xhelpForm(_AM_XHELP_TEXT_DEFAULT_STATUS, 'default_status', xhelpMakeURI(XHELP_ADMIN_URL.'/status.php', array('op'=>'manageStatus'))); |
||
211 | $status_select = new XoopsFormSelect(_AM_XHELP_TEXT_STATUS, 'default', $default_status); |
||
212 | $status_select->addOptionArray($aStatuses); |
||
213 | $btn_tray = new XoopsFormElementTray(''); |
||
214 | $btn_tray->addElement(new XoopsFormButton('', 'changeDefaultStatus', _AM_XHELP_BUTTON_SUBMIT, 'submit')); |
||
215 | $form->addElement($status_select); |
||
216 | $form->addElement($btn_tray); |
||
217 | $form->setLabelWidth('20%'); |
||
218 | echo $form->render(); |
||
219 | |||
220 | $nav = new XoopsPageNav($total, $limit, $start, 'start', "op=manageStatus&limit=$limit"); |
||
221 | |||
222 | echo "<form action='". XHELP_ADMIN_URL."/status.php?op=manageStatus' style='margin:0; padding:0;' method='post'>"; |
||
223 | echo "<table width='100%' cellspacing='1' class='outer'>"; |
||
224 | echo "<tr><td align='right'>"._AM_XHELP_TEXT_SORT_BY." |
||
225 | <select name='sort'>"; |
||
226 | View Code Duplication | foreach($aSortBy as $value=>$text){ |
|
227 | ($sort == $value) ? $selected = "selected='selected'" : $selected = ''; |
||
228 | echo "<option value='$value' $selected>$text</option>"; |
||
229 | } |
||
230 | echo "</select> |
||
231 | |
||
232 | "._AM_XHELP_TEXT_ORDER_BY." |
||
233 | <select name='order'>"; |
||
234 | View Code Duplication | foreach($aOrderBy as $value=>$text){ |
|
235 | ($order == $value) ? $selected = "selected='selected'" : $selected = ''; |
||
236 | echo "<option value='$value' $selected>$text</option>"; |
||
237 | } |
||
238 | echo "</select> |
||
239 | |
||
240 | "._AM_XHELP_TEXT_NUMBER_PER_PAGE." |
||
241 | <select name='limit'>"; |
||
242 | View Code Duplication | foreach($aLimitBy as $value=>$text){ |
|
243 | ($limit == $value) ? $selected = "selected='selected'" : $selected = ''; |
||
244 | echo "<option value='$value' $selected>$text</option>"; |
||
245 | } |
||
246 | echo "</select> |
||
247 | <input type='submit' name='status_sort' id='status_sort' value='"._AM_XHELP_BUTTON_SUBMIT."' /> |
||
248 | </td> |
||
249 | </tr>"; |
||
250 | echo "</table></form>"; |
||
251 | |||
252 | echo "<table width='100%' cellspacing='1' class='outer'> |
||
253 | <tr><th colspan='4'><label>"._AM_XHELP_TEXT_MANAGE_STATUSES."</label></th></tr>"; |
||
254 | echo "<tr class='head'> |
||
255 | <td>"._AM_XHELP_TEXT_ID."</td> |
||
256 | <td>"._AM_XHELP_TEXT_DESCRIPTION."</td> |
||
257 | <td>"._AM_XHELP_TEXT_STATE."</td> |
||
258 | <td>"._AM_XHELP_TEXT_ACTIONS."</td> |
||
259 | </tr>"; |
||
260 | foreach($statuses as $status){ |
||
261 | echo "<tr class='even'><td>".$status->getVar('id')."</td><td>".$status->getVar('description')."</td> |
||
262 | <td>".xhelpGetState($status->getVar('state'))."</td> |
||
263 | <td> |
||
264 | <a href='status.php?op=editStatus&statusid=".$status->getVar('id')."'><img src='".XHELP_IMAGE_URL."/button_edit.png' title='"._AM_XHELP_TEXT_EDIT."' name='editStatus' /></a> |
||
265 | <a href='status.php?op=deleteStatus&statusid=".$status->getVar('id')."'><img src='".XHELP_IMAGE_URL."/button_delete.png' title='"._AM_XHELP_TEXT_DELETE."' name='deleteStatus' /></a></td></tr> |
||
266 | </td></tr>"; |
||
267 | } |
||
268 | echo "</table>"; |
||
269 | echo "<div id='status_nav'>".$nav->renderNav()."</div>"; |
||
270 | include_once "admin_footer.php"; |
||
271 | |||
272 | } |
||
273 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.