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

SmallWorldUser::createUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 8
loc 8
rs 10
c 0
b 0
f 0
1
<?php
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 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...
23
{
24
25
    /**
26
     * @Check if user has profile
27
     * @param int $userID
28
     * @return int
29
     */
30
    public function CheckIfProfile($userID)
31
    {
32
        global $xoopsUser, $xoopsDB;
33
        $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...
34
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . $userID . "'";
35
        $result = $xoopsDB->queryF($sql);
36
        $i      = $xoopsDB->getRowsNum($result);
37
        if ($xoopsUser) {
38
            // If xoopsuser but no smallworld profile
39
            if (0 == $i) {
40
                $i = 1;
41
            } else {
42
                // if xoopsuser and has profile
43
                $i = 2;
44
            }
45
        } else {
46
            // if not xoopsUser ie anonymous user
47
            $i = 0;
48
        }
49
        return $i;
50
    }
51
52
    /**
53
     * @Create user
54
     * @param int $userid
55
     * @return void
56
     */
57
    public function createUser($userid)
58
    {
59
        global $xoopsUser, $xoopsDB;
60
        $a      = new $xoopsUser($userid);
61
        $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...
62
        $sql    = 'INSERT INTO ' . $xoopsDB->prefix('smallworld_user') . ' (userid) VALUES (' . (int)$userid . ')';
63
        $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...
64
    }
65
66
    /**
67
     * @Check is user is smallworld user
68
     * @return void
69
     */
70
    public function chkUser()
71
    {
72
        global $xoopsUser, $xoopsTpl;
73
        $greeting = '<br>';
74
        $greeting .= _SMALLWORLD_NOTYETUSER_GREETING . ' ' . $xoopsUser->uname() . '.<br><br>';
75
        $greeting .= _SMALLWORLD_NOTYETUSER_BOXTEXT;
76
77
        $xoopsTpl->assign('notyetusercontent', $greeting);
78
        $xoopsTpl->assign('check', 0);
79
    }
80
81
    /**
82
     * @Check is user is friend
83
     * @param int $user
84
     * @param int $userID
85
     * @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...
86
     */
87
    public function friendcheck($user, $userID)
88
    {
89
        global $xoopsUser, $xoopsDB;
90
        $respons = [];
91
        if ($user == $userID) {
92
            $respons[0] = 2;
93
            return $respons;
94
        }
95
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE me = '" . (int)$user . "' AND you = '" . (int)$userID . "'";
96
        $result = $xoopsDB->query($sql);
97
        $i      = $xoopsDB->getRowsNum($result);
98
        if (0 == $i) {
99
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$user . "' AND me = '" . (int)$userID . "'";
100
            $result = $xoopsDB->query($sql);
101
            $i      = $xoopsDB->getRowsNum($result);
102
        }
103
        while ($row = $xoopsDB->fetchArray($result)) {
104
            if (0 == $i and '' == $i) {
105
                $respons[0] = 0;
106
            }
107
108
            if (1 == $i and 1 == $row['status']) {
109
                $respons[0] = 1;
110
            }
111
            if (1 == $i and 2 == $row['status']) {
112
                $respons[0] = 2;
113
            }
114
            return $respons;
115
        }
116
    }
117
118
    /**
119
     * @Get name from userid
120
     * @param int $userID
121
     * @return string
122
     */
123
    public function getName($userID)
124
    {
125
        global $xoopsUser, $xoopsDB;
126
        $sql    = 'SELECT username FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE userid = '" . (int)$userID . "'";
127
        $result = $xoopsDB->queryF($sql);
128
        while ($row = $xoopsDB->fetchArray($result)) {
129
            $name = $row['username'];
130
        }
131
        return $name;
0 ignored issues
show
Bug introduced by
The variable $name 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...
132
    }
133
134
    /**
135
     * @Check if user is follower
136
     * @param int $userid
137
     * @param int $friendid
138
     * @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...
139
     */
140
    public function following_or($userid, $friendid)
141
    {
142
        global $xoopsDB, $xoopsUser;
143
        $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...
144
        if ($userid != $friendid) {
145
            $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_followers') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'";
146
            $result = $xoopsDB->query($sql);
147
            $i      = $xoopsDB->getRowsNum($result);
148
            while ($row = $xoopsDB->fetchArray($result)) {
149
                if (0 == $i) {
150
                    $respons[0] = 0;
151
                }
152
153
                if (1 == $i and 1 == $row['status']) {
154
                    $respons[0] = 1;
155
                }
156
                if (1 == $i and 2 == $row['status']) {
157
                    $respons[0] = 2;
158
                }
159
            }
160
        } else {
161
        }
162
        return $respons;
163
    }
164
165
    /**
166
     * @Get requests
167
     * @param int $userid
168
     * @return array
169
     */
170
    public function getRequests($userid)
171
    {
172
        global $xoopsDB, $xoopsUser;
173
        $msg      = [];
174
        $sql      = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_friends') . " WHERE you = '" . (int)$userid . "' AND status = '1'";
175
        $result   = $xoopsDB->queryF($sql);
176
        $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...
177
        $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...
178
        $Wall     = new Wall_Updates();
179
        $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...
180
        $start    = 0;
181
        while ($row = $xoopsDB->fetchArray($result) and $start <= count($row)) {
182
            $msg[$start]['friendname']  = $this->getName($row['me']);
183
            $msg[$start]['img']         = $Wall->Gravatar($row['me']);
184
            $msg[$start]['friendimage'] = "<img src='" . XOOPS_UPLOAD_URL . '/' . $msg[$start]['img'] . "' height='40px'>";
185
            $msg[$start]['frienddate']  = date('d-m-Y', $row['date']);
186
            $msg[$start]['accept']      = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_ACCEPT . '</a>';
187
            $msg[$start]['deny']        = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(-1,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_DENY . '</a>';
188
            $msg[$start]['later']       = '<a class="smallworldrequestlink" id = "smallworldfriendrequest_' . $msg[$start]['friendname'] . '" href = "javascript:Smallworld_AcceptDenyFriend(0,' . $row['me'] . ',' . $row['you'] . ',' . $start . ');">' . _SMALLWORLD_LATER . '</a>';
189
            $msg[$start]['cnt']         = $start;
190
            ++$start;
191
        }
192
        return $msg;
193
    }
194
195
    /**
196
     * @Get partner
197
     * @param string $name
198
     * @return int
199
     */
200
    public function spousexist($name)
201
    {
202
        global $xoopsUser, $xoopsDB;
203
        $sql    = 'SELECT * FROM ' . $xoopsDB->prefix('smallworld_user') . " WHERE username = '" . $name . "'";
204
        $result = $xoopsDB->queryF($sql);
205
        $i      = $xoopsDB->getRowsNum($result);
206
        return $i;
207
    }
208
209
    /**
210
     * Get all users
211
     * @return array
212
     */
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...
213
214
    public function allUsers()
215
    {
216
        global $xoopsDB;
217
        $sql    = 'SELECT userid FROM ' . $xoopsDB->prefix('smallworld_user') . ' ORDER BY userid';
218
        $result = $xoopsDB->queryF($sql);
219
        $i      = $xoopsDB->getRowsNum($result);
220
        if (0 != $i) {
221
            while ($r = $xoopsDB->fetchArray($result)) {
222
                $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...
223
            }
224
            if (!empty($data)) {
225
                return Smallworld_array_flatten($data, 0);
226
            }
227
        } else {
228
            //redirect_header(XOOPS_URL . "/modules/smallworld/register.php");
229
        }
230
    }
231
}
232