Conditions | 15 |
Paths | 41 |
Total Lines | 112 |
Code Lines | 70 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
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} |
||
131 | NPC Kills: {$npcKills} |
||
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 | |||
150 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.