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) |
||
110 | |||
111 | public function tick() |
||
132 | |||
133 | // remove user roles |
||
134 | |||
135 | |||
136 | private function checkPermissions() |
||
137 | { |
||
138 | $this->characterCache = array(); |
||
139 | $this->corpCache = array(); |
||
140 | // Load database users |
||
141 | $dbh = new PDO("mysql:host={$this->db};dbname={$this->dbName}", $this->dbUser, $this->dbPass); |
||
142 | $this->dbusers = array_column($dbh->query("SELECT discordID, characterID, eveName, role FROM authUsers where active='yes'")->fetchAll(), null, 'discordID'); |
||
143 | if (!$this->dbusers) { |
||
144 | return; |
||
145 | } |
||
146 | foreach ($this->discord->guilds->get('id', $this->guildID)->members as $member) { |
||
147 | $discordNick = $member->nick ?: $member->user->username; |
||
148 | if ($this->discord->id == $member->id || $member->getRolesAttribute()->isEmpty()) { |
||
149 | continue; |
||
150 | } |
||
151 | $this->logger->addDebug("AuthCheck: Username: $discordNick"); |
||
152 | try { |
||
153 | if (!($this->isMemberInValidCorp($member) || $this->isMemberInValidAlliance($member) || $this->isMemberCorpOrAllianceContact($member))) { |
||
154 | $this->deactivateMember($member); |
||
155 | } |
||
156 | if ($this->nameCheck) { |
||
157 | $this->resetMemberNick($member); |
||
158 | } |
||
159 | } catch (Exception $e) { |
||
160 | $this->logger->addError("AuthCheck: " . $e->getMessage()); |
||
161 | if ($e->getMessage() == 'The datasource tranquility is temporarily unavailable') { |
||
162 | return; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | setPermCache('permsLastChecked', time() + 1800); |
||
167 | } |
||
168 | |||
169 | private function isMemberInValidCorp($member) |
||
170 | { |
||
171 | $corpArray = array_column($this->authGroups, 'corpID'); |
||
172 | $discordID = $member->id; |
||
173 | $return = false; |
||
174 | if (isset($this->dbusers[$discordID])) { |
||
175 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
176 | $return = in_array($character['corporation_id'], $corpArray); |
||
177 | } |
||
178 | return $return; |
||
179 | } |
||
180 | |||
181 | View Code Duplication | private function getCharacterDetails($charID, $retries = 3) |
|
182 | { |
||
183 | if (isset($this->characterCache[$charID])) { |
||
184 | return $this->characterCache[$charID]; |
||
185 | } |
||
186 | $character = null; |
||
187 | for ($i = 0; $i < $retries; $i++) { |
||
188 | $character = characterDetails($charID); |
||
189 | if (!is_null($character)) { |
||
190 | break; |
||
191 | } |
||
192 | } |
||
193 | if (!$character || isset($character['error'])) { |
||
194 | $this->logger->addInfo('AuthCheck: characterDetails lookup failed.'); |
||
195 | $msg = isset($character['error']) ? $character['error'] : 'characterDetails lookup failed'; |
||
196 | throw new Exception($msg); |
||
197 | } |
||
198 | $this->characterCache[$charID] = $character; |
||
199 | return $character; |
||
200 | } |
||
201 | |||
202 | private function isMemberInValidAlliance($member) |
||
203 | { |
||
204 | $allianceArray = array_column($this->authGroups, 'allianceID'); |
||
205 | $discordID = $member->id; |
||
206 | $return = false; |
||
207 | // get charID |
||
208 | if (isset($this->dbusers[$discordID])) { |
||
209 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
210 | $corp = $this->getCorpDetails($character['corporation_id']); |
||
211 | $return = isset($corp['alliance_id']) && in_array($corp['alliance_id'], $allianceArray); |
||
212 | } |
||
213 | return $return; |
||
214 | } |
||
215 | |||
216 | View Code Duplication | private function getCorpDetails($corpID, $retries = 3) |
|
217 | { |
||
218 | if (isset($this->corpCache[$corpID])) { |
||
219 | return $this->corpCache[$corpID]; |
||
220 | } |
||
221 | $corporationDetails = null; |
||
222 | for ($i = 0; $i < $retries; $i++) { |
||
223 | $corporationDetails = corpDetails($corpID); |
||
224 | if (!is_null($corporationDetails)) { |
||
225 | break; |
||
226 | } |
||
227 | } |
||
228 | if (!$corporationDetails || isset($corporationDetails['error'])) { |
||
229 | $this->logger->addInfo('AuthCheck: corpDetails lookup failed.'); |
||
230 | $msg = isset($corporationDetails['error']) ? $corporationDetails['error'] : 'corpDetails lookup failed'; |
||
231 | throw new Exception($msg); |
||
232 | } |
||
233 | $this->corpCache[$corpID] = $corporationDetails; |
||
234 | return $corporationDetails; |
||
235 | } |
||
236 | |||
237 | private function isMemberCorpOrAllianceContact($member) |
||
238 | { |
||
239 | $return = false; |
||
240 | $discordID = $member->id; |
||
241 | if (isset($this->dbusers[$discordID])) { |
||
242 | $role = $this->dbusers[$discordID]['role']; |
||
243 | if ($role === 'blue' || 'neut' || 'red') { |
||
244 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
245 | $corporationDetails = $this->getCorpDetails($character['corporation_id']); |
||
246 | $allianceContacts = getContacts($corporationDetails['alliance_id']); |
||
247 | $corpContacts = getContacts($character['corporation_id']); |
||
248 | if ($role === 'blue' && ((int)$allianceContacts['standing'] === 5 || 10 || (int)$corpContacts['standing'] === 5 || 10)) { |
||
249 | $return = true; |
||
250 | } |
||
251 | if ($role === 'red' && ((int)$allianceContacts['standing'] === -5 || -10 || (int)$corpContacts['standing'] === -5 || -10)) { |
||
252 | $return = true; |
||
253 | } |
||
254 | if ($role === 'neut' && ((int)$allianceContacts['standing'] === 0 || (int)$corpContacts['standing'] === 0 || (@(int)$allianceContacts['standings'] === null || '' && @(int)$corpContacts['standings'] === null || ''))) { |
||
255 | $return = true; |
||
256 | } |
||
257 | } |
||
258 | } |
||
259 | return $return; |
||
260 | } |
||
261 | |||
262 | private function deactivateMember($member) |
||
263 | { |
||
264 | $discordID = $member->id; |
||
265 | $discordNick = $member->nick ?: $member->user->username; |
||
266 | $this->logger->addInfo("AuthCheck: User [$discordNick] not found in database."); |
||
267 | $dbh = new PDO("mysql:host={$this->db};dbname={$this->dbName}", $this->dbUser, $this->dbPass); |
||
268 | $sql = "UPDATE authUsers SET active='no' WHERE discordID='$discordID'"; |
||
269 | $dbh->query($sql); |
||
270 | unset($this->dbusers[$member->id]); |
||
271 | $this->removeRoles($member); |
||
272 | } |
||
273 | |||
274 | private function removeRoles($member) |
||
275 | { |
||
276 | $discordNick = $member->nick ?: $member->user->username; |
||
277 | $roleRemoved = false; |
||
278 | $guild = $this->discord->guilds->get('id', $this->guildID); |
||
279 | if (!is_null($member->roles)) { |
||
280 | if (!isset($this->dbusers[$member->id])) { |
||
281 | foreach ($member->roles as $role) { |
||
282 | if (!in_array($role->name, $this->exempt, true)) { |
||
283 | $roleRemoved = true; |
||
284 | $member->removeRole($role); |
||
285 | $guild->members->save($member); |
||
286 | break; //temp to see if an issue lies in removing multiple roles from the same person too quickly |
||
287 | } |
||
288 | } |
||
289 | } |
||
290 | if ($roleRemoved) { |
||
291 | $this->logger->addInfo("AuthCheck: Roles removed from $discordNick"); |
||
292 | $msg = "Discord roles removed from $discordNick"; |
||
293 | queueMessage($msg, $this->alertChannel, $this->guildID); |
||
294 | } |
||
295 | } |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * @return null |
||
300 | */ |
||
301 | |||
302 | //Check user corp/alliance affiliation |
||
303 | //Remove members who have roles but never authed |
||
304 | private function resetMemberNick($member) |
||
305 | { |
||
306 | $discordID = $member->id; |
||
307 | $discordNick = $member->nick ?: $member->user->username; |
||
308 | if (!isset($this->dbusers[$discordID])) { |
||
309 | return false; |
||
310 | } |
||
311 | $character = $this->getCharacterDetails($this->dbusers[$discordID]['characterID']); |
||
312 | $corpTicker = ''; |
||
313 | if ($this->corpTickers === 'true') { |
||
314 | $corporationDetails = $this->getCorpDetails($character['corporation_id']); |
||
315 | if ($corporationDetails && isset($corporationDetails['ticker']) && $corporationDetails['ticker'] !== 'U') { |
||
316 | $corpTicker = "[{$corporationDetails['ticker']}] "; |
||
317 | } |
||
318 | } |
||
319 | if ($this->nameEnforce) { |
||
320 | $newNick = $corpTicker . $this->dbusers[$discordID]['eveName']; |
||
321 | } else { |
||
322 | $newNick = $corpTicker . $discordNick; |
||
323 | } |
||
324 | if ($newNick !== $discordNick) { |
||
325 | queueRename($discordID, $newNick, $this->guildID); |
||
326 | } |
||
327 | } |
||
328 | |||
329 | private function standingsUpdate() |
||
352 | } |
||
353 |
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.