Complex classes like Fixture 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 Fixture, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 12 | class Fixture |
||
| 13 | {
|
||
| 14 | /** |
||
| 15 | * @var int |
||
| 16 | * |
||
| 17 | * @ORM\Id |
||
| 18 | * @ORM\GeneratedValue |
||
| 19 | * @ORM\Column(type="integer") |
||
| 20 | */ |
||
| 21 | private $id; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var int |
||
| 25 | * |
||
| 26 | * @ORM\Column(type="integer") |
||
| 27 | */ |
||
| 28 | private $season; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var int |
||
| 32 | * |
||
| 33 | * @ORM\Column(type="integer") |
||
| 34 | */ |
||
| 35 | private $week; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var League |
||
| 39 | * |
||
| 40 | * @ORM\ManyToOne(targetEntity="League", inversedBy="fixtures") |
||
| 41 | */ |
||
| 42 | private $league; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var Team |
||
| 46 | * |
||
| 47 | * @ORM\ManyToOne(targetEntity="Team") |
||
| 48 | */ |
||
| 49 | private $teamHome; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var Team |
||
| 53 | * |
||
| 54 | * @ORM\ManyToOne(targetEntity="Team") |
||
| 55 | */ |
||
| 56 | private $teamAway; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var ArrayCollection|Event[] |
||
| 60 | * |
||
| 61 | * @ORM\OneToMany(targetEntity="Event", mappedBy="fixture", cascade={"all"})
|
||
| 62 | */ |
||
| 63 | private $events; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var bool |
||
| 67 | * |
||
| 68 | * @ORM\Column(type="boolean") |
||
| 69 | */ |
||
| 70 | private $finished = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var int |
||
| 74 | * |
||
| 75 | * @ORM\Column(type="integer") |
||
| 76 | */ |
||
| 77 | private $minutesPlayed = 0; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var int |
||
| 81 | * |
||
| 82 | * @ORM\Column(type="integer", nullable=true) |
||
| 83 | */ |
||
| 84 | private $scoreHome; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int |
||
| 88 | * |
||
| 89 | * @ORM\Column(type="integer", nullable=true) |
||
| 90 | */ |
||
| 91 | private $scoreAway; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var Lineup[] |
||
| 95 | * |
||
| 96 | * @ORM\OneToMany(targetEntity="Lineup", mappedBy="fixture") |
||
| 97 | */ |
||
| 98 | private $lineups; |
||
| 99 | |||
| 100 | 16 | public function __construct() |
|
| 104 | |||
| 105 | /** |
||
| 106 | * @return int |
||
| 107 | */ |
||
| 108 | 9 | public function getScoreHome() |
|
| 112 | |||
| 113 | /** |
||
| 114 | * @return int |
||
| 115 | */ |
||
| 116 | 9 | public function getScoreAway() |
|
| 120 | |||
| 121 | 8 | public function resetScoreHome() |
|
| 125 | |||
| 126 | 8 | public function resetScoreAway() |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | 1 | public function getGoalsScored() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 1 | public function isFinished() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param bool $isFinished |
||
| 149 | */ |
||
| 150 | 5 | public function setFinished($isFinished) |
|
| 154 | |||
| 155 | /** |
||
| 156 | * @return bool |
||
| 157 | */ |
||
| 158 | 11 | public function hasTeamHome() |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 10 | public function hasTeamAway() |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param Team $teamHome |
||
| 173 | * |
||
| 174 | * @throws MatchException |
||
| 175 | */ |
||
| 176 | 10 | public function setTeamHome(Team $teamHome) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @param Team $teamAway |
||
| 187 | * |
||
| 188 | * @throws MatchException |
||
| 189 | */ |
||
| 190 | 10 | public function setTeamAway(Team $teamAway) |
|
| 198 | |||
| 199 | /** |
||
| 200 | * @return Event[] |
||
| 201 | */ |
||
| 202 | 2 | public function getEvents() |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @return Event[] |
||
| 209 | */ |
||
| 210 | 2 | public function getGoalEvents() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * @param Event $event |
||
| 224 | * |
||
| 225 | * @throws MatchException |
||
| 226 | */ |
||
| 227 | 8 | public function addEvent(Event $event) |
|
| 240 | |||
| 241 | /** |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | 5 | public function getMinutesPlayed() |
|
| 248 | |||
| 249 | 6 | public function incrementMinutesPlayed() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * @return Team |
||
| 256 | */ |
||
| 257 | 10 | public function getTeamHome() |
|
| 261 | |||
| 262 | /** |
||
| 263 | * @return Team |
||
| 264 | */ |
||
| 265 | 6 | public function getTeamAway() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * @param int $season |
||
| 272 | */ |
||
| 273 | public function setSeason($season) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @param int $week |
||
| 280 | */ |
||
| 281 | public function setWeek($week) |
||
| 285 | |||
| 286 | /** |
||
| 287 | * @return int |
||
| 288 | */ |
||
| 289 | public function getWeek() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @return Team |
||
| 296 | * |
||
| 297 | * @throws MatchException |
||
| 298 | */ |
||
| 299 | 1 | public function getWinner() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @return bool |
||
| 310 | */ |
||
| 311 | 6 | public function isDraw() |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @return bool |
||
| 318 | * |
||
| 319 | * @throws MatchException |
||
| 320 | */ |
||
| 321 | public function isHomeTeamWinner() |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function getId() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param League $league |
||
| 336 | */ |
||
| 337 | public function setLeague(League $league) |
||
| 341 | |||
| 342 | /** |
||
| 343 | * @return Lineup |
||
| 344 | * |
||
| 345 | * @throws \Exception |
||
| 346 | */ |
||
| 347 | public function getLineupHome() |
||
| 351 | |||
| 352 | /** |
||
| 353 | * @return Lineup |
||
| 354 | * |
||
| 355 | * @throws \Exception |
||
| 356 | */ |
||
| 357 | public function getLineupAway() |
||
| 361 | |||
| 362 | /** |
||
| 363 | * @param Team $team |
||
| 364 | * |
||
| 365 | * @return Lineup |
||
| 366 | * |
||
| 367 | * @throws \Exception |
||
| 368 | */ |
||
| 369 | private function getLineup(Team $team) |
||
| 379 | } |
||
| 380 |