Passed
Push — master ( c5fe27...4f1a31 )
by Tomáš
01:38
created
src/classes/class_group.php 1 patch
Braces   +147 added lines, -69 removed lines patch added patch discarded remove patch
@@ -33,27 +33,40 @@  discard block
 block discarded – undo
33 33
 		foreach ($settings as $key => $value) {
34 34
 			switch ($key) {
35 35
 				case 'name':
36
-					if (gettype($value) !== 'string') throw new \Exception('Expected string as group name '.gettype($value).' given');
36
+					if (gettype($value) !== 'string') {
37
+						throw new \Exception('Expected string as group name '.gettype($value).' given');
38
+					}
37 39
 					$this->name = $value;
38 40
 					break;
39 41
 				case 'type':
40
-					if (!in_array($value, groupTypes)) throw new \Exception('Unknown group type: '.$value);
42
+					if (!in_array($value, groupTypes)) {
43
+						throw new \Exception('Unknown group type: '.$value);
44
+					}
41 45
 					$this->type = $value;
42 46
 					break;
43 47
 				case 'ordering':
44
-					if (!in_array($value, orderingTypes)) throw new \Exception('Unknown group ordering: '.$value);
48
+					if (!in_array($value, orderingTypes)) {
49
+						throw new \Exception('Unknown group ordering: '.$value);
50
+					}
45 51
 					$this->ordering = $value;
46 52
 					break;
47 53
 				case 'inGame':
48
-					if (gettype($value) !== 'integer') throw new \Exception('Expected integer as inGame '.gettype($value).' given');
49
-					else if ($value < 2 || $value > 4) throw new \Exception('Expected 2,3 or 4 as inGame '.$value.' given');
54
+					if (gettype($value) !== 'integer') {
55
+						throw new \Exception('Expected integer as inGame '.gettype($value).' given');
56
+					} else if ($value < 2 || $value > 4) {
57
+						throw new \Exception('Expected 2,3 or 4 as inGame '.$value.' given');
58
+					}
50 59
 					$this->inGame = $value;
51 60
 					break;
52 61
 				case 'maxSize':
53
-					if (gettype($value) === 'integer') $this->maxSize = $value;
62
+					if (gettype($value) === 'integer') {
63
+						$this->maxSize = $value;
64
+					}
54 65
 					break;
55 66
 				case 'order':
56
-					if (gettype($value) === 'integer') $this->order = $value;
67
+					if (gettype($value) === 'integer') {
68
+						$this->order = $value;
69
+					}
57 70
 					break;
58 71
 			}
59 72
 		}
@@ -92,10 +105,11 @@  discard block
 block discarded – undo
92 105
 					'second' => 0,
93 106
 					'third'  => 0
94 107
 				];
95
-			}
96
-			elseif (gettype($team) === 'array') {
108
+			} elseif (gettype($team) === 'array') {
97 109
 				foreach ($team as $team2) {
98
-					if ($team2 instanceof Team) $this->teams[] = $team2;
110
+					if ($team2 instanceof Team) {
111
+						$this->teams[] = $team2;
112
+					}
99 113
 					$team2->groupResults[$this->id] = [
100 114
 						'group' => $this,
101 115
 						'points' => 0,
@@ -107,16 +121,20 @@  discard block
 block discarded – undo
107 121
 						'third'  => 0
108 122
 					];
109 123
 				}
124
+			} else {
125
+				throw new \Exception('Trying to add team which is not an instance of Team class');
110 126
 			}
111
-			else throw new \Exception('Trying to add team which is not an instance of Team class');
112 127
 		}
113 128
 		return $this;
114 129
 	}
115 130
 	public function getTeams($filters = []) {
116 131
 		$teams = $this->teams;
117 132
 
118
-		if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) $filters = [$filters];
119
-		elseif (gettype($filters) !== 'array') $filters = [];
133
+		if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) {
134
+			$filters = [$filters];
135
+		} elseif (gettype($filters) !== 'array') {
136
+			$filters = [];
137
+		}
120 138
 
121 139
 		// APPLY FILTERS
122 140
 		foreach ($filters as $key => $filter) {
@@ -124,27 +142,31 @@  discard block
 block discarded – undo
124 142
 				switch (strtolower($key)) {
125 143
 					case 'and':
126 144
 						foreach ($teams as $tkey => $team) {
127
-							if (!$this->filterAnd($team, $filter)) unset($teams[$tkey]); // IF FILTER IS NOT VALIDATED REMOVE TEAM FROM RETURN ARRAY
145
+							if (!$this->filterAnd($team, $filter)) {
146
+								unset($teams[$tkey]);
147
+							}
148
+							// IF FILTER IS NOT VALIDATED REMOVE TEAM FROM RETURN ARRAY
128 149
 						}
129 150
 						break;
130 151
 					case 'or':
131 152
 						foreach ($teams as $tkey => $team) {
132
-							if (!$this->filterOr($team, $filter)) unset($teams[$tkey]); // IF FILTER IS NOT VALIDATED REMOVE TEAM FROM RETURN ARRAY
153
+							if (!$this->filterOr($team, $filter)) {
154
+								unset($teams[$tkey]);
155
+							}
156
+							// IF FILTER IS NOT VALIDATED REMOVE TEAM FROM RETURN ARRAY
133 157
 						}
134 158
 						break;
135 159
 					default:
136 160
 						throw new \Exception('Unknown opperand type "'.$key.'". Expected "and" or "or".');
137 161
 						break;
138 162
 				}
139
-			}
140
-			elseif ($filter instanceof TeamFilter) {
163
+			} elseif ($filter instanceof TeamFilter) {
141 164
 				foreach ($teams as $tkey => $team) {
142 165
 					if (!$filter->validate($team, $this->id, 'sum', $this)) {
143 166
 						unset($teams[$tkey]); // IF FILTER IS NOT VALIDATED REMOVE TEAM FROM RETURN ARRAY
144 167
 					}
145 168
 				}
146
-			}
147
-			else {
169
+			} else {
148 170
 				throw new \Exception('Filer ['.$key.'] is not an instance of TeamFilter class');
149 171
 			}
150 172
 		}
@@ -155,20 +177,24 @@  discard block
 block discarded – undo
155 177
 			if (gettype($value) === 'array') {
156 178
 				switch (strtolower($key)) {
157 179
 					case 'and':
158
-						if ($this->filterAnd($team, $value)) return false;
180
+						if ($this->filterAnd($team, $value)) {
181
+							return false;
182
+						}
159 183
 						break;
160 184
 					case 'or':
161
-						if ($this->filterOr($team, $value)) return false;
185
+						if ($this->filterOr($team, $value)) {
186
+							return false;
187
+						}
162 188
 						break;
163 189
 					default:
164 190
 						throw new \Exception('Unknown opperand type "'.$key.'". Expected "and" or "or".');
165 191
 						break;
166 192
 				}
167
-			}
168
-			elseif ($value instanceof TeamFilter) {
169
-				if (!$value->validate($team, $this->id, 'sum', $this)) return false;
170
-			}
171
-			else {
193
+			} elseif ($value instanceof TeamFilter) {
194
+				if (!$value->validate($team, $this->id, 'sum', $this)) {
195
+					return false;
196
+				}
197
+			} else {
172 198
 				throw new \Exception('Filer ['.$key.'] is not an instance of TeamFilter class');
173 199
 			}
174 200
 		}
@@ -179,20 +205,24 @@  discard block
 block discarded – undo
179 205
 			if (gettype($value) === 'array') {
180 206
 				switch (strtolower($key)) {
181 207
 					case 'and':
182
-						if ($this->filterAnd($team, $value)) return true;
208
+						if ($this->filterAnd($team, $value)) {
209
+							return true;
210
+						}
183 211
 						break;
184 212
 					case 'or':
185
-						if ($this->filterOr($team, $value)) return true;
213
+						if ($this->filterOr($team, $value)) {
214
+							return true;
215
+						}
186 216
 						break;
187 217
 					default:
188 218
 						throw new \Exception('Unknown opperand type "'.$key.'". Expected "and" or "or".');
189 219
 						break;
190 220
 				}
191
-			}
192
-			elseif ($value instanceof TeamFilter) {
193
-				if (!$value->validate($team, $this->id, 'sum', $this)) return true;
194
-			}
195
-			else {
221
+			} elseif ($value instanceof TeamFilter) {
222
+				if (!$value->validate($team, $this->id, 'sum', $this)) {
223
+					return true;
224
+				}
225
+			} else {
196 226
 				throw new \Exception('Filer ['.$key.'] is not an instance of TeamFilter class');
197 227
 			}
198 228
 		}
@@ -214,16 +244,22 @@  discard block
 block discarded – undo
214 244
 		return $t;
215 245
 	}
216 246
 	public function setType(string $type = R_R) {
217
-		if (in_array($type, groupTypes)) $this->type = $type;
218
-		else throw new \Exception('Unknown group type: '.$type);
247
+		if (in_array($type, groupTypes)) {
248
+			$this->type = $type;
249
+		} else {
250
+			throw new \Exception('Unknown group type: '.$type);
251
+		}
219 252
 		return $this;
220 253
 	}
221 254
 	public function getType() {
222 255
 		return $this->type;
223 256
 	}
224 257
 	public function setOrdering(string $ordering = POINTS) {
225
-		if (in_array($ordering, orderingTypes)) $this->ordering = $ordering;
226
-		else throw new \Exception('Unknown group ordering: '.$ordering);
258
+		if (in_array($ordering, orderingTypes)) {
259
+			$this->ordering = $ordering;
260
+		} else {
261
+			throw new \Exception('Unknown group ordering: '.$ordering);
262
+		}
227 263
 		return $this;
228 264
 	}
229 265
 	public function getOrdering() {
@@ -233,14 +269,20 @@  discard block
 block discarded – undo
233 269
 		switch ($this->ordering) {
234 270
 			case POINTS:{
235 271
 				usort($this->teams, function($a, $b) {
236
-					if ($a->groupResults[$this->id]["points"] === $b->groupResults[$this->id]["points"] && $a->groupResults[$this->id]["score"] === $b->groupResults[$this->id]["score"]) return 0;
237
-					if ($a->groupResults[$this->id]["points"] === $b->groupResults[$this->id]["points"]) return ($a->groupResults[$this->id]["score"] > $b->groupResults[$this->id]["score"] ? -1 : 1);
272
+					if ($a->groupResults[$this->id]["points"] === $b->groupResults[$this->id]["points"] && $a->groupResults[$this->id]["score"] === $b->groupResults[$this->id]["score"]) {
273
+						return 0;
274
+					}
275
+					if ($a->groupResults[$this->id]["points"] === $b->groupResults[$this->id]["points"]) {
276
+						return ($a->groupResults[$this->id]["score"] > $b->groupResults[$this->id]["score"] ? -1 : 1);
277
+					}
238 278
 					return ($a->groupResults[$this->id]["points"] > $b->groupResults[$this->id]["points"] ? -1 : 1);
239 279
 				});
240 280
 				break;}
241 281
 			case SCORE:{
242 282
 				usort($this->teams, function($a, $b) {
243
-					if ($a->groupResults[$this->id]["score"] === $b->groupResults[$this->id]["score"]) return 0;
283
+					if ($a->groupResults[$this->id]["score"] === $b->groupResults[$this->id]["score"]) {
284
+						return 0;
285
+					}
244 286
 					return ($a->groupResults[$this->id]["score"] > $b->groupResults[$this->id]["score"] ? -1 : 1);
245 287
 				});
246 288
 				break;}
@@ -249,10 +291,14 @@  discard block
 block discarded – undo
249 291
 	}
250 292
 	public function setInGame(int $inGame) {
251 293
 		if (gettype($inGame) === 'integer') {
252
-			if ($inGame === 2 || $inGame === 3 || $inGame === 4) $this->inGame = $inGame;
253
-			else throw new \Exception('Expected 2,3 or 4 as inGame '.$inGame.' given');
294
+			if ($inGame === 2 || $inGame === 3 || $inGame === 4) {
295
+				$this->inGame = $inGame;
296
+			} else {
297
+				throw new \Exception('Expected 2,3 or 4 as inGame '.$inGame.' given');
298
+			}
299
+		} else {
300
+			throw new \Exception('Expected integer as inGame '.gettype($inGame).' given');
254 301
 		}
255
-		else throw new \Exception('Expected integer as inGame '.gettype($inGame).' given');
256 302
 	}
257 303
 	public function getInGame() {
258 304
 		return $this->inGame;
@@ -280,16 +326,13 @@  discard block
 block discarded – undo
280 326
 		foreach ($teams as $team) {
281 327
 			if ($team instanceOf Team) {
282 328
 				$this->progressed[] = $team->id;
283
-			}
284
-			elseif (gettype($team) === 'string' || gettype($team) === 'integer') {
329
+			} elseif (gettype($team) === 'string' || gettype($team) === 'integer') {
285 330
 				$this->progressed[] = $team;
286
-			}
287
-			elseif (gettype($team) === 'array') {
331
+			} elseif (gettype($team) === 'array') {
288 332
 				foreach ($team as $teamInner) {
289 333
 					if ($teamInner instanceOf Team) {
290 334
 						$this->progressed[] = $teamInner->id;
291
-					}
292
-					elseif (gettype($teamInner) === 'string' || gettype($teamInner) === 'integer') {
335
+					} elseif (gettype($teamInner) === 'string' || gettype($teamInner) === 'integer') {
293 336
 						$this->progressed[] = $teamInner;
294 337
 					}
295 338
 				}
@@ -325,7 +368,9 @@  discard block
 block discarded – undo
325 368
 					$this->game($tInGame);
326 369
 				}
327 370
 
328
-				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.' - allow skip '.($this->allowSkip ? 'True' : 'False'));
371
+				if (count($discard) > 0 && !$this->allowSkip) {
372
+					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.' - allow skip '.($this->allowSkip ? 'True' : 'False'));
373
+				}
329 374
 				break;
330 375
 			case COND_SPLIT:
331 376
 				$games = [];
@@ -341,12 +386,15 @@  discard block
 block discarded – undo
341 386
 					while ($g > 0) {
342 387
 						foreach ($games as $key => $group) {
343 388
 							$this->games[] = array_shift($games[$key]);
344
-							if (count($games[$key]) === 0) unset($games[$key]);
389
+							if (count($games[$key]) === 0) {
390
+								unset($games[$key]);
391
+							}
345 392
 							$g--;
346 393
 						}
347 394
 					}
395
+				} else {
396
+					$this->games = $this->r_rGames();
348 397
 				}
349
-				else $this->games = $this->r_rGames();
350 398
 				break;
351 399
 		}
352 400
 		return $this->games;
@@ -358,8 +406,11 @@  discard block
 block discarded – undo
358 406
 	}
359 407
 	public function addGame(Game ...$games){
360 408
 		foreach ($games as $game) {
361
-			if ($game instanceof Game) $this->games[] = $game;
362
-			else throw new \Exception('Trying to add game which is not instance of Game object.');
409
+			if ($game instanceof Game) {
410
+				$this->games[] = $game;
411
+			} else {
412
+				throw new \Exception('Trying to add game which is not instance of Game object.');
413
+			}
363 414
 		}
364 415
 		return $this;
365 416
 	}
@@ -387,21 +438,30 @@  discard block
 block discarded – undo
387 438
 				$teams[$team->id] = 0;
388 439
 			}
389 440
 			foreach (end($this->games)->getTeams() as $team) {
390
-				if (!isset($teams[$team->id])) $teams[$team->id] = 4;
391
-				else $teams[$team->id] += 4;
441
+				if (!isset($teams[$team->id])) {
442
+					$teams[$team->id] = 4;
443
+				} else {
444
+					$teams[$team->id] += 4;
445
+				}
392 446
 			}
393 447
 			$g = prev($this->games);
394 448
 			if ($g instanceof Game) {
395 449
 				foreach ($g->getTeams() as $team) {
396
-					if (!isset($teams[$team->id])) $teams[$team->id] = 2;
397
-					else $teams[$team->id] += 2;
450
+					if (!isset($teams[$team->id])) {
451
+						$teams[$team->id] = 2;
452
+					} else {
453
+						$teams[$team->id] += 2;
454
+					}
398 455
 				}
399 456
 			}
400 457
 			$g = prev($this->games);
401 458
 			if ($g instanceof Game) {
402 459
 				foreach ($g->getTeams() as $team) {
403
-					if (!isset($teams[$team->id])) $teams[$team->id] = 1;
404
-					else $teams[$team->id]++;
460
+					if (!isset($teams[$team->id])) {
461
+						$teams[$team->id] = 1;
462
+					} else {
463
+						$teams[$team->id]++;
464
+					}
405 465
 				}
406 466
 			}
407 467
 
@@ -423,7 +483,9 @@  discard block
 block discarded – undo
423 483
 					break;
424 484
 				}
425 485
 			}
426
-			if ($found) continue;
486
+			if ($found) {
487
+				continue;
488
+			}
427 489
 
428 490
 			// CYCLE 2
429 491
 			// ! TEAM WHICH PLAYED IN LAST TWO GAMES (NOT 6 or 7)
@@ -444,7 +506,9 @@  discard block
 block discarded – undo
444 506
 					break;
445 507
 				}
446 508
 			}
447
-			if ($found) continue;
509
+			if ($found) {
510
+				continue;
511
+			}
448 512
 
449 513
 			// CYCLE 3
450 514
 			// ! TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7)
@@ -475,7 +539,9 @@  discard block
 block discarded – undo
475 539
 					break;
476 540
 				}
477 541
 			}
478
-			if ($found) continue;
542
+			if ($found) {
543
+				continue;
544
+			}
479 545
 
480 546
 			// CYCLE 4
481 547
 			// ! TEAM WHICH PLAYED IN LAST THREE GAMES (NOT 7)
@@ -496,7 +562,9 @@  discard block
 block discarded – undo
496 562
 					break;
497 563
 				}
498 564
 			}
499
-			if ($found) continue;
565
+			if ($found) {
566
+				continue;
567
+			}
500 568
 
501 569
 			// CYCLE 5
502 570
 			// TEAMS THAT DIDN'T PLAY IN LAST GAME WILL PLAY THIS GAME (< 4)
@@ -521,7 +589,9 @@  discard block
 block discarded – undo
521 589
 					break;
522 590
 				}
523 591
 			}
524
-			if ($found) continue;
592
+			if ($found) {
593
+				continue;
594
+			}
525 595
 
526 596
 			// CYCLE 6
527 597
 			// FIRST AVAILABLE GAME
@@ -532,7 +602,9 @@  discard block
 block discarded – undo
532 602
 	}
533 603
 	public function r_rGames(array $teams = []) {
534 604
 		$games = [];
535
-		if (count($teams) === 0) $teams = $this->teams;
605
+		if (count($teams) === 0) {
606
+			$teams = $this->teams;
607
+		}
536 608
 		switch ($this->inGame) {
537 609
 			case 2:
538 610
 				$games = circle_genGames2($teams, $this);
@@ -580,7 +652,9 @@  discard block
 block discarded – undo
580 652
 			$game->setResults($results);
581 653
 		}
582 654
 		$return = $this->sortTeams($filters);
583
-		if (!$reset) return $return;
655
+		if (!$reset) {
656
+			return $return;
657
+		}
584 658
 		foreach ($this->getGames() as $game) {
585 659
 			$game->resetResults();
586 660
 		}
@@ -588,13 +662,17 @@  discard block
 block discarded – undo
588 662
 	}
589 663
 	public function resetGames() {
590 664
 		foreach ($this->getGames() as $game) {
591
-			if (isset($game)) $game->resetResults();
665
+			if (isset($game)) {
666
+				$game->resetResults();
667
+			}
592 668
 		}
593 669
 		return $this;
594 670
 	}
595 671
 	public function isPlayed(){
596 672
 		foreach ($this->games as $game) {
597
-			if ((isset($game) || !$this->getSkip()) && !$game->isPlayed()) return false;
673
+			if ((isset($game) || !$this->getSkip()) && !$game->isPlayed()) {
674
+				return false;
675
+			}
598 676
 		}
599 677
 		return true;
600 678
 	}
Please login to merge, or discard this patch.