| Conditions | 13 |
| Paths | 24 |
| Total Lines | 90 |
| Code Lines | 56 |
| Lines | 4 |
| Ratio | 4.44 % |
| 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 |
||
| 73 | function onMessage($msgData, $message) |
||
| 74 | { |
||
| 75 | $channelID = (int)$msgData["message"]["channelID"]; |
||
| 76 | |||
| 77 | if (in_array($channelID, $this->excludeChannel, true)) |
||
| 78 | { |
||
| 79 | return null; |
||
| 80 | } |
||
| 81 | |||
| 82 | $this->message = $message; |
||
| 83 | |||
| 84 | $message = $msgData["message"]["message"]; |
||
| 85 | $user = $msgData["message"]["from"]; |
||
| 86 | |||
| 87 | $data = command($message, $this->information()["trigger"], $this->config["bot"]["trigger"]); |
||
| 88 | if (isset($data["trigger"])) { |
||
| 89 | |||
| 90 | // Most EVE players on Discord use their ingame name, so lets support @highlights |
||
| 91 | $messageString = stristr($data["messageString"], "@") ? str_replace("<@", "", str_replace(">", "", $data["messageString"])) : $data["messageString"]; |
||
| 92 | if (is_numeric($messageString)) { |
||
| 93 | // The person used @highlighting, so now we got a discord id, lets map that to a name |
||
| 94 | $messageString = dbQueryField("SELECT name FROM usersSeen WHERE id = :id", "name", array(":id" => $messageString)); |
||
| 95 | } |
||
| 96 | |||
| 97 | $cleanString = urlencode($messageString); |
||
| 98 | |||
| 99 | //API call 1 |
||
| 100 | $url = "https://api.eveonline.com/eve/CharacterID.xml.aspx?names={$cleanString}"; |
||
| 101 | $xml = makeApiRequest($url); |
||
| 102 | $characterID = null; |
||
| 103 | |||
| 104 | View Code Duplication | if (isset($xml->result->rowset->row)) { |
|
| 105 | foreach ($xml->result->rowset->row as $character) { |
||
| 106 | $characterID = $character->attributes()->characterID; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | if (empty($characterID)) { |
||
| 110 | return $this->message->reply("**Error:** no data available"); |
||
| 111 | } |
||
| 112 | $characterID = urlencode($characterID); |
||
| 113 | |||
| 114 | //API call 2 |
||
| 115 | $url = "https://api.eveonline.com/eve/CharacterInfo.xml.aspx?characterID={$characterID}"; |
||
| 116 | $xml = makeApiRequest($url); |
||
| 117 | foreach ($xml->result as $characterDetails) { |
||
| 118 | $dob = $characterDetails->DoB; |
||
| 119 | $corporationName = $characterDetails->corporation; |
||
| 120 | $corporationJoinDate = $characterDetails->corporationDate; |
||
| 121 | $allianceName = $characterDetails->alliance; |
||
| 122 | $securityStatus = $characterDetails->securityStatus; |
||
| 123 | $characterName = $characterDetails->characterName; |
||
| 124 | } |
||
| 125 | |||
| 126 | //ZKill lookup |
||
| 127 | $url = "https://zkillboard.com/api/orderDirection/desc/limit/1/no-items/characterID/{$characterID}/xml/"; |
||
| 128 | $xml = makeApiRequest($url); |
||
| 129 | if (empty($xml)) { |
||
| 130 | return $this->message->reply("**Error:** ZKill is down. Try again later."); |
||
| 131 | } |
||
| 132 | foreach ($xml->result->rowset->row as $kill) { |
||
| 133 | $lastSeenSystemID = $kill->attributes()->solarSystemID; |
||
| 134 | $lastSeenSystem = apiCharacterName($lastSeenSystemID); |
||
| 135 | $lastSeenDate = $kill->attributes()->killTime; |
||
| 136 | } |
||
| 137 | foreach ($xml->result->rowset->row->rowset->row as $attacker) { |
||
| 138 | if ($attacker->attributes()->characterID == $characterID){ |
||
| 139 | $lastSeenShipID = $attacker->attributes()->shipTypeID; |
||
| 140 | $lastSeenShip = apiTypeName($lastSeenShipID); |
||
| 141 | } |
||
| 142 | } |
||
| 143 | $url = "https://zkillboard.com/character/{$characterID}/"; |
||
| 144 | |||
| 145 | $msg = "```Name: {$characterName} |
||
| 146 | |||
| 147 | Corporation Name: {$corporationName} |
||
| 148 | Joined Corporation On: {$corporationJoinDate} |
||
| 149 | Alliance Name: {$allianceName} |
||
| 150 | Security Status: {$securityStatus} |
||
| 151 | |||
| 152 | Last Seen In System: {$lastSeenSystem} |
||
| 153 | Last Seen Flying a: {$lastSeenShip} |
||
| 154 | Last Seen On: {$lastSeenDate}``` |
||
| 155 | |||
| 156 | For more info, visit: $url"; |
||
| 157 | |||
| 158 | $this->logger->addInfo("charInfo: Sending character info to {$user}"); |
||
| 159 | $this->message->reply($msg); |
||
| 160 | } |
||
| 161 | return null; |
||
| 162 | } |
||
| 163 | |||
| 181 |
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.