Total Complexity | 48 |
Total Lines | 186 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Tournament 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 Tournament, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Tournament |
||
9 | { |
||
10 | |||
11 | private $name = ''; |
||
12 | private $categories = []; |
||
13 | private $rounds = []; |
||
14 | private $teams = []; |
||
15 | |||
16 | private $expectedPlay = 0; |
||
17 | private $expectedGameWait = 0; |
||
18 | private $expectedRoundWait = 0; |
||
19 | private $expectedCategoryWait = 0; |
||
20 | |||
21 | private $allowSkip = false; |
||
22 | |||
23 | function __construct(string $name = ''){ |
||
25 | } |
||
26 | public function __toString() { |
||
28 | } |
||
29 | |||
30 | public function setName(string $name) { |
||
31 | $this->name = $name; |
||
32 | return $this; |
||
33 | } |
||
34 | public function getName() { |
||
35 | return $this->name; |
||
36 | } |
||
37 | |||
38 | public function setPlay(int $play) { |
||
39 | $this->expectedPlay = $play; |
||
40 | return $this; |
||
41 | } |
||
42 | public function getPlay() { |
||
43 | return $this->expectedPlay; |
||
44 | } |
||
45 | public function setGameWait(int $wait) { |
||
46 | $this->expectedGameWait = $wait; |
||
47 | return $this; |
||
48 | } |
||
49 | public function getGameWait() { |
||
50 | return $this->expectedGameWait; |
||
51 | } |
||
52 | public function setRoundWait(int $wait) { |
||
53 | $this->expectedRoundWait = $wait; |
||
54 | return $this; |
||
55 | } |
||
56 | public function getRoundWait() { |
||
57 | return $this->expectedRoundWait; |
||
58 | } |
||
59 | public function setCategoryWait(int $wait) { |
||
62 | } |
||
63 | public function getCategoryWait() { |
||
64 | return $this->expectedCategoryWait; |
||
65 | } |
||
66 | public function getTournamentTime(){ |
||
69 | } |
||
70 | |||
71 | public function allowSkip(){ |
||
72 | $this->allowSkip = true; |
||
73 | return $this; |
||
74 | } |
||
75 | public function disallowSkip(){ |
||
76 | $this->allowSkip = false; |
||
77 | return $this; |
||
78 | } |
||
79 | public function setSkip(bool $skip) { |
||
82 | } |
||
83 | public function getSkip() { |
||
84 | return $this->allowSkip; |
||
85 | } |
||
86 | |||
87 | public function addCategory(Category ...$categories){ |
||
88 | foreach ($categories as $category) { |
||
89 | $this->categories[] = $category; |
||
90 | } |
||
91 | return $this; |
||
92 | } |
||
93 | public function category(string $name = '') { |
||
94 | $c = new Category($name); |
||
95 | $this->categories[] = $c->setSkip($this->allowSkip); |
||
96 | return $c; |
||
97 | } |
||
98 | public function getCategories() { |
||
99 | return $this->categories; |
||
100 | } |
||
101 | |||
102 | public function addRound(Round ...$rounds) { |
||
107 | } |
||
108 | public function round(string $name = '', $id = null) { |
||
109 | $r = new Round($name, $id); |
||
110 | $this->rounds[] = $r->setSkip($this->allowSkip); |
||
111 | return $r; |
||
112 | } |
||
113 | public function getRounds() { |
||
114 | if (count($this->categories) > 0) { |
||
115 | $rounds = []; |
||
116 | foreach ($this->categories as $category) { |
||
117 | $rounds = array_merge($rounds, $category->getRounds()); |
||
118 | } |
||
119 | return array_merge($rounds, $this->rounds); |
||
120 | } |
||
121 | return $this->rounds; |
||
122 | } |
||
123 | |||
124 | public function addTeam(Team ...$teams) { |
||
125 | foreach ($teams as $team) { |
||
126 | $this->teams[] = $team; |
||
127 | } |
||
128 | return $this; |
||
129 | } |
||
130 | public function team(string $name = '', $id = null) { |
||
131 | $t = new Team($name, $id); |
||
132 | $this->teams[] = $t; |
||
133 | return $t; |
||
134 | } |
||
135 | public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS) { |
||
136 | $teams = $this->teams; |
||
137 | foreach ($this->categories as $category) { |
||
138 | $teams = array_merge($teams, $category->getTeams()); |
||
139 | } |
||
140 | foreach ($this->rounds as $round) { |
||
141 | $teams = array_merge($teams, $round->getTeams()); |
||
142 | } |
||
143 | $this->teams = \array_unique($teams); |
||
144 | if ($ordered) $this->sortTeams($ordering); |
||
145 | return $this->teams; |
||
146 | } |
||
147 | public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS) { |
||
148 | $teams = []; |
||
149 | for ($i = count($this->rounds)-1; $i >= 0; $i--) { |
||
150 | $rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); }); |
||
151 | $teams = array_merge($teams, $rTeams); |
||
152 | } |
||
153 | $this->teams = $teams; |
||
154 | return $this->teams; |
||
155 | } |
||
156 | |||
157 | public function getGames() { |
||
158 | $games = []; |
||
159 | foreach ($this->getRounds() as $round) { |
||
160 | $games = array_merge($games, $round->getGames()); |
||
161 | } |
||
162 | return $games; |
||
163 | } |
||
164 | |||
165 | public function splitTeams(Round ...$wheres) { |
||
166 | |||
167 | if (count($wheres) === 0) $wheres = $this->getRounds(); |
||
168 | |||
169 | $teams = $this->getTeams(); |
||
170 | shuffle($teams); |
||
171 | |||
172 | while (count($teams) > 0) { |
||
173 | foreach ($wheres as $where) { |
||
174 | if (count($teams) > 0) $where->addTeam(array_shift($teams)); |
||
175 | } |
||
176 | } |
||
177 | foreach ($wheres as $where) { |
||
178 | $where->splitTeams(); |
||
179 | } |
||
180 | return $this; |
||
181 | } |
||
182 | |||
183 | public function genGamesSimulate(bool $returnTime = false) { |
||
184 | $games = Utilis\Simulator::simulateTournament($this); |
||
185 | |||
186 | if ($returnTime) return $this->getTournamentTime(); |
||
187 | return $games; |
||
188 | } |
||
189 | public function genGamesSimulateReal(bool $returnTime = false) { |
||
194 | } |
||
195 | |||
196 | } |
||
197 |