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 |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | protected $login; |
||
| 20 | |||
| 21 | /** @var bool */ |
||
| 22 | protected $isConnected = true; |
||
| 23 | |||
| 24 | /** @var string */ |
||
| 25 | protected $nickName; |
||
| 26 | |||
| 27 | /** @var int */ |
||
| 28 | protected $playerId; |
||
| 29 | |||
| 30 | /** @var int */ |
||
| 31 | protected $teamId; |
||
| 32 | |||
| 33 | /** @var bool */ |
||
| 34 | protected $isInOfficialMode; |
||
| 35 | |||
| 36 | /** @var int */ |
||
| 37 | protected $ladderRanking; |
||
| 38 | |||
| 39 | /** @var int */ |
||
| 40 | protected $spectatorStatus; |
||
| 41 | |||
| 42 | /** @var int */ |
||
| 43 | protected $flags; |
||
| 44 | |||
| 45 | /** @var int */ |
||
| 46 | protected $forceSpectator; |
||
| 47 | |||
| 48 | /** @var bool */ |
||
| 49 | protected $isReferee; |
||
| 50 | |||
| 51 | /** @var bool */ |
||
| 52 | protected $isPodiumReady; |
||
| 53 | |||
| 54 | /** @var bool */ |
||
| 55 | protected $isUsingStereoscopy; |
||
| 56 | |||
| 57 | /** @var bool */ |
||
| 58 | protected $isManagedByAnOtherServer; |
||
| 59 | |||
| 60 | /** @var bool */ |
||
| 61 | protected $isServer; |
||
| 62 | |||
| 63 | /** @var bool */ |
||
| 64 | protected $hasPlayerSlot; |
||
| 65 | |||
| 66 | /** @var bool */ |
||
| 67 | protected $isBroadcasting; |
||
| 68 | |||
| 69 | /** @var bool */ |
||
| 70 | protected $hasJoinedGame = false; |
||
| 71 | |||
| 72 | /** @var bool */ |
||
| 73 | protected $spectator; |
||
| 74 | |||
| 75 | /** @var bool */ |
||
| 76 | protected $temporarySpectator; |
||
| 77 | |||
| 78 | /** @var bool */ |
||
| 79 | protected $pureSpectator; |
||
| 80 | |||
| 81 | /** @var bool */ |
||
| 82 | protected $autoTarget; |
||
| 83 | |||
| 84 | /** @var int */ |
||
| 85 | protected $currentTargetId; |
||
| 86 | |||
| 87 | /** @var string */ |
||
| 88 | protected $path; |
||
| 89 | |||
| 90 | /** @var string */ |
||
| 91 | protected $language; |
||
| 92 | |||
| 93 | /** @var string */ |
||
| 94 | protected $clientVersion; |
||
| 95 | |||
| 96 | /** @var string */ |
||
| 97 | protected $clientTitleVersion; |
||
| 98 | |||
| 99 | /** @var string */ |
||
| 100 | protected $iPAddress; |
||
| 101 | |||
| 102 | /** @var int */ |
||
| 103 | protected $downloadRate; |
||
| 104 | |||
| 105 | /** @var int */ |
||
| 106 | protected $uploadRate; |
||
| 107 | |||
| 108 | /** @var FileDesc */ |
||
| 109 | protected $avatar; |
||
| 110 | |||
| 111 | /** @var Skin[] */ |
||
| 112 | protected $skins; |
||
| 113 | |||
| 114 | /** @var mixed[] */ |
||
| 115 | protected $ladderStats; |
||
| 116 | |||
| 117 | /** @var int */ |
||
| 118 | protected $hoursSinceZoneInscription; |
||
| 119 | |||
| 120 | /** @var string */ |
||
| 121 | protected $broadcasterLogin; |
||
| 122 | |||
| 123 | /** @var string[] */ |
||
| 124 | protected $allies = array(); |
||
| 125 | |||
| 126 | /** @var string */ |
||
| 127 | protected $clubLink; |
||
| 128 | /** @var int */ |
||
| 129 | protected $rank; |
||
| 130 | |||
| 131 | /** @var int */ |
||
| 132 | protected $bestTime; |
||
| 133 | |||
| 134 | /** @var int[] */ |
||
| 135 | protected $bestCheckpoints; |
||
| 136 | |||
| 137 | /** @var int */ |
||
| 138 | protected $score; |
||
| 139 | |||
| 140 | /** @var int */ |
||
| 141 | protected $nbrLapsFinished; |
||
| 142 | |||
| 143 | /** @var float */ |
||
| 144 | protected $ladderScore; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 1 | public function isIsConnected() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | 2 | public function getNickName() |
|
| 161 | |||
| 162 | /** |
||
| 163 | * @return int |
||
| 164 | */ |
||
| 165 | 2 | public function getPlayerId() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return int |
||
| 172 | */ |
||
| 173 | 1 | public function getTeamId() |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return boolean |
||
| 180 | */ |
||
| 181 | public function isIsSpectator() |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return boolean |
||
| 188 | */ |
||
| 189 | 1 | public function isIsInOfficialMode() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @return int |
||
| 196 | */ |
||
| 197 | 1 | public function getLadderRanking() |
|
| 201 | |||
| 202 | /** |
||
| 203 | * @return int |
||
| 204 | */ |
||
| 205 | 3 | public function getSpectatorStatus() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return int |
||
| 212 | */ |
||
| 213 | 1 | public function getFlags() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @return int |
||
| 220 | */ |
||
| 221 | 1 | public function getForceSpectator() |
|
| 225 | |||
| 226 | /** |
||
| 227 | * @return boolean |
||
| 228 | */ |
||
| 229 | 1 | public function isIsReferee() |
|
| 233 | |||
| 234 | /** |
||
| 235 | * @return boolean |
||
| 236 | */ |
||
| 237 | 1 | public function isIsPodiumReady() |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @return boolean |
||
| 244 | */ |
||
| 245 | 1 | public function isIsUsingStereoscopy() |
|
| 249 | |||
| 250 | /** |
||
| 251 | * @return boolean |
||
| 252 | */ |
||
| 253 | 1 | public function isIsManagedByAnOtherServer() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * @return boolean |
||
| 260 | */ |
||
| 261 | 2 | public function isIsServer() |
|
| 265 | |||
| 266 | /** |
||
| 267 | * @return boolean |
||
| 268 | */ |
||
| 269 | 1 | public function isHasPlayerSlot() |
|
| 273 | |||
| 274 | /** |
||
| 275 | * @return boolean |
||
| 276 | */ |
||
| 277 | 1 | public function isIsBroadcasting() |
|
| 281 | |||
| 282 | /** |
||
| 283 | * @return boolean |
||
| 284 | */ |
||
| 285 | 1 | public function isHasJoinedGame() |
|
| 289 | |||
| 290 | /** |
||
| 291 | * @return boolean |
||
| 292 | */ |
||
| 293 | 10 | public function isSpectator() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * @return boolean |
||
| 300 | */ |
||
| 301 | 1 | public function isTemporarySpectator() |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @return boolean |
||
| 308 | */ |
||
| 309 | 1 | public function isPureSpectator() |
|
| 313 | |||
| 314 | /** |
||
| 315 | * @return boolean |
||
| 316 | */ |
||
| 317 | 1 | public function isAutoTarget() |
|
| 321 | |||
| 322 | /** |
||
| 323 | * @return int |
||
| 324 | */ |
||
| 325 | 1 | public function getCurrentTargetId() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | 1 | public function getPath() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * @return string |
||
| 340 | */ |
||
| 341 | 1 | public function getLanguage() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @return string |
||
| 348 | */ |
||
| 349 | 2 | public function getClientVersion() |
|
| 353 | |||
| 354 | /** |
||
| 355 | * @return string |
||
| 356 | */ |
||
| 357 | 1 | public function getClientTitleVersion() |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @return string |
||
| 364 | */ |
||
| 365 | 1 | public function getIPAddress() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @return int |
||
| 372 | */ |
||
| 373 | 1 | public function getDownloadRate() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @return int |
||
| 380 | */ |
||
| 381 | 1 | public function getUploadRate() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @return FileDesc |
||
| 388 | */ |
||
| 389 | 1 | public function getAvatar() |
|
| 393 | |||
| 394 | /** |
||
| 395 | * @return Skin[] |
||
| 396 | */ |
||
| 397 | 1 | public function getSkins() |
|
| 401 | |||
| 402 | /** |
||
| 403 | * @return \mixed[] |
||
| 404 | */ |
||
| 405 | 1 | public function getLadderStats() |
|
| 409 | |||
| 410 | /** |
||
| 411 | * @return int |
||
| 412 | */ |
||
| 413 | 1 | public function getHoursSinceZoneInscription() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * @return string |
||
| 420 | */ |
||
| 421 | 1 | public function getBroadcasterLogin() |
|
| 425 | |||
| 426 | /** |
||
| 427 | * @return string[] |
||
| 428 | */ |
||
| 429 | 1 | public function getAllies() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * @return string |
||
| 436 | */ |
||
| 437 | 1 | public function getClubLink() |
|
| 441 | |||
| 442 | /** |
||
| 443 | * @return int |
||
| 444 | */ |
||
| 445 | 1 | public function getRank() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * @return int |
||
| 452 | */ |
||
| 453 | 1 | public function getBestTime() |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @return integer[] |
||
| 460 | */ |
||
| 461 | 1 | public function getBestCheckpoints() |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @return int |
||
| 468 | */ |
||
| 469 | 1 | public function getScore() |
|
| 473 | |||
| 474 | /** |
||
| 475 | * @return int |
||
| 476 | */ |
||
| 477 | 1 | public function getNbrLapsFinished() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * @return float |
||
| 484 | */ |
||
| 485 | 1 | public function getLadderScore() |
|
| 489 | |||
| 490 | /** |
||
| 491 | * @return string |
||
| 492 | */ |
||
| 493 | 12 | public function getLogin() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * @param \Maniaplanet\DedicatedServer\Structures\Player|array $data |
||
| 500 | * |
||
| 501 | * @return $this |
||
| 502 | */ |
||
| 503 | 26 | function merge($data) |
|
| 512 | } |
||
| 513 |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.