| Conditions | 53 |
| Paths | 2171 |
| Total Lines | 157 |
| Code Lines | 106 |
| Lines | 28 |
| Ratio | 17.83 % |
| 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 |
||
| 78 | public function onMessage($msgData, $message) |
||
| 79 | { |
||
| 80 | $channelID = (int) $msgData['message']['channelID']; |
||
| 81 | |||
| 82 | if (in_array($channelID, $this->excludeChannel, true)) { |
||
| 83 | return null; |
||
| 84 | } |
||
| 85 | |||
| 86 | $this->message = $message; |
||
| 87 | $userID = $msgData['message']['fromID']; |
||
| 88 | $userName = $msgData['message']['from']; |
||
| 89 | $message = $msgData['message']['message']; |
||
| 90 | $channelInfo = $this->message->channel; |
||
| 91 | $guildID = $channelInfo[@guild_id]; |
||
| 92 | $data = command($message, $this->information()['trigger'], $this->config['bot']['trigger']); |
||
| 93 | if (isset($data['trigger'])) { |
||
| 94 | if (isset($this->config['bot']['primary'])) { |
||
| 95 | if ($guildID != $this->config['bot']['primary']) { |
||
| 96 | $this->message->reply('**Failure:** The auth code your attempting to use is for another discord server'); |
||
| 97 | return null; |
||
| 98 | } |
||
| 99 | |||
| 100 | } |
||
| 101 | // If config is outdated |
||
| 102 | if (null === $this->authGroups) { |
||
| 103 | $this->message->reply('**Failure:** Please update the bots config to the latest version.'); |
||
| 104 | return null; |
||
| 105 | } |
||
| 106 | |||
| 107 | $code = $data['messageString']; |
||
| 108 | $result = selectPending($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
||
| 109 | |||
| 110 | if (strlen($code) < 12) { |
||
| 111 | $this->message->reply('Invalid Code, check ' . $this->config['bot']['trigger'] . 'help auth for more info.'); |
||
| 112 | return null; |
||
| 113 | } |
||
| 114 | |||
| 115 | while ($rows = $result->fetch_assoc()) { |
||
| 116 | $charID = (int) $rows['characterID']; |
||
| 117 | $corpID = (int) $rows['corporationID']; |
||
| 118 | $allianceID = (int) $rows['allianceID']; |
||
| 119 | |||
| 120 | //If corp is new store in DB |
||
| 121 | $corpInfo = getCorpInfo($corpID); |
||
| 122 | if (null === $corpInfo) { |
||
| 123 | $corpDetails = corpDetails($corpID); |
||
| 124 | if (null === $corpDetails) { // Make sure it's always set. |
||
| 125 | $this->message->reply('**Failure:** Unable to auth at this time, ESI is down. Please try again later.'); |
||
| 126 | return null; |
||
| 127 | } |
||
| 128 | $corpTicker = $corpDetails['ticker']; |
||
| 129 | $corpName = (string)$corpDetails['corporation_name']; |
||
| 130 | if (null !== $corpTicker) { |
||
| 131 | addCorpInfo($corpID, $corpTicker, $corpName); |
||
| 132 | } |
||
| 133 | } else { |
||
| 134 | $corpTicker = $corpInfo['corpTicker']; |
||
| 135 | } |
||
| 136 | |||
| 137 | //Add corp ticker to name |
||
| 138 | if ($this->corpTickers === 'true') { |
||
| 139 | $setTicker = 1; |
||
| 140 | } |
||
| 141 | |||
| 142 | //Set eve name if nameCheck is true |
||
| 143 | if ($this->nameEnforce === 'true') { |
||
| 144 | $nameEnforce = 1; |
||
| 145 | } |
||
| 146 | $role = null; |
||
| 147 | |||
| 148 | $roles = @$this->message->channel->guild->roles; |
||
| 149 | $member = @$this->message->channel->guild->members->get('id', $userID); |
||
| 150 | $eveName = characterName($charID); |
||
| 151 | if (null === $eveName) { |
||
| 152 | $this->message->reply('**Failure:** Unable to auth at this time, ESI is down. Please try again later.'); |
||
| 153 | return null; |
||
| 154 | } |
||
| 155 | foreach ($this->authGroups as $authGroup) { |
||
| 156 | //Check if corpID matches |
||
| 157 | if ($corpID === $authGroup['corpID']) { |
||
| 158 | View Code Duplication | foreach ($roles as $role) { |
|
| 159 | if ((string)$role->name === (string)$authGroup['corpMemberRole']) { |
||
| 160 | $member->addRole($role); |
||
| 161 | $role = 'corp'; |
||
| 162 | } |
||
| 163 | } |
||
| 164 | } |
||
| 165 | //Check if allianceID matches |
||
| 166 | if ($allianceID === $authGroup['allianceID'] && $authGroup['allianceID'] != 0) { |
||
| 167 | View Code Duplication | foreach ($roles as $role) { |
|
| 168 | if ((string)$role->name === (string)$authGroup['allyMemberRole']) { |
||
| 169 | $member->addRole($role); |
||
| 170 | $role = 'ally'; |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | //check for standings based roles |
||
| 175 | if ($this->standingsBased === 'true' && $role === null) { |
||
| 176 | $allianceContacts = getContacts($allianceID); |
||
| 177 | $corpContacts = getContacts($corpID); |
||
| 178 | foreach ($roles as $role) { |
||
| 179 | View Code Duplication | if ((@(int)$allianceContacts['standings'] === 5 || @(int)$corpContacts['standings'] === 5) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['plus5Role']) { |
|
| 180 | $member->addRole($role); |
||
| 181 | $role = 'blue'; |
||
| 182 | } |
||
| 183 | View Code Duplication | if ((@(int)$allianceContacts['standings'] === 10 || @(int)$corpContacts['standings'] === 10) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['plus10Role']) { |
|
| 184 | $member->addRole($role); |
||
| 185 | $role = 'blue'; |
||
| 186 | } |
||
| 187 | if ((@(int)$allianceContacts['standings'] === 0 || @(int)$corpContacts['standings'] === 0 || (@(int)$allianceContacts['standings'] && @(int)$corpContacts['standings'] === null || '')) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['neutralRole']) { |
||
| 188 | $member->addRole($role); |
||
| 189 | $role = 'neut'; |
||
| 190 | } |
||
| 191 | View Code Duplication | if ((@(int)$allianceContacts['standings'] === -5 || @(int)$corpContacts['standings'] === -5) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['minus5Role']) { |
|
| 192 | $member->addRole($role); |
||
| 193 | $role = 'red'; |
||
| 194 | } |
||
| 195 | View Code Duplication | if ((@(int)$allianceContacts['standings'] === -10 || @(int)$corpContacts['standings'] === -10) && (string)$role->name === (string)$this->config['plugins']['auth']['standings']['minus10Role']) { |
|
| 196 | $member->addRole($role); |
||
| 197 | $role = 'red'; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | if (null !== $role) { |
||
| 202 | $guild = $this->discord->guilds->get('id', $guildID); |
||
| 203 | insertUser($this->db, $this->dbUser, $this->dbPass, $this->dbName, $userID, $charID, $eveName, $role); |
||
| 204 | disableReg($this->db, $this->dbUser, $this->dbPass, $this->dbName, $code); |
||
| 205 | $msg = ":white_check_mark: **Success:** {$userName} has been successfully authed."; |
||
| 206 | $this->logger->addInfo("auth: {$eveName} authed"); |
||
| 207 | $this->message->reply($msg); |
||
| 208 | //Add ticker if set and change name if nameEnforce is on |
||
| 209 | if (isset($setTicker) || isset($nameEnforce)) { |
||
| 210 | if (isset($setTicker) && isset($nameEnforce)) { |
||
| 211 | $nick = "[{$corpTicker}] {$eveName}"; |
||
| 212 | } elseif (null === $setTicker && isset($nameEnforce)) { |
||
| 213 | $nick = "{$eveName}"; |
||
| 214 | } elseif (isset($setTicker) && !isset($nameEnforce)) { |
||
| 215 | $nick = "[{$corpTicker}] {$userName}"; |
||
| 216 | } |
||
| 217 | } |
||
| 218 | if (null !== $nick) { |
||
| 219 | queueRename($userID, $nick, $this->guild); |
||
| 220 | } |
||
| 221 | $guild->members->save($member); |
||
| 222 | return null; |
||
| 223 | } |
||
| 224 | } |
||
| 225 | $this->message->reply('**Failure:** There are no roles available for your corp/alliance.'); |
||
| 226 | $this->logger->addInfo('Auth: User was denied due to not being in the correct corp or alliance ' . $eveName); |
||
| 227 | return null; |
||
| 228 | } |
||
| 229 | $this->message->reply('**Failure:** There was an issue with your code.'); |
||
| 230 | $this->logger->addInfo('Auth: User was denied due to the code being invalid ' . $userName); |
||
| 231 | return null; |
||
| 232 | } |
||
| 233 | return null; |
||
| 234 | } |
||
| 235 | |||
| 248 |
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.