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 Player 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 Player, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class Player extends AvatarModel implements NamedModel |
||
17 | { |
||
18 | /** |
||
19 | * These are built-in roles that cannot be deleted via the web interface so we will be storing these values as |
||
20 | * constant variables. Hopefully, a user won't be silly enough to delete them manually from the database. |
||
21 | * |
||
22 | * @TODO Deprecate these and use the Role constants |
||
23 | */ |
||
24 | const DEVELOPER = Role::DEVELOPER; |
||
25 | const ADMIN = Role::ADMINISTRATOR; |
||
26 | const COP = Role::COP; |
||
27 | const REFEREE = Role::REFEREE; |
||
28 | const S_ADMIN = Role::SYSADMIN; |
||
29 | const PLAYER = Role::PLAYER; |
||
30 | const PLAYER_NO_PM = Role::PLAYER_NO_PM; |
||
31 | |||
32 | /** |
||
33 | * The bzid of the player |
||
34 | * @var int |
||
35 | */ |
||
36 | protected $bzid; |
||
37 | |||
38 | /** |
||
39 | * The id of the player's team |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $team; |
||
43 | |||
44 | /** |
||
45 | * The player's status |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $status; |
||
49 | |||
50 | /** |
||
51 | * The player's e-mail address |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $email; |
||
55 | |||
56 | /** |
||
57 | * Whether the player has verified their e-mail address |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $verified; |
||
61 | |||
62 | /** |
||
63 | * What kind of events the player should be e-mailed about |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $receives; |
||
67 | |||
68 | /** |
||
69 | * A confirmation code for the player's e-mail address verification |
||
70 | * @var string |
||
71 | */ |
||
72 | protected $confirmCode; |
||
73 | |||
74 | /** |
||
75 | * Whether the callsign of the player is outdated |
||
76 | * @var bool |
||
77 | */ |
||
78 | protected $outdated; |
||
79 | |||
80 | /** |
||
81 | * The player's profile description |
||
82 | * @var string |
||
83 | */ |
||
84 | protected $description; |
||
85 | |||
86 | /** |
||
87 | * The id of the player's country |
||
88 | * @var int |
||
89 | */ |
||
90 | protected $country; |
||
91 | |||
92 | /** |
||
93 | * The player's timezone PHP identifier, e.g. "Europe/Paris" |
||
94 | * @var string |
||
95 | */ |
||
96 | protected $timezone; |
||
97 | |||
98 | /** |
||
99 | * The date the player joined the site |
||
100 | * @var TimeDate |
||
101 | */ |
||
102 | protected $joined; |
||
103 | |||
104 | /** |
||
105 | * The date of the player's last login |
||
106 | * @var TimeDate |
||
107 | */ |
||
108 | protected $last_login; |
||
109 | |||
110 | /** |
||
111 | * The roles a player belongs to |
||
112 | * @var Role[] |
||
113 | */ |
||
114 | protected $roles; |
||
115 | |||
116 | /** |
||
117 | * The permissions a player has |
||
118 | * @var Permission[] |
||
119 | */ |
||
120 | protected $permissions; |
||
121 | |||
122 | /** |
||
123 | * A section for admins to write notes about players |
||
124 | * @var string |
||
125 | */ |
||
126 | protected $admin_notes; |
||
127 | |||
128 | /** |
||
129 | * The ban of the player, or null if the player is not banned |
||
130 | * @var Ban|null |
||
131 | */ |
||
132 | protected $ban; |
||
133 | |||
134 | /** |
||
135 | * The name of the database table used for queries |
||
136 | */ |
||
137 | const TABLE = "players"; |
||
138 | |||
139 | /** |
||
140 | * The location where avatars will be stored |
||
141 | */ |
||
142 | const AVATAR_LOCATION = "/web/assets/imgs/avatars/players/"; |
||
143 | |||
144 | const EDIT_PERMISSION = Permission::EDIT_USER; |
||
145 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_USER; |
||
146 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_USER; |
||
147 | |||
148 | /** |
||
149 | * {@inheritdoc} |
||
150 | */ |
||
151 | 38 | protected function assignResult($player) |
|
161 | |||
162 | /** |
||
163 | * {@inheritdoc} |
||
164 | */ |
||
165 | 38 | protected function assignLazyResult($player) |
|
181 | |||
182 | /** |
||
183 | * Add a player a new role |
||
184 | * |
||
185 | * @param Role|int $role_id The role ID to add a player to |
||
186 | * |
||
187 | * @return bool Whether the operation was successful or not |
||
188 | */ |
||
189 | 38 | public function addRole($role_id) |
|
190 | { |
||
191 | 38 | if ($role_id instanceof Role) { |
|
192 | 1 | $role_id = $role_id->getId(); |
|
193 | 1 | } |
|
194 | |||
195 | 38 | $this->lazyLoad(); |
|
196 | |||
197 | // Make sure the player doesn't already have the role |
||
198 | 38 | foreach ($this->roles as $playerRole) { |
|
199 | 14 | if ($playerRole->getId() == $role_id) { |
|
200 | return false; |
||
201 | } |
||
202 | 38 | } |
|
203 | |||
204 | 38 | $status = $this->modifyRole($role_id, "add"); |
|
205 | 38 | $this->refresh(); |
|
206 | |||
207 | 38 | return $status; |
|
208 | } |
||
209 | |||
210 | /** |
||
211 | * Get the notes admins have left about a player |
||
212 | * @return string The notes |
||
213 | */ |
||
214 | public function getAdminNotes() |
||
220 | |||
221 | /** |
||
222 | * Get the player's BZID |
||
223 | * @return int The BZID |
||
224 | */ |
||
225 | public function getBZID() |
||
229 | |||
230 | /** |
||
231 | * Get the country a player belongs to |
||
232 | * |
||
233 | * @return Country The country belongs to |
||
234 | */ |
||
235 | public function getCountry() |
||
236 | { |
||
237 | return Country::get($this->country); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Get the e-mail address of the player |
||
242 | * |
||
243 | * @return string The address |
||
244 | */ |
||
245 | public function getEmailAddress() |
||
251 | |||
252 | /** |
||
253 | * Returns whether the player has verified their e-mail address |
||
254 | * |
||
255 | * @return bool `true` for verified players |
||
256 | */ |
||
257 | public function isVerified() |
||
263 | |||
264 | /** |
||
265 | * Returns the confirmation code for the player's e-mail address verification |
||
266 | * |
||
267 | * @return string The player's confirmation code |
||
268 | */ |
||
269 | public function getConfirmCode() |
||
275 | |||
276 | /** |
||
277 | * Returns what kind of events the player should be e-mailed about |
||
278 | * |
||
279 | * @return string The type of notifications |
||
280 | */ |
||
281 | public function getReceives() |
||
287 | |||
288 | /** |
||
289 | * Finds out whether the specified player wants and can receive an e-mail |
||
290 | * message |
||
291 | * |
||
292 | * @param string $type |
||
293 | * @return bool `true` if the player should be sent an e-mail |
||
294 | */ |
||
295 | public function canReceive($type) |
||
310 | |||
311 | /** |
||
312 | * Find out whether the specified confirmation code is correct |
||
313 | * |
||
314 | * This method protects against timing attacks |
||
315 | * |
||
316 | * @param string $code The confirmation code to check |
||
317 | * @return bool `true` for a correct e-mail verification code |
||
318 | */ |
||
319 | public function isCorrectConfirmCode($code) |
||
329 | |||
330 | /** |
||
331 | * Get the player's sanitized description |
||
332 | * @return string The description |
||
333 | */ |
||
334 | public function getDescription() |
||
340 | |||
341 | /** |
||
342 | * Get the joined date of the player |
||
343 | * @return TimeDate The joined date of the player |
||
344 | */ |
||
345 | public function getJoinedDate() |
||
351 | |||
352 | /** |
||
353 | * Get all of the known IPs used by the player |
||
354 | * |
||
355 | * @return string[][] An array containing IPs and hosts |
||
356 | */ |
||
357 | public function getKnownIPs() |
||
361 | |||
362 | /** |
||
363 | * Get the last login for a player |
||
364 | * @return TimeDate The date of the last login |
||
365 | */ |
||
366 | public function getLastLogin() |
||
372 | |||
373 | /** |
||
374 | * Get all of the callsigns a player has used to log in to the website |
||
375 | * @return string[] An array containing all of the past callsigns recorded for a player |
||
376 | */ |
||
377 | public function getPastCallsigns() |
||
381 | |||
382 | /** |
||
383 | * Get the player's team |
||
384 | * @return Team The object representing the team |
||
385 | */ |
||
386 | 1 | public function getTeam() |
|
390 | |||
391 | /** |
||
392 | * Get the player's timezone PHP identifier (example: "Europe/Paris") |
||
393 | * @return string The timezone |
||
394 | */ |
||
395 | public function getTimezone() |
||
401 | |||
402 | /** |
||
403 | * Get the roles of the player |
||
404 | * @return Role[] |
||
405 | */ |
||
406 | public function getRoles() |
||
412 | |||
413 | /** |
||
414 | * Rebuild the list of permissions a user has been granted |
||
415 | */ |
||
416 | 38 | private function updateUserPermissions() |
|
425 | |||
426 | /** |
||
427 | * Check if a player has a specific permission |
||
428 | * |
||
429 | * @param string|null $permission The permission to check for |
||
430 | * |
||
431 | * @return bool Whether or not the player has the permission |
||
432 | */ |
||
433 | 1 | public function hasPermission($permission) |
|
443 | |||
444 | /** |
||
445 | * Check whether the callsign of the player is outdated |
||
446 | * |
||
447 | * Returns true if this player has probably changed their callsign, making |
||
448 | * the current username stored in the database obsolete |
||
449 | * |
||
450 | * @return bool Whether or not the player is disabled |
||
451 | */ |
||
452 | public function isOutdated() |
||
458 | |||
459 | /** |
||
460 | * Check if a player's account has been disabled |
||
461 | * |
||
462 | * @return bool Whether or not the player is disabled |
||
463 | */ |
||
464 | public function isDisabled() |
||
468 | |||
469 | /** |
||
470 | * Check if everyone can log in as this user on a test environment |
||
471 | * |
||
472 | * @return bool |
||
473 | */ |
||
474 | public function isTestUser() |
||
478 | |||
479 | /** |
||
480 | * Check if a player is teamless |
||
481 | * |
||
482 | * @return bool True if the player is teamless |
||
483 | */ |
||
484 | 17 | public function isTeamless() |
|
488 | |||
489 | /** |
||
490 | * Mark a player's account as banned |
||
491 | */ |
||
492 | 1 | public function markAsBanned() |
|
500 | |||
501 | /** |
||
502 | * Mark a player's account as unbanned |
||
503 | */ |
||
504 | public function markAsUnbanned() |
||
512 | |||
513 | /** |
||
514 | * Find out if a player is banned |
||
515 | * |
||
516 | * @return bool |
||
517 | */ |
||
518 | 2 | public function isBanned() |
|
522 | |||
523 | /** |
||
524 | * Get the ban of the player |
||
525 | * |
||
526 | * This method performs a load of all the lazy parameters of the Player |
||
527 | * |
||
528 | * @return Ban|null The current ban of the player, or null if the player is |
||
529 | * is not banned |
||
530 | */ |
||
531 | public function getBan() |
||
537 | |||
538 | /** |
||
539 | * Remove a player from a role |
||
540 | * |
||
541 | * @param int $role_id The role ID to add or remove |
||
542 | * |
||
543 | * @return bool Whether the operation was successful or not |
||
544 | */ |
||
545 | public function removeRole($role_id) |
||
552 | |||
553 | /** |
||
554 | * Set the player's email address and reset their verification status |
||
555 | * @param string $email The address |
||
556 | */ |
||
557 | public function setEmailAddress($email) |
||
572 | |||
573 | /** |
||
574 | * Set whether the player has verified their e-mail address |
||
575 | * |
||
576 | * @param bool $verified Whether the player is verified or not |
||
577 | * @return self |
||
578 | */ |
||
579 | public function setVerified($verified) |
||
589 | |||
590 | /** |
||
591 | * Generate a new random confirmation token for e-mail address verification |
||
592 | * |
||
593 | * @return self |
||
594 | */ |
||
595 | public function generateNewConfirmCode() |
||
602 | |||
603 | /** |
||
604 | * Set the confirmation token for e-mail address verification |
||
605 | * |
||
606 | * @param string $code The confirmation code |
||
607 | * @return self |
||
608 | */ |
||
609 | private function setConfirmCode($code) |
||
615 | |||
616 | /** |
||
617 | * Set what kind of events the player should be e-mailed about |
||
618 | * |
||
619 | * @param string $receives The type of notification |
||
620 | * @return self |
||
621 | */ |
||
622 | public function setReceives($receives) |
||
628 | |||
629 | /** |
||
630 | * Set whether the callsign of the player is outdated |
||
631 | * |
||
632 | * @param bool $outdated Whether the callsign is outdated |
||
633 | * @return self |
||
634 | */ |
||
635 | 38 | public function setOutdated($outdated) |
|
641 | |||
642 | /** |
||
643 | * Set the player's description |
||
644 | * @param string $description The description |
||
645 | */ |
||
646 | public function setDescription($description) |
||
651 | |||
652 | /** |
||
653 | * Set the player's timezone |
||
654 | * @param string $timezone The timezone |
||
655 | */ |
||
656 | public function setTimezone($timezone) |
||
661 | |||
662 | /** |
||
663 | * Set the player's team |
||
664 | * @param int $team The team's ID |
||
665 | */ |
||
666 | 17 | public function setTeam($team) |
|
671 | |||
672 | /** |
||
673 | * Set the player's status |
||
674 | * @param string $status The new status |
||
675 | */ |
||
676 | public function setStatus($status) |
||
680 | |||
681 | /** |
||
682 | * Set the player's admin notes |
||
683 | * @param string $admin_notes The new admin notes |
||
684 | * @return self |
||
685 | */ |
||
686 | public function setAdminNotes($admin_notes) |
||
690 | |||
691 | /** |
||
692 | * Set the player's country |
||
693 | * @param int $country The ID of the new country |
||
694 | * @return self |
||
695 | */ |
||
696 | public function setCountry($country) |
||
700 | |||
701 | /** |
||
702 | * Updates this player's last login |
||
703 | */ |
||
704 | public function updateLastLogin() |
||
708 | |||
709 | /** |
||
710 | * Get the player's username |
||
711 | * @return string The username |
||
712 | */ |
||
713 | public function getUsername() |
||
717 | |||
718 | /** |
||
719 | * Get the player's username, safe for use in your HTML |
||
720 | * @return string The username |
||
721 | */ |
||
722 | public function getEscapedUsername() |
||
726 | |||
727 | /** |
||
728 | * Alias for Player::setUsername() |
||
729 | * |
||
730 | * @param string $username The new username |
||
731 | * @return self |
||
732 | */ |
||
733 | public function setName($username) |
||
737 | |||
738 | /** |
||
739 | * Mark all the unread messages of a player as read |
||
740 | * |
||
741 | * @return void |
||
742 | */ |
||
743 | public function markMessagesAsRead() |
||
750 | |||
751 | /** |
||
752 | * Set the roles of a user |
||
753 | * |
||
754 | * @todo Is it worth making this faster? |
||
755 | * @param Role[] $roles The new roles of the user |
||
756 | * @return self |
||
757 | */ |
||
758 | public function setRoles($roles) |
||
781 | |||
782 | /** |
||
783 | * Give or remove a role to/form a player |
||
784 | * |
||
785 | * @param int $role_id The role ID to add or remove |
||
786 | * @param string $action Whether to "add" or "remove" a role for a player |
||
787 | * |
||
788 | * @return bool Whether the operation was successful or not |
||
789 | */ |
||
790 | 38 | private function modifyRole($role_id, $action) |
|
808 | |||
809 | /** |
||
810 | * Given a player's BZID, get a player object |
||
811 | * |
||
812 | * @param int $bzid The player's BZID |
||
813 | * @return Player |
||
814 | */ |
||
815 | public static function getFromBZID($bzid) |
||
819 | |||
820 | /** |
||
821 | * Get a single player by their username |
||
822 | * |
||
823 | * @param string $username The username to look for |
||
824 | * @return Player |
||
825 | */ |
||
826 | public static function getFromUsername($username) |
||
832 | |||
833 | /** |
||
834 | * Get all the players in the database that have an active status |
||
835 | * @return Player[] An array of player BZIDs |
||
836 | */ |
||
837 | public static function getPlayers() |
||
843 | |||
844 | /** |
||
845 | * Show the number of notifications the user hasn't read yet |
||
846 | * @return int |
||
847 | */ |
||
848 | public function countUnreadNotifications() |
||
852 | |||
853 | /** |
||
854 | * Show the number of messages the user hasn't read yet |
||
855 | * @return int |
||
856 | */ |
||
857 | public function countUnreadMessages() |
||
863 | |||
864 | /** |
||
865 | * Get all of the members belonging to a team |
||
866 | * @param int $teamID The ID of the team to fetch the members of |
||
867 | * @return Player[] An array of Player objects of the team members |
||
868 | */ |
||
869 | 1 | public static function getTeamMembers($teamID) |
|
875 | |||
876 | /** |
||
877 | * {@inheritdoc} |
||
878 | */ |
||
879 | public static function getActiveStatuses() |
||
883 | |||
884 | /** |
||
885 | * {@inheritdoc} |
||
886 | */ |
||
887 | 38 | public static function getEagerColumns() |
|
891 | |||
892 | /** |
||
893 | * {@inheritdoc} |
||
894 | */ |
||
895 | 38 | public static function getLazyColumns() |
|
899 | |||
900 | /** |
||
901 | * Get a query builder for players |
||
902 | * @return QueryBuilder |
||
903 | */ |
||
904 | View Code Duplication | public static function getQueryBuilder() |
|
916 | |||
917 | /** |
||
918 | * Enter a new player to the database |
||
919 | * @param int $bzid The player's bzid |
||
920 | * @param string $username The player's username |
||
921 | * @param int $team The player's team |
||
922 | * @param string $status The player's status |
||
923 | * @param int $role_id The player's role when they are first created |
||
924 | * @param string $avatar The player's profile avatar |
||
925 | * @param string $description The player's profile description |
||
926 | * @param int $country The player's country |
||
927 | * @param string $timezone The player's timezone |
||
928 | * @param string|\TimeDate $joined The date the player joined |
||
929 | * @param string|\TimeDate $last_login The timestamp of the player's last login |
||
930 | * @return Player An object representing the player that was just entered |
||
931 | */ |
||
932 | 38 | public static function newPlayer($bzid, $username, $team = null, $status = "active", $role_id = self::PLAYER, $avatar = "", $description = "", $country = 1, $timezone = null, $joined = "now", $last_login = "now") |
|
958 | |||
959 | /** |
||
960 | * Determine if a player exists in the database |
||
961 | * @param int $bzid The player's bzid |
||
962 | * @return bool Whether the player exists in the database |
||
963 | */ |
||
964 | public static function playerBZIDExists($bzid) |
||
968 | |||
969 | /** |
||
970 | * Change a player's callsign and add it to the database if it does not |
||
971 | * exist as a past callsign |
||
972 | * |
||
973 | * @param string $username The new username of the player |
||
974 | * @return self |
||
975 | */ |
||
976 | 38 | public function setUsername($username) |
|
1007 | |||
1008 | /** |
||
1009 | * Alphabetical order function for use in usort (case-insensitive) |
||
1010 | * @return Closure The sort function |
||
1011 | */ |
||
1012 | public static function getAlphabeticalSort() |
||
1018 | |||
1019 | /** |
||
1020 | * {@inheritdoc} |
||
1021 | * @todo Add a constraint that does this automatically |
||
1022 | */ |
||
1023 | 38 | public function wipe() |
|
1029 | |||
1030 | /** |
||
1031 | * Find whether the player can delete a model |
||
1032 | * |
||
1033 | * @param PermissionModel $model The model that will be seen |
||
1034 | * @param bool $showDeleted Whether to show deleted models to admins |
||
1035 | * @return bool |
||
1036 | */ |
||
1037 | public function canSee($model, $showDeleted = false) |
||
1041 | |||
1042 | /** |
||
1043 | * Find whether the player can delete a model |
||
1044 | * |
||
1045 | * @param PermissionModel $model The model that will be deleted |
||
1046 | * @param bool $hard Whether to check for hard-delete perms, as opposed |
||
1047 | * to soft-delete ones |
||
1048 | * @return bool |
||
1049 | */ |
||
1050 | public function canDelete($model, $hard = false) |
||
1058 | |||
1059 | /** |
||
1060 | * Find whether the player can create a model |
||
1061 | * |
||
1062 | * @param string $modelName The PHP class identifier of the model type |
||
1063 | * @return bool |
||
1064 | */ |
||
1065 | public function canCreate($modelName) |
||
1069 | |||
1070 | /** |
||
1071 | * Find whether the player can edit a model |
||
1072 | * |
||
1073 | * @param PermissionModel $model The model which will be edited |
||
1074 | * @return bool |
||
1075 | */ |
||
1076 | public function canEdit($model) |
||
1080 | } |
||
1081 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..