Passed
Branch master (763a28)
by
unknown
03:03
created

sysInfo::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
use Discord\Discord;
4
5
class sysInfo
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...
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(systemID($cleanString));
52
53
			//Check if we get a system back, otherwise check for partials
54
			if(empty($sysID))
55
			{
56
				$search = "http://tools.pandemic-legion.pl/api/search/{$cleanString}";
57
				$search = json_decode(file_get_contents($search));
58
				$tot = 0;
59
				$res = array();
60
				foreach ($search as $s){
61
					if ($s->type == "system"){
62
						$tot++;
63
						$res[] = $s->name;
64
					}
65
				}
66
				if ($tot == 0){
67
					return $this->message->reply('**Error:** no data available');
68
				}
69
				if ($tot == 1){
70
					$sysID = urlencode(systemID($res[0]));
71
					if(empty($sysID)){
72
						return $this->message->reply('**Error:** no data available');
73
					}
74
				}
75
				if ($tot > 1){
76
					$res = implode(", ",$res);
77
					return $this->message->reply("**Error:** Did you mean one of these? {$res}");
78
				}			
79
			}
80
81
			$systemDetails = systemDetails($sysID);
82
			if (null === $systemDetails)
83
			{
84
				return $this->message->reply('**Error:** ESI is down. Try again later.');
85
			}
86
87
			$url = "https://zkillboard.com/api/stats/solarSystemID/{$sysID}/";
88
			$json = json_decode(file_get_contents($url));
89
90
			$regionID = $json->info->regionID;
91
			$regionDetails = regionDetails($regionID);
92
			$regionName = $regionDetails['name'];
93
94
			$thisMonth = (string)date('Ym');
95
            $lastMonth = (string)date('Ym', strtotime('first day of previous month'));
96
97
            $thisMonthKill = $json->months->$thisMonth->shipsDestroyed;
98
            $lastMonthKill = $json->months->$lastMonth->shipsDestroyed;
99
100
            $activePVP = $json->activepvp;
101
            $activeKills = $activePVP->kills->count;
102
            $activeChars = $activePVP->characters->count;
103
104
			$sysName = $systemDetails['name'];
105
			$secStatus = round($systemDetails['security_status'],2);
106
107
			$npc = "https://api.eveonline.com/map/kills.xml.aspx";
108
			$npc = new SimpleXMLElement(file_get_contents($npc));
109
			foreach($npc->result->rowset->row as $row){
110
				if($row->attributes()->solarSystemID == $sysID){
111
					$npcKills = $row->attributes()->factionKills;
112
					$podKills = $row->attributes()->podKills;
113
				}
114
			}
115
116
			$url = "https://zkillboard.com/system/{$sysID}/";
117
118
			$msg = "```Systeminfo
119
Name: {$sysName} 
120
Region: {$regionName}
121
Security: {$secStatus}
122
123
Kills
124
This Month: {$thisMonthKill}
125
Last Month: {$lastMonthKill}
126
127
Activity
128
Characters: {$activeChars}
129
Kills: {$activeKills}
130
Pod Kills: {$podKills}
0 ignored issues
show
Bug introduced by
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...
131
NPC Kills: {$npcKills}
0 ignored issues
show
Bug introduced by
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...
132
```
133
134
More information: $url";
135
			$this->logger->addInfo("sysInfo: Sending system information to {$user}");
136
			$this->message->reply($msg);
137
		}
138
		return null;
139
	}
140
141
	public function information()
142
	{
143
		return array(
144
			'name' => 'sys',
145
			'trigger' => $this->triggers,
146
			'information' => 'Returns information for the given system'
147
			);
148
	}
149
}
150