Conditions | 8 |
Paths | 72 |
Total Lines | 149 |
Code Lines | 71 |
Lines | 21 |
Ratio | 14.09 % |
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 |
||
67 | function editcat($showmenu = false, $categoryid = 0) |
||
68 | { |
||
69 | //$moderators = array(); // just to define the variable |
||
70 | //$allmods = array(); |
||
71 | $startfaq = isset($_GET['startfaq']) ? (int)$_GET['startfaq'] : 0; |
||
72 | global $categoryHandler, $xoopsUser, $xoopsUser, $myts, $xoopsConfig, $xoopsDB, $modify, $xoopsModuleConfig, $xoopsModule, $_GET; |
||
73 | include_once XOOPS_ROOT_PATH . '/class/xoopsformloader.php'; |
||
74 | |||
75 | // Creating the faq handler object |
||
76 | $faqHandler = sf_gethandler('faq'); |
||
77 | |||
78 | echo "<script type=\"text/javascript\" src=\"funcs.js\"></script>"; |
||
79 | echo '<style>'; |
||
80 | echo '<!-- '; |
||
81 | echo 'select { width: 130px; }'; |
||
82 | echo '-->'; |
||
83 | echo '</style>'; |
||
84 | // If there is a parameter, and the id exists, retrieve data: we're editing a category |
||
85 | if ($categoryid != 0) { |
||
86 | |||
87 | // Creating the category object for the selected category |
||
88 | $categoryObj = new sfCategory($categoryid); |
||
89 | |||
90 | echo "<br />\n"; |
||
91 | if ($categoryObj->notLoaded()) { |
||
92 | redirect_header('category.php', 1, _AM_SF_NOCOLTOEDIT); |
||
93 | } |
||
94 | sf_collapsableBar('bottomtable', 'bottomtableicon'); |
||
95 | echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a> " . _AM_SF_EDITCOL . '</h3>'; |
||
96 | echo "<div id='bottomtable'>"; |
||
97 | } else { |
||
98 | $categoryObj = $categoryHandler->create(); |
||
99 | echo "<br />\n"; |
||
100 | sf_collapsableBar('bottomtable', 'bottomtableicon'); |
||
101 | echo "<img id='bottomtableicon' src=" . XOOPS_URL . '/modules/' . $xoopsModule->dirname() . "/assets/images/icon/close12.gif alt='' /></a> " . _AM_SF_CATEGORY_CREATE . '</h3>'; |
||
102 | echo "<div id='bottomtable'>"; |
||
103 | } |
||
104 | // Start category form |
||
105 | $sform = new XoopsThemeForm(_AM_SF_CATEGORY, 'op', xoops_getenv('PHP_SELF')); |
||
106 | $sform->setExtra('enctype="multipart/form-data"'); |
||
107 | |||
108 | // Name |
||
109 | $sform->addElement(new XoopsFormText(_AM_SF_CATEGORY, 'name', 50, 255, $categoryObj->name('e')), true); |
||
110 | |||
111 | // Parent Category |
||
112 | $mytree = new XoopsTree($xoopsDB->prefix('smartfaq_categories'), 'categoryid', 'parentid'); |
||
113 | ob_start(); |
||
114 | $mytree->makeMySelBox('name', 'weight', $categoryObj->parentid(), 1, 'parentid'); |
||
115 | |||
116 | //makeMySelBox($title,$order="",$preset_id=0, $none=0, $sel_name="", $onchange="") |
||
117 | $sform->addElement(new XoopsFormLabel(_AM_SF_PARENT_CATEGORY_EXP, ob_get_contents())); |
||
118 | ob_end_clean(); |
||
119 | |||
120 | /* $mytree = new XoopsTree($xoopsDB->prefix("smartfaq_categories"), "categoryid" , "parentid"); |
||
121 | ob_start(); |
||
122 | $sform->addElement(new XoopsFormHidden('categoryid', $categoryObj->categoryid())); |
||
123 | $mytree->makeMySelBox("name", "weight", $categoryObj->categoryid()); |
||
124 | $sform->addElement(new XoopsFormLabel(_AM_SF_CATEGORY_FAQ, ob_get_contents())); |
||
125 | ob_end_clean(); |
||
126 | */ |
||
127 | |||
128 | // Decsription |
||
129 | $sform->addElement(new XoopsFormTextArea(_AM_SF_COLDESCRIPT, 'description', $categoryObj->description('e'), 7, 60)); |
||
130 | |||
131 | // Weight |
||
132 | $sform->addElement(new XoopsFormText(_AM_SF_COLPOSIT, 'weight', 4, 4, $categoryObj->weight())); |
||
133 | |||
134 | // READ PERMISSIONS |
||
135 | $memberHandler = xoops_getHandler('member'); |
||
136 | $group_list = $memberHandler->getGroupList(); |
||
137 | |||
138 | $groups_read_checkbox = new XoopsFormCheckBox(_AM_SF_PERMISSIONS_CAT_READ, 'groups_read[]', $categoryObj->getGroups_read()); |
||
139 | foreach ($group_list as $group_id => $group_name) { |
||
140 | if ($group_id != XOOPS_GROUP_ADMIN) { |
||
141 | $groups_read_checkbox->addOption($group_id, $group_name); |
||
142 | } |
||
143 | } |
||
144 | $sform->addElement($groups_read_checkbox); |
||
145 | // Apply permissions on all faqs |
||
146 | $addapplyall_radio = new XoopsFormRadioYN(_AM_SF_PERMISSIONS_APPLY_ON_FAQS, 'applyall', 0, ' ' . _AM_SF_YES . '', ' ' . _AM_SF_NO . ''); |
||
147 | $sform->addElement($addapplyall_radio); |
||
148 | // MODERATORS |
||
149 | //$moderators_tray = new XoopsFormElementTray(_AM_SF_MODERATORS_DEF, ''); |
||
150 | |||
151 | $module_id = $xoopsModule->getVar('mid'); |
||
152 | |||
153 | /*$gpermHandler = xoops_getHandler('groupperm'); |
||
154 | $mod_perms = $gpermHandler->getGroupIds('category_moderation', $categoryid, $module_id); |
||
155 | |||
156 | $moderators_select = new XoopsFormSelect('', 'moderators', $moderators, 5, true); |
||
157 | $moderators_tray->addElement($moderators_select); |
||
158 | |||
159 | $butt_mngmods = new XoopsFormButton('', '', 'Manage mods', 'button'); |
||
160 | $butt_mngmods->setExtra('onclick="javascript:small_window(\'pop.php\', 370, 350);"'); |
||
161 | $moderators_tray->addElement($butt_mngmods); |
||
162 | |||
163 | $butt_delmod = new XoopsFormButton('', '', 'Delete mod', 'button'); |
||
164 | $butt_delmod->setExtra('onclick="javascript:deleteSelectedItemsFromList(this.form.elements[\'moderators[]\']);"'); |
||
165 | $moderators_tray->addElement($butt_delmod); |
||
166 | |||
167 | $sform->addElement($moderators_tray); |
||
168 | */ |
||
169 | $sform->addElement(new XoopsFormHidden('categoryid', $categoryid)); |
||
170 | |||
171 | // Action buttons tray |
||
172 | $button_tray = new XoopsFormElementTray('', ''); |
||
173 | |||
174 | /*for ($i = 0; $i < count($moderators); ++$i) { |
||
175 | $allmods[] = $moderators[$i]; |
||
176 | } |
||
177 | |||
178 | $hiddenmods = new XoopsFormHidden('allmods', $allmods); |
||
179 | $button_tray->addElement($hiddenmods); |
||
180 | */ |
||
181 | $hidden = new XoopsFormHidden('op', 'addcategory'); |
||
182 | $button_tray->addElement($hidden); |
||
183 | // No ID for category -- then it's new category, button says 'Create' |
||
184 | View Code Duplication | if (!$categoryid) { |
|
185 | $butt_create = new XoopsFormButton('', '', _AM_SF_CREATE, 'submit'); |
||
186 | $butt_create->setExtra('onclick="this.form.elements.op.value=\'addcategory\'"'); |
||
187 | $button_tray->addElement($butt_create); |
||
188 | |||
189 | $butt_clear = new XoopsFormButton('', '', _AM_SF_CLEAR, 'reset'); |
||
190 | $button_tray->addElement($butt_clear); |
||
191 | |||
192 | $butt_cancel = new XoopsFormButton('', '', _AM_SF_CANCEL, 'button'); |
||
193 | $butt_cancel->setExtra('onclick="history.go(-1)"'); |
||
194 | $button_tray->addElement($butt_cancel); |
||
195 | } else { |
||
196 | // button says 'Update' |
||
197 | $butt_create = new XoopsFormButton('', '', _AM_SF_MODIFY, 'submit'); |
||
198 | $butt_create->setExtra('onclick="this.form.elements.op.value=\'addcategory\'"'); |
||
199 | $button_tray->addElement($butt_create); |
||
200 | |||
201 | $butt_cancel = new XoopsFormButton('', '', _AM_SF_CANCEL, 'button'); |
||
202 | $butt_cancel->setExtra('onclick="history.go(-1)"'); |
||
203 | $button_tray->addElement($butt_cancel); |
||
204 | } |
||
205 | |||
206 | $sform->addElement($button_tray); |
||
207 | $sform->display(); |
||
208 | echo '</div>'; |
||
209 | |||
210 | if ($categoryid) { |
||
211 | include_once XOOPS_ROOT_PATH . '/modules/smartfaq/include/displayfaqs.php'; |
||
212 | } |
||
213 | |||
214 | unset($hidden); |
||
215 | } |
||
216 | |||
359 |
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.