Total Complexity | 81 |
Total Lines | 320 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Generator 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 Generator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Generator |
||
9 | { |
||
10 | |||
11 | private $group = null; |
||
12 | private $type = \TournamentGenerator\R_R; // TYPE OF ROUND TO CREATE A LAYOUT |
||
13 | private $inGame = 2; // NUMBER OF TEAMS IN ONE GAME - 2/3/4 |
||
14 | private $maxSize = 4; // MAX SIZE OF GROUP BEFORE SPLIT |
||
15 | private $allowSkip = false; // IF IS NUMBER OF TEAMS LESS THAN $this->inGame THEN SKIP PLAYING THIS GROUP |
||
16 | private $games = []; // ARRAY OF GAME OBJECTS |
||
|
|||
17 | |||
18 | function __construct(Group $group) { |
||
19 | $this->group = $group; |
||
20 | } |
||
21 | |||
22 | public function allowSkip(){ |
||
23 | $this->allowSkip = true; |
||
24 | return $this; |
||
25 | } |
||
26 | public function disallowSkip(){ |
||
27 | $this->allowSkip = false; |
||
28 | return $this; |
||
29 | } |
||
30 | public function setSkip(bool $skip) { |
||
31 | $this->allowSkip = $skip; |
||
32 | return $this; |
||
33 | } |
||
34 | public function getSkip() { |
||
35 | return $this->allowSkip; |
||
36 | } |
||
37 | |||
38 | |||
39 | public function setType(/** @scrutinizer ignore-all */ string $type = \TournamentGenerator\R_R) { |
||
40 | if (in_array($type, \TournamentGenerator\groupTypes)) $this->type = $type; |
||
41 | else throw new \Exception('Unknown group type: '.$type); |
||
42 | return $this; |
||
43 | } |
||
44 | public function getType() { |
||
45 | return $this->type; |
||
46 | } |
||
47 | |||
48 | public function setInGame(int $inGame) { |
||
49 | if ($inGame < 2 || $inGame > 4) throw new \Exception('Expected 2,3 or 4 as inGame '.$inGame.' given'); |
||
50 | $this->inGame = $inGame; |
||
51 | return $this; |
||
52 | } |
||
53 | public function getInGame() { |
||
54 | return $this->inGame; |
||
55 | } |
||
56 | |||
57 | public function setMaxSize(int $maxSize) { |
||
58 | if ($maxSize < 2) throw new \Exception('Max group size has to be at least 2, '.$maxSize.' given'); |
||
59 | $this->maxSize = $maxSize; |
||
60 | return $this; |
||
61 | } |
||
62 | public function getMaxSize() { |
||
63 | return $this->maxSize; |
||
64 | } |
||
65 | |||
66 | public function genGames() { |
||
67 | switch ($this->type) { |
||
68 | case \TournamentGenerator\R_R:{ |
||
69 | $this->group->addGame($this->r_rGames()); |
||
70 | break;} |
||
71 | case \TournamentGenerator\TWO_TWO: |
||
72 | $teams = $this->group->getTeams(); |
||
73 | $discard = []; |
||
74 | shuffle($teams); |
||
75 | $count = count($teams); |
||
76 | while (count($teams) % $this->inGame !== 0) { $discard[] = array_shift($teams); } |
||
77 | |||
78 | while (count($teams) > 0) { |
||
79 | $tInGame = []; |
||
80 | for ($i=0; $i < $this->inGame; $i++) { $tInGame[] = array_shift($teams); } |
||
81 | $this->group->game($tInGame); |
||
82 | } |
||
83 | |||
84 | if (count($discard) > 0 && !$this->allowSkip) throw new \Exception('Couldn\'t make games with all teams. Expected k*'.$this->inGame.' teams '.$count.' teams given - discarting '.count($discard).' teams ('.implode(', ', $discard).') in group '.$this->group.' - allow skip '.($this->allowSkip ? 'True' : 'False')); |
||
85 | break; |
||
86 | case \TournamentGenerator\COND_SPLIT: |
||
87 | $games = []; |
||
88 | $teams = $this->group->getTeams(); |
||
89 | if (count($teams) > $this->maxSize) { |
||
90 | $groups = array_chunk($teams, /** @scrutinizer ignore-type */ ceil(count($teams)/ceil(count($teams)/$this->maxSize))); // SPLIT TEAMS INTO GROUP OF MAXIMUM SIZE OF $this->maxSize |
||
91 | foreach ($groups as $group) { $games[] = $this->r_rGames($group); } |
||
92 | $g = 0; |
||
93 | foreach ($games as $group) { |
||
94 | $g += count($group); |
||
95 | } |
||
96 | while ($g > 0) { |
||
97 | foreach ($games as $key => $group) { |
||
98 | $this->group->addGame(array_shift($games[$key])); |
||
99 | if (count($games[$key]) === 0) unset($games[$key]); |
||
100 | $g--; |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | else $this->group->addGame($this->r_rGames()); |
||
105 | break; |
||
106 | } |
||
107 | return $this->group->getGames(); |
||
108 | } |
||
109 | public function r_rGames(array $teams = []) { |
||
110 | $games = []; |
||
111 | if (count($teams) === 0) $teams = $this->group->getTeams(); |
||
112 | switch ($this->inGame) { |
||
113 | case 2: |
||
114 | $games = Generator::circle_genGames2($teams, $this->group); |
||
115 | break; |
||
116 | case 3:{ |
||
117 | $teamsB = $teams; |
||
118 | while (count($teamsB) >= 3) { |
||
119 | $lockedTeam = array_shift($teamsB); |
||
120 | $gamesTemp = Generator::circle_genGames2($teamsB, $this->group); |
||
121 | foreach ($gamesTemp as $game) { |
||
122 | $game->addTeam($lockedTeam); |
||
123 | } |
||
124 | $games = array_merge($games, $gamesTemp); |
||
125 | } |
||
126 | break;} |
||
127 | case 4:{ |
||
128 | $teamsB = $teams; |
||
129 | $lockedTeam1 = array_shift($teamsB); |
||
130 | while (count($teamsB) >= 4) { |
||
131 | $teamsB2 = $teamsB; |
||
132 | while (count($teamsB2) >= 3) { |
||
133 | $lockedTeam2 = array_shift($teamsB2); |
||
134 | $gamesTemp = Generator::circle_genGames2($teamsB2, $this->group); |
||
135 | foreach ($gamesTemp as $game) { |
||
136 | $game->addTeam($lockedTeam1, $lockedTeam2); |
||
137 | } |
||
138 | $games = array_merge($games, $gamesTemp); |
||
139 | } |
||
140 | $lockedTeam1 = array_shift($teamsB); |
||
141 | } |
||
142 | $games[] = new \TournamentGenerator\Game(array_merge([$lockedTeam1], $teamsB), $this->group); |
||
143 | break;} |
||
144 | } |
||
145 | return $games; |
||
146 | } |
||
147 | |||
148 | public function orderGames() { |
||
149 | $sorter = new \Sorter\Games($this, $this->group); |
||
150 | |||
151 | return $sorter->orderGames(); |
||
152 | } |
||
153 | |||
154 | // GENERATES A ROBIN-ROBIN BRACKET |
||
155 | public static function circle_genGames2(array $teams = [], Group $group) { |
||
169 | |||
170 | } |
||
171 | // CREATE GAMES FROM BRACKET |
||
172 | public static function circle_saveBracket(array $teams, Group $group) { |
||
189 | |||
190 | } |
||
191 | // ROTATE TEAMS IN BRACKET |
||
192 | public static function circle_rotateBracket(array $teams) { |
||
193 | |||
194 | $first = array_shift($teams); // THE FIRST TEAM REMAINS FIRST |
||
195 | $last = array_shift($teams); // THE SECOND TEAM MOVES TO LAST PLACE |
||
196 | |||
200 | |||
201 | } |
||
202 | |||
203 | } |
||
204 |