| Conditions | 10 |
| Paths | 22 |
| Total Lines | 34 |
| Code Lines | 20 |
| 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 namespace XoopsModules\Smartfaq; |
||
| 31 | public function getPermissions($type = 'category', $id = null) |
||
| 32 | { |
||
| 33 | global $xoopsUser; |
||
|
|
|||
| 34 | static $permissions; |
||
| 35 | |||
| 36 | if (!isset($permissions[$type]) || (null !== $id && !isset($permissions[$type][$id]))) { |
||
| 37 | $smartModule = Smartfaq\Utility::getModuleInfo(); |
||
| 38 | //Get group permissions handler |
||
| 39 | $gpermHandler = xoops_getHandler('groupperm'); |
||
| 40 | //Get user's groups |
||
| 41 | $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : [XOOPS_GROUP_ANONYMOUS]; |
||
| 42 | |||
| 43 | switch ($type) { |
||
| 44 | case 'category': |
||
| 45 | $gperm_name = 'category_read'; |
||
| 46 | break; |
||
| 47 | |||
| 48 | case 'item': |
||
| 49 | $gperm_name = 'item_read'; |
||
| 50 | break; |
||
| 51 | |||
| 52 | case 'moderation': |
||
| 53 | $gperm_name = 'category_moderation'; |
||
| 54 | $groups = is_object($xoopsUser) ? $xoopsUser->getVar('uid') : 0; |
||
| 55 | } |
||
| 56 | |||
| 57 | //Get all allowed item ids in this module and for this user's groups |
||
| 58 | $userpermissions = $gpermHandler->getItemIds($gperm_name, $groups, $smartModule->getVar('mid')); |
||
| 59 | $permissions[$type] = $userpermissions; |
||
| 60 | } |
||
| 61 | |||
| 62 | //Return the permission array |
||
| 63 | return isset($permissions[$type]) ? $permissions[$type] : []; |
||
| 64 | } |
||
| 65 | } |
||
| 66 |
Instead of relying on
globalstate, we recommend one of these alternatives:1. Pass all data via parameters
2. Create a class that maintains your state