Total Complexity | 54 |
Total Lines | 217 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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->id; |
||
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() { |
||
44 | return $this->group; |
||
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->id; }, $this->teams); |
||
74 | } |
||
75 | public function getTeam(string $id) { |
||
76 | foreach ($this->teams as $team) { |
||
77 | if ($team->id === $id) return $team; |
||
78 | } |
||
79 | return false; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * $results = array ( |
||
84 | * * team->id => 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->id] = ['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($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->id]['score'] += $score; |
||
112 | $i++; |
||
113 | } |
||
114 | return $this; |
||
115 | } |
||
116 | private function setResults2($score, $prev, $next, $team) { |
||
117 | if ($score === $prev || $score === $next) { |
||
118 | $this->drawIds[] = $team->id; |
||
119 | $team->addDraw($this->group->id); |
||
120 | $this->results[$team->id] += ['points' => $this->group->drawPoints, 'type' => 'draw']; |
||
121 | } |
||
122 | elseif ($i === 1) { |
||
|
|||
123 | $this->winId = $team->id; |
||
124 | $team->addWin($this->group->id); |
||
125 | $this->results[$team->id] += ['points' => $this->group->winPoints, 'type' => 'win']; |
||
126 | } |
||
127 | else { |
||
128 | $this->lossId = $team->id; |
||
129 | $team->addLoss($this->group->id); |
||
130 | $this->results[$team->id] += ['points' => $this->group->lostPoints, 'type' => 'loss']; |
||
131 | } |
||
132 | return $this; |
||
133 | } |
||
134 | private function setResults3($i, $team) { |
||
135 | switch ($i) { |
||
136 | case 1: |
||
137 | $this->winId = $team->id; |
||
138 | $team->addWin($this->group->id); |
||
139 | $this->results[$team->id] += ['points' => $this->group->winPoints, 'type' => 'win']; |
||
140 | break; |
||
141 | case 2: |
||
142 | $this->secondId = $team->id; |
||
143 | $team->addSecond($this->group->id); |
||
144 | $this->results[$team->id] += ['points' => $this->group->secondPoints, 'type' => 'second']; |
||
145 | break; |
||
146 | case 3: |
||
147 | $this->lossId = $team->id; |
||
148 | $team->addLoss($this->group->id); |
||
149 | $this->results[$team->id] += ['points' => $this->group->lostPoints, 'type' => 'loss']; |
||
150 | break; |
||
151 | } |
||
152 | return $this; |
||
153 | } |
||
154 | private function setResults4($i, $team) { |
||
155 | switch ($i) { |
||
156 | case 1: |
||
157 | $this->winId = $team->id; |
||
158 | $team->addWin($this->group->id); |
||
159 | $this->results[$team->id] += ['points' => $this->group->winPoints, 'type' => 'win']; |
||
160 | break; |
||
161 | case 2: |
||
162 | $this->secondId = $team->id; |
||
163 | $team->addSecond($this->group->id); |
||
164 | $this->results[$team->id] += ['points' => $this->group->secondPoints, 'type' => 'second']; |
||
165 | break; |
||
166 | case 3: |
||
167 | $this->thirdId = $team->id; |
||
168 | $team->addThird($this->group->id); |
||
169 | $this->results[$team->id] += ['points' => $this->group->thirdPoints, 'type' => 'third']; |
||
170 | break; |
||
171 | case 4: |
||
172 | $this->lossId = $team->id; |
||
173 | $team->addLoss($this->group->id); |
||
174 | $this->results[$team->id] += ['points' => $this->group->lostPoints, 'type' => 'loss']; |
||
175 | break; |
||
176 | } |
||
177 | return $this; |
||
178 | } |
||
179 | |||
180 | public function resetResults() { |
||
181 | foreach ($this->results as $teamId => $score) { |
||
182 | $team = $this->getTeam($teamId); |
||
183 | $team->groupResults[$this->group->id]['score'] -= $score['score']; |
||
184 | $team->sumScore -= $score['score']; |
||
185 | switch ($score['type']) { |
||
186 | case 'win': |
||
187 | $team->removeWin($this->group->id); |
||
188 | break; |
||
189 | case 'draw': |
||
190 | $team->removeDraw($this->group->id); |
||
191 | break; |
||
192 | case 'loss': |
||
193 | $team->removeLoss($this->group->id); |
||
194 | break; |
||
195 | case 'second': |
||
196 | $team->removeSecond($this->group->id); |
||
197 | break; |
||
198 | case 'third': |
||
199 | $team->removeThird($this->group->id); |
||
200 | break; |
||
201 | } |
||
202 | } |
||
203 | $this->results = []; |
||
204 | return $this; |
||
205 | } |
||
206 | public function getWin() { |
||
207 | return $this->winId; |
||
208 | } |
||
209 | public function getLoss() { |
||
210 | return $this->lossId; |
||
211 | } |
||
212 | public function getSecond() { |
||
214 | } |
||
215 | public function getThird() { |
||
216 | return $this->thirdId; |
||
217 | } |
||
218 | public function getDraw() { |
||
219 | return $this->drawIds; |
||
220 | } |
||
221 | |||
222 | public function isPlayed() { |
||
225 | } |
||
226 | } |
||
227 |