Completed
Push — master ( 2340fb...76470b )
by Tomáš
02:24
created

Group   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 222
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 108
c 1
b 1
f 0
dl 0
loc 222
ccs 137
cts 137
cp 1
rs 6
wmc 55

45 Methods

Rating   Name   Duplication   Size   Complexity  
A setSkip() 0 3 1
A __construct() 0 4 2
A disallowSkip() 0 3 1
A getSkip() 0 2 1
A allowSkip() 0 3 1
A setMaxSize() 0 3 1
A setLostPoints() 0 3 1
A getSecondPoints() 0 2 1
A getOrdering() 0 2 1
A getLostPoints() 0 2 1
A setOrder() 0 3 1
A setSecondPoints() 0 3 1
A setProgressPoints() 0 3 1
A game() 0 4 1
A isProgressed() 0 2 1
A getInGame() 0 2 1
A addGame() 0 5 2
A getProgressPoints() 0 2 1
A setWinPoints() 0 3 1
A genGames() 0 3 1
A getThirdPoints() 0 2 1
A setTeam() 0 4 1
A getDrawPoints() 0 2 1
A progression() 0 4 1
A resetGames() 0 5 2
A setInGame() 0 3 1
A getGames() 0 2 1
A simulate() 0 2 1
A addProgression() 0 3 1
A addProgressed() 0 8 2
A isPlayed() 0 3 2
A getOrder() 0 2 1
A setOrdering() 0 4 2
A team() 0 5 1
A setDrawPoints() 0 3 1
A setType() 0 3 1
A progress() 0 5 2
A sortTeams() 0 4 2
A getWinPoints() 0 2 1
A getMaxSize() 0 2 1
A setThirdPoints() 0 3 1
A addTeam() 0 5 1
A getTeams() 0 10 2
A orderGames() 0 4 2
A getType() 0 2 1

How to fix   Complexity   

Complex Class

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
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Group extends Base implements WithGeneratorSetters, WithSkipSetters, WithTeams
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 $ordering = \TournamentGenerator\Constants::POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS
15
	private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS
16
	private $games = []; // ARRAY OF GAME OBJECTS
17
	private $winPoints = 3; // POINTS AQUIRED FROM WINNING
18
	private $drawPoints = 1; // POINTS AQUIRED FROM DRAW
19
	private $lostPoints = 0; // POINTS AQUIRED FROM LOOSING
20
	private $secondPoints = 2; // POINTS AQUIRED FROM BEING SECOND (APPLIES ONLY FOR 3 OR 4 INGAME VALUE)
21
	private $thirdPoints = 1; // POINTS AQUIRED FROM BEING THIRD (APPLIES ONLY FOR 4 INGAME VALUE)
22
	private $progressPoints = 50; // POINTS AQUIRED FROM PROGRESSING TO THE NEXT ROUND
23
	private $order = 0; // ORDER OF GROUPS IN ROUND
24
25 136
	function __construct(string $name, $id = null) {
26 136
		$this->setName($name);
27 136
		$this->generator = new Utilis\Generator($this);
28 136
		$this->setId(isset($id) ? $id : uniqid());
29 136
	}
30
31 1
	public function allowSkip(){
32 1
		$this->generator->allowSkip();
33 1
		return $this;
34
	}
35 1
	public function disallowSkip(){
36 1
		$this->generator->disallowSkip();
37 1
		return $this;
38
	}
39 51
	public function setSkip(bool $skip) {
40 51
		$this->generator->setSkip($skip);
41 51
		return $this;
42
	}
43 2
	public function getSkip() {
44 2
		return $this->generator->getSkip();
45
	}
46
47 61
	public function addTeam(...$teams) {
48
		\array_walk_recursive($teams, function($team){
49 61
			$this->setTeam($team);
50 61
		});
51 61
		return $this;
52
	}
53 61
	private function setTeam(Team $team) {
54 61
		$this->teams[] = $team;
55 61
		$team->addGroupResults($this);
56 61
		return $this;
57
	}
58 89
	public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS, array $filters = []) {
59 89
		$teams = $this->teams;
60
61 89
		if ($ordered) return $this->sortTeams($ordering, $filters);
62
63
		// APPLY FILTERS
64 89
		$filter = new Filter([$this], $filters);
65 89
		$filter->filter($teams);
66
67 83
		return $teams;
68
	}
69 51
	public function team(string $name = '', $id = null) {
70 51
		$t = new Team($name, $id);
71 51
		$this->teams[] = $t;
72 51
		$t->addGroupResults($this);
73 51
		return $t;
74
	}
75 39
	public function sortTeams($ordering = null, array $filters = []) {
76 39
		if (!isset($ordering)) $ordering = $this->ordering;
77 39
		$this->teams = Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering);
78 39
		return $this->getTeams(false, null, $filters);
79
	}
80
81 1
	public function setWinPoints(int $points) {
82 1
		$this->winPoints = $points;
83 1
		return $this;
84
	}
85 73
	public function getWinPoints() {
86 73
		return $this->winPoints;
87
	}
88 1
	public function setDrawPoints(int $points) {
89 1
		$this->drawPoints = $points;
90 1
		return $this;
91
	}
92 16
	public function getDrawPoints() {
93 16
		return $this->drawPoints;
94
	}
95 1
	public function setLostPoints(int $points) {
96 1
		$this->lostPoints = $points;
97 1
		return $this;
98
	}
99 73
	public function getLostPoints() {
100 73
		return $this->lostPoints;
101
	}
102 1
	public function setSecondPoints(int $points) {
103 1
		$this->secondPoints = $points;
104 1
		return $this;
105
	}
106 11
	public function getSecondPoints() {
107 11
		return $this->secondPoints;
108
	}
109 1
	public function setThirdPoints(int $points) {
110 1
		$this->thirdPoints = $points;
111 1
		return $this;
112
	}
113 10
	public function getThirdPoints() {
114 10
		return $this->thirdPoints;
115
	}
116 1
	public function setProgressPoints(int $points) {
117 1
		$this->progressPoints = $points;
118 1
		return $this;
119
	}
120 17
	public function getProgressPoints() {
121 17
		return $this->progressPoints;
122
	}
123
124 7
	public function setMaxSize(int $size) {
125 7
		$this->generator->setMaxSize($size);
126 7
		return $this;
127
	}
128 1
	public function getMaxSize() {
129 1
		return $this->generator->getMaxSize();
130
	}
131
132 41
	public function setType(string $type = \TournamentGenerator\Constants::ROUND_ROBIN) {
133 41
		$this->generator->setType($type);
134 41
		return $this;
135
	}
136 1
	public function getType() {
137 1
		return $this->generator->getType();
138
	}
139
140 4
	public function setOrder(int $order) {
141 4
		$this->order = $order;
142 4
		return $this;
143
	}
144 41
	public function getOrder() {
145 41
		return $this->order;
146
	}
147
148 1
	public function setOrdering(string $ordering = \TournamentGenerator\Constants::POINTS) {
149 1
		if (!in_array($ordering, \TournamentGenerator\Constants::OrderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering);
150 1
		$this->ordering = $ordering;
151 1
		return $this;
152
	}
153 1
	public function getOrdering() {
154 1
		return $this->ordering;
155
	}
156
157 53
	public function setInGame(int $inGame) {
158 53
		$this->generator->setInGame($inGame);
159 53
		return $this;
160
	}
161 76
	public function getInGame() {
162 76
		return $this->generator->getInGame();
163
	}
164
165 1
	public function addProgression(Progression $progression) {
166 1
		$this->progressions[] = $progression;
167 1
		return $this;
168
	}
169 33
	public function progression(Group $to, int $start = 0, int $len = null) {
170 33
		$p = new Progression($this, $to, $start, $len);
171 33
		$this->progressions[] = $p;
172 33
		return $p;
173
	}
174 33
	public function progress(bool $blank = false) {
175 33
		foreach ($this->progressions as $progression) {
176 33
			$progression->progress($blank);
177
		}
178 33
		return $this;
179
	}
180 34
	public function addProgressed(...$teams) {
181
		$this->progressed = array_merge($this->progressed, array_map(function ($a) {return $a->getId();}, array_filter($teams, function($a){return $a instanceof Team;})));
182
		foreach (array_filter($teams, function($a){return is_array($a);}) as $team) {
183
			$this->progressed = array_merge($this->progressed, array_map(function ($a) {return $a->getId();}, array_filter($team, function($a) {
184 33
				return ($a instanceof Team);
185 33
			})));
186
		}
187 34
		return $this;
188
	}
189 7
	public function isProgressed(Team $team) {
190 7
		return in_array($team->getId(), $this->progressed);
191
	}
192
193 42
	public function genGames() {
194 42
		$this->generator->genGames();
195 41
		return $this->games;
196
	}
197
198 48
	public function game(array $teams = []) {
199 48
		$g = new Game($teams, $this);
200 48
		$this->games[] = $g;
201 48
		return $g;
202
	}
203 36
	public function addGame(...$games){
204
		array_walk_recursive($games, function($game){
205 36
			if ($game instanceof Game) $this->games[] = $game;
206 36
		});
207 36
		return $this;
208
	}
209 46
	public function getGames() {
210 46
		return $this->games;
211
	}
212 29
	public function orderGames() {
213 29
		if (count($this->games) <= 4) return $this->games;
214 23
		$this->games = $this->generator->orderGames();
215 23
		return $this->games;
216
	}
217
218 31
	public function simulate(array $filters = [], bool $reset = true) {
219 31
		return Utilis\Simulator::simulateGroup($this, $filters, $reset);
220
	}
221 15
	public function resetGames() {
222 15
		foreach ($this->getGames() as $game) {
223 15
			$game->resetResults();
224
		}
225 15
		return $this;
226
	}
227 31
	public function isPlayed(){
228 31
		if (count($this->games) === 0) return false;
229
		return count(array_filter($this->games, function($a){return $a->isPlayed();})) !== 0;
230
	}
231
232
}
233