Completed
Branch master (0e4a87)
by Tomáš
03:29 queued 01:30
created

Round   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 104
dl 0
loc 184
rs 6
c 1
b 0
f 0
wmc 55

22 Methods

Rating   Name   Duplication   Size   Complexity  
A getGroups() 0 3 1
A group() 0 4 1
A addGroup() 0 6 3
A __construct() 0 3 1
A __toString() 0 2 1
A getGames() 0 2 1
A progress() 0 5 2
A orderGroups() 0 3 1
A getSkip() 0 2 1
A getGroupsIds() 0 3 1
A getTeams() 0 12 4
A addTeam() 0 23 6
A setSkip() 0 3 1
A simulate() 0 6 3
B splitTeams() 0 22 8
A team() 0 4 1
A disallowSkip() 0 3 1
A resetGames() 0 5 2
A isPlayed() 0 5 3
A genGames() 0 7 2
B sortTeams() 0 19 10
A allowSkip() 0 3 1

How to fix   Complexity   

Complex Class

Complex classes like Round 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 Round, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Round
9
{
10
11
	public $name = '';
12
	public $id = '';
13
	private $groups = [];
14
	private $games = [];
15
	private $teams = [];
16
	private $allowSkip = false;
17
18
	function __construct(string $name = '') {
19
		$this->id = uniqid();
20
		$this->name = $name;
21
	}
22
	public function __toString() {
23
		return $this->name;
24
	}
25
26
	public function addGroup(Group ...$groups){
27
		foreach ($groups as $group) {
28
			if ($group instanceof Group) $this->groups[] = $group;
29
			else throw new \Exception('Trying to add group which is not an instance of Group class.');
30
		}
31
		return $this;
32
	}
33
	public function group(array $settings = []) {
34
		$g = new Group($settings);
35
		$this->groups[] = $g->setSkip($this->allowSkip);
36
		return $g;
37
	}
38
	public function getGroups(){
39
		$this->orderGroups();
40
		return $this->groups;
41
	}
42
	public function getGroupsIds() {
43
		$this->orderGroups();
44
		return array_map(function($a) { return $a->id; }, $this->groups);
45
	}
46
	public function orderGroups() {
47
		usort($this->groups, function($a, $b){
48
			return $a->order - $b->order;
49
		});
50
	}
51
52
	public function allowSkip(){
53
		$this->allowSkip = true;
54
		return $this;
55
	}
56
	public function disallowSkip(){
57
		$this->allowSkip = false;
58
		return $this;
59
	}
60
	public function setSkip(bool $skip = false) {
61
		$this->allowSkip = $skip;
62
		return $this;
63
	}
64
	public function getSkip() {
65
		return $this->allowSkip;
66
	}
67
68
	public function genGames(){
69
		$g = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $g is dead and can be removed.
Loading history...
70
		foreach ($this->groups as $group) {
71
			$group->genGames();
72
			$this->games = array_merge($this->games, $group->orderGames());
73
		}
74
		return $this->games;
75
	}
76
	public function getGames() {
77
		return $this->games;
78
	}
79
	public function isPlayed(){
80
		foreach ($this->groups as $group) {
81
			if (!$group->isPlayed()) return false;
82
		}
83
		return true;
84
	}
85
86
	public function addTeam(...$teams) {
87
		foreach ($teams as $team) {
88
			if ($team instanceof Team)  {
89
				$this->teams[] = $team;
90
			}
91
			elseif (gettype($team) === 'array') {
92
				foreach ($team as $team2) {
93
					if ($team2 instanceof Team) $this->teams[] = $team2;
94
					$team2->groupResults[$this->id] = [
95
						'group' => $this,
96
						'points' => 0,
97
						'score'  => 0,
98
						'wins'   => 0,
99
						'draws'  => 0,
100
						'losses' => 0,
101
						'second' => 0,
102
						'third'  => 0
103
					];
104
				}
105
			}
106
			else throw new \Exception('Trying to add team which is not an instance of Team class');
107
		}
108
		return $this;
109
	}
110
	public function team(string $name = '') {
111
		$t = new Team($name);
112
		$this->teams[] = $t;
113
		return $t;
114
	}
115
	public function getTeams(bool $ordered = false, $ordering = POINTS) {
1 ignored issue
show
Bug introduced by
The constant TournamentGenerator\POINTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
116
		if (count($this->teams) == 0) {
117
			$teams = [];
118
			foreach ($this->groups as $group) {
119
				$teams = array_merge($teams, $group->getTeams());
120
			}
121
			$this->teams = $teams;
122
		}
123
		if ($ordered) {
124
			$this->sortTeams($ordering);
125
		}
126
		return $this->teams;
127
	}
128
	public function sortTeams($ordering = POINTS) {
1 ignored issue
show
Bug introduced by
The constant TournamentGenerator\POINTS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
129
		$teams = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $teams is dead and can be removed.
Loading history...
130
		$groupsIds = $this->getGroupsIds();
131
		switch ($ordering) {
132
			case POINTS:{
133
				uasort($this->teams, function($a, $b) use ($groupsIds) {
134
					if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds) && $a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0;
135
					if ($a->sumPoints($groupsIds) === $b->sumPoints($groupsIds)) return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
136
					return ($a->sumPoints($groupsIds) > $b->sumPoints($groupsIds) ? -1 : 1);
137
				});
138
				break;}
139
			case SCORE:{
1 ignored issue
show
Bug introduced by
The constant TournamentGenerator\SCORE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
140
				uasort($this->teams, function($a, $b) use ($groupsIds) {
141
					if ($a->sumScore($groupsIds) === $b->sumScore($groupsIds)) return 0;
142
					return ($a->sumScore($groupsIds) > $b->sumScore($groupsIds) ? -1 : 1);
143
				});
144
				break;}
145
		}
146
		return $this->teams;
147
	}
148
149
	public function splitTeams(...$groups) {
150
151
		if (count($groups) === 0) $groups = $this->getGroups();
152
153
		foreach ($groups as $key => $value) {
154
			if (gettype($value) === 'array') {
155
				unset($groups[$key]);
156
				$groups = array_merge($groups, $value);
157
			}
158
		}
159
160
		$teams = $this->getTeams();
161
		shuffle($teams);
162
163
		while (count($teams) > 0) {
164
			foreach ($groups as $group) {
165
				if ($group instanceof Group) {
166
					if (count($teams) > 0) $group->addTeam(array_shift($teams));
167
				}
168
			}
169
		}
170
		return $this;
171
	}
172
173
	public function progress(bool $blank = false){
174
		foreach ($this->groups as $group) {
175
			$group->progress($blank);
176
		}
177
		return $this;
178
	}
179
180
	public function simulate() {
181
		foreach ($this->groups as $group) {
182
			if ($group->isPlayed()) continue;
183
			$group->simulate([], false);
184
		}
185
		return $this;
186
	}
187
	public function resetGames() {
188
		foreach ($this->groups as $group) {
189
			$group->resetGames();
190
		}
191
		return $this;
192
	}
193
}
194