newbb_calculateLevel()   F
last analyzed

Complexity

Conditions 11
Paths 1024

Size

Total Lines 70
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 53
nc 1024
nop 2
dl 0
loc 70
rs 3.5454
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 declare(strict_types=1);
2
3
namespace XoopsModules\Newbb;
4
5
/**
6
 * NewBB,  the forum module for XOOPS project
7
 *
8
 * @copyright      XOOPS Project (https://xoops.org)
9
 * @license        GNU GPL 2.0 or later (https://www.gnu.org/licenses/gpl-2.0.html)
10
 * @author         Taiwen Jiang (phppp or D.J.) <[email protected]>
11
 * @since          4.00
12
 */
13
\defined('NEWBB_FUNCTIONS_INI') || require $GLOBALS['xoops']->path('modules/newbb/include/functions.ini.php');
14
15
/**
16
 * @param int $RPG
17
 * @param int $RPGDIFF
18
 *
19
 * @return (float|string)[]
20
 *
21
 * @psalm-return array{level: float, exp: float, exp_width: string, hp: float, hp_max: float, hp_width: string, mp: float, mp_max: float, mp_width: string}
22
 */
23
function newbb_calculateLevel(int $RPG, int $RPGDIFF): array
24
{
25
    //$RPG = $user->getVar('posts');
26
    //$RPGDIFF = $user->getVar('user_regdate');
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     = \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 \XoopsUser $user;
101
102
    public function __construct()
103
    {
104
    }
105
106
    /**
107
     * @return (mixed|string)[][]
108
     *
109
     * @psalm-return array{profile?: array{link: string, name: mixed}, pm?: array{link: string, name: mixed}, email?: array{link: string, name: mixed}, url?: array{link: string, name: mixed}, icq?: array{link: string, name: mixed}, aim?: array{link: string, name: mixed}, yim?: array{link: string, name: mixed}, msnm?: array{link: string, name: mixed}}
110
     */
111
    public function getUserbar(): array
112
    {
113
        global $isAdmin;
114
115
        $userbar = [];
116
        if (empty($GLOBALS['xoopsModuleConfig']['userbar_enabled'])) {
117
            return $userbar;
118
        }
119
120
        $user               = $this->user;
121
        $userbar['profile'] = [
122
            'link' => XOOPS_URL . '/userinfo.php?uid=' . $user->getVar('uid'),
123
            'name' => _PROFILE,
124
        ];
125
126
        if (\is_object($GLOBALS['xoopsUser'])) {
127
            $userbar['pm'] = [
128
                'link' => "javascript:void openWithSelfMain('" . XOOPS_URL . '/pmlite.php?send2=1&amp;to_userid=' . $user->getVar('uid') . "', 'pmlite', 450, 380);",
129
                'name' => \_MD_NEWBB_PM,
130
            ];
131
        }
132
        if ($user->getVar('user_viewemail') || $isAdmin) {
133
            $userbar['email'] = [
134
                'link' => "javascript:void window.open('mailto:" . $user->getVar('email') . "', 'new');",
135
                'name' => \_MD_NEWBB_EMAIL,
136
            ];
137
        }
138
        $url = $user->getVar('url');
139
        if ($url) {
140
            $userbar['url'] = [
141
                'link' => "javascript:void window.open('" . $url . "', 'new');",
142
                'name' => \_MD_NEWBB_WWW,
143
            ];
144
        }
145
        $icq = $user->getVar('user_icq');
146
        if ($icq) {
147
            $userbar['icq'] = [
148
                'link' => "javascript:void window.open('https://wwp.icq.com/scripts/search.dll?to=" . $icq . "', 'new');",
149
                'name' => \_MD_NEWBB_ICQ,
150
            ];
151
        }
152
        $aim = $user->getVar('user_aim');
153
        if ($aim) {
154
            $userbar['aim'] = [
155
                'link' => "javascript:void window.open('aim:goim?screenname=" . $aim . '&amp;message=Hi+' . $aim . '+Are+you+there?' . "', 'new');",
156
                'name' => \_MD_NEWBB_AIM,
157
            ];
158
        }
159
        $yim = $user->getVar('user_yim');
160
        if ($yim) {
161
            $userbar['yim'] = [
162
                'link' => "javascript:void window.open('https://edit.yahoo.com/config/send_webmesg?.target=" . $yim . '&.src=pg' . "', 'new');",
163
                'name' => \_MD_NEWBB_YIM,
164
            ];
165
        }
166
        $msn = $user->getVar('user_msnm');
167
        if ($msn) {
168
            $userbar['msnm'] = [
169
                'link' => "javascript:void window.open('https://members.msn.com?mem=" . $msn . "', 'new');",
170
                'name' => \_MD_NEWBB_MSNM,
171
            ];
172
        }
173
174
        return $userbar;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getLevel(): string
181
    {
182
        global $forumUrl;
183
184
        $level = newbb_calculateLevel($this->user->getVar('posts'), $this->user->getVar('user_regdate'));
185
        $info  = '';
186
        if (2 == $GLOBALS['xoopsModuleConfig']['user_level']) {
187
            static $rpg_images;
188
            if (null === $rpg_images) {
189
                $iconHandler = \newbbGetIconHandler();
190
                $rpg_path    = $iconHandler->getPath('rpg');
191
                foreach (['img_left', 'img_backing', 'img_right', 'blue', 'green', 'orange'] as $img) {
192
                    // irmtfan fix: double "/" removed
193
                    $rpg_images[$img] = XOOPS_URL . $rpg_path . '/' . $img . '.gif';
194
                }
195
            }
196
            // irmtfan hardcore removed align="left"
197
            $table = "<table class='userlevel'><tr><td class='end'><img src='"
198
                     . $rpg_images['img_left']
199
                     . "' alt='' ></td><td class='center' background='"
200
                     . $rpg_images['img_backing']
201
                     . "'><img src='%s' width='%d' alt='' class='icon_left' ></td><td><img src='"
202
                     . $rpg_images['img_right']
203
                     . "' alt='' ></td></tr></table>";
204
205
            $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']);
206
            $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']);
207
            $info .= '<span title="' . \_MD_NEWBB_EXP_DESC . '">' . \_MD_NEWBB_EXP . ' ' . $level['exp'] . '</span><br>' . \sprintf($table, $rpg_images['blue'], $level['exp_width']);
208
        } else {
209
            $info = \_MD_NEWBB_LEVEL . ' ' . $level['level'] . '; <span title="' . \_MD_NEWBB_EXP_DESC . '">' . \_MD_NEWBB_EXP . ' ' . $level['exp'] . '</span><br>';
210
            $info .= '<span title="' . \_MD_NEWBB_HP_DESC . '">' . \_MD_NEWBB_HP . ' ' . $level['hp'] . ' / ' . $level['hp_max'] . '</span><br>';
211
            $info .= '<span title="' . \_MD_NEWBB_MP_DESC . '">' . \_MD_NEWBB_MP . ' ' . $level['mp'] . ' / ' . $level['mp_max'] . '</span>';
212
        }
213
214
        return $info;
215
    }
216
217
    /**
218
     * @param \XoopsUser $user
219
     *
220
     * @return (array|mixed|string)[]
221
     *
222
     * @psalm-return array{uid?: mixed, name: mixed|string, link: mixed|string, avatar?: mixed, rank?: array{image?: mixed, title?: mixed}, groups?: list<mixed>, from?: mixed, regdate?: string, last_login?: string, posts?: mixed, level?: string, userbar?: array, signature?: mixed}
223
     */
224
    public function getInfo(\XoopsUser $user): array
225
    {
226
        $userinfo = [];
227
        global $myts;
228
        static $name_anonymous;
229
230
        if (!\is_object($user) || !$user->isActive()) {
231
            if (null === $name_anonymous) {
232
                $name_anonymous = \htmlspecialchars((string)$GLOBALS['xoopsConfig']['anonymous'], \ENT_QUOTES | \ENT_HTML5);
233
            }
234
235
            return ['name' => $name_anonymous, 'link' => $name_anonymous];
236
        }
237
238
        $this->user = $user;
239
240
        $userinfo['uid'] = $user->getVar('uid');
241
242
        $name             = empty($GLOBALS['xoopsModuleConfig']['show_realname']) ? $user->getVar('uname') : $user->getVar('name');
243
        $userinfo['name'] = $name ?: $user->getVar('uname');
244
245
        $userinfo['link'] = '<a href=\'' . XOOPS_URL . '/userinfo.php?uid=' . $user->getVar('uid') . '\'>' . $userinfo['name'] . '</a>';
246
247
        $userinfo['avatar'] = $user->getVar('user_avatar');
248
        // START hacked by irmtfan - easier rank getting - consistency with previous version back rank.title and rank.image
249
        $userrank         = $user->rank();
250
        $userinfo['rank'] = [];
251
        if (isset($userrank['image']) && $userrank['image']) {
252
            $userinfo['rank']['image'] = $userrank['image'];
253
            $userinfo['rank']['title'] = $userrank['title'];
254
        }
255
        // END hacked by irmtfan - easier rank getting  - a little correctness dot removed
256
        // START hacked by irmtfan - easier groups getting - can we use $_SESSION['xoopsUserGroups']???
257
        //checks for user's groups
258
        $userinfo['groups'] = [];
259
        /** @var \XoopsMemberHandler $memberHandler */
260
        $memberHandler = \xoops_getHandler('member');
261
        $usergroups    = $memberHandler->getGroupsByUser($userinfo['uid'], true);
262
        foreach ($usergroups as $group) {
263
            $userinfo['groups'][] = $group->getVar('name');
264
        }
265
        // END hacked by irmtfan - easier groups getting - can we use $_SESSION['xoopsUserGroups']???
266
        $userinfo['from'] = $user->getVar('user_from');
267
268
        require_once \dirname(__DIR__) . '/include/functions.time.php';
269
        $userinfo['regdate']    = \newbbFormatTimestamp($user->getVar('user_regdate'), 'reg');
270
        $userinfo['last_login'] = \newbbFormatTimestamp($user->getVar('last_login')); // irmtfan add last_login
271
272
        $userinfo['posts'] = $user->getVar('posts');
273
274
        if (!empty($GLOBALS['xoopsModuleConfig']['user_level'])) {
275
            $userinfo['level'] = $this->getLevel();
276
        }
277
278
        if (!empty($GLOBALS['xoopsModuleConfig']['userbar_enabled'])) {
279
            $userinfo['userbar'] = $this->getUserbar();
280
        }
281
282
        $userinfo['signature'] = $user->getVar('user_sig');
283
284
        return $userinfo;
285
    }
286
}
287