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 | const DEFAULT_STATUS = 'public'; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The name of the database table used for queries |
||
| 79 | */ |
||
| 80 | const TABLE = "bans"; |
||
| 81 | |||
| 82 | const CREATE_PERMISSION = Permission::ADD_BAN; |
||
| 83 | const EDIT_PERMISSION = Permission::EDIT_BAN; |
||
| 84 | const SOFT_DELETE_PERMISSION = Permission::SOFT_DELETE_BAN; |
||
| 85 | const HARD_DELETE_PERMISSION = Permission::HARD_DELETE_BAN; |
||
| 86 | |||
| 87 | /** |
||
| 88 | 2 | * {@inheritdoc} |
|
| 89 | */ |
||
| 90 | 2 | protected function assignResult($ban) |
|
| 104 | |||
| 105 | /** |
||
| 106 | * {@inheritdoc} |
||
| 107 | */ |
||
| 108 | protected function assignLazyResult($result) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Add an IP to the ban |
||
| 115 | * |
||
| 116 | * @param string $ipAddress The IP to add to a ban |
||
| 117 | */ |
||
| 118 | View Code Duplication | public function addIP($ipAddress) |
|
| 125 | |||
| 126 | /** |
||
| 127 | * Remove an IP from the ban |
||
| 128 | * |
||
| 129 | * @param string $ipAddress The IP to remove from the ban |
||
| 130 | */ |
||
| 131 | View Code Duplication | public function removeIP($ipAddress) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set the IP addresses of the ban |
||
| 142 | * |
||
| 143 | * @todo Is it worth making this faster? |
||
| 144 | * @param string[] $ipAddresses The new IP addresses of the ban |
||
| 145 | * @return self |
||
| 146 | */ |
||
| 147 | public function setIPs($ipAddresses) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Check whether or not a player is allowed to join a server when they've been banned |
||
| 170 | * @return bool Whether or not a player is allowed to join |
||
| 171 | */ |
||
| 172 | public function allowedServerJoin() |
||
| 176 | |||
| 177 | /** |
||
| 178 | * Get the user who imposed the ban |
||
| 179 | * @return Player The banner |
||
| 180 | */ |
||
| 181 | public function getAuthor() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Get the creation time of the ban |
||
| 188 | * @return TimeDate The creation time |
||
| 189 | */ |
||
| 190 | public function getCreated() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Get the expiration time of the ban |
||
| 197 | * @return TimeDate |
||
| 198 | */ |
||
| 199 | public function getExpiration() |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Get the ban's description |
||
| 206 | * @return string |
||
| 207 | */ |
||
| 208 | public function getReason() |
||
| 212 | |||
| 213 | /** |
||
| 214 | * Get the ban summary that will appear when a player is denied access to a league server on join |
||
| 215 | * @return string The ban summary |
||
| 216 | */ |
||
| 217 | public function getServerMessage() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Get the IP address of the banned player |
||
| 228 | * @return string[] |
||
| 229 | */ |
||
| 230 | public function getIpAddresses() |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Get the time when the ban was last updated |
||
| 239 | * @return TimeDate |
||
| 240 | */ |
||
| 241 | public function getUpdated() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Get the player who was banned |
||
| 248 | * @return Player The banned player |
||
| 249 | */ |
||
| 250 | public function getVictim() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Get the ID of the player who was banned |
||
| 257 | * @return int The ID of the victim of the ban |
||
| 258 | */ |
||
| 259 | public function getVictimID() |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Calculate whether a ban has expired or not. |
||
| 266 | * |
||
| 267 | * @return bool True if the ban's expiration time has already passed |
||
| 268 | */ |
||
| 269 | public function isExpired() |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Check whether the ban will expire automatically |
||
| 280 | * |
||
| 281 | * @return bool |
||
| 282 | */ |
||
| 283 | public function willExpire() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Mark the ban as expired |
||
| 290 | * |
||
| 291 | * @return self |
||
| 292 | */ |
||
| 293 | public function expire() |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Set the expiration date of the ban |
||
| 303 | * @param TimeDate $expiration The expiration |
||
| 304 | 1 | * @return self |
|
| 305 | */ |
||
| 306 | 1 | public function setExpiration($expiration) |
|
| 314 | |||
| 315 | /** |
||
| 316 | * Set the server message of the ban |
||
| 317 | * @param string $message The new server message |
||
| 318 | * @return self |
||
| 319 | */ |
||
| 320 | public function setServerMessage($message) |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Set the reason of the ban |
||
| 327 | * @param string $reason The new ban reason |
||
| 328 | * @return self |
||
| 329 | */ |
||
| 330 | public function setReason($reason) |
||
| 334 | |||
| 335 | /** |
||
| 336 | * Update the last edit timestamp |
||
| 337 | * @return self |
||
| 338 | */ |
||
| 339 | public function updateEditTimestamp() |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Set whether the ban's victim is allowed to enter a match server |
||
| 346 | * @param bool $allowServerJoin |
||
| 347 | * @return self |
||
| 348 | */ |
||
| 349 | public function setAllowServerJoin($allowServerJoin) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Add a new ban |
||
| 356 | * |
||
| 357 | * @param int $playerID The ID of the victim of the ban |
||
| 358 | * @param int $authorID The ID of the player responsible for the ban |
||
| 359 | * @param mixed|null $expiration The expiration of the ban (set to NULL so that it never expires) |
||
| 360 | * @param string $reason The full reason for the ban |
||
| 361 | * @param string $srvmsg A summary of the ban to be displayed on server banlists (max 150 characters) |
||
| 362 | * @param string[] $ipAddresses An array of IPs that have been banned |
||
| 363 | * @param bool $allowServerJoin Whether or not the player is allowed to join match servers |
||
| 364 | * |
||
| 365 | 2 | * @return Ban An object representing the ban that was just entered or false if the ban was not created |
|
| 366 | */ |
||
| 367 | 2 | public static function addBan($playerID, $authorID, $expiration, $reason, $srvmsg = "", $ipAddresses = array(), $allowServerJoin = false) |
|
| 401 | |||
| 402 | /** |
||
| 403 | * Get a query builder for news |
||
| 404 | * @return QueryBuilder |
||
| 405 | */ |
||
| 406 | public static function getQueryBuilder() |
||
| 415 | |||
| 416 | /** |
||
| 417 | * {@inheritdoc} |
||
| 418 | */ |
||
| 419 | public function getName() |
||
| 423 | |||
| 424 | /** |
||
| 425 | * {@inheritdoc} |
||
| 426 | */ |
||
| 427 | public function delete() |
||
| 432 | |||
| 433 | /** |
||
| 434 | * {@inheritdoc} |
||
| 435 | */ |
||
| 436 | public static function getLazyColumns() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get all the bans in the database that aren't disabled or deleted |
||
| 443 | * @return Ban[] An array of ban objects |
||
| 444 | */ |
||
| 445 | public static function getBans() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Get an active ban for the player |
||
| 452 | * @param int $playerId The player's ID |
||
| 453 | * @return Ban|null null if the player isn't currently banned |
||
| 454 | */ |
||
| 455 | public static function getBan($playerId) |
||
| 465 | } |
||
| 466 |
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..