Completed
Push — master ( da6ead...647271 )
by Tomáš
02:03
created

Group   D

Complexity

Total Complexity 58

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 110
dl 0
loc 216
rs 4.5599
c 0
b 0
f 0
wmc 58

38 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 2 1
A setSkip() 0 3 1
A setId() 0 6 3
A setName() 0 2 1
A getName() 0 2 1
A __construct() 0 4 2
A getId() 0 2 1
A setTeam() 0 4 1
A disallowSkip() 0 3 1
A getSkip() 0 2 1
A allowSkip() 0 3 1
A addTeam() 0 11 4
A setMaxSize() 0 3 1
A getOrdering() 0 2 1
A setOrder() 0 3 1
A game() 0 4 1
A isProgressed() 0 2 1
A getInGame() 0 2 1
A addGame() 0 11 4
A genGames() 0 3 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 10 4
A isPlayed() 0 6 4
A getOrder() 0 2 1
A setOrdering() 0 4 2
A team() 0 5 1
A setType() 0 3 1
A progress() 0 3 2
A sortTeams() 0 4 2
A getMaxSize() 0 2 1
A getTeams() 0 8 1
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
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() {
33
		return $this->name;
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
		// APPLY FILTERS
90
		$filter = new Filter($this, $filters);
91
		$filter->filter($teams);
92
93
		return $teams;
94
	}
95
96
	public function team(string $name = '') {
97
		$t = new Team($name);
98
		$this->teams[] = $t;
99
		$t->addGroupResults($this);
100
		return $t;
101
	}
102
	public function sortTeams(array $filters = [], $ordering = null) {
103
		if (!isset($ordering)) $ordering = $this->ordering;
104
		Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering);
105
		return $this->getTeams($filters);
106
	}
107
108
	public function setMaxSize(int $size) {
109
		$this->generator->setMaxSize($size);
110
		return $this;
111
	}
112
	public function getMaxSize() {
113
		return $this->generator->getMaxSize();
114
	}
115
116
	public function setType(string $type = \TournamentGenerator\Constants::ROUND_ROBIN) {
117
		$this->generator->setType($type);
118
		return $this;
119
	}
120
	public function getType() {
121
		return $this->generator->getType();
122
	}
123
124
	public function setOrder(int $order) {
125
		$this->order = $order;
126
		return $this;
127
	}
128
	public function getOrder() {
129
		return $this->order;
130
	}
131
132
	public function setOrdering(string $ordering = \TournamentGenerator\Constants::POINTS) {
133
		if (!in_array($ordering, \TournamentGenerator\Constants::OrderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering);
134
		$this->ordering = $ordering;
135
		return $this;
136
	}
137
	public function getOrdering() {
138
		return $this->ordering;
139
	}
140
141
	public function setInGame(int $inGame) {
142
		$this->generator->setInGame($inGame);
143
		return $this;
144
	}
145
	public function getInGame() {
146
		return $this->generator->getInGame();
147
	}
148
149
	public function addProgression(Progression $progression) {
150
		$this->progressions[] = $progression;
151
		return $this;
152
	}
153
	public function progression(Group $to, int $start = 0, int $len = null) {
154
		$p = new Progression($this, $to, $start, $len);
155
		$this->progressions[] = $p;
156
		return $p;
157
	}
158
	public function progress(bool $blank = false) {
159
		foreach ($this->progressions as $progression) {
160
			$progression->progress($blank);
161
		}
162
	}
163
	public function addProgressed(...$teams) {
164
		foreach ($teams as $team) {
165
			if ($team instanceOf Team) $this->progressed[] = $team;
166
			elseif (gettype($team) === 'array') {
167
				$this->progressed = array_merge($this->progressed, array_filter($team, function($a) {
168
					return ($a instanceof Team);
169
				}));
170
			}
171
		}
172
		return $this;
173
	}
174
	public function isProgressed(Team $team) {
175
		return in_array($team, $this->progressed);
176
	}
177
178
	public function genGames() {
179
		$this->generator->genGames();
180
		return $this->games;
181
	}
182
183
	public function game(array $teams = []) {
184
		$g = new Game($teams, $this);
185
		$this->games[] = $g;
186
		return $g;
187
	}
188
	public function addGame(...$games){
189
		foreach ($games as $key => $game) {
190
			if (gettype($game) === 'array') {
191
				unset($games[$key]);
192
				$this->games = array_merge($this->games, array_filter($game, function($a){ return ($a instanceof Game); }));
193
				continue;
194
			}
195
			if (!$game instanceof Game) throw new \Exception('Trying to add game which is not instance of Game object.');
196
			$this->games[] = $game;
197
		}
198
		return $this;
199
	}
200
	public function getGames() {
201
		return $this->games;
202
	}
203
	public function orderGames() {
204
		if (count($this->games) <= 4) return $this->games;
205
		$this->games = $this->generator->orderGames();
206
		return $this->games;
207
	}
208
209
	public function simulate(array $filters = [], bool $reset = true) {
210
		return Utilis\Simulator::simulateGroup($this, $filters, $reset);
211
	}
212
	public function resetGames() {
213
		foreach ($this->getGames() as $game) {
214
			$game->resetResults();
215
		}
216
		return $this;
217
	}
218
	public function isPlayed(){
219
		if (count($this->games) === 0) return false;
220
		foreach ($this->games as $game) {
221
			if (!$game->isPlayed()) return false;
222
		}
223
		return true;
224
	}
225
226
}
227