RegisterForm::__construct()   F
last analyzed

Complexity

Conditions 14
Paths 768

Size

Total Lines 84
Code Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 60
c 1
b 0
f 0
dl 0
loc 84
rs 2.4222
cc 14
nc 768
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 XoopsThemeForm;
9
use XoopsFormButton;
10
use XoopsFormHidden;
11
use XoopsFormLabel;
12
use XoopsModules\Suico\{
13
    Helper
14
};
15
/** @var Helper $helper */
16
17
/**
18
 * Get {@link XoopsThemeForm} for registering new users
19
 *
20
 * @param           $profile
21
 * @param XoopsUser $user {@link XoopsUser} to register
22
 * @param int       $step Which step we are at
23
 *
24
 * @return object
25
 * @internal param \profileRegstep $next_step
26
 */
27
class RegisterForm extends XoopsThemeForm
28
{
29
    /**
30
     * RegisterForm constructor.
31
     * @param \XoopsUser $user
32
     * @param            $profile
33
     * @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...
34
     */
35
    public function __construct(\XoopsUser $user, $profile, $step = null)
36
    {
37
        global $opkey; // should be set in register.php
38
        if (empty($opkey)) {
39
            $opkey = 'profile_opname';
40
        }
41
        $next_opname      = 'op' . \mt_rand(10000, 99999);
42
        $_SESSION[$opkey] = $next_opname;
43
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
44
        if (empty($GLOBALS['xoopsConfigUser'])) {
45
            /* @var \XoopsConfigHandler $configHandler */
46
            $configHandler              = \xoops_getHandler('config');
47
            $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(\XOOPS_CONF_USER);
48
        }
49
        $action    = $_SERVER['REQUEST_URI'];
50
        $step_no   = $step['step_no'];
51
        $use_token = $step['step_no'] > 0; // ? true : false;
52
        parent::__construct($step['step_name'], 'regform', $action, 'post', $use_token);
53
        if ($step['step_desc']) {
54
            $this->addElement(new XoopsFormLabel('', $step['step_desc']));
55
        }
56
        if (1 == $step_no) {
57
            //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
58
            $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...
59
                'element'  => new \XoopsFormText(\_MD_SUICO_NICKNAME, 'uname', 35, $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

59
                'element'  => new \XoopsFormText(\_MD_SUICO_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
60
                'required' => true,
61
            ];
62
            $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...
63
            $elements[0][] = ['element' => new \XoopsFormText(\_MD_SUICO_EMAILADDRESS, 'email', 35, 255, $user->getVar('email', 'e')), 'required' => true];
64
            $weights[0][]  = 0;
65
            $elements[0][] = ['element' => new \XoopsFormPassword(\_MD_SUICO_PASSWORD, 'pass', 35, 32, ''), 'required' => true];
66
            $weights[0][]  = 0;
67
            $elements[0][] = ['element' => new \XoopsFormPassword(\_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true];
68
            $weights[0][]  = 0;
69
        }
70
        // Dynamic fields
71
        $profileHandler               = Helper::getInstance()->getHandler('Profile');
72
        $fields                       = $profileHandler->loadFields();
73
        $_SESSION['profile_required'] = [];
74
        foreach (\array_keys($fields) as $i) {
75
            if ($fields[$i]->getVar('step_id') == $step['step_id']) {
76
                $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
77
                //assign and check (=)
78
                if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
79
                    $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
80
                }
81
                $key              = $fields[$i]->getVar('cat_id');
82
                $elements[$key][] = $fieldinfo;
83
                $weights[$key][]  = $fields[$i]->getVar('field_weight');
84
            }
85
        }
86
        \ksort($elements);
87
        // Get categories
88
        $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
89
        $categories      = $categoryHandler->getObjects(null, true, false);
0 ignored issues
show
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
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
Comprehensibility Best Practice introduced by
The variable $weights does not seem to be defined for all execution paths leading up to this point.
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

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
            //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT;
93
            //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
94
            //$this->insertBreak("<p>{$title}</p>{$desc}");
95
            //$this->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
96
            foreach (\array_keys($elements[$k]) as $i) {
97
                $this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
98
            }
99
        }
100
        //end of Dynamic User fields
101
        if (1 == $step_no && 0 != $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] && '' != $GLOBALS['xoopsConfigUser']['reg_disclaimer']) {
102
            $disc_tray = new \XoopsFormElementTray(\_US_DISCLAIMER, '<br>');
103
            $disc_text = new \XoopsFormLabel('', '<div class="pad5">' . $GLOBALS['myts']->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . '</div>');
104
            $disc_tray->addElement($disc_text);
105
            $agree_chk = new \XoopsFormCheckBox('', 'agree_disc');
106
            $agree_chk->addOption(1, \_US_IAGREE);
107
            $disc_tray->addElement($agree_chk);
108
            $this->addElement($disc_tray);
109
        }
110
        global $xoopsModuleConfig;
111
        $useCaptchaAfterStep2 = $xoopsModuleConfig['profileCaptchaAfterStep1'] + 1;
112
        if ($step_no <= $useCaptchaAfterStep2) {
113
            $this->addElement(new \XoopsFormCaptcha(), true);
114
        }
115
        $this->addElement(new XoopsFormHidden($next_opname, 'register'));
116
        $this->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
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

116
        $this->addElement(new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
117
        $this->addElement(new XoopsFormHidden('step', $step_no));
118
        $this->addElement(new XoopsFormButton('', 'submitButton', \_SUBMIT, 'submit'));
119
    }
120
}
121