Conditions | 9 |
Paths | 10 |
Total Lines | 93 |
Code Lines | 68 |
Lines | 6 |
Ratio | 6.45 % |
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 |
||
77 | function userlog_views_edit($options) |
||
78 | { |
||
79 | // require_once XOOPS_ROOT_PATH . "/class/blockform.php"; //reserve for 2.6 |
||
80 | xoops_load('XoopsFormLoader'); |
||
81 | // $form = new XoopsBlockForm(); //reserve for 2.6 |
||
82 | $form = new XoopsThemeForm(_AM_USERLOG_VIEW, 'views', ''); |
||
83 | |||
84 | /** @var XoopsModuleHandler $moduleHandler */ |
||
85 | $moduleHandler = xoops_getHandler('module'); |
||
86 | $criteria = new CriteriaCompo(); |
||
87 | $criteria->add(new Criteria('hasnotification', 1)); |
||
88 | $criteria->add(new Criteria('isactive', 1)); |
||
89 | $modules = $moduleHandler->getObjects($criteria, true); |
||
90 | $hasviews = []; |
||
91 | foreach ($modules as $module) { |
||
92 | $not_config = $module->getInfo('notification'); |
||
93 | View Code Duplication | foreach ($not_config['category'] as $category) { |
|
94 | if (!empty($category['item_name'])) { |
||
95 | $script = is_array($category['subscribe_from']) ? implode(':', $category['subscribe_from']) : $category['subscribe_from']; |
||
96 | $hasviews[$module->dirname() . ':' . $script . '-' . $category['item_name']] = $module->dirname() . '/' . $script . '?' . $category['item_name'] . '=ITEM_ID'; |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | $i = 0; |
||
102 | // number of items to display element |
||
103 | $numitemsEle = new XoopsFormText(_AM_USERLOG_ITEMS_NUM, "options[{$i}]", 10, 255, (int)$options[$i]); |
||
104 | |||
105 | ++$i; |
||
106 | // views element |
||
107 | $options_views = explode(',', $options[$i]); |
||
108 | $viewsEle = new XoopsFormCheckBox(_AM_USERLOG_ITEMS, "options[{$i}][]", !empty($options_views) ? $options_views : 0); |
||
109 | $viewsEle->columns = 3; |
||
110 | if (!empty($hasviews)) { |
||
111 | $viewsEle->addOptionArray($hasviews); |
||
112 | $viewsEle->setExtra("onchange = \"validate('options[{$i}][]','checkbox', true)\""); // prevent user select no option |
||
113 | $check_all = _ALL . ": <input type=\"checkbox\" name=\"item_check\" id=\"item_check\" value=\"1\" onclick=\"xoopsCheckGroup('blockform', 'item_check','options[{$i}][]');\">"; // blockform is the main form |
||
114 | $viewsEle = new XoopsFormLabel(_AM_USERLOG_ITEMS, $check_all . "<br\>" . $viewsEle->render()); |
||
115 | } else { |
||
116 | // prevent to select |
||
117 | $viewsEle->addOption(0, _NONE); |
||
118 | $viewsEle->setExtra('class="hidden"'); |
||
119 | } |
||
120 | |||
121 | $viewsEle->setDescription(_AM_USERLOG_ITEMS_DSC); |
||
122 | |||
123 | ++$i; |
||
124 | $timeEle = new XoopsFormText(_AM_USERLOG_LOG_TIMEGT, "options[{$i}]", 10, 255, $options[$i]); |
||
125 | $timeEle->setDescription(_AM_USERLOG_LOG_TIMEGT_FORM); |
||
126 | |||
127 | ++$i; |
||
128 | $userRadioEle = new XoopsFormRadio(_AM_USERLOG_UID, "options[{$i}]", $options[$i]); |
||
129 | $userRadioEle->addOption(-1, _ALL); |
||
130 | $userRadioEle->addOption(($options[$i] != -1) ? $options[$i] : 0, _SELECT); // if no user in selection box it select uid=0 anon users |
||
131 | $userRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if user dont select any option it select "all" |
||
132 | $userSelectEle = new XoopsFormSelectUser(_AM_USERLOG_UID, "options[{$i}]", true, explode(',', $options[$i]), 3, true); |
||
133 | $userEle = new XoopsFormLabel(_AM_USERLOG_UID, $userRadioEle->render() . $userSelectEle->render()); |
||
134 | |||
135 | ++$i; |
||
136 | $groupRadioEle = new XoopsFormRadio(_AM_USERLOG_GROUPS, "options[{$i}]", !empty($options[$i])); |
||
137 | $groupRadioEle->addOption(0, _ALL); |
||
138 | $groupRadioEle->addOption(!empty($options[$i]) ? $options[$i] : 2, _SELECT); // if no group in selection box it select gid=2 registered users |
||
139 | $groupRadioEle->setExtra("onchange=\"var el=document.getElementById('options[{$i}]'); el.disabled=(this.id == 'options[{$i}]1'); if (!el.value) {el.value= this.value}\""); // if group dont select any option it select "all" |
||
140 | $groupSelectEle = new XoopsFormSelectGroup(_AM_USERLOG_GROUPS, "options[{$i}]", true, explode(',', $options[$i]), 3, true); |
||
141 | $groupEle = new XoopsFormLabel(_AM_USERLOG_GROUPS, $groupRadioEle->render() . $groupSelectEle->render()); |
||
142 | |||
143 | ++$i; |
||
144 | $sortEle = new XoopsFormSelect(_AM_USERLOG_SORT, "options[{$i}]", $options[$i]); |
||
145 | $sortEle->addOptionArray([ |
||
146 | 'count' => _AM_USERLOG_VIEW, |
||
147 | 'module' => _AM_USERLOG_MODULE, |
||
148 | 'module_name' => _AM_USERLOG_MODULE_NAME, |
||
149 | 'module_count' => _AM_USERLOG_VIEW_MODULE |
||
150 | ]); |
||
151 | $sortEle->setDescription(_AM_USERLOG_SORT_DSC); |
||
152 | |||
153 | ++$i; |
||
154 | $orderEle = new XoopsFormSelect(_AM_USERLOG_ORDER, "options[{$i}]", $options[$i]); |
||
155 | $orderEle->addOption('DESC', _DESCENDING); |
||
156 | $orderEle->addOption('ASC', _ASCENDING); |
||
157 | $orderEle->setDescription(_AM_USERLOG_ORDER_DSC); |
||
158 | |||
159 | // add all elements to form |
||
160 | $form->addElement($numitemsEle); |
||
161 | $form->addElement($viewsEle); |
||
162 | $form->addElement($timeEle); |
||
163 | $form->addElement($userEle); |
||
164 | $form->addElement($groupEle); |
||
165 | $form->addElement($sortEle); |
||
166 | $form->addElement($orderEle); |
||
167 | |||
168 | return $form->render(); |
||
169 | } |
||
170 |
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.