| Conditions | 25 |
| Paths | 13824 |
| Total Lines | 114 |
| Code Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 26 | function __construct(\XoopsUser $user, Profile $profile = null, $action = false) |
||
|
|
|||
| 27 | { |
||
| 28 | $helper = \XoopsModules\Suico\Helper::getInstance(); |
||
| 29 | if (!$action) { |
||
| 30 | $action = $_SERVER['REQUEST_URI']; |
||
| 31 | } |
||
| 32 | if (empty($GLOBALS['xoopsConfigUser'])) { |
||
| 33 | /* @var \XoopsConfigHandler $configHandler */ |
||
| 34 | $configHandler = xoops_getHandler('config'); |
||
| 35 | $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER); |
||
| 36 | } |
||
| 37 | require_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
||
| 38 | $title = $user->isNew() ? _AM_SUICO_ADDUSER : _US_EDITPROFILE; |
||
| 39 | parent::__construct($title, 'userinfo', $action, 'post', true); |
||
| 40 | /* @var ProfileHandler $profileHandler */ |
||
| 41 | $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile'); |
||
| 42 | // Dynamic fields |
||
| 43 | if (!$profile) { |
||
| 44 | /* @var ProfileHandler $profileHandler */ |
||
| 45 | $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile'); |
||
| 46 | $profile = $profileHandler->get($user->getVar('uid')); |
||
| 47 | } |
||
| 48 | // Get fields |
||
| 49 | $fields = $profileHandler->loadFields(); |
||
| 50 | // Get ids of fields that can be edited |
||
| 51 | /* @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 52 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
| 53 | $editable_fields = $grouppermHandler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid')); |
||
| 54 | if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) { |
||
| 55 | $elements[0][] = [ |
||
| 56 | 'element' => new \XoopsFormText(_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')), |
||
| 57 | 'required' => 1, |
||
| 58 | ]; |
||
| 59 | $email_text = new \XoopsFormText('', 'email', 30, 60, $user->getVar('email')); |
||
| 60 | } else { |
||
| 61 | $elements[0][] = ['element' => new XoopsFormLabel(_MD_SUICO_NICKNAME, $user->getVar('uname')), 'required' => 0]; |
||
| 62 | $email_text = new XoopsFormLabel('', $user->getVar('email')); |
||
| 63 | } |
||
| 64 | $email_tray = new \XoopsFormElementTray(_MD_SUICO_EMAILADDRESS, '<br>'); |
||
| 65 | $email_tray->addElement($email_text, ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0); |
||
| 66 | $weights[0][] = 0; |
||
| 67 | $elements[0][] = ['element' => $email_tray, 'required' => 0]; |
||
| 68 | $weights[0][] = 0; |
||
| 69 | if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) { |
||
| 70 | //If the user is an admin and is editing someone else |
||
| 71 | $pwd_text = new \XoopsFormPassword('', 'password', 10, 32); |
||
| 72 | $pwd_text2 = new \XoopsFormPassword('', 'vpass', 10, 32); |
||
| 73 | $pwd_tray = new \XoopsFormElementTray(_MD_SUICO_PASSWORD . '<br>' . _MD_SUICO_CONFIRMPASSWORD); |
||
| 74 | $pwd_tray->addElement($pwd_text); |
||
| 75 | $pwd_tray->addElement($pwd_text2); |
||
| 76 | $elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required |
||
| 77 | $weights[0][] = 0; |
||
| 78 | $level_radio = new \XoopsFormRadio(_MD_SUICO_USERLEVEL, 'level', $user->getVar('level')); |
||
| 79 | $level_radio->addOption(1, _MD_SUICO_ACTIVE); |
||
| 80 | $level_radio->addOption(0, _MD_SUICO_INACTIVE); |
||
| 81 | //$level_radio->addOption(-1, _MD_SUICO_DISABLED); |
||
| 82 | $elements[0][] = ['element' => $level_radio, 'required' => 0]; |
||
| 83 | $weights[0][] = 0; |
||
| 84 | } |
||
| 85 | $elements[0][] = ['element' => new XoopsFormHidden('uid', $user->getVar('uid')), 'required' => 0]; |
||
| 86 | $weights[0][] = 0; |
||
| 87 | $elements[0][] = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0]; |
||
| 88 | $weights[0][] = 0; |
||
| 89 | $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category'); |
||
| 90 | $categories = []; |
||
| 91 | $all_categories = $categoryHandler->getObjects(null, true, false); |
||
| 92 | $count_fields = count($fields); |
||
| 93 | foreach (array_keys($fields) as $i) { |
||
| 94 | if (in_array($fields[$i]->getVar('field_id'), $editable_fields)) { |
||
| 95 | // Set default value for user fields if available |
||
| 96 | if ($user->isNew()) { |
||
| 97 | $default = $fields[$i]->getVar('field_default'); |
||
| 98 | if ('' !== $default && null !== $default) { |
||
| 99 | $user->setVar($fields[$i]->getVar('field_name'), $default); |
||
| 100 | } |
||
| 101 | } |
||
| 102 | if (null === $profile->getVar($fields[$i]->getVar('field_name'), 'n')) { |
||
| 103 | $default = $fields[$i]->getVar('field_default', 'n'); |
||
| 104 | $profile->setVar($fields[$i]->getVar('field_name'), $default); |
||
| 105 | } |
||
| 106 | $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile); |
||
| 107 | $fieldinfo['required'] = $fields[$i]->getVar('field_required'); |
||
| 108 | $key = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id'); |
||
| 109 | $elements[$key][] = $fieldinfo; |
||
| 110 | $weights[$key][] = $fields[$i]->getVar('field_weight'); |
||
| 111 | $categories[$key] = @$all_categories[$fields[$i]->getVar('cat_id')]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) { |
||
| 115 | xoops_loadLanguage('admin', 'profile'); |
||
| 116 | /* @var \XoopsGroupPermHandler $grouppermHandler */ |
||
| 117 | $grouppermHandler = xoops_getHandler('groupperm'); |
||
| 118 | //If user has admin rights on groups |
||
| 119 | include_once $GLOBALS['xoops']->path('modules/system/constants.php'); |
||
| 120 | if ($grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) { |
||
| 121 | //add group selection |
||
| 122 | $group_select = new \XoopsFormSelectGroup(_MD_SUICO_USERGROUPS, 'groups', false, $user->getGroups(), 5, true); |
||
| 123 | $elements[0][] = ['element' => $group_select, 'required' => 0]; |
||
| 124 | //set as latest; |
||
| 125 | $weights[0][] = $count_fields + 1; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | ksort($elements); |
||
| 129 | foreach (array_keys($elements) as $k) { |
||
| 130 | array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]); |
||
| 131 | $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT; |
||
| 132 | $desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : ''; |
||
| 133 | $this->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false); |
||
| 134 | foreach (array_keys($elements[$k]) as $i) { |
||
| 135 | $this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | $this->addElement(new XoopsFormHidden('uid', $user->getVar('uid'))); |
||
| 139 | $this->addElement(new XoopsFormButton('', 'submit', _MD_SUICO_SAVECHANGES, 'submit')); |
||
| 140 | } |
||
| 142 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.