Total Complexity | 55 |
Total Lines | 220 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Game 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.
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 Game, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Game |
||
9 | { |
||
10 | |||
11 | private $teams = []; |
||
12 | private $results = []; |
||
13 | private $group = null; |
||
14 | private $winId = null; |
||
15 | private $lossId = null; |
||
16 | private $secondId = null; |
||
17 | private $thirdId = null; |
||
18 | private $drawIds = []; |
||
19 | |||
20 | function __construct(array $teams, Group $group) { |
||
21 | $this->group = $group; |
||
22 | $error = []; |
||
23 | $tids = []; |
||
24 | foreach ($teams as $key => $team) { |
||
25 | if (!$team instanceof Team) { |
||
26 | $error[] = $team; |
||
27 | unset($teams[$key]); |
||
28 | continue; |
||
29 | } |
||
30 | $team->addGame($this); |
||
31 | $tids[] = $team->getId(); |
||
32 | } |
||
33 | $this->teams = $teams; |
||
34 | foreach ($this->teams as $team) { |
||
35 | foreach ($this->teams as $team2) { |
||
36 | if ($team === $team2) continue; |
||
37 | $team->addGameWith($team2, $group); |
||
38 | } |
||
39 | } |
||
40 | if (count($error) > 0) throw new \Exception('Trying to add teams ('.count($error).') that are not instance of Team class'.PHP_EOL.print_r($error, true)); |
||
41 | } |
||
42 | |||
43 | public function getGroup() { |
||
45 | } |
||
46 | |||
47 | public function addTeam(...$teams) { |
||
48 | $error = []; |
||
49 | foreach ($teams as $key => $team) { |
||
50 | if (!$team instanceof Team) { |
||
51 | $error[] = $team; |
||
52 | unset($teams[$key]); |
||
53 | continue; |
||
54 | } |
||
55 | $this->teams[] = $team; |
||
56 | $team->addGame($this); |
||
57 | |||
58 | foreach ($this->teams as $team2) { |
||
59 | if ($team === $team2) continue; |
||
60 | if ($team instanceof Team) { |
||
61 | $team->addGameWith($team2, $this->group); |
||
62 | $team2->addGameWith($team, $this->group); |
||
63 | } |
||
64 | } |
||
65 | } |
||
66 | if (count($error) > 0) throw new \Exception('Trying to add teams ('.count($error).') that are not instance of Team class'.PHP_EOL.print_r($error, true)); |
||
67 | return $this; |
||
68 | } |
||
69 | public function getTeams(){ |
||
70 | return $this->teams; |
||
71 | } |
||
72 | public function getTeamsIds(){ |
||
73 | return array_map(function($a){ return $a->getId(); }, $this->teams); |
||
74 | } |
||
75 | public function getTeam(string $id) { |
||
76 | foreach ($this->teams as $team) { |
||
77 | if ($team->getId() === $id) return $team; |
||
78 | } |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * $results = array ( |
||
84 | * * team->getId() => team->score |
||
85 | * ) |
||
86 | */ |
||
87 | public function setResults(array $results = []) { |
||
88 | if (count($this->results) === 0) $this->resetResults(); |
||
89 | arsort($results); |
||
90 | $inGame = /** @scrutinizer ignore-call */ $this->group->getInGame(); |
||
91 | $i = 1; |
||
92 | foreach ($results as $id => $score) { |
||
93 | $team = $this->getTeam($id); |
||
94 | if ($team === false) throw new \Exception('Couldn\'t find team with id of "'.$id.'"'); |
||
95 | $this->results[$team->getId()] = ['score' => $score]; |
||
96 | $team->sumScore += $score; |
||
97 | $prev = prev($results); |
||
98 | next($results); |
||
99 | $next = next($results); |
||
100 | switch ($inGame) { |
||
101 | case 2: |
||
102 | $this->setResults2($i, $score, $prev, $next, $team); |
||
103 | break; |
||
104 | case 3: |
||
105 | $this->setResults3($i, $team); |
||
106 | break; |
||
107 | case 4: |
||
108 | $this->setResults4($i, $team); |
||
109 | break; |
||
110 | } |
||
111 | $team->groupResults[$this->group->getId()]['score'] += $score; |
||
112 | $i++; |
||
113 | } |
||
114 | return $this; |
||
115 | } |
||
116 | private function setResults2($i, $score, $prev, $next, $team) { |
||
133 | } |
||
134 | private function setResults3($i, $team) { |
||
135 | switch ($i) { |
||
136 | case 1: |
||
137 | $this->winId = $team->getId(); |
||
138 | $team->addWin($this->group->getId()); |
||
153 | } |
||
154 | private function setResults4($i, $team) { |
||
178 | } |
||
179 | public function getResults() { |
||
180 | return $this->results; |
||
181 | } |
||
182 | |||
183 | public function resetResults() { |
||
184 | foreach ($this->results as $teamId => $score) { |
||
185 | $team = $this->getTeam($teamId); |
||
186 | $team->groupResults[$this->group->getId()]['score'] -= $score['score']; |
||
187 | $team->sumScore -= $score['score']; |
||
188 | switch ($score['type']) { |
||
189 | case 'win': |
||
190 | $team->removeWin($this->group->getId()); |
||
191 | break; |
||
192 | case 'draw': |
||
193 | $team->removeDraw($this->group->getId()); |
||
194 | break; |
||
195 | case 'loss': |
||
196 | $team->removeLoss($this->group->getId()); |
||
197 | break; |
||
198 | case 'second': |
||
199 | $team->removeSecond($this->group->getId()); |
||
200 | break; |
||
201 | case 'third': |
||
202 | $team->removeThird($this->group->getId()); |
||
203 | break; |
||
204 | } |
||
205 | } |
||
206 | $this->results = []; |
||
207 | return $this; |
||
208 | } |
||
209 | public function getWin() { |
||
210 | return $this->winId; |
||
211 | } |
||
212 | public function getLoss() { |
||
213 | return $this->lossId; |
||
214 | } |
||
215 | public function getSecond() { |
||
217 | } |
||
218 | public function getThird() { |
||
219 | return $this->thirdId; |
||
220 | } |
||
221 | public function getDraw() { |
||
222 | return $this->drawIds; |
||
223 | } |
||
224 | |||
225 | public function isPlayed() { |
||
228 | } |
||
229 | } |
||
230 |