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

WallUpdates::ParsePubArray()   F

Complexity

Conditions 28
Paths > 20000

Size

Total Lines 127

Duplication

Lines 105
Ratio 82.68 %

Importance

Changes 0
Metric Value
cc 28
nc 76804
nop 2
dl 105
loc 127
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
/**
4
 * You may not change or alter any portion of this comment or credits
5
 * of supporting developers from this source code or any supporting source code
6
 * which is considered copyrighted (c) material of the original comment or credit authors.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 */
12
13
/**
14
 * SmallWorld
15
 *
16
 * @copyright    The XOOPS Project (https://xoops.org)
17
 * @copyright    2011 Culex
18
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
19
 * @package      SmallWorld
20
 * @since        1.0
21
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
22
 */
23
24
//include_once $GLOBALS['xoops']->path('include/common.php');
25
26
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
27
28
class WallUpdates
29
{
30
    /**
31
     * @return array
32
     */
33
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
34
    {
35
        global $xoopsDB, $xoopsUser;
36
        $data   = [];
37
        $sql    = 'SELECT userid
38
                FROM ' . $xoopsDB->prefix('smallworld_user') . ' su
39
                LEFT JOIN ' . $xoopsDB->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
40
                WHERE xu.uid IN (1)';
41
        $result = $xoopsDB->queryF($sql);
42
        while ($row = $xoopsDB->fetchArray($result)) {
43
            $data[] = $row;
44
        }
45
        return $data;
46
    }
47
48
    /**
49
     * @param $last
50
     * @param $uid
51
     * @param $followers
52
     * @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...
53
     */
54
    public function Updates($last, $uid, $followers)
55
    {
56
        global $xoopsUser, $xoopsDB, $moduleConfig;
57
        $query     = '';
58
        $hm        = smallworld_GetModuleOption('msgtoshow');
59
        $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...
60
        $followers = is_array($followers) ? $followers : [$followers];
61
        $followers = array_unique(Smallworld_array_flatten($followers, 0));
62
63
        $i = 0;
64
        if (0 == $last) {
65
            $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 . "'";
66
        } elseif ($last > 0) {
67
            $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 . "'";
68
        } elseif ('a' === $last) {
69
            $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 . "'";
70
        }
71
72 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...
73
            foreach ($followers as $k => $v) {
74
                if ($last > 0) {
75
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "' and M.msg_id < '" . $last . "'";
76
                } elseif (0 == $last) {
77
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "'";
78
                } elseif ('a' === $last) {
79
                    $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $v . "'";
80
                }
81
                ++$i;
82
            }
83
        }
84 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...
85
            $followers = $uid;
86
            if ($last > 0) {
87
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "' and M.msg_id < '" . $last . "'";
88
            } elseif (0 == $last) {
89
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "'";
90
            } elseif ('a' === $last) {
91
                $query .= " OR M.uid_fk=U.userid and M.uid_fk= '" . $followers . "'";
92
            }
93
        }
94
95 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...
96
            $query .= ' order by created DESC LIMIT ' . $hm;
97
        } elseif ('a' === $last) {
98
            $query .= ' order by M.msg_id DESC LIMIT ' . $hm;
99
        } else {
100
            $query .= ' order by created DESC LIMIT ' . $hm;
101
        }
102
        $result = $xoopsDB->queryF($query);
103
        $count  = $xoopsDB->getRowsNum($result);
104
        if (0 == $count) {
105
            return false;
106
        } else {
107
            while ($row = $xoopsDB->fetchArray($result)) {
108
                $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...
109
            }
110
            if (!empty($data)) {
111
                return $data;
112
            }
113
        }
114
    }
115
116
    /**
117
     * @Get comments based on msg id
118
     * @param int $msg_id
119
     * @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...
120
     */
121
    public function Comments($msg_id)
122
    {
123
        global $xoopsUser, $xoopsDB;
124
        $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 ";
125
        $result = $xoopsDB->queryF($query);
126
        $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...
127
        while ($row = $xoopsDB->fetchArray($result)) {
128
            $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
129
        }
130
        if (!empty($data)) {
131
            return $data;
132
        }
133
    }
134
135
    /**
136
     * @Get user image based on uid
137
     * @param int $uid
138
     * @return string
139
     */
140 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...
141
    {
142
        global $xoopsUser, $xoopsDB;
143
        $image  = '';
144
        $sql    = 'SELECT userimage FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
145
        $result = $xoopsDB->queryF($sql);
146
        while ($r = $xoopsDB->fetchArray($result)) {
147
            $image = $r['userimage'];
148
        }
149
150
        $image = ('' == $image || 'blank.gif' === $image) ? smallworld_getAvatarLink($uid, $image) : $image;
151
152
        $type = [
153
            1 => 'jpg',
154
            2 => 'jpeg',
155
            3 => 'png',
156
            4 => 'gif',
157
        ];
158
159
        $ext = explode('.', $image);
160
161
        if (@!in_array(strtolower($ext[1]), $type) || '' == $image) {
162
            $avatar = '';
163
        } else {
164
            $avatar = $image;
165
        }
166
        return $avatar;
167
    }
168
169
    /**
170
     * @Insert update
171
     * @param int          $uid
172
     * @param string|array $update
173
     * @param int          $priv
174
     * @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...
175
     */
176
    public function Insert_Update($uid, $update, $priv)
177
    {
178
        global $xoopsUser, $xoopsDB;
179
        $update = Smallworld_sanitize(htmlentities($update, ENT_QUOTES, 'UTF-8'));
180
        $time   = time();
181
        if (!isset($priv)) {
182
            $priv = 0;
183
        }
184
        $query  = 'SELECT msg_id,message FROM ' . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk='" . $uid . "' ORDER BY msg_id DESC LIMIT 1";
185
        $result = $xoopsDB->queryF($query);
186
        $row    = $xoopsDB->fetchArray($result);
187
        if ($update != $row['message']) {
188
            $query    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_messages') . " (message, uid_fk, priv, created) VALUES ('" . $update . "', '" . $uid . "', '" . $priv . "', '" . $time . "')";
189
            $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...
190
            $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 ";
191
            $result2  = $xoopsDB->queryF($newquery);
192
            while ($row = $xoopsDB->fetchArray($result2)) {
193
                $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...
194
            }
195
            $count = $xoopsDB->getRowsNum($result2);
196
            if ($count < 1) {
197
                return false;
198
            } else {
199
                while ($row = $xoopsDB->fetchArray($result2)) {
200
                    $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...
201
                }
202
                if (!empty($data)) {
203
                    return $data;
204
                }
205
            }
206
        }
207
    }
208
209
    /**
210
     * @Insert comment
211
     * @param int          $uid
212
     * @param int          $msg_id
213
     * @param string|array $comment
214
     * @return string / void
215
     */
216
    public function Insert_Comment($uid, $msg_id, $comment)
217
    {
218
        global $xoopsUser, $xoopsDB;
219
        $comment = Smallworld_sanitize(htmlentities($comment, ENT_QUOTES, 'UTF-8'));
220
        $time    = time();
221
        $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 ";
222
        $result  = $xoopsDB->fetchArray($query);
223
        if ($comment != $result['comment']) {
224
            $query    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_comments') . " (comment, uid_fk,msg_id_fk,created) VALUES ('" . $comment . "', '" . $uid . "','" . $msg_id . "', '" . $time . "')";
225
            $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...
226
            $newquery = 'SELECT C.com_id, C.uid_fk, C.comment, C.msg_id_fk, C.created, U.username FROM '
227
                        . $xoopsDB->prefix('smallworld_comments')
228
                        . ' C, '
229
                        . $xoopsDB->prefix('smallworld_user')
230
                        . " U WHERE C.uid_fk=U.userid AND C.uid_fk='"
231
                        . $uid
232
                        . "' AND C.msg_id_fk='"
233
                        . $msg_id
234
                        . "' ORDER BY C.com_id DESC LIMIT 1 ";
235
            $result2  = $xoopsDB->queryF($newquery);
236
            while ($row = $xoopsDB->fetchArray($result2)) {
237
                $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...
238
            }
239
            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...
240
        } else {
241
            return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by XoopsModules\Smallworld\...Updates::Insert_Comment of type string.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
242
        }
243
    }
244
245
    /**
246
     * @Get array of users followers
247
     * @param int $me
248
     * @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...
249
     */
250
    public function getFollowers($me)
251
    {
252
        global $xoopsDB, $xoopsUser;
253
        $query  = 'SELECT you FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . $me . "'";
254
        $result = $xoopsDB->queryF($query);
255
        $i      = $xoopsDB->getRowsNum($result);
256
        while ($row = $xoopsDB->fetchArray($result)) {
257
            $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...
258
        }
259
        if (0 == $i) {
260
            $data = [$me];
261
        }
262
        if (!empty($data)) {
263
            return $data;
264
        }
265
    }
266
267
    /**
268
     * @count all votes
269
     * @param int $type
270
     * @param int $val
271
     * @param int $msgid
272
     * @return int
273
     */
274 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...
275
    {
276
        global $xoopsUser, $xoopsDB;
277
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where msg_id = '" . $msgid . "' and com_id = '0'";
278
        $result = $xoopsDB->queryF($query);
279
        while ($row = $xoopsDB->fetchArray($result)) {
280
            $sum = $row['sum'];
281
        }
282
        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...
283
            $sum = '0';
284
        }
285
        return $sum;
286
    }
287
288
    /**
289
     * @Count comments votes
290
     * @param int $type
291
     * @param int $val
292
     * @param int $comid
293
     * @param int $msgid
294
     * @returns int
295
     * @return mixed|string
296
     * @return mixed|string
297
     */
298 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...
299
    {
300
        global $xoopsUser, $xoopsDB;
301
        $query  = 'Select SUM(' . $val . ') as sum from ' . $xoopsDB->prefix('smallworld_vote') . " where com_id = '" . $comid . "' AND msg_id = '" . $msgid . "'";
302
        $result = $xoopsDB->queryF($query);
303
        while ($row = $xoopsDB->fetchArray($result)) {
304
            $sum = $row['sum'];
305
        }
306
        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...
307
            $sum = '0';
308
        }
309
        return $sum;
310
    }
311
312
    /**
313
     * @Check if user is friend
314
     * @param int    $userid
315
     * @param string $type
316
     * @param int    $comid
317
     * @param int    $msgid
318
     * @return int
319
     */
320 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...
321
    {
322
        global $xoopsUser, $xoopsDB;
323
        if ('msg' === $type) {
324
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '0' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
325
            $result = $xoopsDB->queryF($sql);
326
            $i      = $xoopsDB->getRowsNum($result);
327
        } else {
328
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_vote') . " WHERE com_id = '" . $comid . "' AND msg_id = '" . $msgid . "' AND user_id = '" . $userid . "'";
329
            $result = $xoopsDB->queryF($sql);
330
            $i      = $xoopsDB->getRowsNum($result);
331
        }
332
        return $i;
333
    }
334
335
    /**
336
     * @count messages per user
337
     * @param int $userid
338
     * @return int
339
     */
340 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...
341
    {
342
        global $xoopsDB;
343
        $sql    = 'SELECT (SELECT COUNT(*) FROM ' . $xoopsDB->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "') + (SELECT COUNT(*) FROM " . $xoopsDB->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "')";
344
        $result = $xoopsDB->queryF($sql);
345
        $sum    = $xoopsDB->fetchRow($result);
346
        return $sum[0];
347
    }
348
349
    /**
350
     * @Show permaling updates
351
     * @param int $updid
352
     * @param int $uid
353
     * @param int $ownerID
354
     * @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...
355
     */
356 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...
357
    {
358
        global $xoopsUser, $xoopsDB, $moduleConfig;
359
        $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 . "'";
360
        $query  .= " AND M.msg_id = '" . $updid . "'";
361
        $query  .= ' order by M.created DESC LIMIT 1';
362
        $result = $xoopsDB->queryF($query);
363
        $count  = $xoopsDB->getRowsNum($result);
364
        if ($count < 1) {
365
            return false;
366
        } else {
367
            while ($row = $xoopsDB->fetchArray($result)) {
368
                $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...
369
            }
370
            if (!empty($data)) {
371
                return $data;
372
            }
373
        }
374
    }
375
376
    /**
377
     * @Get share link
378
     * @param int $updid
379
     * @param int $ownerID
380
     * @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...
381
     */
382 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...
383
    {
384
        global $xoopsUser, $xoopsDB, $moduleConfig, $xoopsLogger;
385
        $xoopsLogger->activated = false;
386
        //error_reporting(E_ALL);
387
        $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";
388
        $query  .= " AND M.msg_id = '" . $updid . "'";
389
        $query  .= ' order by created DESC LIMIT 1';
390
        $result = $xoopsDB->queryF($query);
391
        $count  = $xoopsDB->getRowsNum($result);
392
        if ($count < 1) {
393
            return false;
394
        } else {
395
            while ($row = $xoopsDB->fetchArray($result)) {
396
                $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...
397
            }
398
            if (!empty($data)) {
399
                return $data;
400
            }
401
        }
402
    }
403
404
    /**
405
     * @Get sharing link
406
     * @param int $id
407
     * @param int $priv
408
     * @return string
409
     */
410 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...
411
    {
412
        if (1 != $priv) {
413
            $text = " | <span class='smallworld_share' id='smallworld_share'>";
414
            $text .= "<a class='share' id='share-page" . $id . "' href='javascript:void(0);'>" . _SMALLWORLD_SHARELINK . '</a></span>';
415
        } else {
416
            $text = '';
417
        }
418
        return $text;
419
    }
420
421
    /**
422
     * @Get content for sharing div
423
     * @param int    $id
424
     * @param int    $priv
425
     * @param string $permalink
426
     * @param string $desc
427
     * @param string $username
428
     * @return string
429
     */
430 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...
431
    {
432
        if (1 != $priv) {
433
            $text = "<div style='display: none;' class='smallworld_bookmarks' id='share-page' name='share-page" . $id . "'>";
434
            $text .= "<span name='share-page" . $id . "' rel1='" . $desc . "' rel2= '" . $username . "' rel=" . $permalink . " id='basicBookmark' title='" . _SMALLWORLD_SHAREBOX_TITLE . "'>";
435
            $text .= '</span></div>';
436
        } else {
437
            $text = '';
438
        }
439
        return $text;
440
    }
441
442
    /**
443
     * @Parse update and comments array to template for public updates
444
     * @param array $updatesarray
445
     * @param int   $id
446
     * @return void
447
     */
448
    public function ParsePubArray($updatesarray, $id)
449
    {
450
        global $xoopsUser, $xoopsTpl, $tpl, $xoopsModule, $xoopsConfig;
451
452
        $check         = new User();
453
        $dBase         = new SwDatabase();
454
        $profile       = $xoopsUser ? $check->checkIfProfile($id) : 0;
455
        $moduleHandler = xoops_getHandler('module');
456
        $module        = $moduleHandler->getByDirname('smallworld');
457
        $configHandler = xoops_getHandler('config');
458
        $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...
459
460
        $myavatar          = $this->Gravatar($id);
461
        $myavatarlink      = smallworld_getAvatarLink($id, $myavatar);
462
        $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...
463
        $myavatar_highwide = smallworld_imageResize($myavatar_size[0], $myavatar_size[1], 35);
464
465
        $xoopsTpl->assign('myavatar', $myavatar);
466
        $xoopsTpl->assign('myavatarlink', $myavatarlink);
467
        $xoopsTpl->assign('myavatar_highwide', $myavatar_highwide);
468
469 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...
470
            foreach ($updatesarray as $data) {
471
                // Is update's user a friend ?
472
                $frU = $check->friendcheck($id, $data['uid_fk']);
473
474
                $USW             = [];
475
                $USW['posts']    = 0;
476
                $USW['comments'] = 0;
477
478
                if ($xoopsUser) {
479
                    if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $data['uid_fk'] == $id) {
480
                        $USW['posts']    = 1;
481
                        $USW['comments'] = 1;
482
                        $frU[0]          = 2;
483
                    } else {
484
                        $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
485
                    }
486
                }
487
488
                if (!$xoopsUser) {
489
                    $USW = json_decode($dBase->GetSettings($data['uid_fk']), true);
490
                }
491
492
                $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...
493
                $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...
494
                $wm['message']         = (1 == $USW['posts'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($data['message']), $data['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETPOSTS;
495
                $wm['message']         = Smallworld_cleanup($wm['message']);
496
                $wm['created']         = smallworld_time_stamp($data['created']);
497
                $wm['username']        = $data['username'];
498
                $wm['uid_fk']          = $data['uid_fk'];
499
                $wm['priv']            = $data['priv'];
500
                $wm['avatar']          = $this->Gravatar($data['uid_fk']);
501
                $wm['avatar_link']     = smallworld_getAvatarLink($data['uid_fk'], $wm['avatar']);
502
                $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...
503
                $wm['avatar_highwide'] = smallworld_imageResize($wm['avatar_size'][0], $wm['avatar_size'][1], 50);
504
                $wm['vote_up']         = $this->countVotes('msg', 'up', $data['msg_id']);
505
                $wm['vote_down']       = $this->countVotes('msg', 'down', $data['msg_id']);
506
                $wm['sharelinkurl']    = XOOPS_URL . '/modules/smallworld/smallworldshare.php?ownerid=' . $data['uid_fk'];
507
                $wm['sharelinkurl']    .= '&updid=' . $data['msg_id'] . '';
508
                $wm['usernameTitle']   = $wm['username'] . _SMALLWORLD_UPDATEONSITEMETA . $xoopsConfig['sitename'];
509
                if (1 == $USW['posts'] || $profile >= 2) {
510
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], $wm['priv']);
511
                } else {
512
                    $wm['sharelink'] = $this->GetSharing($wm['msg_id'], 1);
513
                }
514
515
                if (1 == $USW['posts'] || $profile >= 2) {
516
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], $wm['priv'], $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
517
                } else {
518
                    $wm['sharediv'] = $this->GetSharingDiv($wm['msg_id'], 1, $wm['sharelinkurl'], $wm['orimessage'], $wm['usernameTitle']);
519
                }
520
                $wm['linkimage']     = XOOPS_URL . '/modules/smallworld/assets/images/link.png';
521
                $wm['permalink']     = XOOPS_URL . '/modules/smallworld/permalink.php?ownerid=' . $data['uid_fk'] . '&updid=' . $data['msg_id'];
522
                $wm['commentsarray'] = $this->Comments($data['msg_id']);
523
524
                if (2 == $frU[0] || 1 == $USW['posts']) {
525
                    $xoopsTpl->append('walldata', $wm);
526
                }
527
528
                if (!empty($wm['commentsarray'])) {
529
                    foreach ($wm['commentsarray'] as $cdata) {
530
                        // Is commentuser a friend ?
531
                        $frC = $check->friendcheck($id, $cdata['uid_fk']);
532
533
                        $USC             = [];
534
                        $USC['posts']    = 0;
535
                        $USC['comments'] = 0;
536
537
                        if ($xoopsUser) {
538
                            if ($xoopsUser->isAdmin($xoopsModule->getVar('mid')) || $cdata['uid_fk'] == $id) {
539
                                $USC['posts']    = 1;
540
                                $USC['comments'] = 1;
541
                                $frC[0]          = 2;
542
                            } else {
543
                                $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
544
                            }
545
                        }
546
547
                        if (!$xoopsUser) {
548
                            $USC = json_decode($dBase->GetSettings($cdata['uid_fk']), true);
549
                        }
550
551
                        $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...
552
                        $wc['com_id']          = $cdata['com_id'];
553
                        $wc['comment']         = (1 == $USC['comments'] || $profile >= 2) ? smallworld_tolink(htmlspecialchars_decode($cdata['comment']), $cdata['uid_fk']) : _SMALLWORLD_MESSAGE_PRIVSETCOMMENTS;
554
                        $wc['comment']         = Smallworld_cleanup($wc['comment']);
555
                        $wc['time']            = smallworld_time_stamp($cdata['created']);
556
                        $wc['username']        = $cdata['username'];
557
                        $wc['uid']             = $cdata['uid_fk'];
558
                        $wc['myavatar']        = $this->Gravatar($id);
559
                        $wc['myavatar_link']   = $myavatarlink;
560
                        $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...
561
                        $wc['avatar_highwide'] = smallworld_imageResize($wc['avatar_size'][0], $wc['avatar_size'][1], 35);
562
                        $wc['cface']           = $this->Gravatar($cdata['uid_fk']);
563
                        $wc['avatar_link']     = smallworld_getAvatarLink($cdata['uid_fk'], $wc['cface']);
564
                        $wc['vote_up']         = $this->countVotesCom('com', 'up', $cdata['msg_id_fk'], $cdata['com_id']);
565
                        $wc['vote_down']       = $this->countVotesCom('com', 'down', $cdata['msg_id_fk'], $cdata['com_id']);
566
567
                        if (2 == $frC[0] || 1 == $USC['comments']) {
568
                            $xoopsTpl->append('comm', $wc);
569
                        }
570
                    }
571
                }
572
            }
573
        }
574
    }
575
}
576