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

User   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 211
Duplicated Lines 11.85 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
dl 25
loc 211
rs 9.76
c 0
b 0
f 0
wmc 33
lcom 0
cbo 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A checkIfProfile() 0 21 3
A createUser() 0 8 1
A chkUser() 0 10 1
B friendcheck() 13 30 10
A getName() 0 11 2
B following_or() 12 24 8
A getRequests() 0 24 3
A spousexist() 0 8 1
A allUsers() 0 17 4

How to fix   Duplicated Code   

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:

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