Total Complexity | 53 |
Total Lines | 200 |
Duplicated Lines | 0 % |
Changes | 4 | ||
Bugs | 0 | Features | 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 | public $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 = ''){ |
||
24 | $this->name = $name; |
||
25 | } |
||
26 | public function __toString() { |
||
27 | return $this->name; |
||
28 | } |
||
29 | |||
30 | public function setPlay(int $play) { |
||
31 | $this->expectedPlay = $play; |
||
32 | return $this; |
||
33 | } |
||
34 | public function getPlay() { |
||
35 | return $this->expectedPlay; |
||
36 | } |
||
37 | public function setGameWait(int $wait) { |
||
38 | $this->expectedGameWait = $wait; |
||
39 | return $this; |
||
40 | } |
||
41 | public function getGameWait() { |
||
42 | return $this->expectedGameWait; |
||
43 | } |
||
44 | public function setRoundWait(int $wait) { |
||
45 | $this->expectedRoundWait = $wait; |
||
46 | return $this; |
||
47 | } |
||
48 | public function getRoundWait() { |
||
49 | return $this->expectedRoundWait; |
||
50 | } |
||
51 | public function setCategoryWait(int $wait) { |
||
52 | $this->expectedCategoryWait = $wait; |
||
53 | return $this; |
||
54 | } |
||
55 | public function getCategoryWait() { |
||
56 | return $this->expectedCategoryWait; |
||
57 | } |
||
58 | public function getTournamentTime(){ |
||
59 | $games = count($this->getGames()); |
||
60 | return $games*$this->expectedPlay+$games*$this->expectedGameWait+count($this->getRounds())*$this->expectedRoundWait+count($this->getCategories())*$this->expectedCategoryWait; |
||
61 | } |
||
62 | |||
63 | public function allowSkip(){ |
||
64 | $this->allowSkip = true; |
||
65 | return $this; |
||
66 | } |
||
67 | public function disallowSkip(){ |
||
68 | $this->allowSkip = false; |
||
69 | return $this; |
||
70 | } |
||
71 | public function setSkip(bool $skip) { |
||
72 | $this->allowSkip = $skip; |
||
73 | return $this; |
||
74 | } |
||
75 | public function getSkip() { |
||
76 | return $this->allowSkip; |
||
77 | } |
||
78 | |||
79 | public function addCategory(Category ...$categories){ |
||
80 | foreach ($categories as $category) { |
||
81 | if ($category instanceof Category) $this->categories[] = $category; |
||
82 | else throw new \Exception('Trying to add category which is not an instance of the Category class.'); |
||
83 | } |
||
84 | return $this; |
||
85 | } |
||
86 | public function category(string $name = '') { |
||
87 | $c = new Category($name); |
||
88 | $this->categories[] = $c->setSkip($this->allowSkip); |
||
89 | return $c; |
||
90 | } |
||
91 | public function getCategories() { |
||
92 | return $this->categories; |
||
93 | } |
||
94 | |||
95 | public function addRound(Round ...$rounds) { |
||
101 | } |
||
102 | public function round(string $name = '') { |
||
103 | $r = new Round($name); |
||
104 | $this->rounds[] = $r->setSkip($this->allowSkip); |
||
105 | return $r; |
||
106 | } |
||
107 | public function getRounds() { |
||
116 | } |
||
117 | |||
118 | public function addTeam(...$teams) { |
||
119 | foreach ($teams as $team) { |
||
120 | if ($team instanceof Team) { |
||
121 | $this->teams[] = $team; |
||
122 | continue; |
||
123 | } |
||
124 | elseif (gettype($team) === 'array') { |
||
125 | array_merge($teams, array_filter($team, function($a) { |
||
126 | return ($a instanceof Team); |
||
127 | })); |
||
128 | continue; |
||
129 | } |
||
130 | throw new \Exception('Trying to add team which is not an instance of Team class'); |
||
131 | } |
||
132 | return $this; |
||
133 | } |
||
134 | public function team(string $name = '') { |
||
138 | } |
||
139 | public function getTeams(bool $ordered = false, $ordering = \POINTS) { |
||
140 | if (count($this->teams) === 0) { |
||
141 | $teams = []; |
||
142 | foreach ($this->categories as $category) { |
||
143 | $teams = array_merge($teams, $category->getTeams()); |
||
144 | } |
||
145 | foreach ($this->rounds as $round) { |
||
146 | $teams = array_merge($teams, $round->getTeams()); |
||
147 | } |
||
148 | $this->teams = $teams; |
||
149 | } |
||
150 | if ($ordered) $this->sortTeams($ordering); |
||
151 | return $this->teams; |
||
152 | } |
||
153 | public function sortTeams($ordering = \POINTS) { |
||
154 | $teams = []; |
||
155 | for ($i = count($this->rounds)-1; $i >= 0; $i--) { |
||
156 | $rTeams = array_filter($this->rounds[$i]->getTeams(true, $ordering), function($a) use ($teams) { return !in_array($a, $teams); }); |
||
157 | $teams = array_merge($teams, $rTeams); |
||
158 | } |
||
159 | $this->teams = $teams; |
||
160 | return $this->teams; |
||
161 | } |
||
162 | |||
163 | public function getGames() { |
||
169 | } |
||
170 | |||
171 | public function splitTeams(Round ...$wheres) { |
||
195 | } |
||
196 | |||
197 | public function genGamesSimulate(bool $returnTime = false) { |
||
198 | $games = Utilis\Simulator::simulateTournament($this); |
||
199 | |||
200 | if ($returnTime) return $this->getTournamentTime(); |
||
201 | return $games; |
||
202 | } |
||
203 | public function genGamesSimulateReal(bool $returnTime = false) { |
||
208 | } |
||
209 | |||
210 | } |
||
211 |