Passed
Push — master ( 95e0c4...fc8ab0 )
by
unknown
44s queued 15s
created

RegisterForm::__construct()   F

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
namespace XoopsModules\Suico\Form;
4
5
use XoopsModules\Suico;
6
use XoopsThemeForm;
7
use XoopsFormButton;
8
use XoopsFormHidden;
9
use XoopsFormLabel;
10
11
12
/**
13
 * Get {@link XoopsThemeForm} for registering new users
14
 *
15
 * @param           $profile
16
 * @param XoopsUser $user {@link XoopsUser} to register
17
 * @param int       $step Which step we are at
18
 *
19
 * @return object
20
 * @internal param \profileRegstep $next_step
21
 */
22
23
class RegisterForm extends XoopsThemeForm
24
{
25
26
    function __construct(\XoopsUser $user, $profile, $step = null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        global $opkey; // should be set in register.php
29
        if (empty($opkey)) {
30
            $opkey = 'profile_opname';
31
        }
32
        $next_opname      = 'op' . mt_rand(10000, 99999);
33
        $_SESSION[$opkey] = $next_opname;
34
        include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
35
        if (empty($GLOBALS['xoopsConfigUser'])) {
36
            /* @var \XoopsConfigHandler $configHandler */
37
            $configHandler              = xoops_getHandler('config');
38
            $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
39
        }
40
        $action    = $_SERVER['REQUEST_URI'];
41
        $step_no   = $step['step_no'];
42
        $use_token = $step['step_no'] > 0; // ? true : false;
43
        parent::__construct($step['step_name'], 'regform', $action, 'post', $use_token);
44
        if ($step['step_desc']) {
45
            $this->addElement(new XoopsFormLabel('', $step['step_desc']));
46
        }
47
        if (1 == $step_no) {
48
            //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
49
            $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...
50
                '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

50
                'element'  => new \XoopsFormText(_MD_SUICO_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
51
                'required' => true,
52
            ];
53
            $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...
54
            $elements[0][] = ['element' => new \XoopsFormText(_MD_SUICO_EMAILADDRESS, 'email', 35, 255, $user->getVar('email', 'e')), 'required' => true];
55
            $weights[0][]  = 0;
56
            $elements[0][] = ['element' => new \XoopsFormPassword(_MD_SUICO_PASSWORD, 'pass', 35, 32, ''), 'required' => true];
57
            $weights[0][]  = 0;
58
            $elements[0][] = ['element' => new \XoopsFormPassword(_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true];
59
            $weights[0][]  = 0;
60
        }
61
        // Dynamic fields
62
        $profileHandler               = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
63
        $fields                       = $profileHandler->loadFields();
64
        $_SESSION['profile_required'] = [];
65
        foreach (array_keys($fields) as $i) {
66
            if ($fields[$i]->getVar('step_id') == $step['step_id']) {
67
                $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
68
                //assign and check (=)
69
                if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
70
                    $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
71
                }
72
                $key              = $fields[$i]->getVar('cat_id');
73
                $elements[$key][] = $fieldinfo;
74
                $weights[$key][]  = $fields[$i]->getVar('field_weight');
75
            }
76
        }
77
        ksort($elements);
78
        // Get categories
79
        $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
80
        $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...
81
        foreach (array_keys($elements) as $k) {
82
            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...
83
            //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT;
84
            //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
85
            //$this->insertBreak("<p>{$title}</p>{$desc}");
86
            //$this->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
87
            foreach (array_keys($elements[$k]) as $i) {
88
                $this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
89
            }
90
        }
91
        //end of Dynamic User fields
92
        if (1 == $step_no && 0 != $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] && '' != $GLOBALS['xoopsConfigUser']['reg_disclaimer']) {
93
            $disc_tray = new \XoopsFormElementTray(_US_DISCLAIMER, '<br>');
94
            $disc_text = new \XoopsFormLabel('', '<div class="pad5">' . $GLOBALS['myts']->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . '</div>');
95
            $disc_tray->addElement($disc_text);
96
            $agree_chk = new \XoopsFormCheckBox('', 'agree_disc');
97
            $agree_chk->addOption(1, _US_IAGREE);
98
            $disc_tray->addElement($agree_chk);
99
            $this->addElement($disc_tray);
100
        }
101
        global $xoopsModuleConfig;
102
        $useCaptchaAfterStep2 = $xoopsModuleConfig['profileCaptchaAfterStep1'] + 1;
103
        if ($step_no <= $useCaptchaAfterStep2) {
104
            $this->addElement(new \XoopsFormCaptcha(), true);
105
        }
106
        $this->addElement(new XoopsFormHidden($next_opname, 'register'));
107
        $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

107
        $this->addElement(new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
108
        $this->addElement(new XoopsFormHidden('step', $step_no));
109
        $this->addElement(new XoopsFormButton('', 'submitButton', _SUBMIT, 'submit'));
110
    }
111
}
112