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

PublicWallUpdates   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 490
Duplicated Lines 47.14 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 231
loc 490
rs 2.64
c 0
b 0
f 0
wmc 72
lcom 1
cbo 2

14 Methods

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

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like PublicWallUpdates often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PublicWallUpdates, and based on these observations, apply Extract Interface, too.

1
<?php
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
// Moderated and fitted from the tutorial by Srinivas Tamada http://9lessons.info
26
27
class PublicWallUpdates
28
{
29 View Code Duplication
    private function getAdminModerators()
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
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...
30
    {
31
        global $xoopsDB, $xoopsUser;
32
        $sql    = 'SELECT userid
33
                FROM ' . $xoopsDB->prefix('smallworld_user') . ' su
34
                LEFT JOIN ' . $xoopsDB->prefix('groups_users_link') . ' xu ON su.userid = xu.uid
35
                WHERE xu.uid IN (1)';
36
        $result = $xoopsDB->queryF($sql);
37
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
38
            $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...
39
        }
40
    }
41
42
    /**
43
     * Get arry of users being inspected
44
     */
45
    public function inspected()
46
    {
47
        global $xoopsDB;
48
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_admin') . ' WHERE (inspect_start+inspect_stop) > ' . time() . '';
49
        $result = $xoopsDB->queryF($sql);
50
        $data   = [];
51
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
52
            $data[] = $row;
53
        }
54
        if (!empty($data)) {
55
            $sub = implode(',', Smallworld_array_flatten(array_unique($data), 0));
56
        } else {
57
            $sub = 0;
58
        }
59
60
        return $sub;
61
    }
62
63
    /**
64
     * @Get array of updates
65
     * @param int   $last
66
     * @param array $moderators
67
     * @return array|bool
0 ignored issues
show
Documentation introduced by
Should the return type not be false|array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
68
     */
69
    public function Updates($last, $moderators)
70
    {
71
        global $xoopsUser, $xoopsDB, $moduleConfig, $xoopsLogger;
72
        $moderators = is_array($moderators) ? $moderators : [$moderators];
73
        $hm         = smallworld_GetModuleOption('msgtoshow');
74
        $set        = smallworld_checkPrivateOrPublic();
0 ignored issues
show
Unused Code introduced by
$set is not used, you could remove the assignment.

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

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

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

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

Loading history...
75
        $mods       = implode(',', Smallworld_array_flatten(array_unique($moderators), 0));
76
        $inspected  = $this->inspected();
77
        $perm       = smallworld_GetModuleOption('smallworldshowPoPubPage');
0 ignored issues
show
Unused Code introduced by
$perm is not used, you could remove the assignment.

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

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

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

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

Loading history...
78
        $i          = 0;
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

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

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

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

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

Loading history...
79
80
        if (0 == $last) {
81
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
82
                     . $xoopsDB->prefix('smallworld_messages')
83
                     . ' M, '
84
                     . $xoopsDB->prefix('smallworld_user')
85
                     . ' U WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
86
                     . $mods
87
                     . ') AND M.uid_fk NOT IN ('
88
                     . $inspected
89
                     . ") AND M.priv = '0'";
90
        } elseif ($last > 0) {
91
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
92
                     . $xoopsDB->prefix('smallworld_messages')
93
                     . ' M, '
94
                     . $xoopsDB->prefix('smallworld_user')
95
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
96
                     . $mods
97
                     . ') AND M.uid_fk NOT IN ('
98
                     . $inspected
99
                     . ") AND M.priv = '0' AND M.msg_id < '"
100
                     . $last
101
                     . "'";
102
        } elseif ('a' == $last) {
103
            $query = 'SELECT M.msg_id, M.uid_fk, M.priv, M.message, M.created, U.username FROM '
104
                     . $xoopsDB->prefix('smallworld_messages')
105
                     . ' M, '
106
                     . $xoopsDB->prefix('smallworld_user')
107
                     . ' U  WHERE M.uid_fk=U.userid AND M.uid_fk IN ('
108
                     . $mods
109
                     . ') AND M.uid_fk NOT IN ('
110
                     . $inspected
111
                     . ") AND M.priv = '0'";
112
        }
113
114 View Code Duplication
        if ($last > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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

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

Loading history...
115
            $query .= ' order by created DESC LIMIT ' . $hm;
0 ignored issues
show
Bug introduced by
The variable $query does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
116
        } elseif ('a' == $last) {
117
            $query .= ' order by M.msg_id DESC LIMIT ' . $hm;
118
        } else {
119
            $query .= ' order by created DESC LIMIT ' . $hm;
120
        }
121
122
        $result = $xoopsDB->queryF($query);
123
        $count  = $xoopsDB->getRowsNum($result);
124
        if (0 == $count) {
125
            return false;
126
        }
127
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
128
            $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
129
        }
130
131
        if (!empty($data)) {
132
            return $data;
133
        }
134
    }
135
136
    /**
137
     * @Get comments based on msg id
138
     * @param int $msg_id
139
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
140
     */
141
    public function Comments($msg_id)
142
    {
143
        global $xoopsUser, $xoopsDB;
144
        $inspected = $this->inspected();
145
        $query     = 'SELECT C.msg_id_fk, C.com_id, C.uid_fk, C.comment, C.created, U.username FROM '
146
                     . $xoopsDB->prefix('smallworld_comments')
147
                     . ' C, '
148
                     . $xoopsDB->prefix('smallworld_user')
149
                     . " U WHERE C.uid_fk=U.userid AND C.msg_id_fk='"
150
                     . $msg_id
151
                     . "' AND C.uid_fk NOT IN ("
152
                     . $inspected
153
                     . ') ORDER BY C.com_id ASC ';
154
        $result    = $xoopsDB->queryF($query);
155
        $i         = $xoopsDB->getRowsNum($result);
0 ignored issues
show
Unused Code introduced by
$i is not used, you could remove the assignment.

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

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

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

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

Loading history...
156
        while (false !== ($row = $xoopsDB->fetchArray($result))) {
157
            $data[] = $row;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$data was never initialized. Although not strictly required by PHP, it is generally a good practice to add $data = array(); before regardless.

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

Let’s take a look at an example:

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

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

    // do something with $myArray
}

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

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

Loading history...
158
        }
159
        if (!empty($data)) {
160
            return $data;
161
        }
162
    }
163
164
    /**
165
     * @Get user image based on uid
166
     * @param int $uid
167
     * @return string
168
     */
169
    public function Gravatar($uid)
170
    {
171
        global $xoopsUser, $xoopsDB;
172
        $image  = '';
173
        $sql    = 'SELECT userimage FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $uid . "'";
174
        $result = $xoopsDB->queryF($sql);
175
        while (false !== ($r = $xoopsDB->fetchArray($result))) {
176
            $image = $r['userimage'];
177
        }
178
179
        if ('blank.gif' === $image) {
180
            $image = smallworld_getAvatarLink($uid, $image);
181
        }
182
183
        //$image = ($image == '' || $image == 'blank.gif') ? smallworld_getAvatarLink($uid, $image) : $image;
184
185
        $type = [
186
            1 => 'jpg',
187
            2 => 'jpeg',
188
            3 => 'png',
189
            4 => 'gif',
190
        ];
191
192
        $ext = explode('.', $image);
193
194
        if (@!in_array(mb_strtolower($ext[1]), $type) || '' == $image) {
195
            $avatar = '';
196
        } else {
197
            $avatar = $image;
198
        }
199
200
        return $avatar;
201
    }
202
203
    /**
204
     * @count all votes
205
     * @param int $type
206
     * @param int $val
207
     * @param int $msgid
208
     * @return int
209
     */
210 View Code Duplication
    public function countVotes($type, $val, $msgid)
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

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

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

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