Completed
Pull Request — master (#5)
by Michael
01:37
created

WallUpdates::Insert_Comment()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
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
 * @copyright    The XOOPS Project (https://xoops.org)
19
 * @copyright    2011 Culex
20
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
21
 * @package      SmallWorld
22
 * @since        1.0
23
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
24
 */
25
26
//include_once $GLOBALS['xoops']->path('include/common.php');
27
28
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
29
30
class WallUpdates
31
{
32
    /**
33
     * @return array
34
     */
35
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
36
    {
37
        global $xoopsDB, $xoopsUser;
38
        $data   = [];
39
        $sql    = 'SELECT userid
40
                FROM ' . $xoopsDB->prefix('smallworld_user') . ' su
41
                LEFT JOIN ' . $xoopsDB->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
42
                WHERE xu.uid IN (1)';
43
        $result = $xoopsDB->queryF($sql);
44
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
45
            $data[] = $row;
46
        }
47
48
        return $data;
49
    }
50
51
    /**
52
     * @param $last
53
     * @param $uid
54
     * @param $followers
55
     * @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...
56
     */
57
    public function Updates($last, $uid, $followers)
58
    {
59
        global $xoopsUser, $xoopsDB, $moduleConfig;
60
        $query     = '';
61
        $hm        = smallworld_GetModuleOption('msgtoshow');
62
        $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...
63
        $followers = is_array($followers) ? $followers : [$followers];
64
        $followers = array_unique(Smallworld_array_flatten($followers, 0));
65
66
        $i = 0;
67
        if (0 == $last) {
68
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "'";
69
        } elseif ($last > 0) {
70
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "' AND M.msg_id < '" . $last . "'";
71
        } elseif ('a' === $last) {
72
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U  WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "'";
73
        }
74
75 View Code Duplication
        if (is_array($followers)) {
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...
76
            foreach ($followers as $k => $v) {
77
                if ($last > 0) {
78
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "' and M.msg_id < '" . $last . "'";
79
                } elseif (0 == $last) {
80
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "'";
81
                } elseif ('a' === $last) {
82
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "'";
83
                }
84
                ++$i;
85
            }
86
        }
87 View Code Duplication
        if (!is_array($followers)) {
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...
88
            $followers = $uid;
89
            if ($last > 0) {
90
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "' and M.msg_id < '" . $last . "'";
91
            } elseif (0 == $last) {
92
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "'";
93
            } elseif ('a' === $last) {
94
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "'";
95
            }
96
        }
97
98 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...
99
            $query .= ' order by created DESC LIMIT ' . $hm;
100
        } elseif ('a' === $last) {
101
            $query .= ' order by M.msg_id DESC LIMIT ' . $hm;
102
        } else {
103
            $query .= ' order by created DESC LIMIT ' . $hm;
104
        }
105
        $result = $xoopsDB->queryF($query);
106
        $count  = $xoopsDB->getRowsNum($result);
107
        if (0 == $count) {
108
            return false;
109
        }
110
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
111
            $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...
112
        }
113
        if (!empty($data)) {
114
            return $data;
115
        }
116
    }
117
118
    /**
119
     * @Get comments based on msg id
120
     * @param int $msg_id
121
     * @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...
122
     */
123
    public function Comments($msg_id)
124
    {
125
        global $xoopsUser, $xoopsDB;
126
        $query  = 'SELECT C.msg_id_fk, C.com_id, C.uid_fk, C.comment, C.created, U.username FROM ' . $xoopsDB->prefix('smallworld_comments') . ' C, ' . $xoopsDB->prefix('smallworld_user') . " U WHERE C.uid_fk=U.userid AND C.msg_id_fk='" . $msg_id . "' ORDER BY C.com_id ASC ";
127
        $result = $xoopsDB->queryF($query);
128
        $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...
129
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
130
            $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...
131
        }
132
        if (!empty($data)) {
133
            return $data;
134
        }
135
    }
136
137
    /**
138
     * @Get user image based on uid
139
     * @param int $uid
140
     * @return string
141
     */
142 View Code Duplication
    public function Gravatar($uid)
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...
143
    {
144
        global $xoopsUser, $xoopsDB;
145
        $image  = '';
146
        $sql    = 'SELECT userimage FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
147
        $result = $xoopsDB->queryF($sql);
148
        while (false !== ($r = $xoopsDB->fetchArray($result))) {
149
            $image = $r['userimage'];
150
        }
151
152
        $image = ('' == $image || 'blank.gif' === $image) ? smallworld_getAvatarLink($uid, $image) : $image;
153
154
        $type = [
155
            1 => 'jpg',
156
            2 => 'jpeg',
157
            3 => 'png',
158
            4 => 'gif',
159
        ];
160
161
        $ext = explode('.', $image);
162
163
        if (@!in_array(mb_strtolower($ext[1]), $type) || '' == $image) {
164
            $avatar = '';
165
        } else {
166
            $avatar = $image;
167
        }
168
169
        return $avatar;
170
    }
171
172
    /**
173
     * @Insert update
174
     * @param int          $uid
175
     * @param string|array $update
176
     * @param int          $priv
177
     * @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...
178
     */
179
    public function Insert_Update($uid, $update, $priv)
180
    {
181
        global $xoopsUser, $xoopsDB;
182
        $update = Smallworld_sanitize(htmlentities($update, ENT_QUOTES, 'UTF-8'));
183
        $time   = time();
184
        if (!isset($priv)) {
185
            $priv = 0;
186
        }
187
        $query  = 'SELECT msg_id,message FROM ' . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk='" . $uid . "' ORDER BY msg_id DESC LIMIT 1";
188
        $result = $xoopsDB->queryF($query);
189
        $row    = $xoopsDB->fetchArray($result);
190
        if ($update != $row['message']) {
191
            $query    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_messages') . " (message, uid_fk, priv, created) VALUES ('" . $update . "', '" . $uid . "', '" . $priv . "', '" . $time . "')";
192
            $result   = $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...
193
            $newquery = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM ' . $xoopsDB->prefix('smallworld_messages') . ' M, ' . $xoopsDB->prefix('smallworld_user') . " U WHERE M.uid_fk=U.userid AND M.uid_fk='" . $uid . "' ORDER BY M.msg_id DESC LIMIT 1 ";
194
            $result2  = $xoopsDB->queryF($newquery);
195
            while (false !== ($row = $xoopsDB->fetchArray($result2))) {
196
                $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...
197
            }
198
            $count = $xoopsDB->getRowsNum($result2);
199
            if ($count < 1) {
200
                return false;
201
            }
202
            while (false !== ($row = $xoopsDB->fetchArray($result2))) {
203
                $data[] = $row;
0 ignored issues
show
Bug introduced by
The variable $data 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...
204
            }
205
            if (!empty($data)) {
206
                return $data;
207
            }
208
        }
209
    }
210
211
    /**
212
     * @Insert comment
213
     * @param int          $uid
214
     * @param int          $msg_id
215
     * @param string|array $comment
216
     * @return string|bool
217
     */
218
    public function Insert_Comment($uid, $msg_id, $comment)
219
    {
220
        global $xoopsUser, $xoopsDB;
221
        $comment = Smallworld_sanitize(htmlentities($comment, ENT_QUOTES, 'UTF-8'));
222
        $time    = time();
223
        $query   = 'SELECT com_id,comment FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk='" . $uid . "' AND msg_id_fk='" . $msg_id . "' ORDER BY com_id DESC LIMIT 1 ";
224
        $result  = $xoopsDB->fetchArray($query);
225
        if ($comment != $result['comment']) {
226
            $query    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_comments') . " (comment, uid_fk,msg_id_fk,created) VALUES ('" . $comment . "', '" . $uid . "','" . $msg_id . "', '" . $time . "')";
227
            $result   = $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...
228
            $newquery = 'SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username FROM '
229
                        . $xoopsDB->prefix('smallworld_comments')
230
                        . ' C, '
231
                        . $xoopsDB->prefix('smallworld_user')
232
                        . " U WHERE C.uid_fk=U.userid AND C.uid_fk='"
233
                        . $uid
234
                        . "' AND C.msg_id_fk='"
235
                        . $msg_id
236
                        . "' ORDER BY C.com_id DESC LIMIT 1 ";
237
            $result2  = $xoopsDB->queryF($newquery);
238
            while (false !== ($row = $xoopsDB->fetchArray($result2))) {
239
                $data[0] = $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...
240
            }
241
242
            return $data[0];
0 ignored issues
show
Bug introduced by
The variable $data 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...
243
        }
244
245
        return false;
246
    }
247
248
    /**
249
     * @Get array of users followers
250
     * @param int $me
251
     * @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...
252
     */
253
    public function getFollowers($me)
254
    {
255
        global $xoopsDB, $xoopsUser;
256
        $query  = 'SELECT you FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . $me . "'";
257
        $result = $xoopsDB->queryF($query);
258
        $i      = $xoopsDB->getRowsNum($result);
259
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
260
            $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...
261
        }
262
        if (0 == $i) {
263
            $data = [$me];
264
        }
265
        if (!empty($data)) {
266
            return $data;
267
        }
268
    }
269
270
    /**
271
     * @count all votes
272
     * @param int $type
273
     * @param int $val
274
     * @param int $msgid
275
     * @return int
276
     */
277 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...
278
    {
279
        global $xoopsUser, $xoopsDB;
280
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where msg_id = '" . $msgid . "' and com_id = '0'";
281
        $result = $xoopsDB->queryF($query);
282
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
283
            $sum = $row['sum'];
284
        }
285
        if ('' == $sum) {
0 ignored issues
show
Bug introduced by
The variable $sum does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
286
            $sum = '0';
287
        }
288
289
        return $sum;
290
    }
291
292
    /**
293
     * @Count comments votes
294
     * @param int $type
295
     * @param int $val
296
     * @param int $comid
297
     * @param int $msgid
298
     * @returns int
299
     * @return mixed|string
300
     * @return mixed|string
301
     */
302 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...
303
    {
304
        global $xoopsUser, $xoopsDB;
305
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
306
        $result = $xoopsDB->queryF($query);
307
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
308
            $sum = $row['sum'];
309
        }
310
        if ('' == $sum) {
0 ignored issues
show
Bug introduced by
The variable $sum does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
311
            $sum = '0';
312
        }
313
314
        return $sum;
315
    }
316
317
    /**
318
     * @Check if user is friend
319
     * @param int    $userid
320
     * @param string $type
321
     * @param int    $comid
322
     * @param int    $msgid
323
     * @return int
324
     */
325 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...
326
    {
327
        global $xoopsUser, $xoopsDB;
328
        if ('msg' === $type) {
329
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
330
            $result = $xoopsDB->queryF($sql);
331
            $i      = $xoopsDB->getRowsNum($result);
332
        } else {
333
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
334
            $result = $xoopsDB->queryF($sql);
335
            $i      = $xoopsDB->getRowsNum($result);
336
        }
337
338
        return $i;
339
    }
340
341
    /**
342
     * @count messages per user
343
     * @param int $userid
344
     * @return int
345
     */
346 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...
347
    {
348
        global $xoopsDB;
349
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
350
        $result = $xoopsDB->queryF($sql);
351
        $sum    = $xoopsDB->fetchRow($result);
352
353
        return $sum[0];
354
    }
355
356
    /**
357
     * @Show permaling updates
358
     * @param int $updid
359
     * @param int $uid
360
     * @param int $ownerID
361
     * @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...
362
     */
363 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...
364
    {
365
        global $xoopsUser, $xoopsDB, $moduleConfig;
366
        $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 . "'";
367
        $query  .= " AND M.msg_id = '" . $updid . "'";
368
        $query  .= ' order by M.created DESC LIMIT 1';
369
        $result = $xoopsDB->queryF($query);
370
        $count  = $xoopsDB->getRowsNum($result);
371
        if ($count < 1) {
372
            return false;
373
        }
374
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
375
            $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...
376
        }
377
        if (!empty($data)) {
378
            return $data;
379
        }
380
    }
381
382
    /**
383
     * @Get share link
384
     * @param int $updid
385
     * @param int $ownerID
386
     * @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...
387
     */
388 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...
389
    {
390
        global $xoopsUser, $xoopsDB, $moduleConfig, $xoopsLogger;
391
        $xoopsLogger->activated = false;
392
        //error_reporting(E_ALL);
393
        $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";
394
        $query  .= " AND M.msg_id = '" . $updid . "'";
395
        $query  .= ' order by created DESC LIMIT 1';
396
        $result = $xoopsDB->queryF($query);
397
        $count  = $xoopsDB->getRowsNum($result);
398
        if ($count < 1) {
399
            return false;
400
        }
401
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
402
            $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...
403
        }
404
        if (!empty($data)) {
405
            return $data;
406
        }
407
    }
408
409
    /**
410
     * @Get sharing link
411
     * @param int $id
412
     * @param int $priv
413
     * @return string
414
     */
415 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...
416
    {
417
        if (1 != $priv) {
418
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
419
            $text .= "<a class='share' id='share-page" . $id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
420
        } else {
421
            $text = '';
422
        }
423
424
        return $text;
425
    }
426
427
    /**
428
     * @Get content for sharing div
429
     * @param int    $id
430
     * @param int    $priv
431
     * @param string $permalink
432
     * @param string $desc
433
     * @param string $username
434
     * @return string
435
     */
436 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...
437
    {
438
        if (1 != $priv) {
439
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . $id . "'>";
440
            $text .= "<span name='share-page" . $id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>";
441
            $text .= '</span></div>';
442
        } else {
443
            $text = '';
444
        }
445
446
        return $text;
447
    }
448
449
    /**
450
     * @Parse update and comments array to template for public updates
451
     * @param array $updatesarray
452
     * @param int   $id
453
     */
454
    public function ParsePubArray($updatesarray, $id)
455
    {
456
        global $xoopsUser, $xoopsTpl, $tpl, $xoopsModule, $xoopsConfig;
457
458
        $check         = new User();
459
        $dBase         = new SwDatabase();
460
        $profile       = $xoopsUser ? $check->checkIfProfile($id) : 0;
461
        $moduleHandler = xoops_getHandler('module');
462
        $module        = $moduleHandler->getByDirname('smallworld');
463
        $configHandler = xoops_getHandler('config');
464
        $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...
465
466
        $myavatar          = $this->Gravatar($id);
467
        $myavatarlink      = smallworld_getAvatarLink($id, $myavatar);
468
        $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...
469
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 35);
470
471
        $xoopsTpl->assign('myavatar', $myavatar);
472
        $xoopsTpl->assign('myavatarlink', $myavatarlink);
473
        $xoopsTpl->assign('myavatar_highwide', $myavatar_highwide);
474
475 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...
476
            foreach ($updatesarray as $data) {
477
                // Is update's user a friend ?
478
                $frU = $check->friendcheck($id, $data['uid_fk']);
479
480
                $USW             = [];
481
                $USW['posts']    = 0;
482
                $USW['comments'] = 0;
483
484
                if ($xoopsUser) {
485
                    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $data['uid_fk'] == $id) {
486
                        $USW['posts']    = 1;
487
                        $USW['comments'] = 1;
488
                        $frU[0]          = 2;
489
                    } else {
490
                        $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
491
                    }
492
                }
493
494
                if (!$xoopsUser) {
495
                    $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
496
                }
497
498
                $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...
499
                $wm['orimessage']      = (1 == $USW['posts'] || $profile >= 2) ? str_replace(["\r", "\n"], '', Smallworld_stripWordsKeepUrl($data['message'])) : '';
0 ignored issues
show
Bug introduced by
The variable $wm does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
500
                $wm['message']         = (1 == $USW['posts'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
501
                $wm['message']         = Smallworld_cleanup($wm['message']);
502
                $wm['created']         = smallworld_time_stamp($data['created']);
503
                $wm['username']        = $data['username'];
504
                $wm['uid_fk']          = $data['uid_fk'];
505
                $wm['priv']            = $data['priv'];
506
                $wm['avatar']          = $this->Gravatar($data['uid_fk']);
507
                $wm['avatar_link']     = smallworld_getAvatarLink($data['uid_fk'], $wm['avatar']);
508
                $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...
509
                $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
510
                $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
511
                $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
512
                $wm['sharelinkurl']    = XOOPS_URL . '/modules/smallworld/smallworldshare.php?ownerid=' . $data['uid_fk'];
513
                $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
514
                $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $xoopsConfig['sitename'];
515
                if (1 == $USW['posts'] || $profile >= 2) {
516
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], $wm['priv']);
517
                } else {
518
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], 1);
519
                }
520
521
                if (1 == $USW['posts'] || $profile >= 2) {
522
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
523
                } else {
524
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
525
                }
526
                $wm['linkimage']     = XOOPS_URL . '/modules/smallworld/assets/images/link.png';
527
                $wm['permalink']     = XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $data['uid_fk'] . '&updid=' . $data['msg_id'];
528
                $wm['commentsarray'] = $this->Comments($data['msg_id']);
529
530
                if (2 == $frU[0] || 1 == $USW['posts']) {
531
                    $xoopsTpl->append('walldata', $wm);
532
                }
533
534
                if (!empty($wm['commentsarray'])) {
535
                    foreach ($wm['commentsarray'] as $cdata) {
536
                        // Is commentuser a friend ?
537
                        $frC = $check->friendcheck($id, $cdata['uid_fk']);
538
539
                        $USC             = [];
540
                        $USC['posts']    = 0;
541
                        $USC['comments'] = 0;
542
543
                        if ($xoopsUser) {
544
                            if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $cdata['uid_fk'] == $id) {
545
                                $USC['posts']    = 1;
546
                                $USC['comments'] = 1;
547
                                $frC[0]          = 2;
548
                            } else {
549
                                $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
550
                            }
551
                        }
552
553
                        if (!$xoopsUser) {
554
                            $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
555
                        }
556
557
                        $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...
558
                        $wc['com_id']          = $cdata['com_id'];
559
                        $wc['comment']         = (1 == $USC['comments'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
560
                        $wc['comment']         = Smallworld_cleanup($wc['comment']);
561
                        $wc['time']            = smallworld_time_stamp($cdata['created']);
562
                        $wc['username']        = $cdata['username'];
563
                        $wc['uid']             = $cdata['uid_fk'];
564
                        $wc['myavatar']        = $this->Gravatar($id);
565
                        $wc['myavatar_link']   = $myavatarlink;
566
                        $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...
567
                        $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
568
                        $wc['cface']           = $this->Gravatar($cdata['uid_fk']);
569
                        $wc['avatar_link']     = smallworld_getAvatarLink($cdata['uid_fk'], $wc['cface']);
570
                        $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
571
                        $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
572
573
                        if (2 == $frC[0] || 1 == $USC['comments']) {
574
                            $xoopsTpl->append('comm', $wc);
575
                        }
576
                    }
577
                }
578
            }
579
        }
580
    }
581
}
582