profile_getUserForm()   F
last analyzed

Complexity

Conditions 25
Paths 13056

Size

Total Lines 124
Code Lines 80

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 25
eloc 80
nc 13056
nop 3
dl 0
loc 124
rs 0
c 1
b 0
f 0

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
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
*/
11
12
use Xoops\Core\Kernel\Handlers\XoopsUser;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, XoopsUser. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/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 before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
13
14
/**
15
 * Extended User Profile
16
 *
17
 * @copyright       2000-2016 XOOPS Project (http://xoops.org)
18
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
19
 * @package         profile
20
 * @since           2.3.0
21
 * @author          Jan Pedersen
22
 * @author          Taiwen Jiang <[email protected]>
23
 */
24
25
/**
26
 * Get {@link Xoops\Form\ThemeForm} for registering new users
27
 *
28
 * @param XoopsUser $user
29
 * @param $profile
30
 * @param null $step
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $step is correct as it would always require null to be passed?
Loading history...
31
 * @return Xoops\Form\ThemeForm
32
 */
33
function profile_getRegisterForm(XoopsUser $user, $profile, $step = null)
34
{
35
    $xoops = Xoops::getInstance();
36
    $action = $_SERVER['REQUEST_URI'];
37
    $step_no = $step['step_no'];
38
    $use_token = $step['step_no'] > 0 ? true : false;
39
    $reg_form = new Xoops\Form\ThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
40
41
    if ($step['step_desc']) {
42
        $reg_form->addElement(new Xoops\Form\Label('', $step['step_desc']));
43
    }
44
45
    if ($step_no == 1) {
46
        //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
47
48
        $elements[0][] = array(
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...
49
            'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->getConfig('maxuname'), $user->getVar('uname', 'e')),
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname', 'e') can also be of type string[]; however, parameter $value of Xoops\Form\Text::__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

49
            'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->getConfig('maxuname'), /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
50
            'required' => true
51
        );
52
        $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...
53
54
        $elements[0][] = array(
55
            'element' => new Xoops\Form\Text(XoopsLocale::EMAIL, 'email', 40, 160, $user->getVar('email', 'e')), 'required' => true
56
        );
57
        $weights[0][] = 0;
58
59
        $elements[0][] =
60
            array('element' => new Xoops\Form\Password(XoopsLocale::PASSWORD, 'pass'), 'required' => true);
61
        $weights[0][] = 0;
62
63
        $elements[0][] =
64
            array('element' => new Xoops\Form\Password(XoopsLocale::VERIFY_PASSWORD, 'vpass'), 'required' => true);
65
        $weights[0][] = 0;
66
    }
67
68
    // Dynamic fields
69
    /* @var $profile_handler ProfileProfileHandler */
70
    $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
71
    $fields = $profile_handler->loadFields();
72
    $_SESSION['profile_required'] = array();
73
    $weights = array();
74
    /* @var ProfileField $field */
75
    foreach ($fields as $field) {
76
        if ($field->getVar('step_id') == $step['step_id']) {
77
            $fieldinfo['element'] = $field->getEditElement($user, $profile);
78
            //assign and check (=)
79
            if ($fieldinfo['required'] = $field->getVar('field_required')) {
80
                $_SESSION['profile_required'][$field->getVar('field_name')] = $field->getVar('field_title');
81
            }
82
83
            $key = $field->getVar('cat_id');
84
            $elements[$key][] = $fieldinfo;
85
            $weights[$key][] = $field->getVar('field_weight');
86
        }
87
    }
88
    ksort($elements);
89
90
    foreach (array_keys($elements) as $k) {
91
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
0 ignored issues
show
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

91
        array_multisort($weights[$k], /** @scrutinizer ignore-type */ SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
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

91
        array_multisort($weights[$k], SORT_ASC, /** @scrutinizer ignore-type */ array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
92
        foreach (array_keys($elements[$k]) as $i) {
93
            $reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
94
        }
95
    }
96
    //end of Dynamic User fields
97
    $myts = \Xoops\Core\Text\Sanitizer::getInstance();
98
    if ($step_no == 1 && $xoops->getConfig('reg_dispdsclmr') != 0 && $xoops->getConfig('reg_disclaimer') != '') {
99
        $disc_tray = new Xoops\Form\ElementTray(XoopsLocale::DISCLAIMER, '<br />');
100
        $disc_text = new Xoops\Form\Label("", "<div class=\"pad5\">" . $myts->displayTarea($xoops->getConfig('reg_disclaimer'), 1) . "</div>");
101
        $disc_tray->addElement($disc_text);
102
        $agree_chk = new Xoops\Form\Checkbox('', 'agree_disc');
103
        $agree_chk->addOption(1, XoopsLocale::I_AGREE_TO_THE_ABOVE);
104
        $disc_tray->addElement($agree_chk);
105
        $reg_form->addElement($disc_tray);
106
    }
107
108
    if ($step_no == 1) {
109
        $reg_form->addElement(new Xoops\Form\Captcha(), true);
110
    }
111
112
    $reg_form->addElement(new Xoops\Form\Hidden('uid', $user->getVar('uid')));
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uid') can also be of type string[]; however, parameter $value of Xoops\Form\Hidden::__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

112
    $reg_form->addElement(new Xoops\Form\Hidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
113
    $reg_form->addElement(new Xoops\Form\Hidden('step', $step_no));
114
    $reg_form->addElement(new Xoops\Form\Button('', 'submitButton', XoopsLocale::A_SUBMIT, 'submit'));
115
    return $reg_form;
116
}
117
118
119
/**
120
 * Get {@link Xoops\Form\ThemeForm} for editing a user
121
 *
122
 * @param XoopsUser $user
123
 * @param ProfileProfile|null $profile
124
 * @param bool $action
125
 * @return Xoops\Form\ThemeForm
126
 */
127
function profile_getUserForm(XoopsUser $user, ProfileProfile $profile = null, $action = false)
128
{
129
    $xoops = Xoops::getInstance();
130
131
    if ($action === false) {
132
        $action = $_SERVER['REQUEST_URI'];
133
    }
134
135
    $title = $user->isNew() ? _PROFILE_AM_ADDUSER : XoopsLocale::EDIT_PROFILE;
136
137
    $form = new Xoops\Form\ThemeForm($title, 'userinfo', $action, 'post', true);
138
139
    /* @var $profile_handler ProfileProfileHandler */
140
    $profile_handler = \Xoops::getModuleHelper('profile')->getHandler('profile');
141
    // Dynamic fields
142
    if (!$profile) {
143
        $profile = $profile_handler->getProfile($user->getVar('uid'));
144
    }
145
    // Get fields
146
    $fields = $profile_handler->loadFields();
147
148
    // Get ids of fields that can be edited
149
    $gperm_handler = $xoops->getHandlerGroupPermission();
150
    $editable_fields = $gperm_handler->getItemIds('profile_edit', $xoops->user->getGroups(), $xoops->module->getVar('mid'));
0 ignored issues
show
Bug introduced by
The method getVar() does not exist on null. ( Ignorable by Annotation )

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

150
    $editable_fields = $gperm_handler->getItemIds('profile_edit', $xoops->user->getGroups(), $xoops->module->/** @scrutinizer ignore-call */ getVar('mid'));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
151
152
    if ($user->isNew() || $xoops->user->isAdmin()) {
153
        $elements[0][] = array(
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...
154
            'element' => new Xoops\Form\Text(XoopsLocale::USERNAME, 'uname', 40, $xoops->user->isAdmin() ? 60
155
                    : $xoops->getConfig('maxuname'), $user->getVar('uname', 'e')), 'required' => 1
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname', 'e') can also be of type string[]; however, parameter $value of Xoops\Form\Text::__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

155
                    : $xoops->getConfig('maxuname'), /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')), 'required' => 1
Loading history...
156
        );
157
        $email_text = new Xoops\Form\Text('', 'email', 40, 160, $user->getVar('email'));
158
    } else {
159
        $elements[0][] = array('element' => new Xoops\Form\Label(XoopsLocale::USERNAME, $user->getVar('uname')), 'required' => 0);
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname') can also be of type string[]; however, parameter $value of Xoops\Form\Label::__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

159
        $elements[0][] = array('element' => new Xoops\Form\Label(XoopsLocale::USERNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0);
Loading history...
160
        $email_text = new Xoops\Form\Label('', $user->getVar('email'));
161
    }
162
    $email_tray = new Xoops\Form\ElementTray(XoopsLocale::EMAIL, '<br />');
163
    $email_tray->addElement($email_text, ($user->isNew() || $xoops->user->isAdmin()) ? 1 : 0);
0 ignored issues
show
Bug introduced by
$user->isNew() || $xoops->user->isAdmin() ? 1 : 0 of type integer is incompatible with the type boolean expected by parameter $required of Xoops\Form\ElementTray::addElement(). ( Ignorable by Annotation )

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

163
    $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $xoops->user->isAdmin()) ? 1 : 0);
Loading history...
164
    $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...
165
    $elements[0][] = array('element' => $email_tray, 'required' => 0);
166
    $weights[0][] = 0;
167
168
    if ($xoops->user->isAdmin() && $user->getVar('uid') != $xoops->user->getVar('uid')) {
169
        //If the user is an admin and is editing someone else
170
        $pwd_text = new Xoops\Form\Password('', 'password');
171
        $pwd_text2 = new Xoops\Form\Password('', 'vpass');
172
        $pwd_tray = new Xoops\Form\ElementTray(XoopsLocale::PASSWORD . '<br />' . XoopsLocale::TYPE_NEW_PASSWORD_TWICE_TO_CHANGE_IT);
173
        $pwd_tray->addElement($pwd_text);
174
        $pwd_tray->addElement($pwd_text2);
175
        $elements[0][] = array('element' => $pwd_tray, 'required' => 0); //cannot set an element tray required
176
        $weights[0][] = 0;
177
178
        $level_radio = new Xoops\Form\Radio(_PROFILE_MA_USERLEVEL, 'level', $user->getVar('level'));
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('level') can also be of type string[]; however, parameter $value of Xoops\Form\Radio::__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

178
        $level_radio = new Xoops\Form\Radio(_PROFILE_MA_USERLEVEL, 'level', /** @scrutinizer ignore-type */ $user->getVar('level'));
Loading history...
179
        $level_radio->addOption(1, _PROFILE_MA_ACTIVE);
180
        $level_radio->addOption(0, _PROFILE_MA_INACTIVE);
181
        //$level_radio->addOption(-1, _PROFILE_MA_DISABLED);
182
        $elements[0][] = array('element' => $level_radio, 'required' => 0);
183
        $weights[0][] = 0;
184
    }
185
186
    $elements[0][] = array('element' => new Xoops\Form\Hidden('uid', $user->getVar('uid')), 'required' => 0);
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uid') can also be of type string[]; however, parameter $value of Xoops\Form\Hidden::__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

186
    $elements[0][] = array('element' => new Xoops\Form\Hidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0);
Loading history...
187
    $weights[0][] = 0;
188
    $elements[0][] = array('element' => new Xoops\Form\Hidden('op', 'save'), 'required' => 0);
189
    $weights[0][] = 0;
190
191
    $cat_handler = \Xoops::getModuleHelper('profile')->getHandler('category');
192
    $categories = array();
193
    $all_categories = $cat_handler->getObjects(null, true, false);
0 ignored issues
show
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. ( Ignorable by Annotation )

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

193
    /** @scrutinizer ignore-call */ 
194
    $all_categories = $cat_handler->getObjects(null, true, false);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

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

193
    /** @scrutinizer ignore-call */ 
194
    $all_categories = $cat_handler->getObjects(null, true, false);
Loading history...
194
    $count_fields = count($fields);
195
    /* @var ProfileField $field */
196
    foreach ($fields as $field) {
197
        if (in_array($field->getVar('field_id'), $editable_fields)) {
198
            // Set default value for user fields if available
199
            if ($user->isNew()) {
200
                $default = $field->getVar('field_default');
201
                if ($default !== '' && $default !== null) {
202
                    $user->setVar($field->getVar('field_name'), $default);
203
                }
204
            }
205
206
            if ($profile->getVar($field->getVar('field_name'), 'n') === null) {
207
                $default = $field->getVar('field_default', 'n');
208
                $profile->setVar($field->getVar('field_name'), $default);
209
            }
210
211
            $fieldinfo['element'] = $field->getEditElement($user, $profile);
212
            $fieldinfo['required'] = $field->getVar('field_required');
213
214
            $key = @$all_categories[$field->getVar('cat_id')]['cat_weight'] * $count_fields + $field->getVar('cat_id');
215
            $elements[$key][] = $fieldinfo;
216
            $weights[$key][] = $field->getVar('field_weight');
217
            $categories[$key] = @$all_categories[$field->getVar('cat_id')];
218
        }
219
    }
220
221
    if ($xoops->isUser() && $xoops->user->isAdmin()) {
222
        $xoops->loadLanguage('admin', 'profile');
223
        $gperm_handler = $xoops->getHandlerGroupPermission();
224
        //If user has admin rights on groups
225
        include_once $xoops->path('modules/system/constants.php');
226
        if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $xoops->user->getGroups(), 1)) {
227
            //add group selection
228
            $group_select = new Xoops\Form\SelectGroup(XoopsLocale::USER_GROUPS, 'groups', false, $user->getGroups(), 5, true);
229
            $elements[0][] = array('element' => $group_select, 'required' => 0);
230
            //set as latest;
231
            $weights[0][] = $count_fields + 1;
232
        }
233
    }
234
235
    ksort($elements);
236
    foreach (array_keys($elements) as $k) {
237
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
0 ignored issues
show
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

237
        array_multisort($weights[$k], /** @scrutinizer ignore-type */ SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
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

237
        array_multisort($weights[$k], SORT_ASC, /** @scrutinizer ignore-type */ array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
238
        $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
239
        $desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
240
        //$form->addElement(new Xoops\Form\Label("<div class='break'>{$title}</div>", $desc), false);
241
        $desc = ($desc != '' ? ' - ' . $desc : '');
242
        $form->insertBreak($title . $desc);
243
        foreach (array_keys($elements[$k]) as $i) {
244
            $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
245
        }
246
    }
247
248
    $form->addElement(new Xoops\Form\Hidden('uid', $user->getVar('uid')));
249
    $form->addElement(new Xoops\Form\Button('', 'submit', XoopsLocale::SAVE_CHANGES, 'submit'));
250
    return $form;
251
}
252