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 Ban 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 Ban, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class Ban extends UrlModel implements NamedModel |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The id of the banned player |
||
| 17 | * @var int |
||
| 18 | */ |
||
| 19 | protected $player; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * The ban expiration date |
||
| 23 | * @var TimeDate |
||
| 24 | */ |
||
| 25 | protected $expiration; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The message that will appear when a player is denied connecting to a game server |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $srvmsg; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The ban reason |
||
| 35 | * @var string |
||
| 36 | */ |
||
| 37 | protected $reason; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Whether or not a player is allowed to join a server when they are banned |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $allowServerJoin; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * The ban creation date |
||
| 47 | * @var TimeDate |
||
| 48 | */ |
||
| 49 | protected $created; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The date the ban was last updated |
||
| 53 | * @var TimeDate |
||
| 54 | */ |
||
| 55 | protected $updated; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The id of the ban author |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $author; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The IP of the banned player if the league would like to implement a global ban list |
||
| 65 | * @var string[] |
||
| 66 | */ |
||
| 67 | protected $ipAddresses; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * The ban's status |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | protected $status; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The name of the database table used for queries |
||
| 77 | */ |
||
| 78 | const TABLE = "bans"; |
||
| 79 | |||
| 80 | const CREATE_PERMISSION = Permission::ADD_BAN; |
||
| 81 | const EDIT_PERMISSION = Permission::EDIT_BAN; |
||
| 82 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_BAN; |
||
| 83 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_BAN; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * {@inheritdoc} |
||
| 87 | */ |
||
| 88 | protected function assignResult($ban) |
||
| 89 | { |
||
| 90 | $this->player = $ban['player']; |
||
| 91 | $this->expiration = ($ban['expiration'] === null) |
||
| 92 | ? null |
||
| 93 | : TimeDate::fromMysql($ban['expiration']); |
||
| 94 | $this->srvmsg = $ban['server_message']; |
||
| 95 | $this->reason = $ban['reason']; |
||
| 96 | $this->allowServerJoin = $ban['allow_server_join']; |
||
| 97 | $this->created = TimeDate::fromMysql($ban['created']); |
||
| 98 | $this->updated = TimeDate::fromMysql($ban['updated']); |
||
| 99 | $this->author = $ban['author']; |
||
| 100 | $this->status = $ban['status']; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * {@inheritdoc} |
||
| 105 | */ |
||
| 106 | protected function assignLazyResult($result) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Add an IP to the ban |
||
| 113 | * |
||
| 114 | * @param string $ipAddress The IP to add to a ban |
||
| 115 | */ |
||
| 116 | View Code Duplication | public function addIP($ipAddress) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Remove an IP from the ban |
||
| 126 | * |
||
| 127 | * @param string $ipAddress The IP to remove from the ban |
||
| 128 | */ |
||
| 129 | View Code Duplication | public function removeIP($ipAddress) |
|
| 137 | |||
| 138 | /** |
||
| 139 | * Set the IP addresses of the ban |
||
| 140 | * |
||
| 141 | * @todo Is it worth making this faster? |
||
| 142 | * @param string[] $ipAddresses The new IP addresses of the ban |
||
| 143 | * @return self |
||
| 144 | */ |
||
| 145 | public function setIPs($ipAddresses) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Check whether or not a player is allowed to join a server when they've been banned |
||
| 168 | * @return bool Whether or not a player is allowed to join |
||
| 169 | */ |
||
| 170 | public function allowedServerJoin() |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the user who imposed the ban |
||
| 177 | * @return Player The banner |
||
| 178 | */ |
||
| 179 | public function getAuthor() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get the creation time of the ban |
||
| 186 | * @return TimeDate The creation time |
||
| 187 | */ |
||
| 188 | public function getCreated() |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Get the expiration time of the ban |
||
| 195 | * @return TimeDate |
||
| 196 | */ |
||
| 197 | public function getExpiration() |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Get the ban's description |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getReason() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Get the ban summary that will appear when a player is denied access to a league server on join |
||
| 213 | * @return string The ban summary |
||
| 214 | */ |
||
| 215 | public function getServerMessage() |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the IP address of the banned player |
||
| 226 | * @return string[] |
||
| 227 | */ |
||
| 228 | public function getIpAddresses() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Get the time when the ban was last updated |
||
| 237 | * @return TimeDate |
||
| 238 | */ |
||
| 239 | public function getUpdated() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Get the player who was banned |
||
| 246 | * @return Player The banned player |
||
| 247 | */ |
||
| 248 | public function getVictim() |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Get the ID of the player who was banned |
||
| 255 | * @return int The ID of the victim of the ban |
||
| 256 | */ |
||
| 257 | public function getVictimID() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Calculate whether a ban has expired or not. |
||
| 264 | * |
||
| 265 | * @return bool True if the ban's expiration time has already passed |
||
| 266 | */ |
||
| 267 | public function isExpired() |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Check whether the ban will expire automatically |
||
| 278 | * |
||
| 279 | * @return bool |
||
| 280 | */ |
||
| 281 | public function willExpire() |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Mark the ban as expired |
||
| 288 | * |
||
| 289 | * @return self |
||
| 290 | */ |
||
| 291 | public function expire() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set the expiration date of the ban |
||
| 301 | * @param TimeDate $expiration The expiration |
||
| 302 | * @return self |
||
| 303 | */ |
||
| 304 | public function setExpiration($expiration) |
||
| 305 | { |
||
| 306 | if ($expiration !== null) { |
||
| 307 | $expiration = TimeDate::from($expiration); |
||
| 308 | } |
||
| 309 | |||
| 310 | return $this->updateProperty($this->expiration, 'expiration', $expiration); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Set the server message of the ban |
||
| 315 | * @param string $message The new server message |
||
| 316 | * @return self |
||
| 317 | */ |
||
| 318 | public function setServerMessage($message) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set the reason of the ban |
||
| 325 | * @param string $reason The new ban reason |
||
| 326 | * @return self |
||
| 327 | */ |
||
| 328 | public function setReason($reason) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Update the last edit timestamp |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | public function updateEditTimestamp() |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Set whether the ban's victim is allowed to enter a match server |
||
| 344 | * @param bool $allowServerJoin |
||
| 345 | * @return self |
||
| 346 | */ |
||
| 347 | public function setAllowServerJoin($allowServerJoin) |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Add a new ban |
||
| 354 | * |
||
| 355 | * @param int $playerID The ID of the victim of the ban |
||
| 356 | * @param int $authorID The ID of the player responsible for the ban |
||
| 357 | * @param mixed|null $expiration The expiration of the ban (set to NULL so that it never expires) |
||
| 358 | * @param string $reason The full reason for the ban |
||
| 359 | * @param string $srvmsg A summary of the ban to be displayed on server banlists (max 150 characters) |
||
| 360 | * @param string[] $ipAddresses An array of IPs that have been banned |
||
| 361 | * @param bool $allowServerJoin Whether or not the player is allowed to join match servers |
||
| 362 | * |
||
| 363 | * @return Ban An object representing the ban that was just entered or false if the ban was not created |
||
| 364 | */ |
||
| 365 | public static function addBan($playerID, $authorID, $expiration, $reason, $srvmsg = "", $ipAddresses = array(), $allowServerJoin = false) |
||
| 366 | { |
||
| 367 | $player = Player::get($playerID); |
||
| 368 | |||
| 369 | if ($expiration !== null) { |
||
| 370 | $expiration = TimeDate::from($expiration)->toMysql(); |
||
| 371 | } else { |
||
| 372 | $player->markAsBanned(); |
||
| 373 | } |
||
| 374 | |||
| 375 | // If there are no IPs to banned or no server ban message, then we'll allow the players to join as observers |
||
| 376 | if (empty($srvmsg) || empty($ipAddresses)) { |
||
| 377 | $allowServerJoin = true; |
||
| 378 | } |
||
| 379 | |||
| 380 | $ban = self::create(array( |
||
| 381 | 'player' => $playerID, |
||
| 382 | 'expiration' => $expiration, |
||
| 383 | 'server_message' => $srvmsg, |
||
| 384 | 'reason' => $reason, |
||
| 385 | 'allow_server_join' => $allowServerJoin, |
||
| 386 | 'author' => $authorID, |
||
| 387 | ), array('created', 'updated')); |
||
| 388 | |||
| 389 | if (is_array($ipAddresses)) { |
||
| 390 | foreach ($ipAddresses as $ip) { |
||
| 391 | $ban->addIP($ip); |
||
| 392 | } |
||
| 393 | } else { |
||
| 394 | $ban->addIP($ipAddresses); |
||
| 395 | } |
||
| 396 | |||
| 397 | return $ban; |
||
| 398 | } |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Get a query builder for news |
||
| 402 | * @return QueryBuilder |
||
| 403 | */ |
||
| 404 | public static function getQueryBuilder() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * {@inheritdoc} |
||
| 416 | */ |
||
| 417 | public function getName() |
||
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritdoc} |
||
| 424 | */ |
||
| 425 | public function delete() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * {@inheritdoc} |
||
| 433 | */ |
||
| 434 | public static function getActiveStatuses() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * {@inheritdoc} |
||
| 441 | */ |
||
| 442 | public static function getLazyColumns() |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Get all the bans in the database that aren't disabled or deleted |
||
| 449 | * @return Ban[] An array of ban objects |
||
| 450 | */ |
||
| 451 | public static function getBans() |
||
| 455 | |||
| 456 | /** |
||
| 457 | * Get an active ban for the player |
||
| 458 | * @param int $playerId The player's ID |
||
| 459 | * @return Ban|null null if the player isn't currently banned |
||
| 460 | */ |
||
| 461 | 1 | public static function getBan($playerId) |
|
| 471 | } |
||
| 472 |
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..