Completed
Push — master ( 330830...6eccd4 )
by
unknown
01:37
created

SmallWorldUser::following_or()   B

Complexity

Conditions 8
Paths 2

Size

Total Lines 24

Duplication

Lines 24
Ratio 100 %

Importance

Changes 0
Metric Value
cc 8
nc 2
nop 2
dl 24
loc 24
rs 8.4444
c 0
b 0
f 0
1
<?php namespace Xoopsmodules\smallworld;
2
/**
3
 * You may not change or alter any portion of this comment or credits
4
 * of supporting developers from this source code or any supporting source code
5
 * which is considered copyrighted (c) material of the original comment or credit authors.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
/**
13
 * SmallWorld
14
 *
15
 * @copyright    The XOOPS Project (https://xoops.org)
16
 * @copyright    2011 Culex
17
 * @license      GNU GPL (http://www.gnu.org/licenses/gpl-2.0.html/)
18
 * @package      SmallWorld
19
 * @since        1.0
20
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
21
 */
22
23
//include_once $GLOBALS['xoops']->path('include/common.php');
24
25
26 View Code Duplication
class SmallWorldUser
0 ignored issues
show
Duplication introduced by
This class 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...
27
{
28
29
    /**
30
     * @Check if user has profile
31
     * @param int $userID
32
     * @return int
33
     */
34
    public function checkIfProfile($userID)
35
    {
36
        global $xoopsUser, $xoopsDB;
37
        $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...
38
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $userID . "'";
39
        $result = $xoopsDB->queryF($sql);
40
        $i      = $xoopsDB->getRowsNum($result);
41
        if ($xoopsUser) {
42
            // If xoopsuser but no smallworld profile
43
            if (0 == $i) {
44
                $i = 1;
45
            } else {
46
                // if xoopsuser and has profile
47
                $i = 2;
48
            }
49
        } else {
50
            // if not xoopsUser ie anonymous user
51
            $i = 0;
52
        }
53
        return $i;
54
    }
55
56
    /**
57
     * @Create user
58
     * @param int $userid
59
     * @return void
60
     */
61
    public function createUser($userid)
62
    {
63
        global $xoopsUser, $xoopsDB;
64
        $a      = new $xoopsUser($userid);
65
        $b      = $a->uname();
0 ignored issues
show
Unused Code introduced by
$b 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...
66
        $sql    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_user') . ' (userid) VALUES (' . (int)$userid . ')';
67
        $result = $xoopsDB->queryF($sql);
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...
68
    }
69
70
    /**
71
     * @Check is user is smallworld user
72
     * @return void
73
     */
74
    public function chkUser()
75
    {
76
        global $xoopsUser, $xoopsTpl;
77
        $greeting = '<br>';
78
        $greeting .= _SMALLWORLD_NOTYETUSER_GREETING . ' ' . $xoopsUser->uname() . '.<br><br>';
79
        $greeting .= _SMALLWORLD_NOTYETUSER_BOXTEXT;
80
81
        $xoopsTpl->assign('notyetusercontent', $greeting);
82
        $xoopsTpl->assign('check', 0);
83
    }
84
85
    /**
86
     * @Check is user is friend
87
     * @param int $user
88
     * @param int $userID
89
     * @return array|int
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...
90
     */
91
    public function friendcheck($user, $userID)
92
    {
93
        global $xoopsUser, $xoopsDB;
94
        $respons = [];
95
        if ($user == $userID) {
96
            $respons[0] = 2;
97
            return $respons;
98
        }
99
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$user . "' AND you = '" . (int)$userID . "'";
100
        $result = $xoopsDB->query($sql);
101
        $i      = $xoopsDB->getRowsNum($result);
102
        if (0 == $i) {
103
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$user . "' AND me = '" . (int)$userID . "'";
104
            $result = $xoopsDB->query($sql);
105
            $i      = $xoopsDB->getRowsNum($result);
106
        }
107
        while ($row = $xoopsDB->fetchArray($result)) {
108
            if (0 == $i and '' == $i) {
109
                $respons[0] = 0;
110
            }
111
112
            if (1 == $i and 1 == $row['status']) {
113
                $respons[0] = 1;
114
            }
115
            if (1 == $i and 2 == $row['status']) {
116
                $respons[0] = 2;
117
            }
118
            return $respons;
119
        }
120
    }
121
122
    /**
123
     * @Get name from userid
124
     * @param int $userID
125
     * @return string
126
     */
127
    public function getName($userID)
128
    {
129
        global $xoopsUser, $xoopsDB;
130
        $name = '';
131
        $sql    = 'SELECT username FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . (int)$userID . "'";
132
        $result = $xoopsDB->queryF($sql);
133
        while ($row = $xoopsDB->fetchArray($result)) {
134
            $name = $row['username'];
135
        }
136
        return $name;
137
    }
138
139
    /**
140
     * @Check if user is follower
141
     * @param int $userid
142
     * @param int $friendid
143
     * @return int
0 ignored issues
show
Documentation introduced by
Should the return type not be integer[]?

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...
144
     */
145
    public function following_or($userid, $friendid)
146
    {
147
        global $xoopsDB, $xoopsUser;
148
        $respons[0] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$respons was never initialized. Although not strictly required by PHP, it is generally a good practice to add $respons = 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...
149
        if ($userid != $friendid) {
150
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'";
151
            $result = $xoopsDB->query($sql);
152
            $i      = $xoopsDB->getRowsNum($result);
153
            while ($row = $xoopsDB->fetchArray($result)) {
154
                if (0 == $i) {
155
                    $respons[0] = 0;
156
                }
157
158
                if (1 == $i and 1 == $row['status']) {
159
                    $respons[0] = 1;
160
                }
161
                if (1 == $i and 2 == $row['status']) {
162
                    $respons[0] = 2;
163
                }
164
            }
165
        } else {
166
        }
167
        return $respons;
168
    }
169
170
    /**
171
     * @Get requests
172
     * @param int $userid
173
     * @return array
174
     */
175
    public function getRequests($userid)
176
    {
177
        global $xoopsDB, $xoopsUser;
178
        $msg      = [];
179
        $sql      = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$userid . "' AND status = '1'";
180
        $result   = $xoopsDB->queryF($sql);
181
        $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...
182
        $db       = new SmallWorldDB;
0 ignored issues
show
Unused Code introduced by
$db 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...
183
        $Wall     = new WallUpdates();
184
        $myavatar = $Wall->Gravatar($userid);
0 ignored issues
show
Unused Code introduced by
$myavatar 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...
185
        $start    = 0;
186
        while ($row = $xoopsDB->fetchArray($result) and $start <= count($row)) {
187
            $msg[$start]['friendname']  = $this->getName($row['me']);
188
            $msg[$start]['img']         = $Wall->Gravatar($row['me']);
189
            $msg[$start]['friendimage'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $msg[$start]['img'] . "' height='40px'>";
190
            $msg[$start]['frienddate']  = date('d-m-Y', $row['date']);
191
            $msg[$start]['accept']      = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_ACCEPT . '</a>';
192
            $msg[$start]['deny']        = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(-1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_DENY . '</a>';
193
            $msg[$start]['later']       = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(0,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_LATER . '</a>';
194
            $msg[$start]['cnt']         = $start;
195
            ++$start;
196
        }
197
        return $msg;
198
    }
199
200
    /**
201
     * @Get partner
202
     * @param string $name
203
     * @return int
204
     */
205
    public function spousexist($name)
206
    {
207
        global $xoopsUser, $xoopsDB;
208
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE username = '" . $name . "'";
209
        $result = $xoopsDB->queryF($sql);
210
        $i      = $xoopsDB->getRowsNum($result);
211
        return $i;
212
    }
213
214
    /**
215
     * Get all users
216
     * @return array
217
     */
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...
218
219
    public function allUsers()
220
    {
221
        global $xoopsDB;
222
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_user') . ' ORDER BY userid';
223
        $result = $xoopsDB->queryF($sql);
224
        $i      = $xoopsDB->getRowsNum($result);
225
        if (0 != $i) {
226
            while ($r = $xoopsDB->fetchArray($result)) {
227
                $data[] = $r;
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...
228
            }
229
            if (!empty($data)) {
230
                return Smallworld_array_flatten($data, 0);
231
            }
232
        } else {
233
            //redirect_header(XOOPS_URL . "/modules/smallworld/register.php");
234
        }
235
    }
236
}
237