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 $player; |
||
| 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 | 39 | protected function assignResult($player) |
|
| 152 | { |
||
| 153 | 39 | $this->bzid = $player['bzid']; |
|
| 154 | 39 | $this->name = $player['username']; |
|
| 155 | 39 | $this->alias = $player['alias']; |
|
| 156 | 39 | $this->team = $player['team']; |
|
| 157 | 39 | $this->status = $player['status']; |
|
| 158 | 39 | $this->avatar = $player['avatar']; |
|
| 159 | 39 | $this->country = $player['country']; |
|
| 160 | 39 | } |
|
| 161 | |||
| 162 | /** |
||
| 163 | * {@inheritdoc} |
||
| 164 | */ |
||
| 165 | 39 | protected function assignLazyResult($player) |
|
| 166 | { |
||
| 167 | 39 | $this->email = $player['email']; |
|
| 168 | 39 | $this->verified = $player['verified']; |
|
| 169 | 39 | $this->receives = $player['receives']; |
|
| 170 | 39 | $this->confirmCode = $player['confirm_code']; |
|
| 171 | 39 | $this->outdated = $player['outdated']; |
|
| 172 | 39 | $this->description = $player['description']; |
|
| 173 | 39 | $this->timezone = $player['timezone']; |
|
| 174 | 39 | $this->joined = TimeDate::fromMysql($player['joined']); |
|
| 175 | 39 | $this->last_login = TimeDate::fromMysql($player['last_login']); |
|
| 176 | 39 | $this->admin_notes = $player['admin_notes']; |
|
| 177 | 39 | $this->ban = Ban::getBan($this->id); |
|
|
|
|||
| 178 | |||
| 179 | 39 | $this->updateUserPermissions(); |
|
| 180 | 39 | } |
|
| 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 | 39 | public function addRole($role_id) |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Get the notes admins have left about a player |
||
| 212 | * @return string The notes |
||
| 213 | */ |
||
| 214 | public function getAdminNotes() |
||
| 215 | { |
||
| 216 | $this->lazyLoad(); |
||
| 217 | |||
| 218 | return $this->admin_notes; |
||
| 219 | } |
||
| 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 | 1 | public function getCountry() |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Get the e-mail address of the player |
||
| 242 | * |
||
| 243 | * @return string The address |
||
| 244 | */ |
||
| 245 | public function getEmailAddress() |
||
| 246 | { |
||
| 247 | $this->lazyLoad(); |
||
| 248 | |||
| 249 | return $this->email; |
||
| 250 | } |
||
| 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() |
||
| 258 | { |
||
| 259 | $this->lazyLoad(); |
||
| 260 | |||
| 261 | return $this->verified; |
||
| 262 | } |
||
| 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() |
||
| 270 | { |
||
| 271 | $this->lazyLoad(); |
||
| 272 | |||
| 273 | return $this->confirmCode; |
||
| 274 | } |
||
| 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() |
||
| 282 | { |
||
| 283 | $this->lazyLoad(); |
||
| 284 | |||
| 285 | return $this->receives; |
||
| 286 | } |
||
| 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 | 1 | public function canReceive($type) |
|
| 296 | { |
||
| 297 | 1 | $this->lazyLoad(); |
|
| 298 | |||
| 299 | 1 | if (!$this->email || !$this->isVerified()) { |
|
| 300 | // Unverified e-mail means the user will receive nothing |
||
| 301 | 1 | return false; |
|
| 302 | } |
||
| 303 | |||
| 304 | if ($this->receives == 'everything') { |
||
| 305 | return true; |
||
| 306 | } |
||
| 307 | |||
| 308 | return $this->receives == $type; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Find out whether the specified confirmation code is correct |
||
| 313 | * |
||
| 314 | * This method protects against timing attacks |
||
| 315 | * |
||
| 316 | * @return bool `true` for a correct e-mail verification code |
||
| 317 | */ |
||
| 318 | public function isCorrectConfirmCode($code) |
||
| 319 | { |
||
| 320 | $this->lazyLoad(); |
||
| 321 | |||
| 322 | if ($this->confirmCode === null) { |
||
| 323 | return false; |
||
| 324 | } |
||
| 325 | |||
| 326 | return StringUtils::equals($code, $this->confirmCode); |
||
| 327 | } |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get the player's sanitized description |
||
| 331 | * @return string The description |
||
| 332 | */ |
||
| 333 | public function getDescription() |
||
| 334 | { |
||
| 335 | $this->lazyLoad(); |
||
| 336 | |||
| 337 | return $this->description; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the joined date of the player |
||
| 342 | * @return TimeDate The joined date of the player |
||
| 343 | */ |
||
| 344 | public function getJoinedDate() |
||
| 345 | { |
||
| 346 | $this->lazyLoad(); |
||
| 347 | |||
| 348 | return $this->joined; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Get all of the known IPs used by the player |
||
| 353 | * |
||
| 354 | * @return string[][] An array containing IPs and hosts |
||
| 355 | */ |
||
| 356 | public function getKnownIPs() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the last login for a player |
||
| 363 | * @return TimeDate The date of the last login |
||
| 364 | */ |
||
| 365 | public function getLastLogin() |
||
| 366 | { |
||
| 367 | $this->lazyLoad(); |
||
| 368 | |||
| 369 | return $this->last_login; |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Get all of the callsigns a player has used to log in to the website |
||
| 374 | * @return string[] An array containing all of the past callsigns recorded for a player |
||
| 375 | */ |
||
| 376 | public function getPastCallsigns() |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Get the player's team |
||
| 383 | * @return Team The object representing the team |
||
| 384 | */ |
||
| 385 | 2 | public function getTeam() |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Get the player's timezone PHP identifier (example: "Europe/Paris") |
||
| 392 | * @return string The timezone |
||
| 393 | */ |
||
| 394 | 1 | public function getTimezone() |
|
| 395 | { |
||
| 396 | 1 | $this->lazyLoad(); |
|
| 397 | |||
| 398 | 1 | return ($this->timezone) ?: date_default_timezone_get(); |
|
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Rebuild the list of permissions a user has been granted |
||
| 403 | */ |
||
| 404 | 39 | private function updateUserPermissions() |
|
| 405 | { |
||
| 406 | 39 | $this->roles = Role::getRoles($this->id); |
|
| 407 | 39 | $this->permissions = array(); |
|
| 408 | |||
| 409 | 39 | foreach ($this->roles as $role) { |
|
| 410 | 39 | $this->permissions = array_merge($this->permissions, $role->getPerms()); |
|
| 411 | } |
||
| 412 | 39 | } |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Check if a player has a specific permission |
||
| 416 | * |
||
| 417 | * @param string|null $permission The permission to check for |
||
| 418 | * |
||
| 419 | * @return bool Whether or not the player has the permission |
||
| 420 | */ |
||
| 421 | 2 | public function hasPermission($permission) |
|
| 422 | { |
||
| 423 | 2 | if ($permission === null) { |
|
| 424 | 1 | return false; |
|
| 425 | } |
||
| 426 | |||
| 427 | 2 | $this->lazyLoad(); |
|
| 428 | |||
| 429 | 2 | return isset($this->permissions[$permission]); |
|
| 430 | } |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Check whether the callsign of the player is outdated |
||
| 434 | * |
||
| 435 | * Returns true if this player has probably changed their callsign, making |
||
| 436 | * the current username stored in the database obsolete |
||
| 437 | * |
||
| 438 | * @return bool Whether or not the player is disabled |
||
| 439 | */ |
||
| 440 | public function isOutdated() |
||
| 441 | { |
||
| 442 | $this->lazyLoad(); |
||
| 443 | |||
| 444 | return $this->outdated; |
||
| 445 | } |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Check if a player's account has been disabled |
||
| 449 | * |
||
| 450 | * @return bool Whether or not the player is disabled |
||
| 451 | */ |
||
| 452 | public function isDisabled() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Check if everyone can log in as this user on a test environment |
||
| 459 | * |
||
| 460 | * @return bool |
||
| 461 | */ |
||
| 462 | 1 | public function isTestUser() |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Check if a player is teamless |
||
| 469 | * |
||
| 470 | * @return bool True if the player is teamless |
||
| 471 | */ |
||
| 472 | 18 | public function isTeamless() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Mark a player's account as banned |
||
| 479 | */ |
||
| 480 | 1 | public function markAsBanned() |
|
| 481 | { |
||
| 482 | 1 | if ($this->status != 'active') { |
|
| 483 | return $this; |
||
| 484 | } |
||
| 485 | |||
| 486 | 1 | return $this->updateProperty($this->status, "status", "banned", 's'); |
|
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Mark a player's account as unbanned |
||
| 491 | */ |
||
| 492 | public function markAsUnbanned() |
||
| 493 | { |
||
| 494 | if ($this->status != 'banned') { |
||
| 495 | return $this; |
||
| 496 | } |
||
| 497 | |||
| 498 | return $this->updateProperty($this->status, "status", "active", 's'); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Find out if a player is banned |
||
| 503 | * |
||
| 504 | * @return bool |
||
| 505 | */ |
||
| 506 | 2 | public function isBanned() |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get the ban of the player |
||
| 513 | * |
||
| 514 | * This method performs a load of all the lazy parameters of the Player |
||
| 515 | * |
||
| 516 | * @return Ban|null The current ban of the player, or null if the player is |
||
| 517 | * is not banned |
||
| 518 | */ |
||
| 519 | public function getBan() |
||
| 520 | { |
||
| 521 | $this->lazyLoad(); |
||
| 522 | |||
| 523 | return $this->ban; |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * Remove a player from a role |
||
| 528 | * |
||
| 529 | * @param int $role_id The role ID to add or remove |
||
| 530 | * |
||
| 531 | * @return bool Whether the operation was successful or not |
||
| 532 | */ |
||
| 533 | public function removeRole($role_id) |
||
| 534 | { |
||
| 535 | $status = $this->modifyRole($role_id, "remove"); |
||
| 536 | $this->refresh(); |
||
| 537 | |||
| 538 | return $status; |
||
| 539 | } |
||
| 540 | |||
| 541 | /** |
||
| 542 | * Set the player's email address and reset their verification status |
||
| 543 | * @param string $email The address |
||
| 544 | */ |
||
| 545 | public function setEmailAddress($email) |
||
| 546 | { |
||
| 547 | $this->lazyLoad(); |
||
| 548 | |||
| 549 | if ($this->email == $email) { |
||
| 550 | // The e-mail hasn't changed, don't do anything |
||
| 551 | return; |
||
| 552 | } |
||
| 553 | |||
| 554 | $this->setVerified(false); |
||
| 555 | $this->generateNewConfirmCode(); |
||
| 556 | |||
| 557 | $this->email = $email; |
||
| 558 | $this->update("email", $email, 's'); |
||
| 559 | } |
||
| 560 | |||
| 561 | /** |
||
| 562 | * Set whether the player has verified their e-mail address |
||
| 563 | * |
||
| 564 | * @param bool $verified Whether the player is verified or not |
||
| 565 | * @return self |
||
| 566 | */ |
||
| 567 | public function setVerified($verified) |
||
| 568 | { |
||
| 569 | $this->lazyLoad(); |
||
| 570 | |||
| 571 | if ($verified) { |
||
| 572 | $this->setConfirmCode(null); |
||
| 573 | } |
||
| 574 | |||
| 575 | return $this->updateProperty($this->verified, 'verified', $verified, 'i'); |
||
| 576 | } |
||
| 577 | |||
| 578 | /** |
||
| 579 | * Generate a new random confirmation token for e-mail address verification |
||
| 580 | * |
||
| 581 | * @return self |
||
| 582 | */ |
||
| 583 | public function generateNewConfirmCode() |
||
| 584 | { |
||
| 585 | $generator = new SecureRandom(); |
||
| 586 | $random = $generator->nextBytes(16); |
||
| 587 | |||
| 588 | return $this->setConfirmCode(bin2hex($random)); |
||
| 589 | } |
||
| 590 | |||
| 591 | /** |
||
| 592 | * Set the confirmation token for e-mail address verification |
||
| 593 | * |
||
| 594 | * @param string $code The confirmation code |
||
| 595 | * @return self |
||
| 596 | */ |
||
| 597 | private function setConfirmCode($code) |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Set what kind of events the player should be e-mailed about |
||
| 604 | * |
||
| 605 | * @param string $receives The type of notification |
||
| 606 | * @return self |
||
| 607 | */ |
||
| 608 | public function setReceives($receives) |
||
| 612 | |||
| 613 | /** |
||
| 614 | * Set whether the callsign of the player is outdated |
||
| 615 | * |
||
| 616 | * @param bool $outdated Whether the callsign is outdated |
||
| 617 | * @return self |
||
| 618 | */ |
||
| 619 | public function setOutdated($outdated) |
||
| 623 | |||
| 624 | /** |
||
| 625 | * Set the player's description |
||
| 626 | * @param string $description The description |
||
| 627 | */ |
||
| 628 | public function setDescription($description) |
||
| 629 | { |
||
| 630 | $this->description = $description; |
||
| 631 | $this->update("description", $description, 's'); |
||
| 632 | } |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Set the player's timezone |
||
| 636 | * @param string $timezone The timezone |
||
| 637 | */ |
||
| 638 | public function setTimezone($timezone) |
||
| 639 | { |
||
| 640 | $this->timezone = $timezone; |
||
| 641 | $this->update("timezone", $timezone, 's'); |
||
| 642 | } |
||
| 643 | |||
| 644 | /** |
||
| 645 | * Set the player's team |
||
| 646 | * @param int $team The team's ID |
||
| 647 | */ |
||
| 648 | 18 | public function setTeam($team) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Set the player's status |
||
| 656 | * @param string $status The new status |
||
| 657 | */ |
||
| 658 | public function setStatus($status) |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Set the player's admin notes |
||
| 665 | * @param string $admin_notes The new admin notes |
||
| 666 | * @return self |
||
| 667 | */ |
||
| 668 | public function setAdminNotes($admin_notes) |
||
| 672 | |||
| 673 | /** |
||
| 674 | * Set the player's country |
||
| 675 | * @param int $country The ID of the new country |
||
| 676 | * @return self |
||
| 677 | */ |
||
| 678 | public function setCountry($country) |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Updates this player's last login |
||
| 685 | */ |
||
| 686 | 1 | public function updateLastLogin() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Get the player's username |
||
| 693 | * @return string The username |
||
| 694 | */ |
||
| 695 | 1 | public function getUsername() |
|
| 699 | |||
| 700 | /** |
||
| 701 | * Get the player's username, safe for use in your HTML |
||
| 702 | * @return string The username |
||
| 703 | */ |
||
| 704 | public function getEscapedUsername() |
||
| 708 | |||
| 709 | /** |
||
| 710 | * Alias for Player::setUsername() |
||
| 711 | * |
||
| 712 | * @param string $username The new username |
||
| 713 | * @return self |
||
| 714 | */ |
||
| 715 | public function setName($username) |
||
| 719 | |||
| 720 | /** |
||
| 721 | * Mark all the unread messages of a player as read |
||
| 722 | * |
||
| 723 | * @return void |
||
| 724 | */ |
||
| 725 | public function markMessagesAsRead() |
||
| 732 | |||
| 733 | /** |
||
| 734 | * Set the roles of a user |
||
| 735 | * |
||
| 736 | * @todo Is it worth making this faster? |
||
| 737 | * @param Role[] $roles The new roles of the user |
||
| 738 | * @return self |
||
| 739 | */ |
||
| 740 | public function setRoles($roles) |
||
| 763 | 39 | ||
| 764 | /** |
||
| 765 | 39 | * Give or remove a role to/form a player |
|
| 766 | * |
||
| 767 | 39 | * @param int $role_id The role ID to add or remove |
|
| 768 | 39 | * @param string $action Whether to "add" or "remove" a role for a player |
|
| 769 | 39 | * |
|
| 770 | * @return bool Whether the operation was successful or not |
||
| 771 | */ |
||
| 772 | private function modifyRole($role_id, $action) |
||
| 790 | 1 | ||
| 791 | /** |
||
| 792 | * Given a player's BZID, get a player object |
||
| 793 | * |
||
| 794 | * @param int $bzid The player's BZID |
||
| 795 | * @return Player |
||
| 796 | */ |
||
| 797 | public static function getFromBZID($bzid) |
||
| 801 | 1 | ||
| 802 | /** |
||
| 803 | 1 | * Get a single player by their username |
|
| 804 | * |
||
| 805 | * @param string $username The username to look for |
||
| 806 | * @return Player |
||
| 807 | */ |
||
| 808 | public static function getFromUsername($username) |
||
| 814 | |||
| 815 | /** |
||
| 816 | * Get all the players in the database that have an active status |
||
| 817 | * @return Player[] An array of player BZIDs |
||
| 818 | */ |
||
| 819 | public static function getPlayers() |
||
| 825 | |||
| 826 | /** |
||
| 827 | * Show the number of notifications the user hasn't read yet |
||
| 828 | * @return int |
||
| 829 | */ |
||
| 830 | 1 | public function countUnreadNotifications() |
|
| 834 | |||
| 835 | /** |
||
| 836 | * Show the number of messages the user hasn't read yet |
||
| 837 | * @return int |
||
| 838 | */ |
||
| 839 | public function countUnreadMessages() |
||
| 845 | 2 | ||
| 846 | /** |
||
| 847 | * Get all of the members belonging to a team |
||
| 848 | * @param int $teamID The ID of the team to fetch the members of |
||
| 849 | * @return Player[] An array of Player objects of the team members |
||
| 850 | */ |
||
| 851 | public static function getTeamMembers($teamID) |
||
| 857 | |||
| 858 | /** |
||
| 859 | * {@inheritdoc} |
||
| 860 | 39 | */ |
|
| 861 | public static function getActiveStatuses() |
||
| 865 | |||
| 866 | /** |
||
| 867 | * {@inheritdoc} |
||
| 868 | 39 | */ |
|
| 869 | public static function getEagerColumns() |
||
| 873 | |||
| 874 | /** |
||
| 875 | * {@inheritdoc} |
||
| 876 | */ |
||
| 877 | public static function getLazyColumns() |
||
| 881 | |||
| 882 | /** |
||
| 883 | * Get a query builder for players |
||
| 884 | * @return QueryBuilder |
||
| 885 | */ |
||
| 886 | View Code Duplication | public static function getQueryBuilder() |
|
| 898 | |||
| 899 | /** |
||
| 900 | * Enter a new player to the database |
||
| 901 | * @param int $bzid The player's bzid |
||
| 902 | * @param string $username The player's username |
||
| 903 | * @param int $team The player's team |
||
| 904 | * @param string $status The player's status |
||
| 905 | 39 | * @param int $role_id The player's role when they are first created |
|
| 906 | * @param string $avatar The player's profile avatar |
||
| 907 | 39 | * @param string $description The player's profile description |
|
| 908 | 39 | * @param int $country The player's country |
|
| 909 | 39 | * @param string $timezone The player's timezone |
|
| 910 | * @param string|\TimeDate $joined The date the player joined |
||
| 911 | 39 | * @param string|\TimeDate $last_login The timestamp of the player's last login |
|
| 912 | 39 | * @return Player An object representing the player that was just entered |
|
| 913 | 39 | */ |
|
| 914 | 39 | 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") |
|
| 940 | |||
| 941 | /** |
||
| 942 | * Determine if a player exists in the database |
||
| 943 | * @param int $bzid The player's bzid |
||
| 944 | * @return bool Whether the player exists in the database |
||
| 945 | */ |
||
| 946 | public static function playerBZIDExists($bzid) |
||
| 950 | |||
| 951 | /** |
||
| 952 | * Change a player's callsign and add it to the database if it does not |
||
| 953 | * exist as a past callsign |
||
| 954 | * |
||
| 955 | * @param string $username The new username of the player |
||
| 956 | * @return self |
||
| 957 | */ |
||
| 958 | public function setUsername($username) |
||
| 989 | 1 | ||
| 990 | /** |
||
| 991 | * Alphabetical order function for use in usort (case-insensitive) |
||
| 992 | * @return Closure The sort function |
||
| 993 | */ |
||
| 994 | public static function getAlphabeticalSort() |
||
| 1000 | |||
| 1001 | 1 | /** |
|
| 1002 | * Find whether the player can delete a model |
||
| 1003 | * |
||
| 1004 | * @param PermissionModel $model The model that will be seen |
||
| 1005 | * @param bool $showDeleted Whether to show deleted models to admins |
||
| 1006 | * @return bool |
||
| 1007 | */ |
||
| 1008 | public function canSee($model, $showDeleted = false) |
||
| 1012 | 1 | ||
| 1013 | /** |
||
| 1014 | 1 | * Find whether the player can delete a model |
|
| 1015 | * |
||
| 1016 | * @param PermissionModel $model The model that will be deleted |
||
| 1017 | 1 | * @param bool $hard Whether to check for hard-delete perms, as opposed |
|
| 1018 | * to soft-delete ones |
||
| 1019 | * @return bool |
||
| 1020 | */ |
||
| 1021 | public function canDelete($model, $hard = false) |
||
| 1029 | 1 | ||
| 1030 | /** |
||
| 1031 | * Find whether the player can create a model |
||
| 1032 | * |
||
| 1033 | * @param string $modelName The PHP class identifier of the model type |
||
| 1034 | * @return bool |
||
| 1035 | */ |
||
| 1036 | public function canCreate($modelName) |
||
| 1040 | 1 | ||
| 1041 | /** |
||
| 1042 | * Find whether the player can edit a model |
||
| 1043 | * |
||
| 1044 | * @param PermissionModel $model The model which will be edited |
||
| 1045 | * @return bool |
||
| 1046 | */ |
||
| 1047 | public function canEdit($model) |
||
| 1051 | } |
||
| 1052 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: