| Conditions | 18 |
| Paths | 44 |
| Total Lines | 105 |
| Code Lines | 71 |
| Lines | 34 |
| Ratio | 32.38 % |
| Changes | 11 | ||
| Bugs | 4 | Features | 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 |
||
| 96 | public function onMessage($msgData, $message) |
||
| 97 | { |
||
| 98 | $this->message = $message; |
||
| 99 | $userID = $msgData['message']['fromID']; |
||
| 100 | $userName = $msgData['message']['from']; |
||
| 101 | $message = $msgData['message']['message']; |
||
| 102 | $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
||
| 103 | if (isset($data['trigger'])) { |
||
| 104 | if (isset($this->config['bot']['primary'])) { |
||
| 105 | $userID = $msgData['message']['fromID']; |
||
| 106 | $channelInfo = $this->message->getFullChannelAttribute(); |
||
| 107 | $guildID = $channelInfo[@guild_id]; |
||
| 108 | if ($guildID != $this->config['bot']['primary']) { |
||
| 109 | $this->message->reply('**Failure:** The auth code your attempting to use is for another discord server'); |
||
| 110 | |||
| 111 | return; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | $code = $data['messageString']; |
||
| 115 | $result = selectPending($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
||
| 116 | |||
| 117 | if (strlen($code) < 12) { |
||
| 118 | $this->message->reply('Invalid Code, check '.$this->config['bot']['trigger'].'help auth for more info.'); |
||
| 119 | |||
| 120 | return; |
||
| 121 | } |
||
| 122 | |||
| 123 | while ($rows = $result->fetch_assoc()) { |
||
| 124 | $charid = (int) $rows['characterID']; |
||
| 125 | $corpid = (int) $rows['corporationID']; |
||
| 126 | $allianceid = (int) $rows['allianceID']; |
||
| 127 | $url = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=$charid"; |
||
| 128 | $xml = makeApiRequest($url); |
||
| 129 | |||
| 130 | |||
| 131 | |||
| 132 | // We have an error, show it it |
||
| 133 | if ($xml->error) { |
||
| 134 | $this->message->reply('**Failure:** Eve API error, please try again in a little while.'); |
||
| 135 | |||
| 136 | return; |
||
| 137 | } |
||
| 138 | |||
| 139 | if (!isset($xml->result->rowset->row)) { |
||
| 140 | $this->message->reply('**Failure:** Eve API error, please try again in a little while.'); |
||
| 141 | |||
| 142 | return; |
||
| 143 | } elseif ($this->nameEnforce == 'true') { |
||
| 144 | foreach ($xml->result->rowset->row as $character) { |
||
| 145 | if ($character->attributes()->name != $userName) { |
||
| 146 | $this->message->reply('**Failure:** Your discord name must match your character name.'); |
||
| 147 | $this->logger->addInfo('User was denied due to not having the correct name '.$character->attributes()->name); |
||
| 148 | |||
| 149 | return; |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | foreach ($xml->result->rowset->row as $character) { |
||
| 154 | $eveName = $character->attributes()->name; |
||
| 155 | View Code Duplication | if ($corpid === $this->corpID) { |
|
| 156 | $roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute(); |
||
| 157 | $member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get('id', $userID); |
||
| 158 | foreach ($roles as $role) { |
||
| 159 | $roleName = $role->name; |
||
| 160 | if ($roleName == $this->roleName) { |
||
| 161 | $member->addRole($role); |
||
| 162 | $member->save(); |
||
| 163 | insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charid, $eveName, 'corp'); |
||
| 164 | disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
||
| 165 | $this->message->reply('**Success:** You have now been added to the '.$this->roleName.' group. To get more roles, talk to the CEO / Directors'); |
||
| 166 | $this->logger->addInfo('User authed and added to corp group '.$eveName); |
||
| 167 | |||
| 168 | return; |
||
| 169 | } |
||
| 170 | } |
||
| 171 | } |
||
| 172 | View Code Duplication | if ($allianceid === $this->allianceID) { |
|
| 173 | $roles = $this->message->getFullChannelAttribute()->getGuildAttribute()->getRolesAttribute(); |
||
| 174 | $member = $this->message->getFullChannelAttribute()->getGuildAttribute()->getMembersAttribute()->get('id', $userID); |
||
| 175 | foreach ($roles as $role) { |
||
| 176 | $roleName = $role->name; |
||
| 177 | if ($roleName == $this->allyroleName) { |
||
| 178 | $member->addRole($role); |
||
| 179 | $member->save(); |
||
| 180 | insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charid, $eveName, 'ally'); |
||
| 181 | disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
||
| 182 | $this->message->reply('**Success:** You have now been added to the '.$this->allyroleName.' group. To get more roles, talk to the CEO / Directors'); |
||
| 183 | $this->logger->addInfo('User authed and added to the alliance group '.$eveName); |
||
| 184 | |||
| 185 | return; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | $this->message->reply('**Failure:** There are no roles available for your corp/alliance.'); |
||
| 190 | $this->logger->addInfo('User was denied due to not being in the correct corp or alliance '.$eveName); |
||
| 191 | |||
| 192 | return; |
||
| 193 | } |
||
| 194 | } |
||
| 195 | $this->message->reply('**Failure:** There was an issue with your code.'); |
||
| 196 | $this->logger->addInfo('User was denied due to not being in the correct corp or alliance '.$userName); |
||
| 197 | |||
| 198 | return; |
||
| 199 | } |
||
| 200 | } |
||
| 201 | |||
| 221 |
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.