Completed
Push — next ( 5f2bc0...cef70f )
by Thomas
25s queued 12s
created

mydetails.php ➔ change()   F

Complexity

Conditions 34
Paths > 20000

Size

Total Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 34
nc 9331200
nop 0
dl 0
loc 54
rs 0
c 0
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
 * for license information see LICENSE.md
4
 ***************************************************************************/
5
6
require __DIR__ . '/lib2/web.inc.php';
7
require_once __DIR__ . '/lib2/edithelper.inc.php';
8
9
$tpl->name = 'mydetails';
10
$tpl->menuitem = MNU_MYPROFILE_DETAILS;
11
12
$login->verify();
13
if ($login->userid == 0) {
14
    $tpl->redirect_login();
15
}
16
17
if (isset($_REQUEST['cancel'])) {
18
    $tpl->redirect('mydetails.php');
19
}
20
21
$action = isset($_REQUEST['action']) ? mb_strtolower($_REQUEST['action']) : 'view';
22
if ($action != 'change' && $action != 'changetext' && $action != 'view') {
23
    $action = 'view';
24
}
25
26
if ($action == 'change') {
27
    change();
28
} elseif ($action == 'changetext') {
29
    changetext();
30
} else {
31
    display();
32
}
33
34
exit;
35
36
37
function change()
38
{
39
    global $tpl, $login, $opt;
40
41
    $useroptions = new useroptions($login->userid);
42
43
    if (isset($_REQUEST['save'])) {
44
        $rs = sql('SELECT `id` FROM `profile_options` WHERE `optionset`=1 ORDER BY `id`');
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
45
        $bError = false;
46
        $error = ': ';
47
        $errorlen = ': ';
48
        $bErrorlen = false;
49
50
        while ($record = sql_fetch_array($rs)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_fetch_array() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
Documentation introduced by
$rs is of type object<mysqli_result>, but the function expects a resource.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
            $id = $record['id'];
52
            $vis = isset($_REQUEST['chk' . $id]) ? $_REQUEST['chk' . $id] + 0 : 0;
53
            $value = isset($_REQUEST['inp' . $id]) ? $_REQUEST['inp' . $id] : '';
54
            if ($vis != 1) {
55
                $vis = 0;
56
            }
57
58
            $useroptions->setOptVisible($id, $vis);
59
            if (strlen($value) > 2000) {
60
                $errorlen .= $useroptions->getOptName($id);
61
                $bErrorlen = true;
62
            } else {
63
                if (!$useroptions->setOptValue($id, $value)) {
64
                    $error .= $useroptions->getOptName($id) . ', ';
65
                    $bError = true;
66
                }
67
            }
68
        }
69
70
        sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
71
72
        $error = substr($error, 0, - 2);
73
74
        $tpl->assign('error', $bError);
75
        $tpl->assign('errormsg', $error);
76
        $tpl->assign('errorlen', $bErrorlen);
77
        $tpl->assign('errormsglen', $errorlen);
78
79
        if (!$useroptions->save()) {
80
            $bError = true;
0 ignored issues
show
Unused Code introduced by
$bError is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
81
            $tpl->assign('errorUnknown', true);
82
        } elseif (!$bError) {
83
            $tpl->redirect('mydetails.php');
84
        }
85
    }
86
87
    assignFromDB($login->userid, false);
88
    $tpl->assign('edit', true);
89
    $tpl->display();
90
}
91
92
93
function changetext()
94
{
95
    global $tpl, $login, $opt;
96
97
    if (isset($_REQUEST['save'])) {
98
        $purifier = new OcHTMLPurifier($opt);
99
        $desctext = isset($_REQUEST['desctext']) ? $purifier->purify($_REQUEST['desctext']) : "";
100
        $desc_htmledit = isset($_REQUEST['descMode']) && $_REQUEST['descMode'] == '2' ? '0' : '1';
101
        sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
102
            "
103
            UPDATE `user`
104
            SET `description`='&2', `desc_htmledit`='&3'
105
            WHERE `user_id`='&1'",
106
            $login->userid,
107
            $desctext,
108
            $desc_htmledit
109
        );
110
        $tpl->redirect('mydetails.php');
111
    } else {
112
        $tpl->name = 'mydescription';
113
        assignFromDB($login->userid, true);
114
        $tpl->assign('scrollposx', isset($_REQUEST['scrollposx']) ? $_REQUEST['scrollposx'] + 0 : 0);
115
        $tpl->assign('scrollposy', isset($_REQUEST['scrollposy']) ? $_REQUEST['scrollposy'] + 0 : 0);
116
        $tpl->acceptsAndPurifiesHtmlInput();
117
        $tpl->display();
118
    }
119
}
120
121
122
function display()
123
{
124
    global $tpl, $login;
125
    assignFromDB($login->userid, false);
126
    $tpl->display();
127
}
128
129
130
/**
131
 * @param boolean $include_editor
132
 */
133
function assignFromDB($userid, $include_editor)
134
{
135
    global $tpl, $opt, $smilies, $_REQUEST;
136
137
    $rs = sql(
0 ignored issues
show
Deprecated Code introduced by
The function sql() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
138
        "SELECT `p`.`id`,
139
                IFNULL(`tt`.`text`, `p`.`name`) AS `name`,
140
                `p`.`default_value`,
141
                `p`.`check_regex`,
142
                `p`.`option_order`,
143
                `u`.`option_visible`,
144
                `p`.`internal_use`,
145
                `p`.`option_input`,
146
                IFNULL(`u`.`option_value`,
147
                `p`.`default_value`) AS `option_value`
148
         FROM `profile_options` AS `p`
149
         LEFT JOIN `user_options` AS `u`
150
           ON `p`.`id`=`u`.`option_id`
151
           AND (`u`.`user_id` IS NULL OR `u`.`user_id`='&1')
152
         LEFT JOIN `sys_trans` AS `st`
153
           ON `p`.`trans_id`=`st`.`id`
154
           AND `p`.`name`=`st`.`text`
155
         LEFT JOIN `sys_trans_text` AS `tt`
156
           ON `st`.`id`=`tt`.`trans_id`
157
           AND `tt`.`lang`='&2'
158
         WHERE `optionset`=1
159
         ORDER BY `p`.`internal_use` DESC, `p`.`option_order`",
160
        $userid + 0,
161
        $opt['template']['locale']
162
    );
163
    $tpl->assign_rs('useroptions', $rs);
164
    sql_free_result($rs);
0 ignored issues
show
Deprecated Code introduced by
The function sql_free_result() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
165
166
    if (isset($_REQUEST['desctext'])) {
167
        $tpl->assign('desctext', $_REQUEST['desctext']);
168
    } else {
169
        $tpl->assign(
170
            'desctext',
171
            sql_value(
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
172
                "SELECT `description` FROM `user` WHERE `user_id`='&1'",
173
                '',
174
                $userid + 0
175
            )
176
        );
177
    }
178
179
    // Use the same descMode values here like in log and cacheDesc editor:
180
    if ($include_editor) {
181
        if (isset($_REQUEST['descMode'])) {
182
            $descMode = min(3, max(2, $_REQUEST['descMode'] + 0));
183
        } else {
184
            $descMode = 2;
185
            if (sql_value("SELECT `desc_htmledit` FROM `user` WHERE `user_id`='&1'", 0, $userid + 0)) {
0 ignored issues
show
Deprecated Code introduced by
The function sql_value() has been deprecated with message: use DBAL Conenction instead. See adminreports.php for an example implementation

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
186
                $descMode = 3;
187
            }
188
        }
189 View Code Duplication
        if ($descMode == 3) {
190
            $tpl->add_header_javascript('resource2/tinymce/tiny_mce_gzip.js');
191
            $tpl->add_header_javascript(
192
                'resource2/tinymce/config/user.js.php?lang=' . strtolower($opt['template']['locale'])
193
            );
194
        }
195
        $tpl->add_header_javascript(editorJsPath());
196
        $tpl->assign('descMode', $descMode);
197
    }
198
}
199