Test Setup Failed
Branch dev (952e73)
by
unknown
06:04
created

src/plugins/onMessage/sysInfo.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Discord\Discord;
4
5
class sysInfo
6
{
7
	public $config;
8
	public $discord;
9
	public $logger;
10
	private $excludeChannel;
11
	private $message;
12
	private $triggers;
13
14
	public function init($config, $discord, $logger)
15
	{
16
		$this->config = $config;
17
		$this->discord = $discord;
18
		$this->logger = $logger;
19
		$this->excludeChannel = $this->config['bot']['restrictedChannels'];
20
		$this->triggers[] = $this->config['bot']['trigger'] . 'sys';
21
		$this->triggers[] = $this->config['bot']['trigger'] . 'Sys';
22
		$this->triggers[] = $this->config['bot']['trigger'] . 'sysinfo';
23
		$this->triggers[] = $this->config['bot']['trigger'] . 'system';
24
		$this->triggers[] = $this->config['bot']['trigger'] . 'System';
25
		$this->triggers[] = $this->config['bot']['trigger'] . 'Sysinfo';
26
	}
27
28
	public function onMessage($msgData, $message)
29
	{
30
		$channelID = (int) $msgData['message']['channelID'];
31
32
		if(in_array($channelID, $this->excludeChannel, true))
33
		{
34
			return null;
35
		}
36
37
		$this->message = $message;
38
39
		$message = $msgData['message']['message'];
40
		$user = $msgData['message']['from'];
41
42
		$data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']);
43
		if(isset($data['trigger']))
44
		{
45
			$messageString = strstr($data['messageString'], '@') ? str_replace('<@', '', str_replace('>', '', $data['messageString'])) : $data['messageString'];
46
			if(is_numeric($messageString))
47
			{
48
				$messageString = dbQueryField('SELECT name FROM usersSeen WHERE id = :id', 'name', array(':id' => $messageString));
49
			}
50
			$cleanString = urlencode($messageString);
51
			$sysID = urlencode(getSystemID($cleanString));
52
53
			//Check if we get a system back, otherwise check for partials
54
			if(empty($sysID))
55
			{
56
                return $this->message->reply('**Error:** no data available');
57
			}
58
59
			$systemDetails = systemDetails($sysID);
60
			if (null === $systemDetails)
61
			{
62
				return $this->message->reply('**Error:** ESI is down. Try again later.');
63
			}
64
65
			$url = "https://zkillboard.com/api/stats/solarSystemID/{$sysID}/";
66
			$json = json_decode(file_get_contents($url));
67
68
			$regionID = $json->info->regionID;
69
			$regionName = getRegionName($regionID);
70
71
			$thisMonth = (string)date('Ym');
72
            $lastMonth = (string)date('Ym', strtotime('first day of previous month'));
73
74
            $thisMonthKill = $json->months->$thisMonth->shipsDestroyed;
75
            $lastMonthKill = $json->months->$lastMonth->shipsDestroyed;
76
77
            $activePVP = $json->activepvp;
78
            $activeKills = $activePVP->kills->count;
79
            $activeChars = $activePVP->characters->count;
80
81
			$sysName = $systemDetails['name'];
82
			$secStatus = round($systemDetails['security_status'],2);
83
84
			$npc = "https://api.eveonline.com/map/kills.xml.aspx";
85
			$npc = new SimpleXMLElement(file_get_contents($npc));
86
			foreach($npc->result->rowset->row as $row){
87
				if($row->attributes()->solarSystemID == $sysID){
88
					$npcKills = $row->attributes()->factionKills;
89
					$podKills = $row->attributes()->podKills;
90
				}
91
			}
92
93
			$url = "https://zkillboard.com/system/{$sysID}/";
94
95
			$msg = "```Systeminfo
96
Name: {$sysName} 
97
Region: {$regionName}
98
Security: {$secStatus}
99
100
Kills
101
This Month: {$thisMonthKill}
102
Last Month: {$lastMonthKill}
103
104
Activity
105
Characters: {$activeChars}
106
Kills: {$activeKills}
107
Pod Kills: {$podKills}
0 ignored issues
show
The variable $podKills 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...
108
NPC Kills: {$npcKills}
0 ignored issues
show
The variable $npcKills 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...
109
```
110
111
More information: $url";
112
			$this->logger->addInfo("sysInfo: Sending system information to {$user}");
113
			$this->message->reply($msg);
114
		}
115
		return null;
116
	}
117
118
	public function information()
119
	{
120
		return array(
121
			'name' => 'sys',
122
			'trigger' => $this->triggers,
123
			'information' => 'Returns information for the given system'
124
			);
125
	}
126
}
127