Completed
Push — master ( 8e591c...fd0c8a )
by
unknown
04:42 queued 02:15
created

SwDatabase::handlePosts()   F

Complexity

Conditions 16
Paths 2049

Size

Total Lines 162

Duplication

Lines 17
Ratio 10.49 %

Importance

Changes 0
Metric Value
cc 16
nc 2049
nop 0
dl 17
loc 162
rs 1.1198
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
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
use Xmf\Request;
16
use XoopsModules\Smallworld\Constants;
17
18
/**
19
 * SmallWorld
20
 *
21
 * @package      \XoopsModules\SmallWorld
22
 * @copyright    The XOOPS Project (https://xoops.org)
23
 * @copyright    2011 Culex
24
 * @license      GNU GPL (https://www.gnu.org/licenses/gpl-2.0.html/)
25
 * @author       Michael Albertsen (http://culex.dk) <[email protected]>
26
 * @link         https://github.com/XoopsModules25x/smallworld
27
 * @since        1.0
28
 */
29
30
/**
31
 *
32
 * SwDatabase to manage SW activity
33
 *
34
 */
35
class SwDatabase
36
{
37
    /**
38
     * getJobsToDiv method
39
     *
40
     * @todo switch to use SwUser class methods
41
     * @param int $id
42
     * @return array
43
     */
44
    public function getJobsToDiv($id)
45
    {
46
        $msg    = [];
47
        $sql    = 'SELECT employer,position,jobstart,jobstop,description  FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid ='" . $id . "'";
48
        $result = $GLOBALS['xoopsDB']->query($sql);
49 View Code Duplication
        while (false !== ($row = $GLOBALS['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...
50
            $employer    = unserialize($row['employer']);
51
            $position    = unserialize($row['position']);
52
            $jobstart    = unserialize($row['jobstart']);
53
            $jobstop     = unserialize($row['jobstop']);
54
            $description = unserialize($row['description']);
55
        }
56
        $start = 0;
57
        $end   = count($employer) - 1;
0 ignored issues
show
Bug introduced by
The variable $employer 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...
58 View Code Duplication
        while ($start <= $end) {
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...
59
            $msg[$start]['employer']    = $employer[$start];
60
            $msg[$start]['position']    = $position[$start];
0 ignored issues
show
Bug introduced by
The variable $position 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...
61
            $msg[$start]['jobstart']    = $jobstart[$start];
0 ignored issues
show
Bug introduced by
The variable $jobstart 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...
62
            $msg[$start]['jobstop']     = $jobstop[$start];
0 ignored issues
show
Bug introduced by
The variable $jobstop 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...
63
            $msg[$start]['description'] = $description[$start];
0 ignored issues
show
Bug introduced by
The variable $description 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...
64
            ++$start;
65
        }
66
67
        return $msg;
68
    }
69
70
    /**
71
     * getSchoolToDiv function
72
     *
73
     * @param int $userId smallworld `userid`
74
     * @return array
75
     */
76
    public function getSchoolToDiv($userId)
77
    {
78
        global $arr7;
79
        $msg         = [];
80
        $school_type = [];
81
        $swUser = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser')->getByUserId($userId);
82
        if ($swUser instanceof \XoopsModules\Smallworld\SwUser) {
83
            $school_type = $swUser->getVar('school_type');
84
            $school      = $swUser->getVar('school');
85
            $schoolstart = $swUser->getVar('schoolstart');
86
            $schoolstop  = $swUser->getVar('schoolstop');
87
        }
88
        /*
89
        $sql    = 'SELECT school_type,school,schoolstart,schoolstop FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid ='" . $userId . "'";
90
        $result = $GLOBALS['xoopsDB']->query($sql);
91
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
92
            $school_type = unserialize($row['school_type']);
93
            $school      = unserialize($row['school']);
94
            $schoolstart = unserialize($row['schoolstart']);
95
            $schoolstop  = unserialize($row['schoolstop']);
96
        }
97
        */
98
        $start = 0;
99
        $end   = count($school_type);
100 View Code Duplication
        while ($start < $end) {
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...
101
            $msg[$start]['school_type'] = $school_type[$start];
102
            $msg[$start]['school']      = $arr7[$school[$start]];
0 ignored issues
show
Bug introduced by
The variable $school 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...
103
            $msg[$start]['schoolstart'] = $schoolstart[$start];
0 ignored issues
show
Bug introduced by
The variable $schoolstart 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...
104
            $msg[$start]['schoolstop']  = $schoolstop[$start];
0 ignored issues
show
Bug introduced by
The variable $schoolstop 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...
105
            ++$start;
106
        }
107
108
        return $msg;
109
    }
110
111
    /**
112
     * getScreennamesToDiv function
113
     *
114
     * @param int $userId smallworld `userid`
115
     * @return array
116
     */
117
    public function getScreennamesToDiv($userId)
118
    {
119
        global $arr06;
120
        $msg             = [];
121
        $screenname_type = [];
122
        $swUser = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser')->getByUserId($userId);
123
        if ($swUser instanceof \XoopsModules\Smallworld\SwUser) {
124
            $screenname_type = $swUser->getVar('screenname_type');
125
            $screenname      = $swUser->getVar('screenname');
126
        }
127
        /*
128
        $sql    = 'SELECT screenname_type,screenname FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid ='" . $userId . "'";
129
        $result = $GLOBALS['xoopsDB']->query($sql);
130
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
131
            $screenname_type = unserialize($row['screenname_type']);
132
            $screenname      = unserialize($row['screenname']);
133
        }
134
        */
135
        $start = 0;
136
        $end   = count($screenname_type);
137 View Code Duplication
        while ($start < $end) {
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...
138
            $msg[$start]['screenname']      = $screenname_type[$start];
139
            $msg[$start]['screenname_type'] = $arr06[$screenname[$start]];
0 ignored issues
show
Bug introduced by
The variable $screenname 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...
140
            $msg[$start]['link']            = "<span class='smallworld_website'>" . smallworld_sociallinks($screenname[$start], $msg[$start]['screenname']);
141
            ++$start;
142
        }
143
144
        return $msg;
145
    }
146
147
    /**
148
     * getVar function
149
     *
150
     * @todo deprecate this method and use SwUser::getVar instead
151
     * @param int    $userId smallworld `userid`
152
     * @param string $var
153
     * @return mixed
154
     */
155
    public function getVar($userId, $var)
156
    {
157
        $swUser = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser')->getByUserId($userId);
158
159
        return [$swUser->getVar($var)];
160
        /*
161
        $msg = [];
162
        $sql    = 'SELECT ' . $GLOBALS['xoopsDB']->escape($var) . ' FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . (int)$userId . "'";
163
        $result = $GLOBALS['xoopsDB']->queryF($sql);
164
        if ($GLOBALS['xoopsDB']->getRowsNum($result) < 1) {
165
            return 0; //_SMALLWORLD_REPLY_NOTSPECIFIED;
166
        }
167
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
168
            $msg[$var] = $row[$var];
169
        }
170
171
        return $msg[$var];
172
        */
173
    }
174
175
    /**
176
     * updateSingleValue function
177
     * @param string $table
178
     * @param int    $userid
179
     * @param string $field
180
     * @param int    $value
181
     */
182
    public function updateSingleValue($table, $userid, $field, $value)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
183
    {
184
        $myts   = \MyTextSanitizer::getInstance();
185
        $sql    = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix($table) . ' SET ' . $field . "='" . $myts->addSlashes($value) . "' WHERE userid='" . (int)$userid . "'";
186
        $result = $GLOBALS['xoopsDB']->queryF($sql);
187
188
        return $result;
189
    }
190
191
    /**
192
     * saveImage function
193
     * @param $values
194
     */
195 View Code Duplication
    public function saveImage($values)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

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...
196
    {
197
        $myts   = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts 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...
198
        $sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . ' VALUES (' . $values . ')';
199
        $result = $GLOBALS['xoopsDB']->queryF($sql);
200
201
        return $result;
202
    }
203
204
    /**
205
     * DeleteImage function
206
     * @param int    $userid
207
     * @param string $imagename
208
     */
209 View Code Duplication
    public function deleteImage($userid, $imagename)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

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...
210
    {
211
        $myts   = \MyTextSanitizer::getInstance();
0 ignored issues
show
Unused Code introduced by
$myts 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...
212
        $sql    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . " WHERE imgname = '" . stripslashes($imagename) . "' AND userid='" . (int)$userid . "'";
213
        $result = $GLOBALS['xoopsDB']->queryF($sql);
214
215
        return $result;
216
    }
217
218
    /**
219
     * handlePosts function
220
     */
221
    public function handlePosts()
222
    {
223
       if ($GLOBALS['xoopsUser'] && ($GLOBALS['xoopsUser'] instanceof \XoopsUser)) {
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
224
            $uid = $GLOBALS['xoopsUser']->uid();
0 ignored issues
show
Unused Code introduced by
$uid 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...
225
        } else {
226
            return false;
227
        }
228
        $uid     = ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser'] instanceof \XoopsUser) ? $GLOBALS['xoopsUser']->uid() : 0;
0 ignored issues
show
Bug introduced by
The class XoopsUser does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
229
230
        $img     = new Images();
231
        $avatar  = $this->getVar($uid, 'userimage');
232
        $partner = '';
233
234
        if (empty($avatar)) {
235
            $avatar = $GLOBALS['xoopsUser']->user_avatar();
236
        }
237
        if (Constants::RELATIONSHIP_SINGLE !== Request::getInt('relationship', Constants::RELATIONSHIP_COMPLICATED, 'POST')) {
238
            $partner = smallworld_sanitize($_POST['partner']);
239
        }
240
241
        $regdate                = time();
242
        $username               = $GLOBALS['xoopsUser']->uname();
243
        $realname               = smallworld_sanitize($_POST['realname']);
244
        $gender                 = Request::getInt('gender', Constants::GENDER_UNKNOWN, 'POST');
245
        $intingender            = isset($_POST['intingender']) ? smallworld_sanitize(serialize($_POST['intingender'])) : smallworld_sanitize(serialize([0 => '3']));
246
        $relationship           = smallworld_sanitize($_POST['relationship']);
247
        $searchrelat            = isset($_POST['searchrelat']) ? smallworld_sanitize(serialize($_POST['searchrelat'])) : smallworld_sanitize(serialize([0 => '0']));
248
        $birthday               = smallworld_sanitize(smallworld_euroToUsDate($_POST['birthday']));
249
        $birthplace             = smallworld_sanitize($_POST['birthplace']);
250
        $birthplace_lat         = smallworld_sanitize($_POST['birthplace_lat']);
251
        $birthplace_lng         = smallworld_sanitize($_POST['birthplace_lng']);
252
        $birthplace_country     = smallworld_sanitize($_POST['birthplace_country']);
253
        $birthplace_country_img = isset($_POST['birthplace_country_img']) ? smallworld_sanitize($_POST['birthplace_country_img']) : '';
0 ignored issues
show
Unused Code introduced by
$birthplace_country_img 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...
254
        $politic                = smallworld_sanitize($_POST['politic']);
255
        $religion               = smallworld_sanitize($_POST['religion']);
256
        $emailtype              = smallworld_sanitize(serialize($_POST['emailtype']));
257
        $screenname_type        = smallworld_sanitize(serialize($_POST['screenname_type']));
258
        $screenname             = smallworld_sanitize(serialize($_POST['screenname']));
259
        $mobile                 = smallworld_sanitize($_POST['mobile']);
260
        $phone                  = smallworld_sanitize($_POST['phone']);
261
        $adress                 = smallworld_sanitize($_POST['adress']);
262
        $present_city           = smallworld_sanitize($_POST['present_city']);
263
        $present_lat            = smallworld_sanitize($_POST['present_lat']);
264
        $present_lng            = smallworld_sanitize($_POST['present_lng']);
265
        $present_country        = smallworld_sanitize($_POST['present_country']);
266
        $present_country_img    = isset($_POST['present_country_img']) ? smallworld_sanitize($_POST['present_country_img']) : '';
0 ignored issues
show
Unused Code introduced by
$present_country_img 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...
267
        $website                = smallworld_sanitize($_POST['website']);
268
        $interests              = smallworld_sanitize($_POST['interests']);
269
        $music                  = smallworld_sanitize($_POST['music']);
270
        $tvshow                 = smallworld_sanitize($_POST['tvshow']);
271
        $movie                  = smallworld_sanitize($_POST['movie']);
272
        $books                  = smallworld_sanitize($_POST['books']);
273
        $aboutme                = smallworld_sanitize($_POST['aboutme']);
274
        $school_type            = smallworld_sanitize(serialize($_POST['school_type']));
275
        $school                 = smallworld_sanitize(serialize($_POST['school']));
276
        $schoolstart            = smallworld_sanitize(serialize($_POST['schoolstart']));
277
        $schoolstop             = smallworld_sanitize(serialize($_POST['schoolstop']));
278
        $jobemployer            = smallworld_sanitize(serialize($_POST['employer']));
279
        $jobposition            = smallworld_sanitize(serialize($_POST['position']));
280
        $jobstart               = smallworld_sanitize(serialize(smallworld_YearOfArray($_POST['jobstart'])));
281
        $jobstop                = smallworld_sanitize(serialize(smallworld_YearOfArray($_POST['jobstop'])));
282
        $jobdescription         = smallworld_sanitize(serialize($_POST['description']));
283
284
        $swUserHandler = \XoopsModules\Smallworld\Helper::getInstance()->getHandler('SwUser');
285
286
        //@todo find better way to terminate routine than just 'die' on error(s)
287
        if ('edit' === $_POST['function']) {
288
            $swUserObj = $swUserHandler->get($uid);
289
            if (!$swUserObj instanceof \XoopsModules\Smallworld\SwUser) {
290
                return;
291
            }
292
            $swUserObj->setVars([
293
                'realname'           => $realname,
294
                'username'           => $username,
295
                'userimage'          => $avatar,
296
                'gender'             => $gender,
297
                'intingender'        => $intingender,
298
                'relationship'       => $relationship,
299
                'partner'            => $partner,
300
                'searchrelat'        => $searchrelat,
301
                'birthday'           => $birthday,
302
                'birthplace'         => $birthplace,
303
                'birthplace_lat'     => (float)$birthplace_lat,
304
                'birthplace_lng'     => (float)$birthplace_lng,
305
                'birthplace_country' => $birthplace_country,
306
                'politic'            => $politic,
307
                'religion'           => $religion,
308
                'emailtype'          => $emailtype,
309
                'screenname_type'    => $screenname_type,
310
                'screenname'         => $screenname,
311
                'mobile'             => (float)$mobile,
312
                'phone'              => (float)$phone,
313
                'adress'             => $adress,
314
                'present_city'       =>  $present_city,
315
                'present_lat'        => (float)$present_lat,
316
                'present_lng'        => (float)$present_lng,
317
                'present_country'    => $present_country,
318
                'website'            => $website,
319
                'interests'          => $interests,
320
                'music'              => $music,
321
                'tvshow'             => $tvshow,
322
                'movie'              => $movie,
323
                'books'              => $books,
324
                'aboutme'            => $aboutme,
325
                'school_type'        => $school_type,
326
                'school'             => $school,
327
                'schoolstart'        => $schoolstart,
328
                'schoolstop'         => $schoolstop,
329
                'employer'           => $jobemployer,
330
                'position'           => $jobposition,
331
                'jobstart'           => $jobstart,
332
                'jobstop'            => $jobstop,
333
                'description'        => $jobdescription
334
            ]);
335
            $result = $swUserHandler->insert($swUserObj);
336
            if (false === $result) {
337
                die('Failed inserting User');
338
            }
339
            /*
340
            // Update all values in user_table
341
            $sql    = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . ' SET ';
342
            $sql    .= "realname = '" . $realname . "', username= '" . $username . "', userimage = '" . $avatar . "', gender = '" . $gender . "',";
343
            $sql    .= "intingender = '" . $intingender . "',relationship = '" . $relationship . "', partner = '" . $partner . "', searchrelat = '" . $searchrelat . "',";
344
            $sql    .= "birthday = '" . $birthday . "',birthplace = '" . $birthplace . "',birthplace_lat = '" . (float)$birthplace_lat . "',";
345
            $sql    .= "birthplace_lng = '" . (float)$birthplace_lng . "',birthplace_country = '" . $birthplace_country . "',politic = '" . $politic . "',";
346
            $sql    .= "religion = '" . $religion . "',emailtype = '" . $emailtype . "',screenname_type = '" . $screenname_type . "',";
347
            $sql    .= "screenname = '" . $screenname . "',mobile = '" . (float)$mobile . "',phone = '" . (float)$phone . "',adress = '" . $adress . "',";
348
            $sql    .= "present_city = '" . $present_city . "',present_lat = '" . (float)$present_lat . "',present_lng = '" . (float)$present_lng . "',";
349
            $sql    .= "present_country = '" . $present_country . "',website = '" . $website . "',interests = '" . $interests . "',";
350
            $sql    .= "music = '" . $music . "',tvshow = '" . $tvshow . "',movie = '" . $movie . "',";
351
            $sql    .= "books = '" . $books . "',aboutme = '" . $aboutme . "',school_type = '" . $school_type . "',";
352
            $sql    .= "school = '" . $school . "', schoolstart = '" . $schoolstart . "',schoolstop = '" . $schoolstop . "',";
353
            $sql    .= "employer = '" . $jobemployer . "', position = '" . $jobposition . "',jobstart = '" . $jobstart . "',";
354
            $sql    .= "jobstop = '" . $jobstop . "', description = '" . $jobdescription . "' ";
355
            $sql    .= "WHERE userid ='" . (int)$uid . "'";
356
            $result = $GLOBALS['xoopsDB']->queryF($sql);
357
            if (false === $result) {
358
                die('SQL error:' . $sql . '');
359
            }
360
            */
361
            $this->EditAdmins($uid, $realname, $avatar);
0 ignored issues
show
Bug introduced by
It seems like $realname defined by smallworld_sanitize($_POST['realname']) on line 243 can also be of type array; however, XoopsModules\Smallworld\SwDatabase::EditAdmins() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
362
            $img->createAlbum($uid);
363
        }
364
365 View Code Duplication
        if ('save' === $_POST['function']) {
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...
366
            $sql    = 'INSERT INTO '
367
                      . $GLOBALS['xoopsDB']->prefix('smallworld_user')
368
                      . ' (userid, regdate, username, userimage, realname, gender, intingender, relationship, partner, searchrelat, birthday, birthplace, birthplace_lat, birthplace_lng, birthplace_country, politic, religion, emailtype, screenname_type, screenname, mobile, phone, adress, present_city, present_lat, present_lng, present_country, website, interests, music, tvshow, movie, books, aboutme, school_type, school, schoolstart, schoolstop, employer, position, jobstart, jobstop, description, friends, followers, admin_flag) ';
369
            $sql    .= "VALUES ('" . (int)$uid . "', '" . $regdate . "', '" . $username . "', '" . $avatar . "', '" . $realname . "', '" . $gender . "', '" . $intingender . "', '" . $relationship . "', '" . $partner . "', '" . $searchrelat . "','";
370
            $sql    .= $birthday . "', '" . $birthplace . "', '" . (float)$birthplace_lat . "', '" . (float)$birthplace_lng . "', '" . $birthplace_country . "', '" . $politic . "', '" . $religion . "','";
371
            $sql    .= $emailtype . "', '" . $screenname_type . "', '" . $screenname . "', '" . (float)$mobile . "', '" . (float)$phone . "', '" . $adress . "', '" . $present_city . "', '" . (float)$present_lat . "','";
372
            $sql    .= (float)$present_lng . "', '" . $present_country . "', '" . $website . "', '" . $interests . "', '" . $music . "', '" . $tvshow . "', '" . $movie . "', '" . $books . "', '" . $aboutme . "', '";
373
            $sql    .= $school_type . "', '" . $school . "', '" . $schoolstart . "', '" . $schoolstop . "', '" . $jobemployer . "', '" . $jobposition . "', '" . $jobstart . "', '" . $jobstop . "', '" . $jobdescription . "', ";
374
            $sql    .= "'0', '0', '0')";
375
            $result = $GLOBALS['xoopsDB']->queryF($sql);
376
            if (false === $result) {
377
                die('SQL error:' . $sql . '');
378
            }
379
            $this->setAdmins($uid, $username, $realname, $avatar);
0 ignored issues
show
Bug introduced by
It seems like $realname defined by smallworld_sanitize($_POST['realname']) on line 243 can also be of type array; however, XoopsModules\Smallworld\SwDatabase::setAdmins() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
380
            $img->createAlbum($uid);
381
        }
382
    }
383
    /**
384
     * SetAdmins function
385
     *
386
     * @param int    $userID
387
     * @param string $username
388
     * @param string $realname
389
     * @param mixed  $avatar
390
     */
391
    public function setAdmins($userID, $username, $realname, $avatar)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
392
    {
393
        $ip     = $_SERVER['REMOTE_ADDR'];
394
        $sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . ' (id,userid,username, realname,userimage,ip,complaint,inspect_start, ' . "inspect_stop) VALUES ('', '" . (int)$userID . "', '" . $username . "','" . $realname . "', '" . $avatar . "','" . $ip . "','0','0','0')";
395
        $result = $GLOBALS['xoopsDB']->queryF($sql);
396
397
        return $result;
398
    }
399
400
    /**
401
     * EditAdmins function
402
     *
403
     * @param int    $userID
404
     * @param string $realname
405
     * @param mixed  $avatar
406
     */
407
    public function EditAdmins($userID, $realname, $avatar)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
408
    {
409
        // @todo need to sanitize realname and avatar
410
        $sql    = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . " SET realname = '" . $realname . "', userimage = '" . $avatar . "' WHERE userid = '" . (int)$userID . "'";
411
        $result = $GLOBALS['xoopsDB']->queryF($sql);
412
413
        return $result;
414
    }
415
416
    /**
417
     * alreadycomplaint function
418
     *
419
     * Check if user has already sent complaint
420
     *
421
     * @param string $msg
422
     * @param int    $by
423
     * @param int    $against
424
     * @return int
425
     */
426
    public function alreadycomplaint($msg, $by, $against)
427
    {
428
        $sql    = 'SELECT * FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_complaints') . " WHERE byuser_id = '" . (int)$by . "' AND owner = '" . (int)$against . "' AND link = '" . addslashes($msg) . "'";
429
        $result = $GLOBALS['xoopsDB']->queryF($sql);
430
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
431
        if (1 > $i) {
432
            $query  = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_complaints') . " (complaint_id,link,byuser_id,owner) VALUES ('', '" . addslashes($msg) . "', '" . (int)$by . "', '" . (int)$against . "')";
433
            $result = $GLOBALS['xoopsDB']->queryF($query);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

Loading history...
434
        }
435
436
        return $i;
437
    }
438
439
    /**
440
     * updateComplaint function
441
     *
442
     * @param int $userID
443
     * @return bool true on successful update
444
     */
445 View Code Duplication
    public function updateComplaint($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...
446
    {
447
        $sql    = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . ' SET complaint = complaint + 1 ' . "WHERE userid = '" . (int)$userID . "'";
448
        $result = $GLOBALS['xoopsDB']->queryF($sql);
449
450
        return $result ? true : false;
451
    }
452
453
    /**
454
     * updateInspection function
455
     * @param int   $userID
456
     * @param int   $start
457
     * @param bool
458
     */
459
    public function updateInspection($userID, $start, $stop)
0 ignored issues
show
Unused Code introduced by
The parameter $start 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...
460
    {
461
        $time    = time();
462
        $newstop = $time + $stop;
463
        $sql     = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . " SET inspect_start = '" . $time . "', instect_stop = '" . $newstop . "' WHERE userid ='" . (int)$userID . "'";
464
        $result  = $GLOBALS['xoopsDB']->queryF($sql);
465
466
        return $result ? true : false;
467
    }
468
469
    /**
470
     * handleImageEdit function
471
     *
472
     * @return bool true on success, false on failure
473
     */
474
    public function handleImageEdit()
475
    {
476
        //@todo need to filter $_POST['imgdesc'] array
477
        $return = true;
478
        $postCount = count($_POST['id']);
479 View Code Duplication
        for ($i = 0, $iMax = $postCount; $i < $iMax; ++$i) {
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...
480
            $id     = (int)$_POST['id'][$i];
481
            $desc   = $_POST['imgdesc'][$i];
482
            $sql    = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . " SET `desc` = '" . addslashes($desc) . "' WHERE `id`='" . $id . "'";
483
            $result = $return && $GLOBALS['xoopsDB']->queryF($sql);
484
        }
485
        return $result ? true : false;
0 ignored issues
show
Bug introduced by
The variable $result 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...
486
    }
487
488
    /**
489
     * updateInspection function
490
     *
491
     * insert application for friendship into db or delete if denied
492
     *
493
     * @param int $status
494
     * @param int $friendid
495
     * @param int $userid
496
     * @return bool
497
     */
498
    public function toogleFriendInvite($status, $friendid, $userid)
499
    {
500
        $result = true;
501
        if (0 == $status) {
502
            $sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " (id,me,you,status,date) VALUES ('', '" . $userid . "', '" . $friendid . "', '1', UNIX_TIMESTAMP())";
503
            $result = $GLOBALS['xoopsDB']->queryF($sql);
504 View Code Duplication
        } elseif ($status > 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...
505
            $sql     = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . (int)$friendid . "' AND you = '" . (int)$userid . "'";
506
            $sql2    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . (int)$userid . "' AND you = '" . (int)$friendid . "'";
507
            $result  = $GLOBALS['xoopsDB']->queryF($sql);
508
            $result = $result && $GLOBALS['xoopsDB']->queryF($sql2);
509
510
            // Since friendship is canceled also following is deleted
511
            $this->toogleFollow(1, $userid, $friendid);
512
        }
513
514
        return $result ? true : false;
515
    }
516
517
    /**
518
     * toogleFollow function
519
     *
520
     * Insert following to db or delete if requested
521
     *
522
     * @param int $following
523
     * @param int $myUid
524
     * @param int $friend
525
     * @return bool true on success
526
     */
527
    public function toogleFollow($following, $myUid, $friend)
528
    {
529
        if (0 == $following) {
530
            $sql    = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " (id,me,you,status,date) VALUES ('', '" . $myUid . "', '" . $friend . "', '1', UNIX_TIMESTAMP())";
531
            $result = $GLOBALS['xoopsDB']->queryF($sql);
532
        } elseif ($following > 0) {
533
            $sql     = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE you = '" . (int)$friend . "'"
0 ignored issues
show
Unused Code introduced by
$sql 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...
534
                     . " AND me = '" . (int)$myUid . "'";
535
            $sql2    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE me = '" . (int)$friend . "'"
536
                     . " AND you = '" . (int)$myUid . "'";
537
            $result = $GLOBALS['xoopsDB']->queryF($sql2);
538
        }
539
540
        return $result ? true : false;
0 ignored issues
show
Bug introduced by
The variable $result 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...
541
    }
542
543
    /**
544
     * Set Friendship Status in dB
545
     *
546
     * @param int $stat
547
     * @param int $myUid
548
     * @param int $friend
549
     * @return bool true on success, false on failure
550
     */
551
    public function setFriendshipStat($stat, $myUid, $friend)
552
    {
553
        $result = $result2 = false;
0 ignored issues
show
Unused Code introduced by
$result2 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...
554
        if (1 == $stat) {
555
            $query  = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " SET status = '2' WHERE `me` = '" . $friend . "' AND `you` = '" . $myUid . "'";
556
            $query2 = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " (id,me,you,status,date) VALUES ('', '" . $myUid . "', '" . $friend . "', '2', UNIX_TIMESTAMP())";
557
            $result = $GLOBALS['xoopsDB']->queryF($query);
558
            $result = $result && $GLOBALS['xoopsDB']->queryF($query2);
559 View Code Duplication
        } elseif (0 > $stat) {
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...
560
            $query  = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . (int)$friend . "' AND you = '" . (int)$myUid . "'";
561
            $query2 = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE you = '" . (int)$friend . "' AND me = '" . (int)$myUid . "'";
562
            $result = $GLOBALS['xoopsDB']->queryF($query);
563
            $result = $result && $GLOBALS['xoopsDB']->queryF($query2);
564
        }
565
        return $result ? true : false;
566
    }
567
568
    /**
569
     * deleteWallMsg function
570
     * @param int $id
571
     * @param int $smallworld_msg_id
572
     * @return bool
573
     */
574
    public function deleteWallMsg($id, $smallworld_msg_id)
0 ignored issues
show
Unused Code introduced by
The parameter $id 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...
575
    {
576
        $query  = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE msg_id = '" . $smallworld_msg_id . "'";
577
        $result = $GLOBALS['xoopsDB']->queryF($query);
578
        $query2 = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE msg_id_fk = '" . $smallworld_msg_id . "'";
579
        $result = $result && $GLOBALS['xoopsDB']->queryF($query2);
580
        //delete votes
581
        $query3 = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE msg_id = '" . $smallworld_msg_id . "'";
582
        $result = $result && $GLOBALS['xoopsDB']->queryF($query3);
583
584
        return $result ? true : false;
585
    }
586
587
    /**
588
     * deleteWallComment function
589
     * - Delete Comments
590
     * @param int $smallworld_com_id
591
     * @return true
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
592
     */
593 View Code Duplication
    public function deleteWallComment($smallworld_com_id)
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...
594
    {
595
        $query   = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE com_id = '" . $smallworld_com_id . "'";
596
        $result  = $GLOBALS['xoopsDB']->queryF($query);
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

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

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

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

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

Loading history...
597
        $query2  = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE com_id = '" . $smallworld_com_id . "'";
598
        $result2 = $GLOBALS['xoopsDB']->queryF($query2);
0 ignored issues
show
Unused Code introduced by
$result2 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...
599
600
        return true;
601
    }
602
603
    /**
604
     * Count Users rates
605
     *
606
     * @param int    $userid
607
     * @param string $column
608
     * @return int
609
     */
610
    public function countUsersRates($userid, $column)
611
    {
612
        $sum    = 0;
613
        // @sanitize $column - make sure it's a valid column in the vote dB table
614
        $validCol = in_array($column, ['up', 'down']) ? $column : 'vote_id';
615
        $query    = 'SELECT SUM(' . $validCol . ') AS sum FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE owner = '" . (int)$userid . "'";
616
        $result   = $GLOBALS['xoopsDB']->queryF($query);
617
        while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
618
            $sum = $row['sum'];
619
        }
620
621
        return (int)$sum;
622
    }
623
624
    /**
625
     * Delete user account and associate rows across tables
626
     *
627
     * echos string to display
628
     *
629
     * @param int $userid
630
     * @return bool  true on success, false on failure
631
     */
632
    public function deleteAccount($userid)
633
    {
634
        $userid = (int)$userid;
635
        $user     = new \XoopsUser($userid);
636
        $username = $user->uname();
637
        $sql01    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_admin') . " WHERE userid = '" . $userid . "'";
638
        $sql02    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_comments') . " WHERE uid_fk = '" . $userid . "'";
639
        $sql03    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_followers') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'";
640
        $sql04    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_friends') . " WHERE me = '" . $userid . "' OR you = '" . $userid . "'";
641
        $sql05    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_images') . " WHERE userid = '" . $userid . "'";
642
        $sql06    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_messages') . " WHERE uid_fk = '" . $userid . "'";
643
        $sql07    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_user') . " WHERE userid = '" . $userid . "'";
644
        $sql08    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_vote') . " WHERE user_id = '" . $userid . "'";
645
        $sql09    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_complaints') . " WHERE owner = '" . $userid . "' OR byuser_id = '" . $userid . "'";
646
        $sql10    = 'DELETE FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . " WHERE userid = '" . $userid . "'";
647
648
        $result01 = $GLOBALS['xoopsDB']->queryF($sql01);
649
        $result02 = $GLOBALS['xoopsDB']->queryF($sql02);
650
        $result03 = $GLOBALS['xoopsDB']->queryF($sql03);
651
        $result04 = $GLOBALS['xoopsDB']->queryF($sql04);
652
        $result05 = $GLOBALS['xoopsDB']->queryF($sql05);
653
        $result06 = $GLOBALS['xoopsDB']->queryF($sql06);
654
        $result07 = $GLOBALS['xoopsDB']->queryF($sql07);
655
        $result08 = $GLOBALS['xoopsDB']->queryF($sql08);
656
        $result09 = $GLOBALS['xoopsDB']->queryF($sql09);
657
        $result10 = $GLOBALS['xoopsDB']->queryF($sql10);
658
        // Remove picture dir
659
        $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . $userid . '/';
660
        $result11 = $this->smallworld_remDir($userid, $dirname, $empty = false);
661
        echo $username . _AM_SMALLWORLD_ADMIN_USERDELETEDALERT;
662
663
        return $result01 && $result02 && $result03 && $result04 && $result05 && $result06 && $result07 && $result08 && $result09 && $result10 && $result11;
664
    }
665
666
    /**
667
     * Delete images from users on delete
668
     *
669
     * @param int $userid
670
     * @return bool
671
     */
672 View Code Duplication
    public function SmallworldDeleteDirectory($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...
673
    {
674
        $dirname = XOOPS_ROOT_PATH . '/uploads/albums_smallworld' . '/' . (int)$userid . '/';
675
        if (is_dir($dirname)) {
676
            $dir_handle = opendir($dirname);
677
        }
678
        if (!$dir_handle) {
0 ignored issues
show
Bug introduced by
The variable $dir_handle 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...
679
            return false;
680
        }
681
        while (false !== ($file = readdir($dir_handle))) {
682
            if ('.' !== $file && '..' !== $file) {
683
                if (!is_dir($dirname . '/' . $file)) {
684
                    unlink($dirname . '/' . $file);
685
                } else {
686
                    $this->SmallworldDeleteDirectory($dirname . '/' . $file);
687
                }
688
            }
689
        }
690
        closedir($dir_handle);
691
        rmdir($dirname);
692
693
        return true;
694
    }
695
696
    /**
697
     * Remove user image dir in uploads
698
     *
699
     * @param int         $userid
700
     * @param string|bool $directory
701
     * @param bool|int    $empty
702
     * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|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...
703
     */
704 View Code Duplication
    public function smallworld_remDir($userid, $directory, $empty = false)
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...
705
    {
706
        //@todo verify $userid should be int and then sanitize $userid accordingly before
707
        //      executing this routine
708
        if (!empty($userid)) {
709
            if ('/' === mb_substr($directory, -1)) {
710
                $directory = mb_substr($directory, 0, -1);
711
            }
712
713
            if (!file_exists($directory) || !is_dir($directory)) {
714
                return false;
715
            } elseif (!is_readable($directory)) {
716
                return false;
717
            }
718
            $directoryHandle = opendir($directory);
719
            while (false !== ($contents = readdir($directoryHandle))) {
720
                if ('.' !== $contents && '..' !== $contents) {
721
                    $path = $directory . '/' . $contents;
722
                    if (is_dir($path)) {
723
                        $this->smallworld_remDir($userid, $path);
724
                    } else {
725
                        unlink($path);
726
                    }
727
                }
728
            }
729
            closedir($directoryHandle);
730
            if (false === $empty) {
731
                if (!rmdir($directory)) {
732
                    return false;
733
                }
734
            }
735
736
            return true;
737
        }
738
    }
739
740
    /**
741
     * Update private settings
742
     *
743
     * @param mixed $id user's id
744
     * @param mixed $posts
745
     * @return string serialized settings for this id
746
     */
747
    public function saveSettings($id, $posts)
748
    {
749
        $id = (int)$id;
750
        $sql    = 'SELECT value FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . ' WHERE userid = ' . $id . '';
751
        $result = $GLOBALS['xoopsDB']->queryF($sql);
752
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
753 View Code Duplication
        if ($i > 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...
754
            $sql = 'UPDATE ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . " SET value = '" . $posts . "' WHERE userid = " . (int)$id . '';
755
        } else {
756
            $sql = 'INSERT INTO ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . " (id,userid,value) VALUES ('', '" . $id . "', '" . $posts . "')";
757
        }
758
        $result = $GLOBALS['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...
759
760
        return $this->getSettings($id);
761
    }
762
763
    /**
764
     * Retrieve private settings
765
     *
766
     * @param mixed $userid
767
     * @return string serialized string
768
     */
769
    public function getSettings($userid)
770
    {
771
        $sql    = 'SELECT value FROM ' . $GLOBALS['xoopsDB']->prefix('smallworld_settings') . ' WHERE userid = ' . (int)$userid . '';
772
        $result = $GLOBALS['xoopsDB']->queryF($sql);
773
        $i      = $GLOBALS['xoopsDB']->getRowsNum($result);
774
        if ($i < 1) {
775
            $posts = serialize(
776
                [
777
                    'posts'    => 0,
778
                    'comments' => 0,
779
                    'notify'   => 1,
780
                ]
781
            );
782
            $this->saveSettings($userid, $posts);
783
            $retVal = $this->getSettings($userid);
784 View Code Duplication
        } else {
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...
785
            while (false !== ($row = $GLOBALS['xoopsDB']->fetchArray($result))) {
786
                $data = $row['value'];
787
            }
788
789
            $retVal = json_encode(unserialize(stripslashes($data)));
0 ignored issues
show
Bug introduced by
The variable $data does not seem to be defined for all execution paths leading up to this point.

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

Let’s take a look at an example:

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

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

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

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

Available Fixes

  1. Check for existence of the variable explicitly:

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

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

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
790
        }
791
792
        return $retVal;
793
    }
794
}
795