| Total Complexity | 69 |
| Total Lines | 230 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| 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 | } |
||
| 123 | elseif (gettype($team) === 'array') { |
||
| 124 | foreach ($team as $team2) { |
||
| 125 | if ($team2 instanceof Team) $this->teams[] = $team2; |
||
| 126 | } |
||
| 127 | } |
||
| 128 | else throw new \Exception('Trying to add team which is not an instance of Team class'); |
||
| 129 | } |
||
| 130 | return $this; |
||
| 131 | } |
||
| 132 | public function team(string $name = '') { |
||
| 136 | } |
||
| 137 | public function getTeams(bool $ordered = false, $ordering = POINTS) { |
||
|
1 ignored issue
–
show
|
|||
| 138 | if (count($this->teams) === 0) { |
||
| 139 | $teams = []; |
||
| 140 | foreach ($this->categories as $category) { |
||
| 141 | $teams = array_merge($teams, $category->getTeams()); |
||
| 142 | } |
||
| 143 | foreach ($this->rounds as $round) { |
||
| 144 | $teams = array_merge($teams, $round->getTeams()); |
||
| 145 | } |
||
| 146 | $this->teams = $teams; |
||
| 147 | } |
||
| 148 | if ($ordered) { |
||
| 149 | $this->sortTeams($ordering); |
||
| 150 | } |
||
| 151 | return $this->teams; |
||
| 152 | } |
||
| 153 | public function sortTeams($ordering = POINTS) { |
||
|
1 ignored issue
–
show
|
|||
| 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() { |
||
| 164 | $games = []; |
||
| 165 | foreach ($this->getRounds() as $round) { |
||
| 166 | $games = array_merge($games, $round->getGames()); |
||
| 167 | } |
||
| 168 | return $games; |
||
| 169 | } |
||
| 170 | |||
| 171 | public function splitTeams(...$wheres) { |
||
| 172 | |||
| 173 | if (count($wheres) === 0) $wheres = array_merge($this->getRounds(), $this->getCategories()); |
||
| 174 | |||
| 175 | foreach ($wheres as $key => $value) { |
||
| 176 | if (gettype($value) === 'array') { |
||
| 177 | unset($wheres[$key]); |
||
| 178 | foreach ($value as $key2 => $value2) { |
||
| 179 | if (!$value2 instanceof Round && !$value2 instanceof Category) throw new \Exception('Trying to split teams to another object, that is not instance of Category or Round.'); |
||
| 180 | } |
||
| 181 | $wheres = array_merge($wheres, $value); |
||
| 182 | continue; |
||
| 183 | } |
||
| 184 | if (!$value instanceof Round && !$value instanceof Category) throw new \Exception('Trying to split teams to another object, that is not instance of Category or Round.'); |
||
| 185 | } |
||
| 186 | |||
| 187 | $teams = $this->getTeams(); |
||
| 188 | shuffle($teams); |
||
| 189 | |||
| 190 | while (count($teams) > 0) { |
||
| 191 | foreach ($wheres as $where) { |
||
| 192 | if (count($teams) > 0) $where->addTeam(array_shift($teams)); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | foreach ($wheres as $where) { |
||
| 196 | $where->splitTeams(); |
||
| 197 | } |
||
| 198 | return $this; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function genGamesSimulate(bool $returnTime = false) { |
||
| 202 | $games = []; |
||
| 203 | if (count($this->categories) > 0) { |
||
| 204 | foreach ($this->categories as $category) { |
||
| 205 | $games = array_merge($games, $category->genGamesSimulate()); |
||
| 206 | } |
||
| 207 | } |
||
| 208 | elseif (count($this->rounds) > 0) { |
||
| 209 | foreach ($this->rounds as $round) { |
||
| 210 | $games = array_merge($games, $round->genGames()); |
||
| 211 | $round->simulate()->progress(true); |
||
| 212 | } |
||
| 213 | foreach ($this->rounds as $round) { |
||
| 214 | $round->resetGames(); |
||
| 215 | } |
||
| 216 | } |
||
| 217 | else throw new \Exception('There are no rounds or categories to simulate games from.'); |
||
| 218 | if ($returnTime) return $this->getTournamentTime(); |
||
| 219 | return $games; |
||
| 220 | } |
||
| 221 | public function genGamesSimulateReal(bool $returnTime = false) { |
||
| 238 | } |
||
| 239 | |||
| 240 | } |
||
| 241 |