Completed
Pull Request — master (#5)
by Michael
02:20
created

PublicWallUpdates   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 489
Duplicated Lines 45.19 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 72
lcom 1
cbo 1
dl 221
loc 489
rs 2.64
c 0
b 0
f 0

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getAdminModerators() 0 12 2
A inspected() 0 16 3
C Updates() 7 67 10
A Comments() 0 22 3
A Gravatar() 0 32 5
A countVotes() 14 14 3
A countVotesCom() 14 14 3
A HasVoted() 14 14 2
A CountMsges() 8 8 1
A UpdatesPermalink() 19 19 4
A UpdatesSharelink() 19 19 4
A GetSharing() 10 10 2
A GetSharingDiv() 11 11 2
F ParsePubArray() 105 129 28

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 PublicWallUpdates 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 PublicWallUpdates, and based on these observations, apply Extract Interface, too.

1
<?php namespace XoopsModules\Smallworld;
2
3
/**
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * SmallWorld
15
 *
16
 * @copyright    The XOOPS Project (https://xoops.org)
17
 * @copyright    2011 Culex
18
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
19
 * @package      SmallWorld
20
 * @since        1.0
21
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
22
 */
23
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
24
25
class PublicWallUpdates
26
{
27
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
28
    {
29
        global $xoopsDB, $xoopsUser;
30
        $sql    = 'SELECT userid
31
                FROM ' . $xoopsDB->prefix('smallworld_user') . ' su
32
                LEFT JOIN ' . $xoopsDB->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
33
                WHERE xu.uid IN (1)';
34
        $result = $xoopsDB->queryF($sql);
35
        while ($row = $xoopsDB->fetchArray($result)) {
36
            $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...
37
        }
38
    }
39
40
    /**
41
     * Get arry of users being inspected
42
     *
43
     *
44
     */
45
46
    public function inspected()
47
    {
48
        global $xoopsDB;
49
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start+inspect_stop) > ' . time() . '';
50
        $result = $xoopsDB->queryF($sql);
51
        $data   = [];
52
        while ($row = $xoopsDB->fetchArray($result)) {
53
            $data[] = $row;
54
        }
55
        if (!empty($data)) {
56
            $sub = implode(',', Smallworld_array_flatten(array_unique($data), 0));
57
        } else {
58
            $sub = 0;
59
        }
60
        return $sub;
61
    }
62
63
    /**
64
     * @Get array of updates
65
     * @param int   $last
66
     * @param array $moderators
67
     * @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...
68
     */
69
    public function Updates($last, $moderators)
70
    {
71
        global $xoopsUser, $xoopsDB, $moduleConfig, $xoopsLogger;
72
        $moderators = is_array($moderators) ? $moderators : [$moderators];
73
        $hm         = smallworld_GetModuleOption('msgtoshow');
74
        $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...
75
        $mods       = implode(',', Smallworld_array_flatten(array_unique($moderators), 0));
76
        $inspected  = $this->inspected();
77
        $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...
78
        $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...
79
80
        if (0 == $last) {
81
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
82
                     . $xoopsDB->prefix('smallworld_messages')
83
                     . ' M, '
84
                     . $xoopsDB->prefix('smallworld_user')
85
                     . ' U WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
86
                     . $mods
87
                     . ') AND M.uid_fk NOT IN ('
88
                     . $inspected
89
                     . ") AND M.priv = '0'";
90
        } elseif ($last > 0) {
91
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
92
                     . $xoopsDB->prefix('smallworld_messages')
93
                     . ' M, '
94
                     . $xoopsDB->prefix('smallworld_user')
95
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
96
                     . $mods
97
                     . ') AND M.uid_fk NOT IN ('
98
                     . $inspected
99
                     . ") AND M.priv = '0' AND M.msg_id < '"
100
                     . $last
101
                     . "'";
102
        } elseif ('a' == $last) {
103
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
104
                     . $xoopsDB->prefix('smallworld_messages')
105
                     . ' M, '
106
                     . $xoopsDB->prefix('smallworld_user')
107
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
108
                     . $mods
109
                     . ') AND M.uid_fk NOT IN ('
110
                     . $inspected
111
                     . ") AND M.priv = '0'";
112
        }
113
114 View Code Duplication
        if ($last > 0) {
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...
115
            $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...
116
        } elseif ('a' == $last) {
117
            $query .= ' order by M.msg_id DESC LIMIT ' . $hm;
118
        } else {
119
            $query .= ' order by created DESC LIMIT ' . $hm;
120
        }
121
122
        $result = $xoopsDB->queryF($query);
123
        $count  = $xoopsDB->getRowsNum($result);
124
        if (0 == $count) {
125
            return false;
126
        } else {
127
            while ($row = $xoopsDB->fetchArray($result)) {
128
                $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...
129
            }
130
131
            if (!empty($data)) {
132
                return $data;
133
            }
134
        }
135
    }
136
137
    /**
138
     * @Get comments based on msg id
139
     * @param int $msg_id
140
     * @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...
141
     */
142
    public function Comments($msg_id)
143
    {
144
        global $xoopsUser, $xoopsDB;
145
        $inspected = $this->inspected();
146
        $query     = 'SELECT C.msg_id_fk, C.com_id, C.uid_fk, C.comment, C.created, U.username FROM '
147
                     . $xoopsDB->prefix('smallworld_comments')
148
                     . ' C, '
149
                     . $xoopsDB->prefix('smallworld_user')
150
                     . " U WHERE C.uid_fk=U.userid AND C.msg_id_fk='"
151
                     . $msg_id
152
                     . "' AND C.uid_fk NOT IN ("
153
                     . $inspected
154
                     . ') ORDER BY C.com_id ASC ';
155
        $result    = $xoopsDB->queryF($query);
156
        $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...
157
        while ($row = $xoopsDB->fetchArray($result)) {
158
            $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...
159
        }
160
        if (!empty($data)) {
161
            return $data;
162
        }
163
    }
164
165
    /**
166
     * @Get user image based on uid
167
     * @param int $uid
168
     * @return string
169
     */
170
    public function Gravatar($uid)
171
    {
172
        global $xoopsUser, $xoopsDB;
173
        $image  = '';
174
        $sql    = 'SELECT userimage FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
175
        $result = $xoopsDB->queryF($sql);
176
        while ($r = $xoopsDB->fetchArray($result)) {
177
            $image = $r['userimage'];
178
        }
179
180
        if ('blank.gif' === $image) {
181
            $image = smallworld_getAvatarLink($uid, $image);
182
        }
183
184
        //$image = ($image == '' || $image == 'blank.gif') ? smallworld_getAvatarLink($uid, $image) : $image;
185
186
        $type = [
187
            1 => 'jpg',
188
            2 => 'jpeg',
189
            3 => 'png',
190
            4 => 'gif',
191
        ];
192
193
        $ext = explode('.', $image);
194
195
        if (@!in_array(strtolower($ext[1]), $type) || '' == $image) {
196
            $avatar = '';
197
        } else {
198
            $avatar = $image;
199
        }
200
        return $avatar;
201
    }
202
203
    /**
204
     * @count all votes
205
     * @param int $type
206
     * @param int $val
207
     * @param int $msgid
208
     * @return int
209
     */
210 View Code Duplication
    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...
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...
211
    {
212
        global $xoopsUser, $xoopsDB;
213
        $sum    = 0;
214
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where msg_id = '" . $msgid . "' and com_id = '0'";
215
        $result = $xoopsDB->queryF($query);
216
        while ($row = $xoopsDB->fetchArray($result)) {
217
            $sum = $row['sum'];
218
        }
219
        if ('' == $sum) {
220
            $sum = 0;
221
        }
222
        return $sum;
223
    }
224
225
    /**
226
     * @Count comments votes
227
     * @param int $type
228
     * @param int $val
229
     * @param int $comid
230
     * @param int $msgid
231
     * @returns int
232
     * @return int|mixed
233
     * @return int|mixed
234
     */
235 View Code Duplication
    public function countVotesCom($type, $val, $comid, $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...
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...
236
    {
237
        global $xoopsUser, $xoopsDB;
238
        $sum    = 0;
239
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
240
        $result = $xoopsDB->queryF($query);
241
        while ($row = $xoopsDB->fetchArray($result)) {
242
            $sum = $row['sum'];
243
        }
244
        if ('' == $sum) {
245
            $sum = 0;
246
        }
247
        return $sum;
248
    }
249
250
    /**
251
     * @Check is user is friend
252
     * @param int    $userid
253
     * @param string $type
254
     * @param int    $comid
255
     * @param int    $msgid
256
     * @return int
257
     */
258 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...
259
    {
260
        global $xoopsUser, $xoopsDB;
261
        if ('msg' === $type) {
262
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
263
            $result = $xoopsDB->queryF($sql);
264
            $i      = $xoopsDB->getRowsNum($result);
265
        } else {
266
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
267
            $result = $xoopsDB->queryF($sql);
268
            $i      = $xoopsDB->getRowsNum($result);
269
        }
270
        return $i;
271
    }
272
273
    /**
274
     * @count messages per user
275
     * @param int $userid
276
     * @return int
277
     */
278 View Code Duplication
    public function CountMsges($userid)
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...
279
    {
280
        global $xoopsDB;
281
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
282
        $result = $xoopsDB->queryF($sql);
283
        $sum    = $xoopsDB->fetchRow($result);
284
        return $sum[0];
285
    }
286
287
    /**
288
     * @Show permaling updates
289
     * @param int $updid
290
     * @param int $uid
291
     * @param int $ownerID
292
     * @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...
293
     */
294 View Code Duplication
    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...
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...
295
    {
296
        global $xoopsUser, $xoopsDB, $moduleConfig;
297
        $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 . "'";
298
        $query  .= " AND M.msg_id = '" . $updid . "'";
299
        $query  .= ' order by M.created DESC LIMIT 1';
300
        $result = $xoopsDB->queryF($query);
301
        $count  = $xoopsDB->getRowsNum($result);
302
        if ($count < 1) {
303
            return false;
304
        } else {
305
            while ($row = $xoopsDB->fetchArray($result)) {
306
                $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...
307
            }
308
            if (!empty($data)) {
309
                return $data;
310
            }
311
        }
312
    }
313
314
    /**
315
     * @Get share link
316
     * @param int $updid
317
     * @param int $ownerID
318
     * @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...
319
     */
320 View Code Duplication
    public function UpdatesSharelink($updid, $ownerID)
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...
321
    {
322
        global $xoopsUser, $xoopsDB, $moduleConfig;
323
        $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";
324
        $query  .= " AND M.msg_id = '" . $updid . "'";
325
        $query  .= ' order by created DESC LIMIT 1';
326
        $result = $xoopsDB->queryF($query);
327
        $count  = $xoopsDB->getRowsNum($result);
328
        if ($count < 1) {
329
            return false;
330
        } else {
331
            while ($row = $xoopsDB->fetchArray($result)) {
332
                $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...
333
            }
334
            if (!empty($data)) {
335
                return $data;
336
            }
337
        }
338
    }
339
340
    /**
341
     * @Get sharing link
342
     * @param int $id
343
     * @param int $priv
344
     * @return string
345
     */
346 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...
347
    {
348
        if (1 != $priv) {
349
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
350
            $text .= "<a class='share' id='share-page" . $id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
351
        } else {
352
            $text = '';
353
        }
354
        return $text;
355
    }
356
357
    /**
358
     * @Get content for sharing div
359
     * @param int    $id
360
     * @param int    $priv
361
     * @param string $permalink
362
     * @param string $desc
363
     * @param string $username
364
     * @return string
365
     */
366 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...
367
    {
368
        if (1 != $priv) {
369
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . $id . "'>";
370
            $text .= "<span name='share-page" . $id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>";
371
            $text .= '</span></div>';
372
        } else {
373
            $text = '';
374
        }
375
        return $text;
376
    }
377
378
    /**
379
     * @Parse update and comments array to template for public updates
380
     * @param array $updatesarray
381
     * @param int   $id
382
     * @return void
383
     */
384
    public function ParsePubArray($updatesarray, $id)
385
    {
386
        global $xoopsUser, $xoopsTpl, $tpl, $xoopsModule, $xoopsTpl, $xoopsConfig;
387
        $wm            = [];
388
        $check         = new Smallworld\User();
389
        $dBase         = new SwDatabase();
390
        $profile       = $xoopsUser ? $check->checkIfProfile($id) : 0;
391
        $moduleHandler = xoops_getHandler('module');
392
        $module        = $moduleHandler->getByDirname('smallworld');
393
        $configHandler = xoops_getHandler('config');
394
        $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...
395
396
        $myavatar          = $this->Gravatar($id);
397
        $myavatarlink      = smallworld_getAvatarLink($id, $myavatar);
398
        $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...
399
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 100);
400
        $user_img          = "<img src='" . smallworld_getAvatarLink($id, $myavatar) . "' id='smallworld_user_img' " . $myavatar_highwide . '>';
401
402
        $xoopsTpl->assign('myavatar', $myavatar);
403
        $xoopsTpl->assign('myavatarlink', $myavatarlink);
404
        $xoopsTpl->assign('myavatar_highwide', $myavatar_highwide);
405
        $xoopsTpl->assign('avatar', $user_img);
406
407 View Code Duplication
        if (!empty($updatesarray)) {
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...
408
            foreach ($updatesarray as $data) {
409
                // Is update's user a friend ?
410
                $frU = $check->friendcheck($id, $data['uid_fk']);
411
412
                $USW             = [];
413
                $USW['posts']    = 0;
414
                $USW['comments'] = 0;
415
416
                if ($xoopsUser) {
417
                    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $data['uid_fk'] == $id) {
418
                        $USW['posts']    = 1;
419
                        $USW['comments'] = 1;
420
                        $frU[0]          = 2;
421
                    } else {
422
                        $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
423
                    }
424
                }
425
426
                if (!$xoopsUser) {
427
                    $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
428
                }
429
430
                $wm['msg_id']          = $data['msg_id'];
431
                $wm['orimessage']      = (1 == $USW['posts'] || $profile >= 2) ? str_replace(["\r", "\n"], '', Smallworld_stripWordsKeepUrl($data['message'])) : '';
432
                $wm['message']         = (1 == $USW['posts'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
433
                $wm['message']         = Smallworld_cleanup($wm['message']);
434
                $wm['created']         = smallworld_time_stamp($data['created']);
435
                $wm['username']        = $data['username'];
436
                $wm['uid_fk']          = $data['uid_fk'];
437
                $wm['priv']            = $data['priv'];
438
                $wm['avatar']          = $this->Gravatar($data['uid_fk']);
439
                $wm['avatar_link']     = smallworld_getAvatarLink($data['uid_fk'], $wm['avatar']);
440
                $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...
441
                $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
442
                $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
443
                $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
444
                $wm['sharelinkurl']    = XOOPS_URL . '/modules/smallworld/smallworldshare.php?ownerid=' . $data['uid_fk'];
445
                $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
446
                $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $xoopsConfig['sitename'];
447
                if (1 == $USW['posts'] || $profile >= 2) {
448
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], $wm['priv']);
449
                } else {
450
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], 1);
451
                }
452
453
                if (1 == $USW['posts'] || $profile >= 2) {
454
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
455
                } else {
456
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
457
                }
458
                $wm['linkimage']     = XOOPS_URL . '/modules/smallworld/assets/images/link.png';
459
                $wm['permalink']     = XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $data['uid_fk'] . '&updid=' . $data['msg_id'];
460
                $wm['commentsarray'] = $this->Comments($data['msg_id']);
461
462
                if (2 == $frU[0] || 1 == $USW['posts']) {
463
                    $xoopsTpl->append('walldata', $wm);
464
                }
465
466
                if (!empty($wm['commentsarray'])) {
467
                    foreach ($wm['commentsarray'] as $cdata) {
468
                        // Is commentuser a friend ?
469
                        $frC = $check->friendcheck($id, $cdata['uid_fk']);
470
471
                        $USC             = [];
472
                        $USC['posts']    = 0;
473
                        $USC['comments'] = 0;
474
475
                        if ($xoopsUser) {
476
                            if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $cdata['uid_fk'] == $id) {
477
                                $USC['posts']    = 1;
478
                                $USC['comments'] = 1;
479
                                $frC[0]          = 2;
480
                            } else {
481
                                $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
482
                            }
483
                        }
484
485
                        if (!$xoopsUser) {
486
                            $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
487
                        }
488
489
                        $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...
490
                        $wc['com_id']          = $cdata['com_id'];
491
                        $wc['comment']         = (1 == $USC['comments'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
492
                        $wc['comment']         = Smallworld_cleanup($wc['comment']);
493
                        $wc['time']            = smallworld_time_stamp($cdata['created']);
494
                        $wc['username']        = $cdata['username'];
495
                        $wc['uid']             = $cdata['uid_fk'];
496
                        $wc['myavatar']        = $this->Gravatar($id);
497
                        $wc['myavatar_link']   = $myavatarlink;
498
                        $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...
499
                        $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
500
                        $wc['cface']           = $this->Gravatar($cdata['uid_fk']);
501
                        $wc['avatar_link']     = smallworld_getAvatarLink($cdata['uid_fk'], $wc['cface']);
502
                        $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
503
                        $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
504
505
                        if (2 == $frC[0] || 1 == $USC['comments']) {
506
                            $xoopsTpl->append('comm', $wc);
507
                        }
508
                    }
509
                }
510
            }
511
        }
512
    }
513
}
514