Completed
Push — master ( fd0c8a...c024a6 )
by
unknown
03:33 queued 01:53
created

Public_Wall_Updates::ParsePubArray()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 130

Duplication

Lines 130
Ratio 100 %

Importance

Changes 0
Metric Value
cc 28
nc 76804
nop 2
dl 130
loc 130
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * SmallWorld
14
 *
15
 * @copyright    The XOOPS Project (https://xoops.org)
16
 * @copyright    2011 Culex
17
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
18
 * @package      SmallWorld
19
 * @since        1.0
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
// Moderrated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
23
24
class Public_Wall_Updates
25
{
26
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
27
    {
28
        global $xoopsDB, $xoopsUser;
29
        $sql    = 'SELECT userid
30
                FROM ' . $xoopsDB->prefix('smallworld_user') . ' su
31
                LEFT JOIN ' . $xoopsDB->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
32
                WHERE xu.uid IN (1)';
33
        $result = $xoopsDB->queryF($sql);
34
        while ($row = $xoopsDB->fetchArray($result)) {
35
            $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
36
        }
37
    }
38
39
    /**
40
     * Get arry of users being inspected
41
     *
42
     *
43
     */
44
45 View Code Duplication
    public function inspected()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
46
    {
47
        global $xoopsDB;
48
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start+inspect_stop) > ' . time() . '';
49
        $result = $xoopsDB->queryF($sql);
50
        $data   = [];
51
        while ($row = $xoopsDB->fetchArray($result)) {
52
            $data[] = $row;
53
        }
54
        if (!empty($data)) {
55
            $sub = implode(',', Smallworld_array_flatten(array_unique($data), 0));
56
        } else {
57
            $sub = 0;
58
        }
59
        return $sub;
60
    }
61
62
    /**
63
     * @Get array of updates
64
     * @param int   $last
65
     * @param  array $moderators
66
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
67
     */
68 View Code Duplication
    public function Updates($last, $moderators)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
69
    {
70
        global $xoopsUser, $xoopsDB, $moduleConfig, $xoopsLogger;
71
        $moderators = is_array($moderators) ? $moderators : [$moderators];
72
        $hm         = smallworld_GetModuleOption('msgtoshow');
73
        $set        = smallworld_checkPrivateOrPublic();
0 ignored issues
show
Unused Code introduced by
$set 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...
74
        $mods       = implode(',', Smallworld_array_flatten(array_unique($moderators), 0));
75
        $inspected  = $this->inspected();
76
        $perm       = smallworld_GetModuleOption('smallworldshowPoPubPage');
0 ignored issues
show
Unused Code introduced by
$perm 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...
77
        $i          = 0;
0 ignored issues
show
Unused Code introduced by
$i 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...
78
79
        if (0 == $last) {
80
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
81
                     . $xoopsDB->prefix('smallworld_messages')
82
                     . ' M, '
83
                     . $xoopsDB->prefix('smallworld_user')
84
                     . ' U WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
85
                     . $mods
86
                     . ') AND M.uid_fk NOT IN ('
87
                     . $inspected
88
                     . ") AND M.priv = '0'";
89
        } elseif ($last > 0) {
90
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
91
                     . $xoopsDB->prefix('smallworld_messages')
92
                     . ' M, '
93
                     . $xoopsDB->prefix('smallworld_user')
94
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
95
                     . $mods
96
                     . ') AND M.uid_fk NOT IN ('
97
                     . $inspected
98
                     . ") AND M.priv = '0' AND M.msg_id < '"
99
                     . $last
100
                     . "'";
101
        } elseif ('a' === $last) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of 'a' (string) and $last (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
102
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
103
                     . $xoopsDB->prefix('smallworld_messages')
104
                     . ' M, '
105
                     . $xoopsDB->prefix('smallworld_user')
106
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
107
                     . $mods
108
                     . ') AND M.uid_fk NOT IN ('
109
                     . $inspected
110
                     . ") AND M.priv = '0'";
111
        }
112
113
        if ($last > 0) {
114
            $query .= ' order by created DESC LIMIT ' . $hm;
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
115
        } elseif ('a' === $last) {
0 ignored issues
show
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of 'a' (string) and $last (integer) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
116
            $query .= ' order by M.msg_id DESC LIMIT ' . $hm;
117
        } else {
118
            $query .= ' order by created DESC LIMIT ' . $hm;
119
        }
120
121
        $result = $xoopsDB->queryF($query);
122
        $count  = $xoopsDB->getRowsNum($result);
123
        if (0 == $count) {
124
            return false;
125
        } else {
126
            while ($row = $xoopsDB->fetchArray($result)) {
127
                $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
128
            }
129
130
            if (!empty($data)) {
131
                return $data;
132
            }
133
        }
134
    }
135
136
    /**
137
     * @Get comments based on msg id
138
     * @param int $msg_id
139
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
140
     */
141 View Code Duplication
    public function Comments($msg_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
142
    {
143
        global $xoopsUser, $xoopsDB;
144
        $inspected = $this->inspected();
145
        $query     = 'SELECT C.msg_id_fk, C.com_id, C.uid_fk, C.comment, C.created, U.username FROM '
146
                     . $xoopsDB->prefix('smallworld_comments')
147
                     . ' C, '
148
                     . $xoopsDB->prefix('smallworld_user')
149
                     . " U WHERE C.uid_fk=U.userid AND C.msg_id_fk='"
150
                     . $msg_id
151
                     . "' AND C.uid_fk NOT IN ("
152
                     . $inspected
153
                     . ') ORDER BY C.com_id ASC ';
154
        $result    = $xoopsDB->queryF($query);
155
        $i         = $xoopsDB->getRowsNum($result);
0 ignored issues
show
Unused Code introduced by
$i 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...
156
        while ($row = $xoopsDB->fetchArray($result)) {
157
            $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
158
        }
159
        if (!empty($data)) {
160
            return $data;
161
        }
162
    }
163
164
    /**
165
     * @Get user image based on uid
166
     * @param int $uid
167
     * @return string
168
     */
169
    public function Gravatar($uid)
170
    {
171
        global $xoopsUser, $xoopsDB;
172
        $image  = '';
173
        $sql    = 'SELECT userimage FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
174
        $result = $xoopsDB->queryF($sql);
175
        while ($r = $xoopsDB->fetchArray($result)) {
176
            $image = $r['userimage'];
177
        }
178
179
        if ('blank.gif' === $image) {
180
            $image = smallworld_getAvatarLink($uid, $image);
181
        }
182
183
        //$image = ($image == '' || $image == 'blank.gif') ? smallworld_getAvatarLink($uid, $image) : $image;
184
185
        $type = [
186
            1 => 'jpg',
187
            2 => 'jpeg',
188
            3 => 'png',
189
            4 => 'gif'
190
        ];
191
192
        $ext = explode('.', $image);
193
194
        if (@!in_array(strtolower($ext[1]), $type) || '' == $image) {
195
            $avatar = '';
196
        } else {
197
            $avatar = $image;
198
        }
199
        return $avatar;
200
    }
201
202
    /**
203
     * @count all votes
204
     * @param int $type
205
     * @param int $val
206
     * @param int $msgid
207
     * @return int
208
     */
209
    public function countVotes($type, $val, $msgid)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
210
    {
211
        global $xoopsUser, $xoopsDB;
212
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where msg_id = '" . $msgid . "' and com_id = '0'";
213
        $result = $xoopsDB->queryF($query);
214
        while ($row = $xoopsDB->fetchArray($result)) {
215
            $sum = $row['sum'];
216
        }
217
        if ('' == $sum) {
0 ignored issues
show
Bug introduced by
The variable $sum does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
218
            $sum = '0';
219
        }
220
        return $sum;
221
    }
222
223
    /**
224
     * @Count comments votes
225
     * @param int $type
226
     * @param int $val
227
     * @param int $comid
228
     * @param int $msgid
229
     * @returns int
230
     */
231 View Code Duplication
    public function countVotesCom($type, $val, $comid, $msgid)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in 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...
232
    {
233
        global $xoopsUser, $xoopsDB;
234
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
235
        $result = $xoopsDB->queryF($query);
236
        while ($row = $xoopsDB->fetchArray($result)) {
237
            $sum = $row['sum'];
238
        }
239
        if ('' == $sum) {
0 ignored issues
show
Bug introduced by
The variable $sum does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
240
            $sum = '0';
241
        }
242
        return $sum;
243
    }
244
245
    /**
246
     * @Check is user is friend
247
     * @param int    $userid
248
     * @param string $type
249
     * @param int    $comid
250
     * @param int    $msgid
251
     * @return int
252
     */
253 View Code Duplication
    public function HasVoted($userid, $type, $comid, $msgid)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
254
    {
255
        global $xoopsUser, $xoopsDB;
256
        if ('msg' === $type) {
257
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
258
            $result = $xoopsDB->queryF($sql);
259
            $i      = $xoopsDB->getRowsNum($result);
260
        } else {
261
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
262
            $result = $xoopsDB->queryF($sql);
263
            $i      = $xoopsDB->getRowsNum($result);
264
        }
265
        return $i;
266
    }
267
268
    /**
269
     * @count messages per user
270
     * @param int $userid
271
     * @return int
272
     */
273
    public function CountMsges($userid)
274
    {
275
        global $xoopsDB;
276
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
277
        $result = $xoopsDB->queryF($sql);
278
        $sum    = $xoopsDB->fetchRow($result);
279
        return $sum[0];
280
    }
281
282
    /**
283
     * @Show permaling updates
284
     * @param int $updid
285
     * @param int $uid
286
     * @param int $ownerID
287
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
288
     */
289
    public function UpdatesPermalink($updid, $uid, $ownerID)
0 ignored issues
show
Unused Code introduced by
The parameter $uid is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
290
    {
291
        global $xoopsUser, $xoopsDB, $moduleConfig;
292
        $query  = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $ownerID . "'";
293
        $query  .= " AND M.msg_id = '" . $updid . "'";
294
        $query  .= ' order by M.created DESC LIMIT 1';
295
        $result = $xoopsDB->queryF($query);
296
        $count  = $xoopsDB->getRowsNum($result);
297
        if ($count < 1) {
298
            return false;
299
        } else {
300
            while ($row = $xoopsDB->fetchArray($result)) {
301
                $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
302
            }
303
            if (!empty($data)) {
304
                return $data;
305
            }
306
        }
307
    }
308
309
    /**
310
     * @Get share link
311
     * @param int $updid
312
     * @param int $ownerID
313
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
314
     */
315
    public function UpdatesSharelink($updid, $ownerID)
316
    {
317
        global $xoopsUser, $xoopsDB, $moduleConfig;
318
        $query  = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U WHERE M.uid_fk=U.userid AND M.uid_fk='" . $ownerID . "' AND M.priv = 0";
319
        $query  .= " AND M.msg_id = '" . $updid . "'";
320
        $query  .= ' order by created DESC LIMIT 1';
321
        $result = $xoopsDB->queryF($query);
322
        $count  = $xoopsDB->getRowsNum($result);
323
        if ($count < 1) {
324
            return false;
325
        } else {
326
            while ($row = $xoopsDB->fetchArray($result)) {
327
                $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
328
            }
329
            if (!empty($data)) {
330
                return $data;
331
            }
332
        }
333
    }
334
335
    /**
336
     * @Get sharing link
337
     * @param int $id
338
     * @param int $priv
339
     * @return string
340
     */
341 View Code Duplication
    public function GetSharing($id, $priv)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
342
    {
343
        if (1 != $priv) {
344
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
345
            $text .= "<a class='share' id='share-page" . $id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
346
        } else {
347
            $text = '';
348
        }
349
        return $text;
350
    }
351
352
    /**
353
     * @Get content for sharing div
354
     * @param int    $id
355
     * @param int    $priv
356
     * @param string $permalink
357
     * @param string $desc
358
     * @param string $username
359
     * @return string
360
     */
361 View Code Duplication
    public function GetSharingDiv($id, $priv, $permalink, $desc, $username)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
362
    {
363
        if (1 != $priv) {
364
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . $id . "'>";
365
            $text .= "<span name='share-page" . $id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>";
366
            $text .= '</span></div>';
367
        } else {
368
            $text = '';
369
        }
370
        return $text;
371
    }
372
373
    /**
374
     * @Parse update and comments array to template for public updates
375
     * @param array $updatesarray
376
     * @param int   $id
377
     * @return void
378
     */
379 View Code Duplication
    public function ParsePubArray($updatesarray, $id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
380
    {
381
        global $xoopsUser, $xoopsTpl, $tpl, $xoopsModule, $xoopsTpl, $xoopsConfig;
382
383
        $check          = new SmallWorldUser;
384
        $dBase          = new SmallWorldDB;
385
        $profile        = $xoopsUser ? $check->checkIfProfile($id) : 0;
386
        $moduleHandler = xoops_getHandler('module');
387
        $module         = $moduleHandler->getByDirname('smallworld');
388
        $configHandler = xoops_getHandler('config');
389
        $moduleConfig   = $configHandler->getConfigsByCat(0, $module->getVar('mid'));
0 ignored issues
show
Unused Code introduced by
$moduleConfig 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...
390
391
        $myavatar          = $this->Gravatar($id);
392
        $myavatarlink      = smallworld_getAvatarLink($id, $myavatar);
393
        $myavatar_size     = smallworld_getImageSize(80, 100, $myavatarlink);
0 ignored issues
show
Documentation introduced by
$myavatarlink is of type string, but the function expects a object<url>.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
394
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 100);
395
        $user_img          = "<img src='" . smallworld_getAvatarLink($id, $myavatar) . "' id='smallworld_user_img' " . $myavatar_highwide . '>';
396
397
        $xoopsTpl->assign('myavatar', $myavatar);
398
        $xoopsTpl->assign('myavatarlink', $myavatarlink);
399
        $xoopsTpl->assign('myavatar_highwide', $myavatar_highwide);
400
        $xoopsTpl->assign('avatar', $user_img);
401
402
        if (!empty($updatesarray)) {
403
            foreach ($updatesarray as $data) {
404
405
                // Is update's user a friend ?
406
                $frU = $check->friendcheck($id, $data['uid_fk']);
407
408
                $USW             = [];
409
                $USW['posts']    = 0;
410
                $USW['comments'] = 0;
411
412
                if ($xoopsUser) {
413
                    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $data['uid_fk'] == $id) {
414
                        $USW['posts']    = 1;
415
                        $USW['comments'] = 1;
416
                        $frU[0]          = 2;
417
                    } else {
418
                        $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
419
                    }
420
                }
421
422
                if (!$xoopsUser) {
423
                    $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
424
                }
425
426
                $wm['msg_id']          = $data['msg_id'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wm was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wm = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
427
                $wm['orimessage']      = (1 == $USW['posts'] || $profile >= 2) ? str_replace(["\r", "\n"], '', Smallworld_stripWordsKeepUrl($data['message'])) : '';
0 ignored issues
show
Bug introduced by
The variable $wm does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
428
                $wm['message']         = (1 == $USW['posts'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
429
                $wm['message']         = Smallworld_cleanup($wm['message']);
430
                $wm['created']         = smallworld_time_stamp($data['created']);
431
                $wm['username']        = $data['username'];
432
                $wm['uid_fk']          = $data['uid_fk'];
433
                $wm['priv']            = $data['priv'];
434
                $wm['avatar']          = $this->Gravatar($data['uid_fk']);
435
                $wm['avatar_link']     = smallworld_getAvatarLink($data['uid_fk'], $wm['avatar']);
436
                $wm['avatar_size']     = smallworld_getImageSize(80, 100, $wm['avatar_link']);
0 ignored issues
show
Documentation introduced by
$wm['avatar_link'] is of type string, but the function expects a object<url>.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
437
                $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
438
                $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
439
                $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
440
                $wm['sharelinkurl']    = XOOPS_URL . '/modules/smallworld/smallworldshare.php?ownerid=' . $data['uid_fk'];
441
                $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
442
                $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $xoopsConfig['sitename'];
443
                if (1 == $USW['posts'] || $profile >= 2) {
444
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], $wm['priv']);
445
                } else {
446
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], 1);
447
                }
448
449
                if (1 == $USW['posts'] || $profile >= 2) {
450
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
451
                } else {
452
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
453
                }
454
                $wm['linkimage']     = XOOPS_URL . '/modules/smallworld/assets/images/link.png';
455
                $wm['permalink']     = XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $data['uid_fk'] . '&updid=' . $data['msg_id'];
456
                $wm['commentsarray'] = $this->Comments($data['msg_id']);
457
458
                if (2 == $frU[0] || 1 == $USW['posts']) {
459
                    $xoopsTpl->append('walldata', $wm);
460
                }
461
462
                if (!empty($wm['commentsarray'])) {
463
                    foreach ($wm['commentsarray'] as $cdata) {
464
                        // Is commentuser a friend ?
465
                        $frC = $check->friendcheck($id, $cdata['uid_fk']);
466
467
                        $USC             = [];
468
                        $USC['posts']    = 0;
469
                        $USC['comments'] = 0;
470
471
                        if ($xoopsUser) {
472
                            if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $cdata['uid_fk'] == $id) {
473
                                $USC['posts']    = 1;
474
                                $USC['comments'] = 1;
475
                                $frC[0]          = 2;
476
                            } else {
477
                                $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
478
                            }
479
                        }
480
481
                        if (!$xoopsUser) {
482
                            $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
483
                        }
484
485
                        $wc['msg_id_fk']       = $cdata['msg_id_fk'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wc was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wc = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
486
                        $wc['com_id']          = $cdata['com_id'];
487
                        $wc['comment']         = (1 == $USC['comments'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
488
                        $wc['comment']         = Smallworld_cleanup($wc['comment']);
489
                        $wc['time']            = smallworld_time_stamp($cdata['created']);
490
                        $wc['username']        = $cdata['username'];
491
                        $wc['uid']             = $cdata['uid_fk'];
492
                        $wc['myavatar']        = $this->Gravatar($id);
493
                        $wc['myavatar_link']   = $myavatarlink;
494
                        $wc['avatar_size']     = smallworld_getImageSize(80, 100, $wc['myavatar_link']);
0 ignored issues
show
Documentation introduced by
$wc['myavatar_link'] is of type string, but the function expects a object<url>.

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

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

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

function acceptsInteger($int) { }

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

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
495
                        $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
496
                        $wc['cface']           = $this->Gravatar($cdata['uid_fk']);
497
                        $wc['avatar_link']     = smallworld_getAvatarLink($cdata['uid_fk'], $wc['cface']);
498
                        $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
499
                        $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
500
501
                        if (2 == $frC[0] || 1 == $USC['comments']) {
502
                            $xoopsTpl->append('comm', $wc);
503
                        }
504
                    }
505
                }
506
            }
507
        }
508
    }
509
}
510