| Conditions | 19 |
| Paths | 247 |
| Total Lines | 136 |
| Code Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 16 | ||
| Bugs | 8 | 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 |
||
| 103 | function checkAuth() |
||
| 104 | { |
||
| 105 | if ($this->config["plugins"]["auth"]["periodicCheck"] == "true") { |
||
| 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 | $clientId = $this->config["bot"]["clientID"]; |
||
| 111 | $id = $this->config["bot"]["guild"]; |
||
| 112 | $allyID = $this->config["plugins"]["auth"]["allianceID"]; |
||
| 113 | $corpID = $this->config["plugins"]["auth"]["corpID"]; |
||
| 114 | $toDiscordChannel = $this->config["plugins"]["auth"]["alertChannel"]; |
||
| 115 | $conn = new mysqli($db, $dbUser, $dbPass, $dbName); |
||
| 116 | |||
| 117 | //get bot ID so we don't remove out own roles |
||
| 118 | $botID = $this->discord->id; |
||
| 119 | |||
| 120 | //Remove members who have roles but never authed |
||
| 121 | $guild = $this->discord->guilds->get('id', $id); |
||
| 122 | foreach($guild->members as $member) { |
||
| 123 | $notifier = null; |
||
| 124 | $id = $member->id; |
||
| 125 | $username = $member->username; |
||
| 126 | $roles = $member->roles; |
||
| 127 | |||
| 128 | $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'"; |
||
| 129 | |||
| 130 | $result = $conn->query($sql); |
||
| 131 | if($result->num_rows == 0) { |
||
| 132 | foreach ($roles as $role) { |
||
| 133 | if(!isset($role->name)){ |
||
| 134 | if($id != $botID){ |
||
| 135 | $member->removeRole($role); |
||
| 136 | $member->save(); |
||
| 137 | // Send the info to the channel |
||
| 138 | $msg = "{$username} has been removed from the {$role->name} role as they never authed (Someone manually assigned them roles)."; |
||
| 139 | $channelID = $toDiscordChannel; |
||
| 140 | $channel = Channel::find($channelID); |
||
| 141 | $channel->sendMessage($msg, false); |
||
| 142 | $this->logger->addInfo("{$username} has been removed from the {$role->name} role as they never authed."); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'"; |
||
| 150 | |||
| 151 | $result = $conn->query($sql); |
||
| 152 | $num_rows = $result->num_rows; |
||
| 153 | |||
| 154 | if ($num_rows >= 2) { |
||
| 155 | while ($rows = $result->fetch_assoc()) { |
||
| 156 | $charID = $rows['characterID']; |
||
| 157 | $discordID = $rows['discordID']; |
||
| 158 | $guild = $this->discord->guilds->first(); |
||
| 159 | $member = $guild->members->get("id", $discordID); |
||
| 160 | $eveName = $rows['eveName']; |
||
| 161 | $roles = $member->roles; |
||
| 162 | $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID"; |
||
| 163 | $xml = makeApiRequest($url); |
||
| 164 | if ($xml->result->rowset->row[0]) { |
||
| 165 | foreach ($xml->result->rowset->row as $character) { |
||
| 166 | if ($character->attributes()->allianceID != $allyID && $character->attributes()->corporationID != $corpID) { |
||
| 167 | foreach ($roles as $role) { |
||
| 168 | $member->removeRole($role); |
||
| 169 | $member->save(); |
||
| 170 | } |
||
| 171 | |||
| 172 | $statsURL = "https://beta.eve-kill.net/api/corpInfo/corporationID/" . urlencode($corpID) . "/"; |
||
| 173 | $stats = json_decode(downloadData($statsURL), true); |
||
| 174 | |||
| 175 | if (empty($stats)) { |
||
| 176 | $stats["corporationName"] = "Unknown"; |
||
| 177 | } |
||
| 178 | |||
| 179 | $corporationName = @$stats["corporationName"]; |
||
| 180 | |||
| 181 | // Send the info to the channel |
||
| 182 | $msg = "{$eveName} roles have been removed, user is now a member of **{$corporationName}**."; |
||
| 183 | $channelID = $toDiscordChannel; |
||
| 184 | $channel = Channel::find($channelID); |
||
| 185 | $channel->sendMessage($msg, false); |
||
| 186 | $this->logger->addInfo("{$eveName} roles ({$role}) have been removed, user is now a member of **{$corporationName}**."); |
||
| 187 | |||
| 188 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
| 189 | $conn->query($sql); |
||
| 190 | |||
| 191 | } |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | $this->logger->addInfo("All users successfully authed."); |
||
| 196 | $nextCheck = time() + 7200; |
||
| 197 | setPermCache("authLastChecked", $nextCheck); |
||
| 198 | if ($this->config["plugins"]["auth"]["nameEnforce"] == "true") { |
||
| 199 | while ($rows = $result->fetch_assoc()) { |
||
| 200 | $discordID = $rows['discordID']; |
||
| 201 | $eveName = $rows['eveName']; |
||
| 202 | $guild = $this->discord->guilds->first(); |
||
| 203 | $member = $guild->members->get("id", $discordID); |
||
| 204 | $discordName = $member->user->username; |
||
| 205 | if ($discordName != $eveName) { |
||
| 206 | foreach ($roles as $role) { |
||
| 207 | $member->removeRole($role); |
||
| 208 | } |
||
| 209 | |||
| 210 | // Send the info to the channel |
||
| 211 | $msg = $discordName . " roles have been removed because their name no longer matches their ingame name."; |
||
| 212 | $channelID = $toDiscordChannel; |
||
| 213 | $channel = Channel::find($channelID); |
||
| 214 | $channel->sendMessage($msg, false); |
||
| 215 | $this->logger->addInfo($discordName . " roles have been removed because their name no longer matches their ingame name."); |
||
| 216 | |||
| 217 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
| 218 | $conn->query($sql); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | $this->logger->addInfo("All users names have been checked."); |
||
| 222 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 223 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 224 | return null; |
||
| 225 | } |
||
| 226 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 227 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 228 | return null; |
||
| 229 | } |
||
| 230 | $this->logger->addInfo("No users found in database."); |
||
| 231 | $nextCheck = time() + 7200; |
||
| 232 | setPermCache("authLastChecked", $nextCheck); |
||
| 233 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 234 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 235 | return null; |
||
| 236 | } |
||
| 237 | return null; |
||
| 238 | } |
||
| 239 | |||
| 247 |
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.