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

PublicWallUpdates::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 namespace Xoopsmodules\smallworld;
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
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
23
24
class PublicWallUpdates
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) {
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) {
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 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...
210
    {
211
        global $xoopsUser, $xoopsDB;
212
        $sum = 0;
213
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where msg_id = '" . $msgid . "' and com_id = '0'";
214
        $result = $xoopsDB->queryF($query);
215
        while ($row = $xoopsDB->fetchArray($result)) {
216
            $sum = $row['sum'];
217
        }
218
        if ('' == $sum) {
219
            $sum = 0;
220
        }
221
        return $sum;
222
    }
223
224
    /**
225
     * @Count comments votes
226
     * @param int $type
227
     * @param int $val
228
     * @param int $comid
229
     * @param int $msgid
230
     * @returns int
231
     */
232 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...
233
    {
234
        global $xoopsUser, $xoopsDB;
235
        $sum = 0;
236
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
237
        $result = $xoopsDB->queryF($query);
238
        while ($row = $xoopsDB->fetchArray($result)) {
239
            $sum = $row['sum'];
240
        }
241
        if ('' == $sum) {
242
            $sum = 0;
243
        }
244
        return $sum;
245
    }
246
247
    /**
248
     * @Check is user is friend
249
     * @param int    $userid
250
     * @param string $type
251
     * @param int    $comid
252
     * @param int    $msgid
253
     * @return int
254
     */
255 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...
256
    {
257
        global $xoopsUser, $xoopsDB;
258
        if ('msg' === $type) {
259
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
260
            $result = $xoopsDB->queryF($sql);
261
            $i      = $xoopsDB->getRowsNum($result);
262
        } else {
263
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
264
            $result = $xoopsDB->queryF($sql);
265
            $i      = $xoopsDB->getRowsNum($result);
266
        }
267
        return $i;
268
    }
269
270
    /**
271
     * @count messages per user
272
     * @param int $userid
273
     * @return int
274
     */
275
    public function CountMsges($userid)
276
    {
277
        global $xoopsDB;
278
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
279
        $result = $xoopsDB->queryF($sql);
280
        $sum    = $xoopsDB->fetchRow($result);
281
        return $sum[0];
282
    }
283
284
    /**
285
     * @Show permaling updates
286
     * @param int $updid
287
     * @param int $uid
288
     * @param int $ownerID
289
     * @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...
290
     */
291
    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...
292
    {
293
        global $xoopsUser, $xoopsDB, $moduleConfig;
294
        $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 . "'";
295
        $query  .= " AND M.msg_id = '" . $updid . "'";
296
        $query  .= ' order by M.created DESC LIMIT 1';
297
        $result = $xoopsDB->queryF($query);
298
        $count  = $xoopsDB->getRowsNum($result);
299
        if ($count < 1) {
300
            return false;
301
        } else {
302
            while ($row = $xoopsDB->fetchArray($result)) {
303
                $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...
304
            }
305
            if (!empty($data)) {
306
                return $data;
307
            }
308
        }
309
    }
310
311
    /**
312
     * @Get share link
313
     * @param int $updid
314
     * @param int $ownerID
315
     * @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...
316
     */
317
    public function UpdatesSharelink($updid, $ownerID)
318
    {
319
        global $xoopsUser, $xoopsDB, $moduleConfig;
320
        $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";
321
        $query  .= " AND M.msg_id = '" . $updid . "'";
322
        $query  .= ' order by created DESC LIMIT 1';
323
        $result = $xoopsDB->queryF($query);
324
        $count  = $xoopsDB->getRowsNum($result);
325
        if ($count < 1) {
326
            return false;
327
        } else {
328
            while ($row = $xoopsDB->fetchArray($result)) {
329
                $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...
330
            }
331
            if (!empty($data)) {
332
                return $data;
333
            }
334
        }
335
    }
336
337
    /**
338
     * @Get sharing link
339
     * @param int $id
340
     * @param int $priv
341
     * @return string
342
     */
343 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...
344
    {
345
        if (1 != $priv) {
346
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
347
            $text .= "<a class='share' id='share-page" . $id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
348
        } else {
349
            $text = '';
350
        }
351
        return $text;
352
    }
353
354
    /**
355
     * @Get content for sharing div
356
     * @param int    $id
357
     * @param int    $priv
358
     * @param string $permalink
359
     * @param string $desc
360
     * @param string $username
361
     * @return string
362
     */
363 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...
364
    {
365
        if (1 != $priv) {
366
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . $id . "'>";
367
            $text .= "<span name='share-page" . $id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>";
368
            $text .= '</span></div>';
369
        } else {
370
            $text = '';
371
        }
372
        return $text;
373
    }
374
375
    /**
376
     * @Parse update and comments array to template for public updates
377
     * @param array $updatesarray
378
     * @param int   $id
379
     * @return void
380
     */
381 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...
382
    {
383
        global $xoopsUser, $xoopsTpl, $tpl, $xoopsModule, $xoopsTpl, $xoopsConfig;
384
        $wm = [];
385
        $check          = new SmallWorldUser;
386
        $dBase          = new SmallWorldDB;
387
        $profile        = $xoopsUser ? $check->checkIfProfile($id) : 0;
388
        $moduleHandler = xoops_getHandler('module');
389
        $module         = $moduleHandler->getByDirname('smallworld');
390
        $configHandler = xoops_getHandler('config');
391
        $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...
392
393
        $myavatar          = $this->Gravatar($id);
394
        $myavatarlink      = smallworld_getAvatarLink($id, $myavatar);
395
        $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...
396
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 100);
397
        $user_img          = "<img src='" . smallworld_getAvatarLink($id, $myavatar) . "' id='smallworld_user_img' " . $myavatar_highwide . '>';
398
399
        $xoopsTpl->assign('myavatar', $myavatar);
400
        $xoopsTpl->assign('myavatarlink', $myavatarlink);
401
        $xoopsTpl->assign('myavatar_highwide', $myavatar_highwide);
402
        $xoopsTpl->assign('avatar', $user_img);
403
404
        if (!empty($updatesarray)) {
405
            foreach ($updatesarray as $data) {
406
407
                // Is update's user a friend ?
408
                $frU = $check->friendcheck($id, $data['uid_fk']);
409
410
                $USW             = [];
411
                $USW['posts']    = 0;
412
                $USW['comments'] = 0;
413
414
                if ($xoopsUser) {
415
                    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $data['uid_fk'] == $id) {
416
                        $USW['posts']    = 1;
417
                        $USW['comments'] = 1;
418
                        $frU[0]          = 2;
419
                    } else {
420
                        $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
421
                    }
422
                }
423
424
                if (!$xoopsUser) {
425
                    $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
426
                }
427
428
                $wm['msg_id']          = $data['msg_id'];
429
                $wm['orimessage']      = (1 == $USW['posts'] || $profile >= 2) ? str_replace(["\r", "\n"], '', Smallworld_stripWordsKeepUrl($data['message'])) : '';
430
                $wm['message']         = (1 == $USW['posts'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
431
                $wm['message']         = Smallworld_cleanup($wm['message']);
432
                $wm['created']         = smallworld_time_stamp($data['created']);
433
                $wm['username']        = $data['username'];
434
                $wm['uid_fk']          = $data['uid_fk'];
435
                $wm['priv']            = $data['priv'];
436
                $wm['avatar']          = $this->Gravatar($data['uid_fk']);
437
                $wm['avatar_link']     = smallworld_getAvatarLink($data['uid_fk'], $wm['avatar']);
438
                $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...
439
                $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
440
                $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
441
                $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
442
                $wm['sharelinkurl']    = XOOPS_URL . '/modules/smallworld/smallworldshare.php?ownerid=' . $data['uid_fk'];
443
                $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
444
                $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $xoopsConfig['sitename'];
445
                if (1 == $USW['posts'] || $profile >= 2) {
446
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], $wm['priv']);
447
                } else {
448
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], 1);
449
                }
450
451
                if (1 == $USW['posts'] || $profile >= 2) {
452
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
453
                } else {
454
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
455
                }
456
                $wm['linkimage']     = XOOPS_URL . '/modules/smallworld/assets/images/link.png';
457
                $wm['permalink']     = XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $data['uid_fk'] . '&updid=' . $data['msg_id'];
458
                $wm['commentsarray'] = $this->Comments($data['msg_id']);
459
460
                if (2 == $frU[0] || 1 == $USW['posts']) {
461
                    $xoopsTpl->append('walldata', $wm);
462
                }
463
464
                if (!empty($wm['commentsarray'])) {
465
                    foreach ($wm['commentsarray'] as $cdata) {
466
                        // Is commentuser a friend ?
467
                        $frC = $check->friendcheck($id, $cdata['uid_fk']);
468
469
                        $USC             = [];
470
                        $USC['posts']    = 0;
471
                        $USC['comments'] = 0;
472
473
                        if ($xoopsUser) {
474
                            if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $cdata['uid_fk'] == $id) {
475
                                $USC['posts']    = 1;
476
                                $USC['comments'] = 1;
477
                                $frC[0]          = 2;
478
                            } else {
479
                                $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
480
                            }
481
                        }
482
483
                        if (!$xoopsUser) {
484
                            $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
485
                        }
486
487
                        $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...
488
                        $wc['com_id']          = $cdata['com_id'];
489
                        $wc['comment']         = (1 == $USC['comments'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
490
                        $wc['comment']         = Smallworld_cleanup($wc['comment']);
491
                        $wc['time']            = smallworld_time_stamp($cdata['created']);
492
                        $wc['username']        = $cdata['username'];
493
                        $wc['uid']             = $cdata['uid_fk'];
494
                        $wc['myavatar']        = $this->Gravatar($id);
495
                        $wc['myavatar_link']   = $myavatarlink;
496
                        $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...
497
                        $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
498
                        $wc['cface']           = $this->Gravatar($cdata['uid_fk']);
499
                        $wc['avatar_link']     = smallworld_getAvatarLink($cdata['uid_fk'], $wc['cface']);
500
                        $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
501
                        $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
502
503
                        if (2 == $frC[0] || 1 == $USC['comments']) {
504
                            $xoopsTpl->append('comm', $wc);
505
                        }
506
                    }
507
                }
508
            }
509
        }
510
    }
511
}
512