Complex classes like Team 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 Team, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 11 | class Team |
||
| 12 | {
|
||
| 13 | /** |
||
| 14 | * @var int |
||
| 15 | * |
||
| 16 | * @ORM\Id |
||
| 17 | * @ORM\GeneratedValue |
||
| 18 | * @ORM\Column(type="integer") |
||
| 19 | */ |
||
| 20 | private $id; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string |
||
| 24 | * |
||
| 25 | * @ORM\Column(type="string") |
||
| 26 | */ |
||
| 27 | private $name; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var League |
||
| 31 | * |
||
| 32 | * @ORM\ManyToOne(targetEntity="League", inversedBy="teams") |
||
| 33 | * @ORM\JoinColumn(nullable=false) |
||
| 34 | */ |
||
| 35 | private $league; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | * |
||
| 40 | * @ORM\Column(type="integer") |
||
| 41 | */ |
||
| 42 | private $points = 0; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | * |
||
| 47 | * @ORM\Column(type="integer") |
||
| 48 | */ |
||
| 49 | private $goalsFor = 0; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int |
||
| 53 | * |
||
| 54 | * @ORM\Column(type="integer") |
||
| 55 | */ |
||
| 56 | private $goalsAgainst = 0; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var Player[]|ArrayCollection |
||
| 60 | * |
||
| 61 | * @ORM\OneToMany(targetEntity="Player", mappedBy="team") |
||
| 62 | */ |
||
| 63 | private $players; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var int |
||
| 67 | * |
||
| 68 | * @ORM\Column(type="integer") |
||
| 69 | */ |
||
| 70 | private $money; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var Trainer |
||
| 74 | * |
||
| 75 | * @ORM\OneToOne(targetEntity="Trainer", mappedBy="team") |
||
| 76 | */ |
||
| 77 | private $trainer; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var Manager |
||
| 81 | * |
||
| 82 | * @ORM\OneToOne(targetEntity="Manager", mappedBy="team") |
||
| 83 | */ |
||
| 84 | private $manager; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var FinalPosition[]|ArrayCollection |
||
| 88 | * |
||
| 89 | * @ORM\OneToMany(targetEntity="FinalPosition", mappedBy="team", cascade={"all"})
|
||
| 90 | */ |
||
| 91 | private $finalPositions; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Transfer[] |
||
| 95 | * |
||
| 96 | * @ORM\OneToMany(targetEntity="Transfer", mappedBy="targetTeam") |
||
| 97 | */ |
||
| 98 | private $transfersIncoming; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @var Transfer[] |
||
| 102 | * |
||
| 103 | * @ORM\OneToMany(targetEntity="Transfer", mappedBy="originTeam") |
||
| 104 | */ |
||
| 105 | private $transfersOutgoing; |
||
| 106 | |||
| 107 | 53 | public function __construct() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 19 | public function getId() |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param Team $team |
||
| 123 | * |
||
| 124 | * @return bool |
||
| 125 | */ |
||
| 126 | 18 | public function equals(Team $team) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param int $id |
||
| 133 | */ |
||
| 134 | 16 | public function setId($id) |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @param int $points |
||
| 141 | */ |
||
| 142 | 5 | public function setPoints($points) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return int |
||
| 149 | */ |
||
| 150 | 7 | public function getPoints() |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @param int $points |
||
| 157 | */ |
||
| 158 | 5 | public function addPoints($points) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @param int $goalsFor |
||
| 165 | */ |
||
| 166 | 3 | public function setGoalsFor($goalsFor) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param int $goalsFor |
||
| 173 | */ |
||
| 174 | 5 | public function addGoalsFor($goalsFor) |
|
| 178 | |||
| 179 | /** |
||
| 180 | * @return int |
||
| 181 | */ |
||
| 182 | 5 | public function getGoalsDifference() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @param int $goalsAgainst |
||
| 189 | */ |
||
| 190 | 2 | public function setGoalsAgainst($goalsAgainst) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @param int $goalsAgainst |
||
| 197 | */ |
||
| 198 | 5 | public function addGoalsAgainst($goalsAgainst) |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return int |
||
| 205 | */ |
||
| 206 | 7 | public function getGoalsFor() |
|
| 210 | |||
| 211 | /** |
||
| 212 | * @return int |
||
| 213 | */ |
||
| 214 | 2 | public function getGoalsAgainst() |
|
| 218 | |||
| 219 | /** |
||
| 220 | * @return string |
||
| 221 | */ |
||
| 222 | 1 | public function getName() |
|
| 226 | |||
| 227 | /** |
||
| 228 | * @param string $name |
||
| 229 | */ |
||
| 230 | 1 | public function setName($name) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param League $league |
||
| 237 | */ |
||
| 238 | public function setLeague(League $league) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @return ArrayCollection|Player[] |
||
| 245 | */ |
||
| 246 | 10 | public function getPlayers() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param Player $player |
||
| 253 | */ |
||
| 254 | 31 | public function addPlayer(Player $player) |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @return Player |
||
| 264 | * |
||
| 265 | * @throws \Exception |
||
| 266 | */ |
||
| 267 | 1 | public function getRandomPlayer() |
|
| 275 | |||
| 276 | /** |
||
| 277 | * @return Player |
||
| 278 | * |
||
| 279 | * @throws \Exception |
||
| 280 | */ |
||
| 281 | 5 | public function getRandomPlayerFromLineup() |
|
| 291 | |||
| 292 | /** |
||
| 293 | * @return Player[] |
||
| 294 | */ |
||
| 295 | 6 | public function getLineup() |
|
| 302 | |||
| 303 | 1 | public function resetPointsAndGoals() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * @return League |
||
| 312 | */ |
||
| 313 | public function getLeague() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * @return FinalPosition[] |
||
| 320 | */ |
||
| 321 | public function getFinalPositions() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | 16 | public function getMoney() |
|
| 333 | |||
| 334 | /** |
||
| 335 | * @param int $money |
||
| 336 | */ |
||
| 337 | 17 | public function setMoney($money) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * @param int $amount |
||
| 344 | */ |
||
| 345 | 1 | public function addMoney($amount) |
|
| 349 | |||
| 350 | /** |
||
| 351 | * @param int $amount |
||
| 352 | */ |
||
| 353 | 1 | public function subtractAmount($amount) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * @return Trainer |
||
| 360 | */ |
||
| 361 | 5 | public function getTrainer() |
|
| 365 | |||
| 366 | /** |
||
| 367 | * @param Trainer $trainer |
||
| 368 | */ |
||
| 369 | 4 | public function setTrainer(Trainer $trainer) |
|
| 376 | |||
| 377 | /** |
||
| 378 | * @return bool |
||
| 379 | */ |
||
| 380 | 3 | public function hasTrainer() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * @return Manager |
||
| 387 | */ |
||
| 388 | 22 | public function getManager() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * @param Manager $manager |
||
| 395 | */ |
||
| 396 | 21 | public function setManager(Manager $manager) |
|
| 403 | |||
| 404 | /** |
||
| 405 | * @return bool |
||
| 406 | */ |
||
| 407 | 1 | public function hasManager() |
|
| 411 | |||
| 412 | /** |
||
| 413 | * @param int $season |
||
| 414 | * |
||
| 415 | * @throws \Exception |
||
| 416 | */ |
||
| 417 | public function createFinalPosition($season) |
||
| 426 | |||
| 427 | /** |
||
| 428 | * @return Transfer[] |
||
| 429 | */ |
||
| 430 | public function getTransfersIncoming() |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @return Transfer[] |
||
| 437 | */ |
||
| 438 | public function getTransfersOutgoing() |
||
| 442 | |||
| 443 | 2 | public function train() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * @param Team $team |
||
| 457 | * @param int $amount |
||
| 458 | */ |
||
| 459 | 1 | public function sendMoney(Team $team, $amount) |
|
| 464 | } |
||
| 465 |