WallUpdates::parsePubArray()   F
last analyzed

Complexity

Conditions 21
Paths 4353

Size

Total Lines 104

Duplication

Lines 28
Ratio 26.92 %

Importance

Changes 0
Metric Value
cc 21
nc 4353
nop 2
dl 28
loc 104
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
namespace XoopsModules\Smallworld;
4
5
/**
6
 * You may not change or alter any portion of this comment or credits
7
 * of supporting developers from this source code or any supporting source code
8
 * which is considered copyrighted (c) material of the original comment or credit authors.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
 */
14
15
/**
16
 * SmallWorld
17
 *
18
 * @package      \XoopsModules\Smallworld
19
 * @license      GNU GPL (https://www.gnu.org/licenses/gpl-2.0.html/)
20
 * @copyright    The XOOPS Project (https://xoops.org)
21
 * @copyright    2011 Culex
22
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
23
 * @link         https://github.com/XoopsModules25x/smallworld
24
 * @since        1.0
25
 */
26
27
use XoopsModules\Smallworld;
28
use XoopsModules\Smallworld\Constants;
29
30
//include_once $GLOBALS['xoops']->path('include/common.php');
31
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
32
33
/**
34
 * Wall Update class
35
 *
36
 * Performs CRUD operations for updating the walldata
37
 *
38
 */
39
class WallUpdates
40
{
41
    /**
42
     * @deprecated - not used
43
     *
44
     * @return array
45
     */
46
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
47
    {
48
        $data   = [];
49
        $sql    = 'SELECT userid
50
                FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' su
51
                LEFT JOIN ' . $GLOBALS['xoopsDB']->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
52
                WHERE xu.uid IN (1)';
53
        $result = $GLOBALS['xoopsDB']->queryF($sql);
54
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
55
            $data[] = $row;
56
        }
57
58
        return $data;
59
    }
60
61
    /**
62
     * @param $last
63
     * @param $uid
64
     * @param $followers
65
     * @return array
66
     */
67
    public function Updates($last, $uid, $followers)
68
    {
69
        $uid       = (int)$uid;
70
        $query     = '';
0 ignored issues
show
Unused Code introduced by
$query 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...
71
        $hm        = \XoopsModules\Smallworld\Helper::getInstance()->getConfig('msgtoshow');
72
        //$set       = smallworld_checkPrivateOrPublic();
73
        $followers = is_array($followers) ? $followers : [$followers];
74
        $followers = array_unique(smallworld_array_flatten($followers, 0));
75
        //$followers = is_array($followers) ? $followers : [$uid];
76
        $fQuery    = '';
77
        foreach ($followers as $follower) {
78
            if ($last > 0) {
79
                $fQuery .= " OR M.uid_fk=U.userid AND M.uid_fk= '" . $follower . "' and M.msg_id < '" . $last . "'";
80
            } elseif (0 == $last) {
81
                $fQuery .= " OR M.uid_fk=U.userid AND M.uid_fk= '" . $follower . "'";
82
            } elseif ('a' === $last) {
83
                $fQuery .= " OR M.uid_fk=U.userid AND M.uid_fk= '" . $follower . "'";
84
            }
85
        }
86
87
        if (0 == $last) {
88
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "'"
89
                   . $fQuery . ' ORDER BY created DESC LIMIT ' . $hm;
90 View Code Duplication
        } elseif ($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...
91
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "' AND M.msg_id < '" . $last . "'"
92
                   . $fQuery . ' ORDER BY created DESC LIMIT ' . $hm;
93
        } elseif ('a' === $last) {
94
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "'"
95
                   . $fQuery . ' ORDER BY M.msg_id DESC LIMIT ' . $hm;
96
        } else {
97
            return [];
98
        }
99
100
        $result = $GLOBALS['xoopsDB']->queryF($query);
101
        $data   = [];
102
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
103
            $data[] = $row;
104
        }
105
106
        return $data;
107
    }
108
109
    /**
110
     * Get comments based on msg id
111
     *
112
     * @param int $msg_id
113
     * @return array
114
     */
115 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...
116
    {
117
        $data = []; //init data array
118
        $query  = 'SELECT C.msg_id_fk, C.com_id, C.uid_fk, C.comment, C.created, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . ' C, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U WHERE C.uid_fk=U.userid AND C.msg_id_fk='" . $msg_id . "' ORDER BY C.com_id ASC ";
119
        $result = $GLOBALS['xoopsDB']->queryF($query);
120
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
121
            $data[] = $row;
122
        }
123
124
        return $data;
125
    }
126
127
    /**
128
     * Get user image based on uid
129
     *
130
     * @deprecated
131
     * @param int $uid
132
     * @return string
133
     */
134
    public function Gravatar($uid)
135
    {
136
        $depMsg = get_class() . __FUNCTION__ . " is deprecated use SwUserHandler::gravatar() instead.";
137 View Code Duplication
        if (isset($GLOBALS['xoopsLogger'])) {
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...
138
            $GLOBALS['xoopsLogger']->addDeprecated($depMsg);
139
        } else {
140
            trigger_error($depMsg, E_USER_WARNING);
141
        }
142
143
        $image  = $avatar = '';
144
        $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
145
        $criteria = new \Criteria('userimage', (int)$uid);
146
        $criteria->setLimit(1);
147
        $swUserArray = $swUserHandler->getAll($criteria, ['userimage'], false);
148
        if (0 < count($swUserArray)) {
149
            $swUser = array_pop($swUserArray);
150
            $image = $swUser['userimage'];
151
        }
152
        /*
153
        $sql    = 'SELECT userimage FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
154
        $result = $GLOBALS['xoopsDB']->queryF($sql);
155
        while (false !== ($r = $GLOBALS['xoopsDB']->fetchArray($result))) {
156
            $image = $r['userimage'];
157
        }
158
        */
159
        $image = ('' == $image || 'blank.gif' === $image) ? $swUserHandler->getAvatarLink($uid, $image) : $image;
160
161
        $type = [
162
            1 => 'jpg',
163
            2 => 'jpeg',
164
            3 => 'png',
165
            4 => 'gif',
166
        ];
167
168
        $ext    = explode('.', $image);
169 View Code Duplication
        if (array_key_exists(1, $ext) && in_array(mb_strtolower($ext[1]), $type)) {
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...
170
            $avatar = $image;
171
        }
172
173
        return $avatar;
174
    }
175
176
    /**
177
     * Insert update
178
     *
179
     * @param int          $uid
180
     * @param string|array $update
181
     * @param int          $priv
182
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be array|false|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...
183
     */
184
    public function insertUpdate($uid, $update, $priv = 0)
185
    {
186
        $uid    = (int)$uid;
187
        $priv   = (int)$priv;
188
        $update = smallworld_sanitize(htmlentities($update, ENT_QUOTES, 'UTF-8'));
189
        $time   = time();
190
        $query  = 'SELECT msg_id,message FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE uid_fk='" . $uid . "' ORDER BY msg_id DESC LIMIT 1";
191
        $result = $GLOBALS['xoopsDB']->queryF($query);
192
        $row    = $GLOBALS['xoopsDB']->fetchArray($result);
193
        if ($update != $row['message']) {
194
            $query    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " (message, uid_fk, priv, created) VALUES ('" . $update . "', '" . $uid . "', '" . $priv . "', '" . $time . "')";
195
            $result   = $GLOBALS['xoopsDB']->queryF($query);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
196
            $newquery = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "' ORDER BY M.msg_id DESC LIMIT 1 ";
197
            $result2  = $GLOBALS['xoopsDB']->queryF($newquery);
198
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result2))) {
199
                $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...
200
            }
201
            $count = $GLOBALS['xoopsDB']->getRowsNum($result2);
202
            $retVal = false;
203
            if (0 < $count) {
204
                $data = []; // init data array
205
                while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result2))) {
206
                    $data[] = $row;
207
                }
208
                if (!empty($data)) {
209
                    $retVal = $data;
210
                }
211
            }
212
            return $retVal;
213
        }
214
    }
215
216
    /**
217
     * Insert comment into the dB
218
     *
219
     * @param int          $uid
220
     * @param int          $msg_id
221
     * @param string|array $comment
222
     * @return bool|string false on failure
223
     */
224
    public function insertComment($uid, $msg_id, $comment)
225
    {
226
        $data    = []; // init the data array
227
        $comment = smallworld_sanitize(htmlentities($comment, ENT_QUOTES, 'UTF-8'));
228
        $time    = time();
229
        $query   = 'SELECT com_id,comment FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE uid_fk='" . $uid . "' AND msg_id_fk='" . $msg_id . "' ORDER BY com_id DESC LIMIT 1 ";
230
        $result  = $GLOBALS['xoopsDB']->fetchArray($query);
231
        if ($comment != $result['comment']) {
232
            $query    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " (comment, uid_fk,msg_id_fk,created) VALUES ('" . $comment . "', '" . $uid . "','" . $msg_id . "', '" . $time . "')";
233
            $result   = $GLOBALS['xoopsDB']->queryF($query);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
234
            $newquery = 'SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username FROM '
235
                        . $GLOBALS['xoopsDB']->prefix('smallworld_comments')
236
                        . ' C, '
237
                        . $GLOBALS['xoopsDB']->prefix('smallworld_user')
238
                        . " U WHERE C.uid_fk=U.userid AND C.uid_fk='"
239
                        . $uid
240
                        . "' AND C.msg_id_fk='"
241
                        . $msg_id
242
                        . "' ORDER BY C.com_id DESC LIMIT 1 ";
243
            $result2  = $GLOBALS['xoopsDB']->queryF($newquery);
244
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result2))) {
245
                $data[0] = $row;
246
            }
247
248
            return $data[0];
249
        }
250
251
        return false;
252
    }
253
254
    /**
255
     * Get array of users followers
256
     *
257
     * @param int $me
258
     * @return array
259
     */
260
    public function getFollowers($me)
261
    {
262
        $data   = [];
263
        $me     = (int)$me;
264
        $query  = 'SELECT you FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE me = '" . $me . "'";
265
        $result = $GLOBALS['xoopsDB']->queryF($query);
266
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
267 View Code Duplication
        if (0 == $i) {
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...
268
            $data = [$me];
269
        } else {
270
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
271
                $data[] = $row;
272
            }
273
        }
274
275
        return $data;
276
    }
277
278
    /**
279
     * Count all votes
280
     *
281
     * @param int $type - not used
282
     * @param int $column name of column in vote dB table
283
     * @param int $msgid
284
     * @return int
285
     */
286
    public function countVotes($type, $column, $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...
287
    {
288
        $sum = 0;
289
        $valCol = in_array($column, ['up', 'down']) ? $column : false;
290
        if (false !== $valCol) {
291
            $query  = 'SELECT SUM(' . $column . ') AS sum FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE msg_id = '" . (int)$msgid . "' AND com_id = '0'";
292
            $result = $GLOBALS['xoopsDB']->queryF($query);
293
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
294
                $sum = $row['sum'];
295
            }
296
        }
297
298
        return (int)$sum;
299
    }
300
301
    /**
302
     * Count comments votes
303
     *
304
     * @param int $type - not used
305
     * @param int $val - not used
306
     * @param int $comid
307
     * @param int $msgid
308
     * @returns int
309
     */
310 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...
311
    {
312
        $sum = 0;
313
        $query  = 'SELECT SUM(' . $val . ') AS sum FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
314
        $result = $GLOBALS['xoopsDB']->queryF($query);
315
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
316
            $sum = $row['sum'];
317
        }
318
319
        return (int)$sum;
320
    }
321
322
    /**
323
     * Check if user has voted
324
     *
325
     * @param int    $userid
326
     * @param string $type
327
     * @param int    $comid
328
     * @param int    $msgid
329
     * @return bool
330
     */
331
    public function hasVoted($userid, $type, $comid, $msgid)
332
    {
333
        $userid = (int)$userid;
334
        $comid  = (int)$comid;
335
        $msgid  = (int)$msgid;
336
337
        if ('msg' === $type) {
338
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
339
            $result = $GLOBALS['xoopsDB']->queryF($sql);
340
            $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
341
        } else {
342
            $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
343
            $result = $GLOBALS['xoopsDB']->queryF($sql);
344
            $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
345
        }
346
347
        return $i ? true : false;
348
    }
349
350
    /**
351
     * Count messages per user
352
     * @param int $userid
353
     * @return int
354
     */
355 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...
356
    {
357
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
358
        $result = $GLOBALS['xoopsDB']->queryF($sql);
359
        $sum    = $GLOBALS['xoopsDB']->fetchRow($result);
360
361
        return $sum[0];
362
    }
363
364
    /**
365
     * Show permalink updates
366
     *
367
     * @param int $updid
368
     * @param int $uid
369
     * @param int $ownerID
370
     * @return array
371
     */
372 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...
373
    {
374
        $query  = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $ownerID . "'";
375
        $query  .= " AND M.msg_id = '" . $updid . "'";
376
        $query  .= ' ORDER BY M.created DESC LIMIT 1';
377
        $result = $GLOBALS['xoopsDB']->queryF($query);
378
        //$count  = $GLOBALS['xoopsDB']->getRowsNum($result);
379
        $data = [];
380
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
381
            $data[] = $row;
382
        }
383
384
        return $data;
385
    }
386
387
    /**
388
     * Updates share link in dB
389
     *
390
     * @param int $updid
391
     * @param int $ownerID
392
     * @return array
393
     */
394 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...
395
    {
396
        $GLOBALS['xoopsLogger']->activated = false;
397
        //error_reporting(E_ALL);
398
        $query  = 'SELECT M.msg_id, M.uid_fk, M.message, M.created, M.priv, U.username FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . ' M, ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " U WHERE M.uid_fk=U.userid AND M.uid_fk='" . $ownerID . "' AND M.priv = 0";
399
        $query  .= " AND M.msg_id = '" . (int)$updid . "'";
400
        $query  .= ' ORDER BY created DESC LIMIT 1';
401
        $result = $GLOBALS['xoopsDB']->queryF($query);
402
        //$count  = $GLOBALS['xoopsDB']->getRowsNum($result);
403
        $data = [];
404
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
405
            $data[] = $row;
406
        }
407
        return $data;
408
    }
409
410
    /**
411
     * Get sharing HTML link
412
     * @param int $id
413
     * @param int $priv
414
     * @return string
415
     */
416 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...
417
    {
418
        $text = '';
419
        if (1 !== $priv) {
420
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
421
            $text .= "<a class='share' id='share-page" . (int)$id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
422
        }
423
424
        return $text;
425
    }
426
427
    /**
428
     * Get content for sharing - HTML div
429
     *
430
     * @param int    $id
431
     * @param int    $priv
432
     * @param string $permalink
433
     * @param string $desc
434
     * @param string $username
435
     * @return string
436
     */
437
    public function getSharingDiv($id, $priv, $permalink, $desc, $username)
438
    {
439
        $text = '';
440
        if (1 != $priv) {
441
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . (int)$id . "'>"
442
                  . "<span name='share-page" . (int)$id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>"
443
                  . '</span></div>';
444
        }
445
446
        return $text;
447
    }
448
449
    /**
450
     * Parse update and comments array to template for public updates
451
     *
452
     * @param array $updatesarray
453
     * @param int   $id
454
     * @return void
455
     */
456
    public function parsePubArray($updatesarray, $id)
457
    {
458
        /**
459
         * @var \XoopsModules\Smallworld\Helper $helper
460
         * @var \XoopsModules\Smallworld\SwUserHandler $swUserHandler
461
         */
462
        $helper            = Helper::getInstance();
463
        $swUserHandler     = $helper->getHandler('SwUser');
464
        $check             = new User();
465
        $swDB              = new SwDatabase();
466
        $profile           = $swUserHandler->checkIfProfile($id);
467
        $myavatar          = $swUserHandler->gravatar($id);
468
        $myavatarlink      = $swUserHandler->getAvatarLink($id, $myavatar);
469
        $myavatar_size     = smallworld_getImageSize(80, 100, $myavatarlink);
470
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 35);
471
472
        $GLOBALS['xoopsTpl']->assign([
473
            'myavatar'          => $myavatar,
474
            'myavatarlink'      => $myavatarlink,
475
            'myavatar_highwide' => $myavatar_highwide
476
        ]);
477
478
        foreach ($updatesarray as $data) {
479
            // Is update's user a friend ?
480
            $frU = $check->friendcheck($id, $data['uid_fk']);
481
            $USW = ['posts' => 0, 'comments' => 0];
0 ignored issues
show
Unused Code introduced by
$USW 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...
482
483 View Code Duplication
            if ($helper->isUserAdmin() || $data['uid_fk'] == $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
484
                $USW    = ['posts' => 1, 'comments' => 1];
485
                $frU[0] = 2;
486
            } else {
487
                $USW = json_decode($swDB->getSettings($data['uid_fk']), true);
488
            }
489
490
            $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...
491
            $wm['orimessage']      = (1 == $USW['posts'] || $profile >= Constants::PROFILE_HAS_BOTH) ? 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...
492
            $wm['message']         = (1 == $USW['posts'] || $profile >= Constants::PROFILE_HAS_BOTH) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
493
            $wm['message']         = smallworld_cleanup($wm['message']);
494
            $wm['created']         = smallworld_time_stamp($data['created']);
495
            $wm['username']        = $data['username'];
496
            $wm['uid_fk']          = $data['uid_fk'];
497
            $wm['priv']            = $data['priv'];
498
            $wm['avatar']          = $swUserHandler->gravatar($data['uid_fk']);
499
            $wm['avatar_link']     = $swUserHandler->getAvatarLink($data['uid_fk'], $wm['avatar']);
500
            $wm['avatar_size']     = smallworld_getImageSize(80, 100, $wm['avatar_link']);
501
            $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
502
            $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
503
            $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
504
            $wm['sharelinkurl']    = $helper->url("smallworldshare.php?ownerid={$data['uid_fk']}");
505
            $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
506
            $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $GLOBALS['xoopsConfig']['sitename'];
507 View Code Duplication
            if (1 == $USW['posts'] || $profile >= Constants::PROFILE_HAS_BOTH) {
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...
508
                $wm['sharelink'] = $this->getSharing($wm['msg_id'], $wm['priv']);
509
            } else {
510
                $wm['sharelink'] = $this->getSharing($wm['msg_id'], 1);
511
            }
512
513 View Code Duplication
            if (1 == $USW['posts'] || $profile >= Constants::PROFILE_HAS_BOTH) {
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...
514
                $wm['sharediv'] = $this->getSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
515
            } else {
516
                $wm['sharediv'] = $this->getSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
517
            }
518
            $wm['linkimage']     = $helper->url('assets/images/link.png');
519
            $wm['permalink']     = $helper->url("permalink.php?ownerid={$data['uid_fk']}&updid={$data['msg_id']}");
520
            $wm['commentsarray'] = $this->Comments($data['msg_id']);
521
522 View Code Duplication
            if (2 == $frU[0] || 1 == $USW['posts']) {
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...
523
                $GLOBALS['xoopsTpl']->append('walldata', $wm);
524
            }
525
526
            foreach ($wm['commentsarray'] as $cdata) {
527
                // Is commentuser a friend ?
528
                $frC = $check->friendcheck($id, $cdata['uid_fk']);
529
                $USC = ['posts' => 0, 'comments' => 0];
0 ignored issues
show
Unused Code introduced by
$USC 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...
530
531 View Code Duplication
                if ($helper->isUserAdmin() || $cdata['uid_fk'] == $id) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
532
                    $USC    = ['posts' => 1, 'comments' => 1];
533
                    $frC[0] = Constants::PROFILE_HAS_BOTH;
534
                } else {
535
                    $USC = json_decode($swDB->getSettings($cdata['uid_fk']), true);
536
                }
537
538
                $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...
539
                $wc['com_id']          = $cdata['com_id'];
540
                $wc['comment']         = (1 == $USC['comments'] || $profile >= Constants::PROFILE_HAS_BOTH) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
541
                $wc['comment']         = smallworld_cleanup($wc['comment']);
542
                $wc['time']            = smallworld_time_stamp($cdata['created']);
543
                $wc['username']        = $cdata['username'];
544
                $wc['uid']             = $cdata['uid_fk'];
545
                $wc['myavatar']        = $myavatar;
546
                $wc['myavatar_link']   = $myavatarlink;
547
                $wc['avatar_size']     = smallworld_getImageSize(80, 100, $wc['myavatar_link']);
548
                $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
549
                $wc['cface']           = $swUserHandler->gravatar($cdata['uid_fk']);
550
                $wc['avatar_link']     = $swUserHandler->getAvatarLink($cdata['uid_fk'], $wc['cface']);
551
                $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
552
                $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
553
554 View Code Duplication
                if (Constants::PROFILE_HAS_BOTH == $frC[0] || 1 == $USC['comments']) {
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...
555
                    $GLOBALS['xoopsTpl']->append('comm', $wc);
556
                }
557
            }
558
        }
559
    }
560
}
561