Passed
Branch master (739b13)
by
unknown
02:43
created

authCheck::information()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * The MIT License (MIT)
4
 *
5
 * Copyright (c) 2016 Robert Sardinia
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to deal
9
 * in the Software without restriction, including without limitation the rights
10
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
 * copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in all
15
 * copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
 * SOFTWARE.
24
 */
25
26
27
/**
28
 * Class fileAuthCheck
29
 * @property int nextCheck
30
 */
31
class authCheck
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
32
{
33
    /**
34
     * @var
35
     */
36
    var $config;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $config.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
37
    /**
38
     * @var
39
     */
40
    var $db;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $db.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
41
    var $dbUser;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dbUser.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
42
    var $dbPass;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dbPass.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
43
    var $dbName;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $dbName.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
44
    var $guildID;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $guildID.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
45
    var $corpTickers;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $corpTickers.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
46
    var $authGroups;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $authGroups.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
47
    var $exempt;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $exempt.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
48
    var $alertChannel;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $alertChannel.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
49
    var $guild;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $guild.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
50
    var $nameEnforce;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $nameEnforce.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
51
    var $nameCheck;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $nameCheck.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
52
    /**
53
     * @var
54
     */
55
    var $discord;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $discord.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
56
    /**
57
     * @var
58
     */
59
    var $channelConfig;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $channelConfig.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
60
    /**
61
     * @var int
62
     */
63
    var $lastCheck = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $lastCheck.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
64
    /**
65
     * @var
66
     */
67
    var $logger;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $logger.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
68
69
    /**
70
     * @param $config
71
     * @param $discord
72
     * @param $logger
73
     */
74
    function init($config, $discord, $logger)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
75
    {
76
        $this->config = $config;
77
        $this->discord = $discord;
78
        $this->logger = $logger;
79
        $this->db = $config["database"]["host"];
80
        $this->dbUser = $config["database"]["user"];
81
        $this->dbPass = $config["database"]["pass"];
82
        $this->dbName = $config["database"]["database"];
83
        $this->guildID = $config["bot"]["guild"];
84
        $this->exempt = $config["plugins"]["auth"]["exempt"];
85
        $this->corpTickers = $config["plugins"]["auth"]["corpTickers"];
86
        $this->nameEnforce = $config["plugins"]["auth"]["nameEnforce"];
87
        $this->authGroups = $config["plugins"]["auth"]["authGroups"];
88
        $this->alertChannel = $config["plugins"]["auth"]["alertChannel"];
89
        $this->guild = $config["bot"]["guild"];
90
        $this->nextCheck = 0;
91
92
        //Set name check to happen if corpTicker or nameEnforce is set
93
        if ($this->nameEnforce == "true" || $this->corpTickers == "true") {
94
            $this->nameCheck = "true";
95
        }
96
97
        //check if cache has been set
98
        $permsChecked = getPermCache("permsLastChecked");
99
        $namesChecked = getPermCache("nameStateLastChecked");
100
        if ($namesChecked == NULL) {
101
            setPermCache("nameStateLastChecked", time());
102
        }
103
104
        //if not set set for now (30 minutes from now for role removal)
105
        if ($permsChecked == NULL) {
106
            setPermCache("permsLastChecked", time() - 5);
107
            setPermCache("authStateLastChecked", time() + 7200);
108
        }
109
    }
110
111
    /**
112
     * @return array
113
     */
114
    function information()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
115
    {
116
        return array(
117
            "name" => "",
118
            "trigger" => array(),
119
            "information" => ""
120
        );
121
    }
122
123
    function tick()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
124
    {
125
        // What was the servers last reported state
126
        $lastStatus = getPermCache("serverState");
127
        if ($lastStatus == "online") {
128
            $permsChecked = getPermCache("permsLastChecked");
129
            $stateChecked = getPermCache("authStateLastChecked");
130
            $namesChecked = getPermCache("nameStateLastChecked");
131
132
            if ($permsChecked <= time()) {
133
                $this->logger->addInfo("AuthCheck: Checking for users who have left corp/alliance....");
134
                $this->checkPermissions();
135
                $this->logger->addInfo("AuthCheck: Corp/alliance check complete.");
136
            }
137
138
            if ($stateChecked <= time()) {
139
                $this->logger->addInfo("AuthCheck: Checking for users who have been wrongly given roles....");
140
                $this->checkAuthState();
141
                $this->logger->addInfo("AuthCheck: Role check complete.");
142
            }
143
144
            if ($this->nameCheck == "true" && $namesChecked <= time()) {
145
                $this->logger->addInfo("AuthCheck: Resetting player names....");
146
                $this->nameReset();
147
                $this->logger->addInfo("AuthCheck: Names reset.");
148
            }
149
        }
150
    }
151
152
    /**
153
     * @return null
154
     */
155
156
    //Remove members who have roles but never authed
157
    function checkPermissions()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
158
    {
159
        //Get guild object
160
        $guild = $this->discord->guilds->get('id', $this->guildID);
161
162
        //Establish connection to mysql
163
        $conn = new mysqli($this->db, $this->dbUser, $this->dbPass, $this->dbName);
164
165
        $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'";
166
167
        $result = $conn->query($sql);
168
169
        //Set empty arrays
170
        $corpArray = array();
171
        $allianceArray = array();
172
173
        // If config is outdated
174 View Code Duplication
        if (is_null($this->authGroups)) {
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...
175
            $msg = "**Auth Failure:** Please update the bots config to the latest version.";
176
            queueMessage($msg, $this->alertChannel, $this->guild);
177
            $nextCheck = time() + 10800;
178
            setPermCache("permsLastChecked", $nextCheck);
179
            return null;
180
        }
181
182
        //Set corp/ally id arrays
183
        foreach ($this->authGroups as $authGroup) {
184
            if ($authGroup["corpID"] != 0) {
185
                array_push($corpArray, (int) $authGroup["corpID"]);
186
            }
187
            if ($authGroup["allianceID"] != 0) {
188
                array_push($allianceArray, (int) $authGroup["allianceID"]);
189
            }
190
        }
191
192
        if ($result->num_rows >= 1) {
193
            while ($rows = $result->fetch_assoc()) {
194
                $charID = $rows['characterID'];
195
                $discordID = $rows['discordID'];
196
                $member = $guild->members->get("id", $discordID);
197
                $eveName = $rows['eveName'];
198
                //Check if member has roles
199
                if (is_null($member->roles)) {
200
                    continue;
201
                }
202
203
                //Get ingame affiliations
204
                $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID";
205
                $xml = makeApiRequest($url);
206
                // Stop the process if the api is throwing an error
207
                if (is_null($xml)) {
208
                    $this->logger->addInfo("{$eveName} cannot be authed, API issues detected.");
209
                    return null;
210
                }
211
212
                //Auth things
213
                if ($xml->result->rowset->row[0]) {
214
                    foreach ($xml->result->rowset->row as $character) {
215
                        if (!in_array((int) $character->attributes()->allianceID, $allianceArray) && !in_array((int) $character->attributes()->corporationID, $corpArray)) {
216
                            // Deactivate user in database
217
                            $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'";
218
                            $this->logger->addInfo("AuthCheck: {$eveName} account has been deactivated as they are no longer in a correct corp/alliance.");
219
                            $conn->query($sql);
220
                            continue;
221
                        }
222
                    }
223
                }
224
            }
225
            $nextCheck = time() + 10800;
226
            setPermCache("permsLastChecked", $nextCheck);
227
            return null;
228
        }
229
        $nextCheck = time() + 10800;
230
        setPermCache("permsLastChecked", $nextCheck);
231
        return null;
232
    }
233
234
    //Check user corp/alliance affiliation
235
236
237
    function checkAuthState()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
238
    {
239
240
        //Check if exempt roles are set
241
        if (is_null($this->exempt)) {
242
            $this->exempt = "0";
243
        }
244
245
        // If config is outdated
246 View Code Duplication
        if (is_null($this->authGroups)) {
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...
247
            $msg = "**Auth Failure:** Please update the bots config to the latest version.";
248
            queueMessage($msg, $this->alertChannel, $this->guild);
249
            //queue up next check
250
            $nextCheck = time() + 1800;
251
            setPermCache("authStateLastChecked", $nextCheck);
252
            return null;
253
        }
254
255
        //Establish connection to mysql
256
        $conn = new mysqli($this->db, $this->dbUser, $this->dbPass, $this->dbName);
257
258
        //get bot ID so we don't remove out own roles
259
        $botID = $this->discord->id;
260
261
        //Get guild object
262
        $guild = $this->discord->guilds->get('id', $this->guildID);
263
264
        //Check to make sure guildID is set correctly
265
        if (is_null($guild)) {
266
            $this->logger->addError("Config Error: Ensure the guild entry in the config is the guildID (aka serverID) for the main server that the bot is in.");
267
            $nextCheck = time() + 7200;
268
            setPermCache("authLastChecked", $nextCheck);
269
            return null;
270
        }
271
272
        //create empty array to store names
273
        $removedRoles = array();
274
        $userCount = 0;
275
276
        //Perform check if roles were added without permission
277
        foreach ($guild->members as $member) {
278
            $id = $member->id;
279
            $username = $member->username;
280
            $roles = $member->roles;
281
282
            //Skip to next member if this user has no roles
283
            if (is_null($roles)) {
284
                continue;
285
            }
286
            $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'";
287
            $result = $conn->query($sql);
288
289
            //If they are NOT active in the db, check for roles to remove
290
            if ($result->num_rows == 0) {
291
                $userCount++;
292
                foreach ($roles as $role) {
293
                    if (!isset($role->name)) {
294
                        if ($id != $botID && !in_array($role->name, $this->exempt, true)) {
295
                            $member->removeRole($role);
296
                            $guild->members->save($member);
297
                            // Add users name to array
298
                            array_push($removedRoles, $username);
299
                        }
300
                    }
301
                }
302
            }
303
        }
304
        //Report removed users to log and channel
305
        $nameList = implode(", ", $removedRoles);
306
        if ($userCount > 0 && strlen($nameList) > 3 && !is_null($nameList)) {
307
            $msg = "Following users roles have been removed - {$nameList}";
308
            queueMessage($msg, $this->alertChannel, $this->guild);
309
            $this->logger->addInfo("AuthCheck: Roles removed from {$nameList}");
310
        }
311
        //queue up next check
312
        $nextCheck = time() + 1800;
313
        setPermCache("authStateLastChecked", $nextCheck);
314
        return null;
315
    }
316
317
    function nameReset()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
318
    {
319
        //Get guild object
320
        $guild = $this->discord->guilds->get('id', $this->guildID);
321
322
        //Establish connection to mysql
323
        $conn = new mysqli($this->db, $this->dbUser, $this->dbPass, $this->dbName);
324
        $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'";
325
        $result = $conn->query($sql);
326
327
        // If config is outdated
328 View Code Duplication
        if (is_null($this->authGroups)) {
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...
329
            $msg = "**Auth Failure:** Please update the bots config to the latest version.";
330
            queueMessage($msg, $this->alertChannel, $this->guild);
331
            $nextCheck = time() + 10800;
332
            setPermCache("permsLastChecked", $nextCheck);
333
            return null;
334
        }
335
336
        if ($result->num_rows >= 1) {
337
            while ($rows = $result->fetch_assoc()) {
338
                $charID = $rows['characterID'];
339
                $discordID = $rows['discordID'];
340
                $member = $guild->members->get("id", $discordID);
341
                $eveName = $rows['eveName'];
342
                //Check if member has roles
343
                if (is_null($member->roles)) {
344
                    continue;
345
                }
346
347
                //Get current nickname
348
                $guild = $this->discord->guilds->get('id', $this->guildID);
349
                $member = $guild->members->get("id", $discordID);
350
                $nickName = $member->nick;
351
                $userName = $member->user->username;
352
                //If nick isn't set than make it username
353
                if ($nickName == "" || is_null($nickName)) {
354
                    $nickName = $userName;
355
                }
356
357
                //Get ingame affiliations
358
                if ($this->corpTickers == "true") {
359
                    $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID";
360
                    $xml = makeApiRequest($url);
361
                    // Stop the process if the api is throwing an error
362
                    if (is_null($xml)) {
363
                        $this->logger->addInfo("{$eveName} cannot be authed, API issues detected.");
364
                        return null;
365
                    }
366
                    if ($xml->result->rowset->row[0]) {
367
                        foreach ($xml->result->rowset->row as $character) {
368
                            $corpInfo = getCorpInfo($character->attributes()->corporationID);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $corpInfo is correct as getCorpInfo($character->...butes()->corporationID) (which targets getCorpInfo()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
369
                            if (!is_null($corpInfo)) {
370
                                $corpTicker = $corpInfo['corpTicker'];
371 View Code Duplication
                                if ($this->nameEnforce == "true") {
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...
372
                                    $nick = "[{$corpTicker}] {$eveName}";
373
                                } elseif ($nickName == "[{$corpTicker}]") {
374
                                    $nick = "[{$corpTicker}] {$userName}";
375
                                } elseif (strpos($nickName, $corpTicker) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($nickName, $corpTicker) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
376
                                    $nick = "[{$corpTicker}] {$nickName}";
377
                                } elseif (strpos($nickName, $corpTicker) !== false) {
378
                                    $nick = "{$nickName}";
379
                                    continue;
380
                                }
381
                                if ($nick != $nickName) {
382
                                    queueRename($discordID, $nick, $this->guildID);
0 ignored issues
show
Bug introduced by
The variable $nick 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...
383
                                }
384
                                continue;
385
                            }
386
                            $url = "https://api.eveonline.com/corp/CorporationSheet.xml.aspx?corporationID={$character->attributes()->corporationID}";
387
                            $xml = makeApiRequest($url);
388
                            $corpTicker = "";
389
                            $corpName = "";
390
                            foreach ($xml->result as $corporation) {
391
                                $corpTicker = $corporation->ticker;
392
                                $corpName = $corporation->corporationName;
393
                            }
394 View Code Duplication
                            if ($this->nameEnforce == "true") {
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...
395
                                $nick = "[{$corpTicker}] {$eveName}";
396
                            } elseif ($nickName == "[{$corpTicker}]") {
397
                                $nick = "[{$corpTicker}] {$userName}";
398
                            } elseif (strpos($nickName, $corpTicker) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($nickName, $corpTicker) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
399
                                $nick = "[{$corpTicker}] {$nickName}";
400
                            } elseif (strpos($nickName, $corpTicker) !== false) {
401
                                $nick = "{$nickName}";
402
                                continue;
403
                            }
404
                            if ($nick != $nickName) {
405
                                queueRename($discordID, $nick, $this->guildID);
406
                            }
407
                            addCorpInfo($character->attributes()->corporationID, $corpTicker, $corpName);
408
                            continue;
409
                        }
410
                    }
411
                }
412
                $nick = "{$eveName}";
413
                if ($nick != $nickName && $this->corpTickers != "true") {
414
                    queueRename($discordID, $nick, $this->guildID);
415
                }
416
                continue;
417
            }
418
            $nextCheck = time() + 43200;
419
            setPermCache("nameStateLastChecked", $nextCheck);
420
            return null;
421
        }
422
        $nextCheck = time() + 43200;
423
        setPermCache("nameStateLastChecked", $nextCheck);
424
        return null;
425
426
    }
427
}
428