UserForm::__construct()   F
last analyzed

Complexity

Conditions 25
Paths 13824

Size

Total Lines 114
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 83
c 1
b 0
f 0
dl 0
loc 114
rs 0
cc 25
nc 13824
nop 3

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace XoopsModules\Suico\Form;
6
7
use XoopsModules\Suico;
8
use XoopsModules\Suico\Profile;
9
use XoopsModules\Suico\ProfileHandler;
10
use XoopsThemeForm;
11
use XoopsFormButton;
12
use XoopsFormHidden;
13
use XoopsFormLabel;
14
use XoopsFormSelectUser;
15
16
/**
17
 * Get {@link XoopsThemeForm} for editing a user
18
 *
19
 * @param \XoopsUser $user {@link \XoopsUser} to edit
20
 * @param Profile    $profile
21
 * @param bool       $action
22
 *
23
 */
24
class UserForm extends XoopsThemeForm
25
{
26
    /**
27
     * UserForm constructor.
28
     * @param \XoopsUser                       $user
29
     * @param \XoopsModules\Suico\Profile|null $profile
30
     * @param bool                             $action
31
     */
32
    public function __construct(\XoopsUser $user, Profile $profile = null, $action = false)
33
    {
34
        $helper = \XoopsModules\Suico\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
35
        if (!$action) {
36
            $action = $_SERVER['REQUEST_URI'];
37
        }
38
        if (empty($GLOBALS['xoopsConfigUser'])) {
39
            /* @var \XoopsConfigHandler $configHandler */
40
            $configHandler              = \xoops_getHandler('config');
41
            $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(\XOOPS_CONF_USER);
42
        }
43
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
44
        $title = $user->isNew() ? \_AM_SUICO_ADDUSER : \_US_EDITPROFILE;
45
        parent::__construct($title, 'userinfo', $action, 'post', true);
46
        /* @var ProfileHandler $profileHandler */
47
        $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
48
        // Dynamic fields
49
        if (!$profile) {
50
            /* @var ProfileHandler $profileHandler */
51
            $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
52
            $profile        = $profileHandler->get($user->getVar('uid'));
53
        }
54
        // Get fields
55
        $fields = $profileHandler->loadFields();
56
        // Get ids of fields that can be edited
57
        /* @var  \XoopsGroupPermHandler $grouppermHandler */
58
        $grouppermHandler = \xoops_getHandler('groupperm');
59
        $editable_fields  = $grouppermHandler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid'));
60
        if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
61
            $elements[0][] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.
Loading history...
62
                'element'  => new \XoopsFormText(\_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')),
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

62
                'element'  => new \XoopsFormText(\_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
63
                'required' => 1,
64
            ];
65
            $email_text    = new \XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
66
        } else {
67
            $elements[0][] = ['element' => new XoopsFormLabel(\_MD_SUICO_NICKNAME, $user->getVar('uname')), 'required' => 0];
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname') can also be of type array and array; however, parameter $value of XoopsFormLabel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

67
            $elements[0][] = ['element' => new XoopsFormLabel(\_MD_SUICO_NICKNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0];
Loading history...
68
            $email_text    = new XoopsFormLabel('', $user->getVar('email'));
69
        }
70
        $email_tray = new \XoopsFormElementTray(\_MD_SUICO_EMAILADDRESS, '<br>');
71
        $email_tray->addElement($email_text, ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
0 ignored issues
show
Bug introduced by
$user->isNew() || $GLOBA...er']->isAdmin() ? 1 : 0 of type integer is incompatible with the type boolean expected by parameter $required of XoopsFormElementTray::addElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

71
        $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
Loading history...
72
        $weights[0][]  = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$weights was never initialized. Although not strictly required by PHP, it is generally a good practice to add $weights = array(); before regardless.
Loading history...
73
        $elements[0][] = ['element' => $email_tray, 'required' => 0];
74
        $weights[0][]  = 0;
75
        if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
76
            //If the user is an admin and is editing someone else
77
            $pwd_text  = new \XoopsFormPassword('', 'password', 10, 32);
78
            $pwd_text2 = new \XoopsFormPassword('', 'vpass', 10, 32);
79
            $pwd_tray  = new \XoopsFormElementTray(\_MD_SUICO_PASSWORD . '<br>' . \_MD_SUICO_CONFIRMPASSWORD);
80
            $pwd_tray->addElement($pwd_text);
81
            $pwd_tray->addElement($pwd_text2);
82
            $elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required
83
            $weights[0][]  = 0;
84
            $level_radio   = new \XoopsFormRadio(\_MD_SUICO_USERLEVEL, 'level', $user->getVar('level'));
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('level') can also be of type array and array; however, parameter $value of XoopsFormRadio::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            $level_radio   = new \XoopsFormRadio(\_MD_SUICO_USERLEVEL, 'level', /** @scrutinizer ignore-type */ $user->getVar('level'));
Loading history...
85
            $level_radio->addOption(1, \_MD_SUICO_ACTIVE);
86
            $level_radio->addOption(0, \_MD_SUICO_INACTIVE);
87
            //$level_radio->addOption(-1, _MD_SUICO_DISABLED);
88
            $elements[0][] = ['element' => $level_radio, 'required' => 0];
89
            $weights[0][]  = 0;
90
        }
91
        $elements[0][]   = ['element' => new XoopsFormHidden('uid', $user->getVar('uid')), 'required' => 0];
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uid') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

91
        $elements[0][]   = ['element' => new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0];
Loading history...
92
        $weights[0][]    = 0;
93
        $elements[0][]   = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0];
94
        $weights[0][]    = 0;
95
        $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
96
        $categories      = [];
97
        $all_categories  = $categoryHandler->getObjects(null, true, false);
98
        $count_fields    = \count($fields);
99
        foreach (\array_keys($fields) as $i) {
100
            if (\in_array($fields[$i]->getVar('field_id'), $editable_fields)) {
101
                // Set default value for user fields if available
102
                if ($user->isNew()) {
103
                    $default = $fields[$i]->getVar('field_default');
104
                    if ('' !== $default && null !== $default) {
105
                        $user->setVar($fields[$i]->getVar('field_name'), $default);
106
                    }
107
                }
108
                if (null === $profile->getVar($fields[$i]->getVar('field_name'), 'n')) {
109
                    $default = $fields[$i]->getVar('field_default', 'n');
110
                    $profile->setVar($fields[$i]->getVar('field_name'), $default);
111
                }
112
                $fieldinfo['element']  = $fields[$i]->getEditElement($user, $profile);
113
                $fieldinfo['required'] = $fields[$i]->getVar('field_required');
114
                $key                   = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id');
115
                $elements[$key][]      = $fieldinfo;
116
                $weights[$key][]       = $fields[$i]->getVar('field_weight');
117
                $categories[$key]      = @$all_categories[$fields[$i]->getVar('cat_id')];
118
            }
119
        }
120
        if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
121
            \xoops_loadLanguage('admin', 'profile');
122
            /* @var  \XoopsGroupPermHandler $grouppermHandler */
123
            $grouppermHandler = \xoops_getHandler('groupperm');
124
            //If user has admin rights on groups
125
            require_once $GLOBALS['xoops']->path('modules/system/constants.php');
126
            if ($grouppermHandler->checkRight('system_admin', \XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
127
                //add group selection
128
                $group_select  = new \XoopsFormSelectGroup(\_MD_SUICO_USERGROUPS, 'groups', false, $user->getGroups(), 5, true);
129
                $elements[0][] = ['element' => $group_select, 'required' => 0];
130
                //set as latest;
131
                $weights[0][] = $count_fields + 1;
132
            }
133
        }
134
        \ksort($elements);
135
        foreach (\array_keys($elements) as $k) {
136
            \array_multisort($weights[$k], \SORT_ASC, \array_keys($elements[$k]), \SORT_ASC, $elements[$k]);
0 ignored issues
show
Bug introduced by
array_keys($elements[$k]) cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
            \array_multisort($weights[$k], \SORT_ASC, /** @scrutinizer ignore-type */ \array_keys($elements[$k]), \SORT_ASC, $elements[$k]);
Loading history...
Bug introduced by
SORT_ASC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

136
            \array_multisort($weights[$k], /** @scrutinizer ignore-type */ \SORT_ASC, \array_keys($elements[$k]), \SORT_ASC, $elements[$k]);
Loading history...
137
            $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : \_MD_SUICO_DEFAULT;
138
            $desc  = isset($categories[$k]) ? $categories[$k]['cat_description'] : '';
139
            $this->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false);
140
            foreach (\array_keys($elements[$k]) as $i) {
141
                $this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
142
            }
143
        }
144
        $this->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
145
        $this->addElement(new XoopsFormButton('', 'submit', \_MD_SUICO_SAVECHANGES, 'submit'));
146
    }
147
}
148