| Conditions | 21 |
| Paths | 504 |
| Total Lines | 123 |
| Code Lines | 83 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 25 | ||
| Bugs | 10 | Features | 1 |
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 |
||
| 104 | function checkAuth($discord) |
||
| 105 | { |
||
| 106 | $db = $this->config["database"]["host"]; |
||
| 107 | $dbUser = $this->config["database"]["user"]; |
||
| 108 | $dbPass = $this->config["database"]["pass"]; |
||
| 109 | $dbName = $this->config["database"]["database"]; |
||
| 110 | $id = $this->config["bot"]["guild"]; |
||
| 111 | $allyID = $this->config["plugins"]["auth"]["allianceID"]; |
||
| 112 | $corpID = $this->config["plugins"]["auth"]["corpID"]; |
||
| 113 | $exempt = $this->config["plugins"]["auth"]["exempt"]; |
||
| 114 | if (is_null($exempt)) { |
||
| 115 | $exempt = "0"; |
||
| 116 | } |
||
| 117 | $toDiscordChannel = $this->config["plugins"]["auth"]["alertChannel"]; |
||
| 118 | $conn = new mysqli($db, $dbUser, $dbPass, $dbName); |
||
| 119 | |||
| 120 | //get bot ID so we don't remove out own roles |
||
| 121 | $botID = $this->discord->id; |
||
| 122 | |||
| 123 | //Remove members who have roles but never authed |
||
| 124 | $guild = $discord->guilds->get('id', $id); |
||
| 125 | foreach($guild->members as $member) { |
||
| 126 | $id = $member->id; |
||
| 127 | $username = $member->username; |
||
| 128 | $roles = $member->roles; |
||
| 129 | |||
| 130 | $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'"; |
||
| 131 | |||
| 132 | $result = $conn->query($sql); |
||
| 133 | if ($result->num_rows == 0) { |
||
| 134 | foreach ($roles as $role) { |
||
| 135 | if (!isset($role->name)) { |
||
| 136 | if ($id != $botID && !in_array($role->name, $exempt, true)) { |
||
| 137 | $member->removeRole($role); |
||
| 138 | $guild->members->save($member); |
||
| 139 | // Send the info to the channel |
||
| 140 | $msg = "{$username} has been removed from the {$role->name} role as they never authed (Someone manually assigned them roles)."; |
||
| 141 | $channelID = $toDiscordChannel; |
||
| 142 | $channel = $guild->channels->get('id', $channelID); |
||
| 143 | $channel->sendMessage($msg, false); |
||
| 144 | $this->logger->addInfo("{$username} has been removed from the {$role->name} role as they never authed."); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'"; |
||
| 152 | |||
| 153 | $result = $conn->query($sql); |
||
| 154 | $num_rows = $result->num_rows; |
||
| 155 | |||
| 156 | if ($num_rows >= 1) { |
||
| 157 | while ($rows = $result->fetch_assoc()) { |
||
| 158 | $charID = $rows['characterID']; |
||
| 159 | $discordID = $rows['discordID']; |
||
| 160 | $member = $guild->members->get("id", $discordID); |
||
| 161 | $eveName = $rows['eveName']; |
||
| 162 | $roles = $member->roles; |
||
| 163 | |||
| 164 | if ($this->config["plugins"]["auth"]["nameEnforce"] == "true" && !is_null($member)) { |
||
| 165 | $nick = $eveName; |
||
| 166 | $member->setNickname($nick); |
||
| 167 | } |
||
| 168 | |||
| 169 | $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID"; |
||
| 170 | $xml = makeApiRequest($url); |
||
| 171 | // Stop the process if the api is throwing an error |
||
| 172 | if (is_null($xml)) { |
||
| 173 | $this->logger->addInfo("{$eveName} cannot be authed, API issues detected."); |
||
| 174 | return null; |
||
| 175 | } |
||
| 176 | if ($xml->result->rowset->row[0]) { |
||
| 177 | foreach ($xml->result->rowset->row as $character) { |
||
| 178 | |||
| 179 | if ($character->attributes()->allianceID != $allyID && $character->attributes()->corporationID != $corpID) { |
||
| 180 | foreach ($roles as $role) { |
||
| 181 | $member->removeRole($role); |
||
| 182 | $guild->members->save($member); |
||
| 183 | } |
||
| 184 | |||
| 185 | $statsURL = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=" . urlencode($character->attributes()->corporationID) . "/"; |
||
| 186 | $stats = makeApiRequest($statsURL); |
||
| 187 | foreach ($stats->result->rowset->row as $corporation) { |
||
| 188 | $corporationName = $corporation->attributes()->name; |
||
| 189 | } |
||
| 190 | |||
| 191 | if (!isset($corporationName)) { // Make sure it's always set. |
||
| 192 | $corporationName = "Unknown"; |
||
| 193 | } |
||
| 194 | |||
| 195 | if (!isset($role)) { // Make sure it's always set. |
||
| 196 | $role = "Unknown"; |
||
| 197 | } |
||
| 198 | |||
| 199 | // Send the info to the channel |
||
| 200 | $msg = "{$eveName} roles have been removed, user is now a member of **{$corporationName}**."; |
||
| 201 | $channelID = $toDiscordChannel; |
||
| 202 | $channel = $guild->channels->get('id', $channelID); |
||
| 203 | $channel->sendMessage($msg, false); |
||
| 204 | $this->logger->addInfo("{$eveName} roles ({$role}) have been removed, user is now a member of **{$corporationName}**."); |
||
| 205 | |||
| 206 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
| 207 | $conn->query($sql); |
||
| 208 | |||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | } |
||
| 213 | $this->logger->addInfo("All users successfully authed."); |
||
| 214 | $nextCheck = time() + 7200; |
||
| 215 | setPermCache("authLastChecked", $nextCheck); |
||
| 216 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 217 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 218 | return null; |
||
| 219 | } |
||
| 220 | $this->logger->addInfo("No users found in database."); |
||
| 221 | $nextCheck = time() + 7200; |
||
| 222 | setPermCache("authLastChecked", $nextCheck); |
||
| 223 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 224 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 225 | return null; |
||
| 226 | } |
||
| 227 | |||
| 232 |
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.