Passed
Push — master ( d3e687...4990f6 )
by Michael
02:43
created

User.php ➔ newbb_calculateLevel()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 72
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 54
nc 1024
nop 2
dl 0
loc 72
rs 3.5064
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 namespace XoopsModules\Newbb;
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 22 and the first side effect is on line 15.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * NewBB 5.0x,  the forum module for XOOPS project
5
 *
6
 * @copyright      XOOPS Project (https://xoops.org)
7
 * @license        GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
8
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
9
 * @since          4.00
10
 * @package        module::newbb
11
 */
12
13
// defined('XOOPS_ROOT_PATH') || die('Restricted access');
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
14
15
defined('NEWBB_FUNCTIONS_INI') || include $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
16
17
/**
18
 * @param $RPG
19
 * @param $RPGDIFF
20
 * @return array|number
21
 */
22
function newbb_calculateLevel($RPG, $RPGDIFF)
23
{
24
25
    //$RPG = $user->getVar('posts');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
26
    //$RPGDIFF = $user->getVar('user_regdate');
0 ignored issues
show
Unused Code Comprehensibility introduced by
64% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
27
28
    $today = time();
29
    $diff  = $today - $RPGDIFF;
30
    $exp   = round($diff / 86400, 0);
31
    if ($exp <= 0) {
32
        $exp = 1;
33
    }
34
    $ppd       = round($RPG / $exp, 0);
35
    $level     = pow(log10($RPG), 3);
36
    $ep        = floor(100 * ($level - floor($level)));
37
    $showlevel = floor($level + 1);
38
    $hpmulti   = round($ppd / 6, 1);
39
    if ($hpmulti > 1.5) {
40
        $hpmulti = 1.5;
41
    }
42
    if ($hpmulti < 1) {
43
        $hpmulti = 1;
44
    }
45
    $maxhp = $level * 25 * $hpmulti;
46
    $hp    = $ppd / 5;
47
    if ($hp >= 1) {
48
        $hp = $maxhp;
49
    } else {
50
        $hp = floor($hp * $maxhp);
51
    }
52
    $hp    = floor($hp);
53
    $maxhp = floor($maxhp);
54
    $zhp   = $maxhp;
55
    if ($maxhp <= 0) {
56
        $zhp = 1;
57
    }
58
    $hpf   = floor(100 * ($hp / $zhp)) - 1;
59
    $maxmp = ($exp * $level) / 5;
60
    $mp    = $RPG / 3;
61
    if ($mp >= $maxmp) {
62
        $mp = $maxmp;
63
    }
64
    $maxmp = floor($maxmp);
65
    $mp    = floor($mp);
66
    $zmp   = $maxmp;
67
    if ($maxmp <= 0) {
68
        $zmp = 1;
69
    }
70
    $mpf = floor(100 * ($mp / $zmp)) - 1;
71
    if ($hpf >= 98) {
72
        $hpf -= 2;
73
    }
74
    if ($ep >= 98) {
75
        $ep -= 2;
76
    }
77
    if ($mpf >= 98) {
78
        $mpf -= 2;
79
    }
80
81
    $level              = [];
82
    $level['level']     = $showlevel;
83
    $level['exp']       = $ep;
84
    $level['exp_width'] = $ep . '%';
85
    $level['hp']        = $hp;
86
    $level['hp_max']    = $maxhp;
87
    $level['hp_width']  = $hpf . '%';
88
    $level['mp']        = $mp;
89
    $level['mp_max']    = $maxmp;
90
    $level['mp_width']  = $mpf . '%';
91
92
    return $level;
93
}
94
95
/**
96
 * Class User
97
 */
98
class User
99
{
100
    public $user;
101
102
    public function __construct()
103
    {
104
    }
105
106
    /**
107
     * @return array
108
     */
109
    public function getUserbar()
110
    {
111
        global $isAdmin;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
112
113
        $userbar = [];
114
        if (empty($GLOBALS['xoopsModuleConfig']['userbar_enabled'])) {
115
            return $userbar;
116
        }
117
118
        $user               = $this->user;
119
        $userbar['profile'] = [
120
            'link' => XOOPS_URL . '/userinfo.php?uid=' . $user->getVar('uid'),
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
121
            'name' => _PROFILE
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\_PROFILE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
122
        ];
123
124
        if (is_object($GLOBALS['xoopsUser'])) {
125
            $userbar['pm'] = [
126
                'link' => "javascript:void openWithSelfMain('" . XOOPS_URL . '/pmlite.php?send2=1&amp;to_userid=' . $user->getVar('uid') . "', 'pmlite', 450, 380);",
127
                'name' => _MD_NEWBB_PM
128
            ];
129
        }
130
        if ($user->getVar('user_viewemail') || $isAdmin) {
131
            $userbar['email'] = [
132
                'link' => "javascript:void window.open('mailto:" . $user->getVar('email') . "', 'new');",
133
                'name' => _MD_NEWBB_EMAIL
134
            ];
135
        }
136
        if ($url = $user->getVar('url')) {
137
            $userbar['url'] = [
138
                'link' => "javascript:void window.open('" . $url . "', 'new');",
139
                'name' => _MD_NEWBB_WWW
140
            ];
141
        }
142
        if ($icq = $user->getVar('user_icq')) {
143
            $userbar['icq'] = [
144
                'link' => "javascript:void window.open('http://wwp.icq.com/scripts/search.dll?to=" . $icq . "', 'new');",
145
                'name' => _MD_NEWBB_ICQ
146
            ];
147
        }
148
        if ($aim = $user->getVar('user_aim')) {
149
            $userbar['aim'] = [
150
                'link' => "javascript:void window.open('aim:goim?screenname=" . $aim . '&amp;message=Hi+' . $aim . '+Are+you+there?' . "', 'new');",
151
                'name' => _MD_NEWBB_AIM
152
            ];
153
        }
154
        if ($yim = $user->getVar('user_yim')) {
155
            $userbar['yim'] = [
156
                'link' => "javascript:void window.open('http://edit.yahoo.com/config/send_webmesg?.target=" . $yim . '&.src=pg' . "', 'new');",
157
                'name' => _MD_NEWBB_YIM
158
            ];
159
        }
160
        if ($msn = $user->getVar('user_msnm')) {
161
            $userbar['msnm'] = [
162
                'link' => "javascript:void window.open('http://members.msn.com?mem=" . $msn . "', 'new');",
163
                'name' => _MD_NEWBB_MSNM
164
            ];
165
        }
166
167
        return $userbar;
168
    }
169
170
    /**
171
     * @return string
172
     */
173
    public function getLevel()
174
    {
175
        global $forumUrl;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
176
177
        $level = newbb_calculateLevel($this->user->getVar('posts'), $this->user->getVar('user_regdate'));
178
        $info  = '';
179
        if (2 == $GLOBALS['xoopsModuleConfig']['user_level']) {
180
            static $rpg_images;
181
            if (!isset($rpg_images)) {
182
                $iconHandler = newbbGetIconHandler();
183
                $rpg_path    = $iconHandler->getPath('rpg');
184
                foreach (['img_left', 'img_backing', 'img_right', 'blue', 'green', 'orange'] as $img) {
185
                    // irmtfan fix: double "/" removed
186
                    $rpg_images[$img] = XOOPS_URL . $rpg_path . '/' . $img . '.gif';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
187
                }
188
            }
189
            // irmtfan hardcore removed align="left"
190
            $table = "<table class='userlevel'><tr><td class='end'><img src='"
191
                     . $rpg_images['img_left']
192
                     . "' alt='' /></td><td class='center' background='"
193
                     . $rpg_images['img_backing']
194
                     . "'><img src='%s' width='%d' alt='' class='icon_left' /></td><td><img src='"
195
                     . $rpg_images['img_right']
196
                     . "' alt='' /></td></tr></table>";
197
198
            $info = _MD_NEWBB_LEVEL . ' ' . $level['level'] . '<br><span title="' . _MD_NEWBB_HP_DESC . '">' . _MD_NEWBB_HP . ' ' . $level['hp'] . ' / ' . $level['hp_max'] . '</span><br>' . sprintf($table, $rpg_images['orange'], $level['hp_width']);
199
            $info .= '<span title="' . _MD_NEWBB_MP_DESC . '">' . _MD_NEWBB_MP . ' ' . $level['mp'] . ' / ' . $level['mp_max'] . '</span><br>' . sprintf($table, $rpg_images['green'], $level['mp_width']);
200
            $info .= '<span title="' . _MD_NEWBB_EXP_DESC . '">' . _MD_NEWBB_EXP . ' ' . $level['exp'] . '</span><br>' . sprintf($table, $rpg_images['blue'], $level['exp_width']);
201
        } else {
202
            $info = _MD_NEWBB_LEVEL . ' ' . $level['level'] . '; <span title="' . _MD_NEWBB_EXP_DESC . '">' . _MD_NEWBB_EXP . ' ' . $level['exp'] . '</span><br>';
203
            $info .= '<span title="' . _MD_NEWBB_HP_DESC . '">' . _MD_NEWBB_HP . ' ' . $level['hp'] . ' / ' . $level['hp_max'] . '</span><br>';
204
            $info .= '<span title="' . _MD_NEWBB_MP_DESC . '">' . _MD_NEWBB_MP . ' ' . $level['mp'] . ' / ' . $level['mp_max'] . '</span>';
205
        }
206
207
        return $info;
208
    }
209
210
    /**
211
     * @param \XoopsUser $user
0 ignored issues
show
Bug introduced by
The type XoopsUser was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
212
     * @return mixed
213
     */
214
    public function getInfo(&$user)
215
    {
216
        global $myts;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
217
        static $name_anonymous;
218
219
        if (!is_object($user) || !$user->isActive()) {
220
            if (!isset($name_anonymous)) {
221
                $name_anonymous = $myts->htmlSpecialChars($GLOBALS['xoopsConfig']['anonymous']);
222
            }
223
224
            return ['name' => $name_anonymous, 'link' => $name_anonymous];
225
        }
226
227
        $this->user = $user;
228
229
        $userinfo['uid'] = $user->getVar('uid');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$userinfo was never initialized. Although not strictly required by PHP, it is generally a good practice to add $userinfo = array(); before regardless.
Loading history...
230
231
        $name             = empty($GLOBALS['xoopsModuleConfig']['show_realname']) ? $user->getVar('uname') : $user->getVar('name');
232
        $userinfo['name'] = $name ?: $user->getVar('uname');
233
234
        $userinfo['link'] = '<a href=\'' . XOOPS_URL . '/userinfo.php?uid=' . $user->getVar('uid') . '\'>' . $userinfo['name'] . '</a>';
0 ignored issues
show
Bug introduced by
The constant XoopsModules\Newbb\XOOPS_URL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
235
236
        $userinfo['avatar'] = $user->getVar('user_avatar');
237
        // START hacked by irmtfan - easier rank getting - consistency with previous version back rank.title and rank.image
238
        $userrank         = $user->rank();
239
        $userinfo['rank'] = [];
240
        if (isset($userrank['image']) && $userrank['image']) {
241
            $userinfo['rank']['image'] = $userrank['image'];
242
            $userinfo['rank']['title'] = $userrank['title'];
243
        }
244
        // END hacked by irmtfan - easier rank getting  - a little correctness dot removed
245
        // START hacked by irmtfan - easier groups getting - can we use $_SESSION['xoopsUserGroups']???
246
        //checks for user's groups
247
        $userinfo['groups'] = [];
248
        /** @var \XoopsMemberHandler $memberHandler */
249
        $memberHandler = xoops_getHandler('member');
0 ignored issues
show
Bug introduced by
The function xoops_getHandler was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

249
        $memberHandler = /** @scrutinizer ignore-call */ xoops_getHandler('member');
Loading history...
250
        $usergroups    = $memberHandler->getGroupsByUser($userinfo['uid'], true);
251
        foreach ($usergroups as $group) {
252
            $userinfo['groups'][] = $group->getVar('name');
253
        }
254
        // END hacked by irmtfan - easier groups getting - can we use $_SESSION['xoopsUserGroups']???
255
        $userinfo['from'] = $user->getVar('user_from');
256
257
        include_once __DIR__ . '/../include/functions.time.php';
258
        $userinfo['regdate']    = newbbFormatTimestamp($user->getVar('user_regdate'), 'reg');
259
        $userinfo['last_login'] = newbbFormatTimestamp($user->getVar('last_login')); // irmtfan add last_login
260
261
        $userinfo['posts'] = $user->getVar('posts');
262
263
        if (!empty($GLOBALS['xoopsModuleConfig']['user_level'])) {
264
            $userinfo['level'] = $this->getLevel();
265
        }
266
267
        if (!empty($GLOBALS['xoopsModuleConfig']['userbar_enabled'])) {
268
            $userinfo['userbar'] = $this->getUserbar();
269
        }
270
271
        $userinfo['signature'] = $user->getVar('user_sig');
272
273
        return $userinfo;
274
    }
275
}
276