Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like authCheck often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use authCheck, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class authCheck |
||
|
|
|||
| 32 | { |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var |
||
| 36 | */ |
||
| 37 | private $active; |
||
| 38 | private $config; |
||
| 39 | private $discord; |
||
| 40 | private $logger; |
||
| 41 | private $db; |
||
| 42 | private $dbUser; |
||
| 43 | private $dbPass; |
||
| 44 | private $dbName; |
||
| 45 | private $guildID; |
||
| 46 | private $exempt; |
||
| 47 | private $corpTickers; |
||
| 48 | private $nameEnforce; |
||
| 49 | private $standingsBased; |
||
| 50 | private $apiKey; |
||
| 51 | private $authGroups; |
||
| 52 | private $alertChannel; |
||
| 53 | private $nameCheck; |
||
| 54 | private $dbusers; |
||
| 55 | private $characterCache; |
||
| 56 | private $corpCache; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @param $config |
||
| 60 | * @param $discord |
||
| 61 | * @param $logger |
||
| 62 | */ |
||
| 63 | public function init($config, $discord, $logger) |
||
| 114 | |||
| 115 | public function tick() |
||
| 136 | |||
| 137 | // remove user roles |
||
| 138 | private function removeRoles($member) |
||
| 159 | |||
| 160 | View Code Duplication | private function getCharacterDetails($charID, $retries = 3) |
|
| 161 | { |
||
| 162 | if (isset($this->characterCache[$charID])) { |
||
| 163 | return $this->characterCache[$charID]; |
||
| 164 | } |
||
| 165 | $character = null; |
||
| 166 | for ($i = 0; $i < $retries; $i++) { |
||
| 167 | $character = characterDetails($charID); |
||
| 168 | if (!is_null($character)) { |
||
| 169 | break; |
||
| 170 | } |
||
| 171 | } |
||
| 172 | if (!$character || isset($character['error'])) { |
||
| 173 | $this->logger->addInfo('AuthCheck: characterDetails lookup failed.'); |
||
| 174 | $msg = isset($character['error']) ? $character['error'] : 'characterDetails lookup failed'; |
||
| 175 | throw new Exception($msg); |
||
| 176 | } |
||
| 177 | $this->characterCache[$charID] = $character; |
||
| 178 | return $character; |
||
| 179 | } |
||
| 180 | |||
| 181 | View Code Duplication | private function getCorpDetails($corpID, $retries = 3) |
|
| 182 | { |
||
| 183 | if (isset($this->corpCache[$corpID])) { |
||
| 184 | return $this->corpCache[$corpID]; |
||
| 185 | } |
||
| 186 | $corporationDetails = null; |
||
| 187 | for ($i = 0; $i < $retries; $i++) { |
||
| 188 | $corporationDetails = corpDetails($corpID); |
||
| 189 | if (!is_null($corporationDetails)) { |
||
| 190 | break; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | if (!$corporationDetails || isset($corporationDetails['error'])) { |
||
| 194 | $this->logger->addInfo('AuthCheck: corpDetails lookup failed.'); |
||
| 195 | $msg = isset($corporationDetails['error']) ? $corporationDetails['error'] : 'corpDetails lookup failed'; |
||
| 196 | throw new Exception($msg); |
||
| 197 | } |
||
| 198 | $this->corpCache[$corpID] = $corporationDetails; |
||
| 199 | return $corporationDetails; |
||
| 200 | } |
||
| 201 | |||
| 202 | View Code Duplication | private function isMemberInValidCorp($member) |
|
| 203 | { |
||
| 204 | $corpArray = array_column($this->authGroups, 'corpID'); |
||
| 205 | $discordID = $member->id; |
||
| 206 | $discordNick = $member->nick ?: $member->user->username; |
||
| 207 | $return = false; |
||
| 208 | if (isset($this->dbusers[$discordID])) { |
||
| 209 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
| 210 | $return = in_array($character['corporation_id'], $corpArray); |
||
| 211 | } else { |
||
| 212 | $this->logger->addInfo("AuthCheck: User [$discordNick] not found in database."); |
||
| 213 | } |
||
| 214 | return $return; |
||
| 215 | } |
||
| 216 | |||
| 217 | View Code Duplication | private function isMemberInValidAlliance($member) |
|
| 218 | { |
||
| 219 | $allianceArray = array_column($this->authGroups, 'allianceID'); |
||
| 220 | $discordID = $member->id; |
||
| 221 | $discordNick = $member->nick ?: $member->user->username; |
||
| 222 | $return = false; |
||
| 223 | // get charID |
||
| 224 | if (isset($this->dbusers[$discordID])) { |
||
| 225 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
| 226 | $corp = $this->getCorpDetails($character['corporation_id']); |
||
| 227 | $return = in_array($corp['alliance_id'], $allianceArray); |
||
| 228 | } else { |
||
| 229 | $this->logger->addInfo("AuthCheck: User [$discordNick] not found in database."); |
||
| 230 | } |
||
| 231 | return $return; |
||
| 232 | } |
||
| 233 | |||
| 234 | private function isMemberCorpOrAllianceContact($member) { |
||
| 235 | $return = false; |
||
| 236 | $discordID = $member->id; |
||
| 237 | if (isset($this->dbusers[$discordID])) { |
||
| 238 | $role = $this->dbusers[$discordID]['role']; |
||
| 239 | if ($role === 'blue' || 'neut' || 'red') { |
||
| 240 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
| 241 | $corporationDetails = $this->getCorpDetails($character['corporation_id']); |
||
| 242 | $allianceContacts = getContacts($corporationDetails['alliance_id']); |
||
| 243 | $corpContacts = getContacts($character['corporation_id']); |
||
| 244 | if ($role === 'blue' && ((int) $allianceContacts['standing'] === 5 || 10 || (int) $corpContacts['standing'] === 5 || 10)) { |
||
| 245 | $return = true; |
||
| 246 | } |
||
| 247 | if ($role === 'red' && ((int) $allianceContacts['standing'] === -5 || -10 || (int) $corpContacts['standing'] === -5 || -10)) { |
||
| 248 | $return = true; |
||
| 249 | } |
||
| 250 | if ($role === 'neut' && ((int) $allianceContacts['standing'] === 0 || (int) $corpContacts['standing'] === 0 || (@(int) $allianceContacts['standings'] === null || '' && @(int) $corpContacts['standings'] === null || ''))) { |
||
| 251 | $return = true; |
||
| 252 | } |
||
| 253 | } |
||
| 254 | } |
||
| 255 | return $return; |
||
| 256 | } |
||
| 257 | |||
| 258 | private function deactivateMember($member) |
||
| 259 | { |
||
| 260 | $discordID = $member->id; |
||
| 261 | $discordNick = $member->nick ?: $member->user->username; |
||
| 262 | $dbh = new PDO("mysql:host={$this->db};dbname={$this->dbName}", $this->dbUser, $this->dbPass); |
||
| 263 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
| 264 | $dbh->query($sql); |
||
| 265 | $this->removeRoles($member); |
||
| 266 | $this->logger->addInfo("AuthCheck: {$discordNick} account has been deactivated as they are no longer in a correct corp/alliance."); |
||
| 267 | } |
||
| 268 | |||
| 269 | private function resetMemberNick($member) |
||
| 270 | { |
||
| 271 | $discordID = $member->id; |
||
| 272 | $discordNick = $member->nick ?: $member->user->username; |
||
| 273 | if (!isset($this->dbusers[$discordID])) { |
||
| 274 | return false; |
||
| 275 | } |
||
| 276 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
| 277 | $corpTicker = ''; |
||
| 278 | if ($this->corpTickers === 'true') { |
||
| 279 | $corporationDetails = $this->getCorpDetails($character['corporation_id']); |
||
| 280 | if ($corporationDetails && isset($corporationDetails['ticker']) && $corporationDetails['ticker'] !== 'U') { |
||
| 281 | $corpTicker = "[{$corporationDetails['ticker']}] "; |
||
| 282 | } |
||
| 283 | } |
||
| 284 | if ($this->nameEnforce) { |
||
| 285 | $newNick = $corpTicker . $this->dbusers[$discordID]['eveName']; |
||
| 286 | } else { |
||
| 287 | $newNick = $corpTicker . $discordNick; |
||
| 288 | } |
||
| 289 | if ($newNick !== $discordNick) { |
||
| 290 | queueRename($discordID, $newNick, $this->guildID); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return null |
||
| 296 | */ |
||
| 297 | |||
| 298 | //Check user corp/alliance affiliation |
||
| 299 | //Remove members who have roles but never authed |
||
| 300 | private function checkPermissions() |
||
| 326 | |||
| 327 | private function standingsUpdate() |
||
| 350 | } |
||
| 351 |
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.