| Conditions | 18 |
| Paths | 216 |
| Total Lines | 116 |
| Code Lines | 80 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 23 | ||
| Bugs | 9 | 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 | $notifier = null; |
||
| 127 | $id = $member->id; |
||
| 128 | $username = $member->username; |
||
| 129 | $roles = $member->roles; |
||
| 130 | |||
| 131 | $sql = "SELECT * FROM authUsers WHERE discordID='$id' AND active='yes'"; |
||
| 132 | |||
| 133 | $result = $conn->query($sql); |
||
| 134 | if($result->num_rows == 0) { |
||
| 135 | foreach ($roles as $role) { |
||
| 136 | if(!isset($role->name)){ |
||
| 137 | if($id != $botID && !in_array($role->name, $exempt, true)){ |
||
| 138 | $member->removeRole($role); |
||
| 139 | $guild->members->save($member); |
||
| 140 | // Send the info to the channel |
||
| 141 | $msg = "{$username} has been removed from the {$role->name} role as they never authed (Someone manually assigned them roles)."; |
||
| 142 | $channelID = $toDiscordChannel; |
||
| 143 | $channel = $guild->channels->get('id', $channelID); |
||
| 144 | $channel->sendMessage($msg, false); |
||
| 145 | $this->logger->addInfo("{$username} has been removed from the {$role->name} role as they never authed."); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | $sql = "SELECT characterID, discordID, eveName FROM authUsers WHERE active='yes'"; |
||
| 153 | |||
| 154 | $result = $conn->query($sql); |
||
| 155 | $num_rows = $result->num_rows; |
||
| 156 | |||
| 157 | if ($num_rows >= 1) { |
||
| 158 | while ($rows = $result->fetch_assoc()) { |
||
| 159 | $charID = $rows['characterID']; |
||
| 160 | $discordID = $rows['discordID']; |
||
| 161 | $member = $guild->members->get("id", $discordID); |
||
| 162 | $eveName = $rows['eveName']; |
||
| 163 | $roles = $member->roles; |
||
| 164 | |||
| 165 | if ($this->config["plugins"]["auth"]["nameEnforce"] == "true") { |
||
| 166 | $nick = $eveName; |
||
| 167 | $member->setNickname($nick); |
||
| 168 | } |
||
| 169 | |||
| 170 | $url = "https://api.eveonline.com/eve/CharacterAffiliation.xml.aspx?ids=$charID"; |
||
| 171 | $xml = makeApiRequest($url); |
||
| 172 | // Stop the process if the api is throwing an error |
||
| 173 | if (is_null($xml)){ |
||
| 174 | $this->logger->addInfo("{$eveName} cannot be authed, API issues detected."); |
||
| 175 | return null; |
||
| 176 | } |
||
| 177 | if ($xml->result->rowset->row[0]) { |
||
| 178 | foreach ($xml->result->rowset->row as $character) { |
||
| 179 | |||
| 180 | if ($character->attributes()->allianceID != $allyID && $character->attributes()->corporationID != $corpID) { |
||
| 181 | foreach ($roles as $role) { |
||
| 182 | $member->removeRole($role); |
||
| 183 | $guild->members->save($member); |
||
| 184 | } |
||
| 185 | |||
| 186 | $statsURL = "https://api.eveonline.com/eve/CharacterName.xml.aspx?ids=" . urlencode($character->attributes()->corporationID) . "/"; |
||
| 187 | $stats = makeApiRequest($statsURL); |
||
| 188 | foreach ($stats->result->rowset->row as $corporation) { |
||
| 189 | $corporationName = $corporation->attributes()->name; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Send the info to the channel |
||
| 193 | $msg = "{$eveName} roles have been removed, user is now a member of **{$corporationName}**."; |
||
| 194 | $channelID = $toDiscordChannel; |
||
| 195 | $channel = $guild->channels->get('id', $channelID); |
||
| 196 | $channel->sendMessage($msg, false); |
||
| 197 | $this->logger->addInfo("{$eveName} roles ({$role}) have been removed, user is now a member of **{$corporationName}**."); |
||
| 198 | |||
| 199 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
| 200 | $conn->query($sql); |
||
| 201 | |||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | } |
||
| 206 | $this->logger->addInfo("All users successfully authed."); |
||
| 207 | $nextCheck = time() + 7200; |
||
| 208 | setPermCache("authLastChecked", $nextCheck); |
||
| 209 | $cacheTimer = gmdate("Y-m-d H:i:s", $nextCheck); |
||
| 210 | $this->logger->addInfo("Next auth and name check at {$cacheTimer} EVE"); |
||
| 211 | return null; |
||
| 212 | } |
||
| 213 | $this->logger->addInfo("No users found in database."); |
||
| 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 | |||
| 228 |
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.