Total Complexity | 61 |
Total Lines | 219 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like Group 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 Group, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class Group |
||
9 | { |
||
10 | |||
11 | private $generator = null; |
||
12 | private $teams = []; // ARRAY OF TEAMS |
||
13 | private $progressed = []; // ARRAY OF TEAMS ALREADY PROGRESSED FROM THIS GROUP |
||
14 | private $name = ''; // DISPLAYABLE NAME |
||
15 | private $ordering = \TournamentGenerator\Constants::POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS |
||
16 | private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS |
||
17 | private $games = []; // ARRAY OF GAME OBJECTS |
||
18 | private $id = ''; // UNIQID OF GROUP FOR IDENTIFICATIONT |
||
19 | public $winPoints = 3; // POINTS AQUIRED FROM WINNING |
||
20 | public $drawPoints = 1; // POINTS AQUIRED FROM DRAW |
||
21 | public $lostPoints = 0; // POINTS AQUIRED FROM LOOSING |
||
22 | public $secondPoints = 2; // POINTS AQUIRED FROM BEING SECOND (APPLIES ONLY FOR 3 OR 4 INGAME VALUE) |
||
23 | public $thirdPoints = 1; // POINTS AQUIRED FROM BEING THIRD (APPLIES ONLY FOR 4 INGAME VALUE) |
||
24 | public $progressPoints = 50; // POINTS AQUIRED FROM PROGRESSING TO THE NEXT ROUND |
||
25 | private $order = 0; // ORDER OF GROUPS IN ROUND |
||
26 | |||
27 | function __construct(string $name, $id = null) { |
||
28 | $this->setName($name); |
||
29 | $this->generator = new Utilis\Generator($this); |
||
30 | $this->setId(isset($id) ? $id : uniqid()); |
||
31 | } |
||
32 | public function __toString() { |
||
34 | } |
||
35 | |||
36 | public function setName(string $name) { |
||
37 | $this->name = $name; |
||
38 | } |
||
39 | public function getName() { |
||
40 | return $this->name; |
||
41 | } |
||
42 | public function setId($id) { |
||
43 | if (!is_string($id) && !is_int($id)) { |
||
44 | $this->id = uniqid(); |
||
45 | throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int'); |
||
46 | } |
||
47 | $this->id = $id; |
||
48 | } |
||
49 | public function getId() { |
||
50 | return $this->id; |
||
51 | } |
||
52 | |||
53 | public function allowSkip(){ |
||
54 | $this->generator->allowSkip(); |
||
55 | return $this; |
||
56 | } |
||
57 | public function disallowSkip(){ |
||
58 | $this->generator->disallowSkip(); |
||
59 | return $this; |
||
60 | } |
||
61 | public function setSkip(bool $skip) { |
||
62 | $this->generator->setSkip($skip); |
||
63 | return $this; |
||
64 | } |
||
65 | public function getSkip() { |
||
66 | return $this->generator->getSkip(); |
||
67 | } |
||
68 | |||
69 | public function addTeam(...$teams) { |
||
70 | foreach ($teams as $team) { |
||
71 | if (is_array($team)) { |
||
72 | foreach ($team as $team2) { |
||
73 | $this->setTeam($team2); |
||
74 | } |
||
75 | continue; |
||
76 | } |
||
77 | $this->setTeam($team); |
||
78 | } |
||
79 | return $this; |
||
80 | } |
||
81 | private function setTeam(Team $team) { |
||
82 | $this->teams[] = $team; |
||
83 | $team->addGroupResults($this); |
||
84 | return $this; |
||
85 | } |
||
86 | public function getTeams(array $filters = []) { |
||
87 | $teams = $this->teams; |
||
88 | |||
89 | if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) $filters = [$filters]; |
||
90 | elseif (gettype($filters) !== 'array') $filters = []; |
||
91 | |||
92 | // APPLY FILTERS |
||
93 | $filter = new Filter($this, $filters); |
||
94 | $filter->filter($teams); |
||
95 | |||
96 | return $teams; |
||
97 | } |
||
98 | |||
99 | public function team(string $name = '') { |
||
100 | $t = new Team($name); |
||
101 | $this->teams[] = $t; |
||
102 | $t->addGroupResults($this); |
||
103 | return $t; |
||
104 | } |
||
105 | public function sortTeams(array $filters = [], $ordering = null) { |
||
106 | if (!isset($ordering)) $ordering = $this->ordering; |
||
107 | Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering); |
||
108 | return $this->getTeams($filters); |
||
109 | } |
||
110 | |||
111 | public function setMaxSize(int $size) { |
||
114 | } |
||
115 | public function getMaxSize() { |
||
116 | return $this->generator->getMaxSize(); |
||
117 | } |
||
118 | |||
119 | public function setType(string $type = \TournamentGenerator\Constants::ROUND_ROBIN) { |
||
120 | $this->generator->setType($type); |
||
121 | return $this; |
||
122 | } |
||
123 | public function getType() { |
||
124 | return $this->generator->getType(); |
||
125 | } |
||
126 | |||
127 | public function setOrder(int $order) { |
||
128 | $this->order = $order; |
||
129 | return $this; |
||
130 | } |
||
131 | public function getOrder() { |
||
132 | return $this->order; |
||
133 | } |
||
134 | |||
135 | public function setOrdering(string $ordering = \TournamentGenerator\Constants::POINTS) { |
||
136 | if (!in_array($ordering, \TournamentGenerator\Constants::OrderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering); |
||
137 | $this->ordering = $ordering; |
||
138 | return $this; |
||
139 | } |
||
140 | public function getOrdering() { |
||
142 | } |
||
143 | |||
144 | public function setInGame(int $inGame) { |
||
145 | $this->generator->setInGame($inGame); |
||
146 | return $this; |
||
147 | } |
||
148 | public function getInGame() { |
||
149 | return $this->generator->getInGame(); |
||
150 | } |
||
151 | |||
152 | public function addProgression(Progression $progression) { |
||
153 | $this->progressions[] = $progression; |
||
154 | return $this; |
||
155 | } |
||
156 | public function progression(Group $to, int $start = 0, int $len = null) { |
||
157 | $p = new Progression($this, $to, $start, $len); |
||
158 | $this->progressions[] = $p; |
||
159 | return $p; |
||
160 | } |
||
161 | public function progress(bool $blank = false) { |
||
162 | foreach ($this->progressions as $progression) { |
||
163 | $progression->progress($blank); |
||
164 | } |
||
165 | } |
||
166 | public function addProgressed(...$teams) { |
||
167 | foreach ($teams as $team) { |
||
168 | if ($team instanceOf Team) $this->progressed[] = $team; |
||
169 | elseif (gettype($team) === 'array') { |
||
170 | $this->progressed = array_merge($this->progressed, array_filter($team, function($a) { |
||
171 | return ($a instanceof Team); |
||
172 | })); |
||
173 | } |
||
174 | } |
||
175 | return $this; |
||
176 | } |
||
177 | public function isProgressed(Team $team) { |
||
178 | return in_array($team, $this->progressed); |
||
179 | } |
||
180 | |||
181 | public function genGames() { |
||
182 | $this->generator->genGames(); |
||
183 | return $this->games; |
||
184 | } |
||
185 | |||
186 | public function game(array $teams = []) { |
||
190 | } |
||
191 | public function addGame(...$games){ |
||
192 | foreach ($games as $key => $game) { |
||
193 | if (gettype($game) === 'array') { |
||
202 | } |
||
203 | public function getGames() { |
||
204 | return $this->games; |
||
205 | } |
||
206 | public function orderGames() { |
||
207 | if (count($this->games) <= 4) return $this->games; |
||
208 | $this->games = $this->generator->orderGames(); |
||
209 | return $this->games; |
||
210 | } |
||
211 | |||
212 | public function simulate(array $filters = [], bool $reset = true) { |
||
213 | return Utilis\Simulator::simulateGroup($this, $filters, $reset); |
||
214 | } |
||
215 | public function resetGames() { |
||
220 | } |
||
221 | public function isPlayed(){ |
||
222 | if (count($this->games) === 0) return false; |
||
223 | foreach ($this->games as $game) { |
||
224 | if (!$game->isPlayed()) return false; |
||
225 | } |
||
226 | return true; |
||
227 | } |
||
228 | |||
229 | } |
||
230 |