Conditions | 33 |
Paths | > 20000 |
Total Lines | 141 |
Code Lines | 96 |
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 |
||
149 | function ¬ificationEvents($category_name, $enabled_only, $module_id = null) |
||
150 | { |
||
151 | if (!isset($module_id)) { |
||
152 | global $xoopsModule; |
||
153 | $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0; |
||
154 | $module = & $xoopsModule; |
||
155 | } else { |
||
156 | /** @var XoopsModuleHandler $module_handler */ |
||
157 | $module_handler = xoops_getHandler('module'); |
||
158 | $module = $module_handler->get($module_id); |
||
159 | } |
||
160 | $not_config = $module->getInfo('notification'); |
||
161 | /** @var XoopsConfigHandler $config_handler */ |
||
162 | $config_handler = xoops_getHandler('config'); |
||
163 | $mod_config = $config_handler->getConfigsByCat(0, $module_id); |
||
164 | |||
165 | $category = & notificationCategoryInfo($category_name, $module_id); |
||
166 | |||
167 | global $xoopsConfig; |
||
168 | $event_array = []; |
||
169 | |||
170 | $override_comment = false; |
||
171 | $override_commentsubmit = false; |
||
172 | $override_bookmark = false; |
||
173 | |||
174 | foreach ($not_config['event'] as $event) { |
||
175 | if ($event['category'] == $category_name) { |
||
176 | if (!is_dir($dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/')) { |
||
177 | $dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/english/mail_template/'; |
||
178 | } |
||
179 | $event['mail_template_dir'] = $dir; |
||
180 | if (!$enabled_only || notificationEventEnabled($category, $event, $module)) { |
||
181 | $event_array[] = $event; |
||
182 | } |
||
183 | if ($event['name'] === 'comment') { |
||
184 | $override_comment = true; |
||
185 | } |
||
186 | if ($event['name'] === 'comment_submit') { |
||
187 | $override_commentsubmit = true; |
||
188 | } |
||
189 | if ($event['name'] === 'bookmark') { |
||
190 | $override_bookmark = true; |
||
191 | } |
||
192 | } |
||
193 | } |
||
194 | |||
195 | xoops_loadLanguage('notification'); |
||
196 | // Insert comment info if applicable |
||
197 | |||
198 | if ($module->getVar('hascomments')) { |
||
199 | $com_config = $module->getInfo('comments'); |
||
200 | if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) { |
||
201 | if (!is_dir($dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/')) { |
||
202 | $dir = XOOPS_ROOT_PATH . '/language/english/mail_template/'; |
||
203 | } |
||
204 | $mail_template_dir = $dir; |
||
205 | |||
206 | include_once $GLOBALS['xoops']->path('include/comment_constants.php'); |
||
207 | /** @var \XoopsConfigHandler $config_handler */ |
||
208 | $config_handler = xoops_getHandler('config'); |
||
209 | $com_config = $config_handler->getConfigsByCat(0, $module_id); |
||
210 | if (!$enabled_only) { |
||
211 | $insert_comment = true; |
||
212 | $insert_submit = true; |
||
213 | } else { |
||
214 | $insert_comment = false; |
||
215 | $insert_submit = false; |
||
216 | switch ($com_config['com_rule']) { |
||
217 | case XOOPS_COMMENT_APPROVENONE: |
||
218 | // comments disabled, no comment events |
||
219 | break; |
||
220 | case XOOPS_COMMENT_APPROVEALL: |
||
221 | // all comments are automatically approved, no 'submit' |
||
222 | if (!$override_comment) { |
||
223 | $insert_comment = true; |
||
224 | } |
||
225 | break; |
||
226 | case XOOPS_COMMENT_APPROVEUSER: |
||
227 | case XOOPS_COMMENT_APPROVEADMIN: |
||
228 | // comments first submitted, require later approval |
||
229 | if (!$override_comment) { |
||
230 | $insert_comment = true; |
||
231 | } |
||
232 | if (!$override_commentsubmit) { |
||
233 | $insert_submit = true; |
||
234 | } |
||
235 | break; |
||
236 | } |
||
237 | } |
||
238 | if ($insert_comment) { |
||
239 | $event = [ |
||
240 | 'name' => 'comment', |
||
241 | 'category' => $category['name'], |
||
242 | 'title' => _NOT_COMMENT_NOTIFY, |
||
243 | 'caption' => _NOT_COMMENT_NOTIFYCAP, |
||
244 | 'description' => _NOT_COMMENT_NOTIFYDSC, |
||
245 | 'mail_template_dir' => $mail_template_dir, |
||
246 | 'mail_template' => 'comment_notify', |
||
247 | 'mail_subject' => _NOT_COMMENT_NOTIFYSBJ, |
||
248 | ]; |
||
249 | if (!$enabled_only || notificationEventEnabled($category, $event, $module)) { |
||
250 | $event_array[] = $event; |
||
251 | } |
||
252 | } |
||
253 | if ($insert_submit) { |
||
254 | $event = [ |
||
255 | 'name' => 'comment_submit', |
||
256 | 'category' => $category['name'], |
||
257 | 'title' => _NOT_COMMENTSUBMIT_NOTIFY, |
||
258 | 'caption' => _NOT_COMMENTSUBMIT_NOTIFYCAP, |
||
259 | 'description' => _NOT_COMMENTSUBMIT_NOTIFYDSC, |
||
260 | 'mail_template_dir' => $mail_template_dir, |
||
261 | 'mail_template' => 'commentsubmit_notify', |
||
262 | 'mail_subject' => _NOT_COMMENTSUBMIT_NOTIFYSBJ, |
||
263 | 'admin_only' => 1, |
||
264 | ]; |
||
265 | if (!$enabled_only || notificationEventEnabled($category, $event, $module)) { |
||
266 | $event_array[] = $event; |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | } |
||
271 | |||
272 | // Insert bookmark info if appropriate |
||
273 | |||
274 | if (!empty($category['allow_bookmark'])) { |
||
275 | if (!$override_bookmark) { |
||
276 | $event = [ |
||
277 | 'name' => 'bookmark', |
||
278 | 'category' => $category['name'], |
||
279 | 'title' => _NOT_BOOKMARK_NOTIFY, |
||
280 | 'caption' => _NOT_BOOKMARK_NOTIFYCAP, |
||
281 | 'description' => _NOT_BOOKMARK_NOTIFYDSC, |
||
282 | ]; |
||
283 | if (!$enabled_only || notificationEventEnabled($category, $event, $module)) { |
||
284 | $event_array[] = $event; |
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | |||
289 | return $event_array; |
||
290 | } |
||
428 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: