Completed
Push — master ( 213be3...2c75bd )
by Michael
11:47
created

XoopsUser   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 582
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 87.34%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
wmc 56
lcom 2
cbo 6
dl 0
loc 582
ccs 138
cts 158
cp 0.8734
rs 6.5957
c 5
b 0
f 0

40 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 49 4
A isGuest() 0 4 1
B getUnameFromId() 0 24 5
A setGroups() 0 6 2
A getGroups() 0 7 2
A isAdmin() 0 11 4
A rank() 0 8 2
A isActive() 0 7 2
A isOnline() 0 9 3
A uid() 0 4 1
A id() 0 4 1
A name() 0 4 1
A uname() 0 4 1
A email() 0 4 1
A url() 0 4 1
A user_avatar() 0 4 1
A user_regdate() 0 4 1
A user_icq() 0 4 1
A user_from() 0 4 1
A user_sig() 0 4 1
A user_viewemail() 0 4 1
A actkey() 0 4 1
A user_aim() 0 4 1
A user_yim() 0 4 1
A user_msnm() 0 4 1
A pass() 0 4 1
A posts() 0 4 1
A attachsig() 0 4 1
A level() 0 4 1
A theme() 0 4 1
A timezone() 0 4 1
A umode() 0 4 1
A uorder() 0 4 1
A notify_method() 0 4 1
A notify_mode() 0 4 1
A user_occ() 0 4 1
A bio() 0 4 1
A user_intrest() 0 4 1
A incrementPost() 0 4 1
A groups() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like XoopsUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use XoopsUser, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * XOOPS user handler
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @package   Kernel
13
 * @author    Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
14
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
15
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
16
 * @since     2.0.0
17
 */
18
19
namespace Xoops\Core\Kernel\Handlers;
20
21
use Xoops\Core\Kernel\Criteria;
22
use Xoops\Core\Kernel\Dtype;
23
use Xoops\Core\Kernel\XoopsObject;
24
25
/**
26
 * Class for users
27
 *
28
 * @category  Xoops\Core\Kernel\Handlers\XoopsUser
29
 * @package   Xoops\Core\Kernel
30
 * @author    Kazumi Ono <[email protected]>
31
 * @copyright 2000-2015 XOOPS Project (http://xoops.org)
32
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
33
 * @link      http://xoops.org
34
 */
35
class XoopsUser extends XoopsObject
36
{
37
    /**
38
     * @var array groups that user belongs to
39
     */
40
    private $groups = array();
41
42
    /**
43
     * @var string user's rank
44
     */
45
    private $rank = null;
46
47
    /**
48
     * @var bool is the user online?
49
     */
50
    private $isOnline = null;
51
52
    /**
53
     * constructor
54
     *
55
     * @param int|array $id ID of the user to be loaded from the database or
56
     *                      Array of key-value-pairs to be assigned to the user.
57
     *                      (for backward compatibility only)
58
     */
59 54
    public function __construct($id = null)
60
    {
61 54
        $this->initVar('uid', Dtype::TYPE_INTEGER, null, false);
62 54
        $this->initVar('name', Dtype::TYPE_TEXT_BOX, null, false, 60);
63 54
        $this->initVar('uname', Dtype::TYPE_TEXT_BOX, null, true, 25);
64 54
        $this->initVar('email', Dtype::TYPE_TEXT_BOX, null, true, 60);
65 54
        $this->initVar('url', Dtype::TYPE_TEXT_BOX, null, false, 100);
66 54
        $this->initVar('user_avatar', Dtype::TYPE_TEXT_BOX, null, false, 30);
67 54
        $this->initVar('user_regdate', Dtype::TYPE_INTEGER, null, false);
68 54
        $this->initVar('user_icq', Dtype::TYPE_TEXT_BOX, null, false, 15);
69 54
        $this->initVar('user_from', Dtype::TYPE_TEXT_BOX, null, false, 100);
70 54
        $this->initVar('user_sig', Dtype::TYPE_TEXT_AREA, null, false, null);
71 54
        $this->initVar('user_viewemail', Dtype::TYPE_INTEGER, 0, false);
72 54
        $this->initVar('actkey', Dtype::TYPE_OTHER, null, false);
73 54
        $this->initVar('user_aim', Dtype::TYPE_TEXT_BOX, null, false, 18);
74 54
        $this->initVar('user_yim', Dtype::TYPE_TEXT_BOX, null, false, 25);
75 54
        $this->initVar('user_msnm', Dtype::TYPE_TEXT_BOX, null, false, 100);
76 54
        $this->initVar('pass', Dtype::TYPE_TEXT_BOX, null, false, 255);
77 54
        $this->initVar('posts', Dtype::TYPE_INTEGER, null, false);
78 54
        $this->initVar('attachsig', Dtype::TYPE_INTEGER, 0, false);
79 54
        $this->initVar('rank', Dtype::TYPE_INTEGER, 0, false);
80 54
        $this->initVar('level', Dtype::TYPE_INTEGER, 0, false);
81 54
        $this->initVar('theme', Dtype::TYPE_OTHER, null, false);
82 54
        $this->initVar('timezone', Dtype::TYPE_TIMEZONE, 'UTC', 32);
83 54
        $this->initVar('last_login', Dtype::TYPE_INTEGER, 0, false);
84 54
        $this->initVar('last_pass_change', Dtype::TYPE_DATETIME, 0, false);
85 54
        $this->initVar('umode', Dtype::TYPE_OTHER, null, false);
86 54
        $this->initVar('uorder', Dtype::TYPE_INTEGER, 1, false);
87
        // RMV-NOTIFY
88 54
        $this->initVar('notify_method', Dtype::TYPE_OTHER, 1, false);
89 54
        $this->initVar('notify_mode', Dtype::TYPE_OTHER, 0, false);
90 54
        $this->initVar('user_occ', Dtype::TYPE_TEXT_BOX, null, false, 100);
91 54
        $this->initVar('bio', Dtype::TYPE_TEXT_AREA, null, false, null);
92 54
        $this->initVar('user_intrest', Dtype::TYPE_TEXT_BOX, null, false, 150);
93 54
        $this->initVar('user_mailok', Dtype::TYPE_INTEGER, 1, false);
94
        // for backward compatibility
95 54
        if (isset($id)) {
96
            if (is_array($id)) {
97
                $this->assignVars($id);
98
            } else {
99
                $xoops = \Xoops::getInstance();
100
                $member_handler = $xoops->getHandlerMember();
101
                $user = $member_handler->getUser($id);
102
                foreach ($user->vars as $k => $v) {
103
                    $this->assignVar($k, $v['value']);
104
                }
105
            }
106
        }
107 54
    }
108
109
    /**
110
     * check if the user is a guest user
111
     *
112
     * @return bool returns false
113
     */
114 1
    public function isGuest()
115
    {
116 1
        return false;
117
    }
118
119
    /**
120
     * Updated by Catzwolf 11 Jan 2004
121
     * find the username for a given ID
122
     *
123
     * @param int $userid  ID of the user to find
124
     * @param int $usereal switch for usename or realname
125
     *
126
     * @return string name of the user. name for 'anonymous' if not found.
127
     */
128 1
    public static function getUnameFromId($userid, $usereal = 0)
129
    {
130 1
        $xoops = \Xoops::getInstance();
131 1
        $userid = (int)($userid);
132 1
        $usereal = (int)($usereal);
133 1
        if ($userid > 0) {
134 1
            $member_handler = $xoops->getHandlerMember();
135 1
            $user = $member_handler->getUser($userid);
136 1
            if (is_object($user)) {
137 1
                $ts = \Xoops\Core\Text\Sanitizer::getInstance();
138 1
                if ($usereal) {
139
                    $name = $user->getVar('name');
140
                    if ($name != '') {
141
                        return $ts->htmlSpecialChars($name);
142
                    } else {
143
                        return $ts->htmlSpecialChars($user->getVar('uname'));
144
                    }
145
                } else {
146 1
                    return $ts->htmlSpecialChars($user->getVar('uname'));
147
                }
148
            }
149
        }
150 1
        return $xoops->getConfig('anonymous');
151
    }
152
153
    /**
154
     * increase the number of posts for the user
155
     *
156
     * @deprecated
157
     * @return bool
158
     */
159
    public function incrementPost()
160
    {
161
        return \Xoops::getInstance()->getHandlerMember()->updateUserByField($this, 'posts', $this->getVar('posts') + 1);
162
    }
163
164
    /**
165
     * set the groups for the user
166
     *
167
     * @param array $groupsArr Array of groups that user belongs to
168
     *
169
     * @return void
170
     */
171 1
    public function setGroups($groupsArr)
172
    {
173 1
        if (is_array($groupsArr)) {
174 1
            $this->groups = $groupsArr;
175 1
        }
176 1
    }
177
178
    /**
179
     * get the groups that the user belongs to
180
     *
181
     * @return array array of groups
182
     */
183 3
    public function getGroups()
184
    {
185 3
        if (empty($this->groups)) {
186 3
            $this->groups = \Xoops::getInstance()->getHandlerMember()->getGroupsByUser($this->getVar('uid'));
187 3
        }
188 3
        return $this->groups;
189
    }
190
191
    /**
192
     * alias for getGroups()
193
     *
194
     * @see getGroups()
195
     * @return array array of groups
196
     * @deprecated
197
     */
198
    public function groups()
199
    {
200
        $groups = $this->getGroups();
201
        return $groups;
202
    }
203
204
    /**
205
     * Is the user admin ?
206
     *
207
     * This method will return true if this user has admin rights for the specified module.<br />
208
     * - If you don't specify any module ID, the current module will be checked.<br />
209
     * - If you set the module_id to -1, it will return true if the user has admin rights for at least one module
210
     *
211
     * @param int $module_id check if user is admin of this module
212
     *
213
     * @return bool is the user admin of that module?
214
     */
215 1
    public function isAdmin($module_id = null)
216
    {
217 1
        $xoops = \Xoops::getInstance();
218 1
        if (is_null($module_id)) {
219 1
            $module_id = $xoops->isModule() ? $xoops->module->getVar('mid', 'n') : 1;
220 1
        } elseif ((int)($module_id) < 1) {
221
            $module_id = 0;
222
        }
223 1
        $moduleperm_handler = $xoops->getHandlerGroupPermission();
224 1
        return $moduleperm_handler->checkRight('module_admin', $module_id, $this->getGroups());
225
    }
226
227
    /**
228
     * get the user's rank
229
     *
230
     * @return array array of rank ID and title
231
     */
232 1
    public function rank()
233
    {
234 1
        $xoops = \Xoops::getInstance();
235 1
        if (!isset($this->rank)) {
236 1
            $this->rank = $xoops->service('userrank')->getUserRank($this)->getValue();
237 1
        }
238 1
        return $this->rank;
239
    }
240
241
    /**
242
     * is the user activated?
243
     *
244
     * @return bool
245
     */
246 1
    public function isActive()
247
    {
248 1
        if ($this->getVar('level') == 0) {
249 1
            return false;
250
        }
251
        return true;
252
    }
253
254
    /**
255
     * is the user currently logged in?
256
     *
257
     * @return bool
258
     */
259 1
    public function isOnline()
260
    {
261 1
        if (!isset($this->isOnline)) {
262 1
            $online_handler = \Xoops::getInstance()->getHandlerOnline();
263 1
            $this->isOnline =
264 1
                ($online_handler->getCount(new Criteria('online_uid', $this->getVar('uid'))) > 0) ? true : false;
0 ignored issues
show
Bug introduced by
The method getCount does only exist in XoopsPersistableObjectHandler, but not in XoopsObjectHandler.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
265 1
        }
266 1
        return $this->isOnline;
267
    }
268
269
    /**
270
     * getter
271
     *
272
     * @param string $format Dtype::FORMAT_xxxx constant
273
     *
274
     * @return mixed
275
     */
276 2
    public function uid($format = '')
277
    {
278 2
        return $this->getVar('uid', $format);
279
    }
280
281
    /**
282
     * getter
283
     *
284
     * @param string $format Dtype::FORMAT_xxxx constant
285
     *
286
     * @return mixed
287
     */
288 1
    public function id($format = Dtype::FORMAT_NONE)
289
    {
290 1
        return $this->getVar('uid', $format);
291
    }
292
293
    /**
294
     * getter
295
     *
296
     * @param string $format Dtype::FORMAT_xxxx constant
297
     *
298
     * @return mixed
299
     */
300 1
    public function name($format = '')
301
    {
302 1
        return $this->getVar('name', $format);
303
    }
304
305
    /**
306
     * getter
307
     *
308
     * @param string $format Dtype::FORMAT_xxxx constant
309
     *
310
     * @return mixed
311
     */
312
    public function uname($format = '')
313
    {
314
        return $this->getVar('uname', $format);
315
    }
316
317
    /**
318
     * getter
319
     *
320
     * @param string $format Dtype::FORMAT_xxxx constant
321
     *
322
     * @return mixed
323
     */
324 1
    public function email($format = '')
325
    {
326 1
        return $this->getVar('email', $format);
327
    }
328
329
    /**
330
     * getter
331
     *
332
     * @param string $format Dtype::FORMAT_xxxx constant
333
     *
334
     * @return mixed
335
     */
336 1
    public function url($format = '')
337
    {
338 1
        return $this->getVar('url', $format);
339
    }
340
341
    /**
342
     * getter
343
     *
344
     * @param string $format Dtype::FORMAT_xxxx constant
345
     *
346
     * @return mixed
347
     */
348 1
    public function user_avatar($format = '')
349
    {
350 1
        return $this->getVar('user_avatar', $format);
351
    }
352
353
    /**
354
     * getter
355
     *
356
     * @param string $format Dtype::FORMAT_xxxx constant
357
     *
358
     * @return mixed
359
     */
360 1
    public function user_regdate($format = '')
361
    {
362 1
        return $this->getVar('user_regdate', $format);
363
    }
364
365
    /**
366
     * getter
367
     *
368
     * @param string $format Dtype::FORMAT_xxxx constant
369
     *
370
     * @return mixed
371
     */
372 1
    public function user_icq($format = 'S')
373
    {
374 1
        return $this->getVar('user_icq', $format);
375
    }
376
377
    /**
378
     * getter
379
     *
380
     * @param string $format Dtype::FORMAT_xxxx constant
381
     *
382
     * @return mixed
383
     */
384 1
    public function user_from($format = '')
385
    {
386 1
        return $this->getVar('user_from', $format);
387
    }
388
389
    /**
390
     * getter
391
     *
392
     * @param string $format Dtype::FORMAT_xxxx constant
393
     *
394
     * @return mixed
395
     */
396 1
    public function user_sig($format = '')
397
    {
398 1
        return $this->getVar('user_sig', $format);
399
    }
400
401
    /**
402
     * getter
403
     *
404
     * @param string $format Dtype::FORMAT_xxxx constant
405
     *
406
     * @return mixed
407
     */
408 1
    public function user_viewemail($format = '')
409
    {
410 1
        return $this->getVar('user_viewemail', $format);
411
    }
412
413
    /**
414
     * getter
415
     *
416
     * @param string $format Dtype::FORMAT_xxxx constant
417
     *
418
     * @return mixed
419
     */
420 1
    public function actkey($format = '')
421
    {
422 1
        return $this->getVar('actkey', $format);
423
    }
424
425
    /**
426
     * getter
427
     *
428
     * @param string $format Dtype::FORMAT_xxxx constant
429
     *
430
     * @return mixed
431
     */
432 1
    public function user_aim($format = '')
433
    {
434 1
        return $this->getVar('user_aim', $format);
435
    }
436
437
    /**
438
     * getter
439
     *
440
     * @param string $format Dtype::FORMAT_xxxx constant
441
     *
442
     * @return mixed
443
     */
444 1
    public function user_yim($format = '')
445
    {
446 1
        return $this->getVar('user_yim', $format);
447
    }
448
449
    /**
450
     * getter
451
     *
452
     * @param string $format Dtype::FORMAT_xxxx constant
453
     *
454
     * @return mixed
455
     */
456 1
    public function user_msnm($format = '')
457
    {
458 1
        return $this->getVar('user_msnm', $format);
459
    }
460
461
    /**
462
     * getter
463
     *
464
     * @param string $format Dtype::FORMAT_xxxx constant
465
     *
466
     * @return mixed
467
     */
468 2
    public function pass($format = '')
469
    {
470 2
        return $this->getVar('pass', $format);
471
    }
472
473
    /**
474
     * getter
475
     *
476
     * @param string $format Dtype::FORMAT_xxxx constant
477
     *
478
     * @return mixed
479
     */
480 1
    public function posts($format = '')
481
    {
482 1
        return $this->getVar('posts', $format);
483
    }
484
485
    /**
486
     * getter
487
     *
488
     * @param string $format Dtype::FORMAT_xxxx constant
489
     *
490
     * @return mixed
491
     */
492 1
    public function attachsig($format = '')
493
    {
494 1
        return $this->getVar('attachsig', $format);
495
    }
496
497
    /**
498
     * getter
499
     *
500
     * @param string $format Dtype::FORMAT_xxxx constant
501
     *
502
     * @return mixed
503
     */
504 1
    public function level($format = '')
505
    {
506 1
        return $this->getVar('level', $format);
507
    }
508
509
    /**
510
     * getter
511
     *
512
     * @param string $format Dtype::FORMAT_xxxx constant
513
     *
514
     * @return mixed
515
     */
516 1
    public function theme($format = '')
517
    {
518 1
        return $this->getVar('theme', $format);
519
    }
520
521
    /**
522
     * getter
523
     *
524
     * @param string $format Dtype::FORMAT_xxxx constant
525
     *
526
     * @return mixed
527
     */
528 1
    public function timezone($format = '')
529
    {
530 1
        return $this->getVar('timezone', $format);
531
    }
532
533
    /**
534
     * getter
535
     *
536
     * @param string $format Dtype::FORMAT_xxxx constant
537
     *
538
     * @return mixed
539
     */
540 1
    public function umode($format = '')
541
    {
542 1
        return $this->getVar('umode', $format);
543
    }
544
545
    /**
546
     * getter
547
     *
548
     * @param string $format Dtype::FORMAT_xxxx constant
549
     *
550
     * @return mixed
551
     */
552 1
    public function uorder($format = '')
553
    {
554 1
        return $this->getVar('uorder', $format);
555
    }
556
557
    /**
558
     * getter
559
     *
560
     * @param string $format Dtype::FORMAT_xxxx constant
561
     *
562
     * @return mixed
563
     */
564 1
    public function notify_method($format = '')
565
    {
566 1
        return $this->getVar('notify_method', $format);
567
    }
568
569
    /**
570
     * getter
571
     *
572
     * @param string $format Dtype::FORMAT_xxxx constant
573
     *
574
     * @return mixed
575
     */
576 1
    public function notify_mode($format = '')
577
    {
578 1
        return $this->getVar('notify_mode', $format);
579
    }
580
581
    /**
582
     * getter
583
     *
584
     * @param string $format Dtype::FORMAT_xxxx constant
585
     *
586
     * @return mixed
587
     */
588 1
    public function user_occ($format = '')
589
    {
590 1
        return $this->getVar('user_occ', $format);
591
    }
592
593
    /**
594
     * getter
595
     *
596
     * @param string $format Dtype::FORMAT_xxxx constant
597
     *
598
     * @return mixed
599
     */
600 1
    public function bio($format = '')
601
    {
602 1
        return $this->getVar('bio', $format);
603
    }
604
605
    /**
606
     * getter
607
     *
608
     * @param string $format Dtype::FORMAT_xxxx constant
609
     *
610
     * @return mixed
611
     */
612 1
    public function user_intrest($format = '')
613
    {
614 1
        return $this->getVar('user_intrest', $format);
615
    }
616
}
617