Completed
Push — master ( 62af87...249590 )
by Michael
03:02
created

OnlineHandler   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 355
Duplicated Lines 17.18 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 61
loc 355
rs 6.8
c 0
b 0
f 0
wmc 55
lcom 1
cbo 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A init() 15 20 4
B update() 0 28 6
C render() 23 55 10
C showOnline() 23 57 10
B write() 0 49 6
A gc() 0 9 1
B getAll() 0 25 6
C checkStatus() 0 28 7
A getCount() 0 13 4

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like OnlineHandler 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 OnlineHandler, and based on these observations, apply Extract Interface, too.

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 23 and the first side effect is on line 18.

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
use Xmf\IPAddress;
14
use XoopsModules\Newbb;
15
16
// defined('XOOPS_ROOT_PATH') || exit('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...
17
18
include_once __DIR__ . '/../include/functions.config.php';
19
20
/**
21
 * Class OnlineHandler
22
 */
23
class OnlineHandler
24
{
25
    public $db;
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
26
    public $forum_id;
0 ignored issues
show
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
27
    public $forumObject;
28
    public $topic_id;
0 ignored issues
show
Coding Style introduced by
$topic_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
29
    public $user_ids = [];
0 ignored issues
show
Coding Style introduced by
$user_ids does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
30
31
    /**
32
     * OnlineHandler constructor.
33
     * @param \XoopsDatabase $db
34
     */
35
    public function __construct(\XoopsDatabase $db)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $db. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
36
    {
37
        $this->db = $db;
38
    }
39
40
    /**
41
     * @param null|Newbb\Forum $forum
42
     * @param null|Topic  $forumtopic
43
     */
44
    public function init($forum = null, $forumtopic = null)
45
    {
46 View Code Duplication
        if (is_object($forum)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
47
            $this->forum_id    = $forum->getVar('forum_id');
48
            $this->forumObject = $forum;
49
        } else {
50
            $this->forum_id    = (int)$forum;
51
            $this->forumObject = $forum;
52
        }
53 View Code Duplication
        if (is_object($forumtopic)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
54
            $this->topic_id = $forumtopic->getVar('topic_id');
55
            if (empty($this->forum_id)) {
56
                $this->forum_id = $forumtopic->getVar('forum_id');
57
            }
58
        } else {
59
            $this->topic_id = (int)$forumtopic;
60
        }
61
62
        $this->update();
63
    }
64
65
    public function update()
0 ignored issues
show
Coding Style introduced by
update uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
66
    {
67
        global $xoopsModule;
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...
68
69
        mt_srand((double)microtime() * 1000000);
70
        // set gc probabillity to 10% for now..
71
        if (mt_rand(1, 100) < 60) {
72
            $this->gc(150);
73
        }
74
        if (is_object($GLOBALS['xoopsUser'])) {
75
            $uid   = $GLOBALS['xoopsUser']->getVar('uid');
76
            $uname = $GLOBALS['xoopsUser']->getVar('uname');
77
            $name  = $GLOBALS['xoopsUser']->getVar('name');
78
        } else {
79
            $uid   = 0;
80
            $uname = '';
81
            $name  = '';
82
        }
83
84
        $xoops_onlineHandler = xoops_getHandler('online');
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
85
        $xoopsupdate         = $xoops_onlineHandler->write($uid, $uname, time(), $xoopsModule->getVar('mid'), \Xmf\IPAddress::fromRequest()->asReadable());
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
86
        if (!$xoopsupdate) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
87
            //xoops_error("newbb online upate error");
88
        }
89
90
        $uname = (empty($GLOBALS['xoopsModuleConfig']['show_realname']) || empty($name)) ? $uname : $name;
91
        $this->write($uid, $uname, time(), $this->forum_id, IPAddress::fromRequest()->asReadable(), $this->topic_id);
92
    }
93
94
    /**
95
     * @param $xoopsTpl
96
     */
97
    public function render(\Smarty $xoopsTpl)
98
    {
99
        include_once __DIR__ . '/../include/functions.render.php';
100
        include_once __DIR__ . '/../include/functions.user.php';
101
        $criteria = null;
102 View Code Duplication
        if ($this->topic_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
103
            $criteria = new \Criteria('online_topic', $this->topic_id);
104
        } elseif ($this->forum_id) {
105
            $criteria = new \Criteria('online_forum', $this->forum_id);
106
        }
107
        $users     = $this->getAll($criteria);
108
        $num_total = count($users);
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
109
110
        $num_user     = 0;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
111
        $users_id     = [];
0 ignored issues
show
Coding Style introduced by
$users_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
112
        $users_online = [];
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
113 View Code Duplication
        for ($i = 0; $i < $num_total; ++$i) {
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
            if (empty($users[$i]['online_uid'])) {
115
                continue;
116
            }
117
            $users_id[]                             = $users[$i]['online_uid'];
0 ignored issues
show
Coding Style introduced by
$users_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
118
            $users_online[$users[$i]['online_uid']] = [
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
119
                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
120
                'uname' => $users[$i]['online_uname']
121
            ];
122
            ++$num_user;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
123
        }
124
        $num_anonymous           = $num_total - $num_user;
0 ignored issues
show
Coding Style introduced by
$num_anonymous does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
125
        $online                  = [];
126
        $online['image']         = newbbDisplayImage('whosonline');
127
        $online['num_total']     = $num_total;
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
128
        $online['num_user']      = $num_user;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
129
        $online['num_anonymous'] = $num_anonymous;
0 ignored issues
show
Coding Style introduced by
$num_anonymous does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
130
        $administrator_list      = newbbIsModuleAdministrators($users_id);
0 ignored issues
show
Coding Style introduced by
$administrator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
131
        $moderator_list          = [];
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
132 View Code Duplication
        if ($member_list = array_diff(array_keys($administrator_list), $users_id)) {
0 ignored issues
show
Coding Style introduced by
$member_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
133
            if (is_object($this->forumObject)) {
134
                $moderator_list = $this->forumObject->getVar('forum_moderator');
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
135
            } else {
136
                $moderator_list = newbbIsForumModerators($member_list);
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
137
            }
138
        }
139
        foreach ($users_online as $uid => $user) {
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
140
            if (!empty($administrator_list[$uid])) {
0 ignored issues
show
Coding Style introduced by
$administrator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
141
                $user['level'] = 2;
142
            } elseif (!empty($moderator_list[$uid])) {
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
143
                $user['level'] = 1;
144
            } else {
145
                $user['level'] = 0;
146
            }
147
            $online['users'][] = $user;
148
        }
149
150
        $xoopsTpl->assign_by_ref('online', $online);
151
    }
152
153
    /**
154
     * Deprecated
155
     */
156
    public function showOnline()
157
    {
158
        include_once __DIR__ . '/../include/functions.render.php';
159
        include_once __DIR__ . '/../include/functions.user.php';
160
        $criteria = null;
161 View Code Duplication
        if ($this->topic_id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
162
            $criteria = new \Criteria('online_topic', $this->topic_id);
163
        } elseif ($this->forum_id) {
164
            $criteria = new \Criteria('online_forum', $this->forum_id);
165
        }
166
        $users     = $this->getAll($criteria);
167
        $num_total = count($users);
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
168
169
        $num_user     = 0;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
170
        $users_id     = [];
0 ignored issues
show
Coding Style introduced by
$users_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
171
        $users_online = [];
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
172 View Code Duplication
        for ($i = 0; $i < $num_total; ++$i) {
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
173
            if (empty($users[$i]['online_uid'])) {
174
                continue;
175
            }
176
            $users_id[]                             = $users[$i]['online_uid'];
0 ignored issues
show
Coding Style introduced by
$users_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
177
            $users_online[$users[$i]['online_uid']] = [
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
178
                'link'  => XOOPS_URL . '/userinfo.php?uid=' . $users[$i]['online_uid'],
179
                'uname' => $users[$i]['online_uname']
180
            ];
181
            ++$num_user;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
182
        }
183
        $num_anonymous           = $num_total - $num_user;
0 ignored issues
show
Coding Style introduced by
$num_anonymous does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
184
        $online                  = [];
185
        $online['image']         = newbbDisplayImage('whosonline');
186
        $online['statistik']     = newbbDisplayImage('statistik');
187
        $online['num_total']     = $num_total;
0 ignored issues
show
Coding Style introduced by
$num_total does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
188
        $online['num_user']      = $num_user;
0 ignored issues
show
Coding Style introduced by
$num_user does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
189
        $online['num_anonymous'] = $num_anonymous;
0 ignored issues
show
Coding Style introduced by
$num_anonymous does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
190
        $administrator_list      = newbbIsModuleAdministrators($users_id);
0 ignored issues
show
Coding Style introduced by
$administrator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
191
        $moderator_list          = [];
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
192 View Code Duplication
        if ($member_list = array_diff($users_id, array_keys($administrator_list))) {
0 ignored issues
show
Coding Style introduced by
$member_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
193
            if (is_object($this->forumObject)) {
194
                $moderator_list = $this->forumObject->getVar('forum_moderator');
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
195
            } else {
196
                $moderator_list = newbbIsForumModerators($member_list);
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
197
            }
198
        }
199
200
        foreach ($users_online as $uid => $user) {
0 ignored issues
show
Coding Style introduced by
$users_online does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
201
            if (in_array($uid, $administrator_list)) {
0 ignored issues
show
Coding Style introduced by
$administrator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
202
                $user['level'] = 2;
203
            } elseif (in_array($uid, $moderator_list)) {
0 ignored issues
show
Coding Style introduced by
$moderator_list does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
204
                $user['level'] = 1;
205
            } else {
206
                $user['level'] = 0;
207
            }
208
            $online['users'][] = $user;
209
        }
210
211
        return $online;
212
    }
213
214
    /**
215
     * Write online information to the database
216
     *
217
     * @param  int    $uid      UID of the active user
218
     * @param  string $uname    Username
219
     * @param         $time
220
     * @param  string $forum_id Current forum_id
221
     * @param  string $ip       User's IP adress
222
     * @param         $topic_id
223
     * @return bool   TRUE on success
224
     * @internal param string $timestamp
225
     */
226
    public function write($uid, $uname, $time, $forum_id, $ip, $topic_id)
0 ignored issues
show
Coding Style introduced by
function write() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $ip. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
227
    {
228
        global $xoopsModule, $xoopsDB;
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...
229
230
        $uid = (int)$uid;
231
        if ($uid > 0) {
232
            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid;
233
        } else {
234
            $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_uid=' . $uid . " AND online_ip='" . $ip . "'";
235
        }
236
        list($count) = $this->db->fetchRow($this->db->queryF($sql));
237
        if ($count > 0) {
238
            $sql = 'UPDATE ' . $this->db->prefix('newbb_online') . " SET online_updated= '" . $time . "', online_forum = '" . $forum_id . "', online_topic = '" . $topic_id . "' WHERE online_uid = " . $uid;
0 ignored issues
show
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
239
            if (0 == $uid) {
240
                $sql .= " AND online_ip='" . $ip . "'";
241
            }
242
        } else {
243
            $sql = sprintf('INSERT INTO %s (online_uid, online_uname, online_updated, online_ip, online_forum, online_topic) VALUES (%u, %s, %u, %s, %u, %u)', $this->db->prefix('newbb_online'), $uid, $this->db->quote($uname), $time, $this->db->quote($ip), $forum_id, $topic_id);
0 ignored issues
show
Coding Style introduced by
$forum_id does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
244
        }
245
        if (!$this->db->queryF($sql)) {
246
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
247
            return false;
248
        }
249
250
        /** @var \XoopsOnlineHandler $xoops_onlineHandler */
251
        $xoops_onlineHandler = xoops_getHandler('online');
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
252
        $xoopsOnlineTable    = $xoops_onlineHandler->table;
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
253
254
        $sql = 'DELETE FROM '
255
               . $this->db->prefix('newbb_online')
256
               . ' WHERE'
257
               . ' ( online_uid > 0 AND online_uid NOT IN ( SELECT online_uid FROM '
258
               . $xoopsOnlineTable
259
               . ' WHERE online_module ='
260
               . $xoopsModule->getVar('mid')
261
               . ' ) )'
262
               . ' OR ( online_uid = 0 AND online_ip NOT IN ( SELECT online_ip FROM '
263
               . $xoopsOnlineTable
264
               . ' WHERE online_module ='
265
               . $xoopsModule->getVar('mid')
266
               . ' AND online_uid = 0 ) )';
267
268
        if ($result = $this->db->queryF($sql)) {
0 ignored issues
show
Unused Code introduced by
$result 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...
Coding Style introduced by
The if-else statement can be simplified to return (bool) ($result =...his->db->queryF($sql));.
Loading history...
269
            return true;
270
        } else {
271
            //xoops_error($this->db->error());
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% 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...
272
            return false;
273
        }
274
    }
275
276
    /**
277
     * Garbage Collection
278
     *
279
     * Delete all online information that has not been updated for a certain time
280
     *
281
     * @param int $expire Expiration time in seconds
282
     */
283
    public function gc($expire)
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 3 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
284
    {
285
        global $xoopsModule;
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...
286
        $sql = 'DELETE FROM ' . $this->db->prefix('newbb_online') . ' WHERE online_updated < ' . (time() - (int)$expire);
287
        $this->db->queryF($sql);
288
289
        $xoops_onlineHandler = xoops_getHandler('online');
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
290
        $xoops_onlineHandler->gc($expire);
0 ignored issues
show
Coding Style introduced by
$xoops_onlineHandler does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
291
    }
292
293
    /**
294
     * Get an array of online information
295
     *
296
     * @param  \CriteriaElement $criteria {@link \CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|\CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
297
     * @return array           Array of associative arrays of online information
298
     */
299
    public function getAll(\CriteriaElement $criteria = null)
300
    {
301
        $ret   = [];
302
        $limit = $start = 0;
303
        $sql   = 'SELECT * FROM ' . $this->db->prefix('newbb_online');
304
        if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
305
            $sql   .= ' ' . $criteria->renderWhere();
306
            $limit = $criteria->getLimit();
307
            $start = $criteria->getStart();
308
        }
309
        $result = $this->db->query($sql, $limit, $start);
310
        if (!$result) {
311
            return $ret;
312
        }
313
        while ($myrow = $this->db->fetchArray($result)) {
314
            $ret[] = $myrow;
315
            if ($myrow['online_uid'] > 0) {
316
                $this->user_ids[] = $myrow['online_uid'];
317
            }
318
            unset($myrow);
319
        }
320
        $this->user_ids = array_unique($this->user_ids);
321
322
        return $ret;
323
    }
324
325
    /**
326
     * @param $uids
327
     * @return array
328
     */
329
    public function checkStatus($uids)
330
    {
331
        $online_users = [];
0 ignored issues
show
Coding Style introduced by
$online_users does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
332
        $ret          = [];
333
        if (!empty($this->user_ids)) {
334
            $online_users = $this->user_ids;
0 ignored issues
show
Coding Style introduced by
$online_users does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
335
        } else {
336
            $sql = 'SELECT online_uid FROM ' . $this->db->prefix('newbb_online');
337
            if (!empty($uids)) {
338
                $sql .= ' WHERE online_uid IN (' . implode(', ', array_map('intval', $uids)) . ')';
339
            }
340
341
            $result = $this->db->query($sql);
342
            if (!$result) {
343
                return $ret;
344
            }
345
            while (list($uid) = $this->db->fetchRow($result)) {
346
                $online_users[] = $uid;
0 ignored issues
show
Coding Style introduced by
$online_users does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
347
            }
348
        }
349
        foreach ($uids as $uid) {
350
            if (in_array($uid, $online_users)) {
0 ignored issues
show
Coding Style introduced by
$online_users does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
351
                $ret[$uid] = 1;
352
            }
353
        }
354
355
        return $ret;
356
    }
357
358
    /**
359
     * Count the number of online users
360
     *
361
     * @param  \CriteriaElement $criteria {@link CriteriaElement}
0 ignored issues
show
Documentation introduced by
Should the type for parameter $criteria not be null|\CriteriaElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
362
     * @return bool
363
     */
364
    public function getCount(\CriteriaElement $criteria = null)
0 ignored issues
show
Coding Style introduced by
function getCount() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
365
    {
366
        $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('newbb_online');
367
        if (is_object($criteria) && is_subclass_of($criteria, 'CriteriaElement')) {
368
            $sql .= ' ' . $criteria->renderWhere();
369
        }
370
        if (!$result = $this->db->query($sql)) {
371
            return false;
372
        }
373
        list($ret) = $this->db->fetchRow($result);
374
375
        return $ret;
376
    }
377
}
378