Test Failed
Branch master (723ca8)
by Tomáš
02:22
created

Round   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 77
dl 0
loc 155
rs 8.8798
c 1
b 0
f 0
wmc 44

26 Methods

Rating   Name   Duplication   Size   Complexity  
A getTeams() 0 12 4
A sortTeams() 0 3 1
A __toString() 0 2 1
A getName() 0 2 1
A setName() 0 2 1
A getGames() 0 2 1
A orderGroups() 0 5 1
A getSkip() 0 2 1
A addGroup() 0 5 2
A getGroups() 0 3 1
A __construct() 0 3 2
A setId() 0 6 3
A group() 0 4 1
A getGroupsIds() 0 3 1
A addTeam() 0 5 2
A setSkip() 0 3 1
A team() 0 4 1
A disallowSkip() 0 3 1
A isPlayed() 0 6 4
A genGames() 0 6 2
A getId() 0 2 1
A allowSkip() 0 3 1
A progress() 0 5 2
A simulate() 0 3 1
A splitTeams() 0 13 5
A resetGames() 0 5 2

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
	private $name = '';
12
	private $id = '';
13
	private $groups = [];
14
	private $games = [];
15
	private $teams = [];
16
	private $allowSkip = false;
17
18
	function __construct(string $name = '', $id = null) {
19
		$this->setName($name);
20
		$this->setId(isset($id) ? $id : uniqid());
21
	}
22
	public function __toString() {
23
		return $this->name;
24
	}
25
26
	public function setName(string $name) {
27
		$this->name = $name;
28
	}
29
	public function getName() {
30
		return $this->name;
31
	}
32
	public function setId($id) {
33
		if (!is_string($id) && !is_int($id)) {
34
			$this->id = uniqid();
35
			throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int');
36
		}
37
		$this->id = $id;
38
	}
39
	public function getId() {
40
		return $this->id;
41
	}
42
43
	public function addGroup(Group ...$groups){
44
		foreach ($groups as $group) {
45
			$this->groups[] = $group;
46
		}
47
		return $this;
48
	}
49
	public function group(string $name, $id = null) {
50
		$g = new Group($name, $id);
51
		$this->groups[] = $g->setSkip($this->allowSkip);
52
		return $g;
53
	}
54
	public function getGroups(){
55
		$this->orderGroups();
56
		return $this->groups;
57
	}
58
	public function getGroupsIds() {
59
		$this->orderGroups();
60
		return array_map(function($a) { return $a->getId(); }, $this->groups);
61
	}
62
	public function orderGroups() {
63
		usort($this->groups, function($a, $b){
64
			return $a->getOrder() - $b->getOrder();
65
		});
66
		return $this->groups;
67
	}
68
69
	public function allowSkip(){
70
		$this->allowSkip = true;
71
		return $this;
72
	}
73
	public function disallowSkip(){
74
		$this->allowSkip = false;
75
		return $this;
76
	}
77
	public function setSkip(bool $skip = false) {
78
		$this->allowSkip = $skip;
79
		return $this;
80
	}
81
	public function getSkip() {
82
		return $this->allowSkip;
83
	}
84
85
	public function genGames(){
86
		foreach ($this->groups as $group) {
87
			$group->genGames();
88
			$this->games = array_merge($this->games, $group->orderGames());
89
		}
90
		return $this->games;
91
	}
92
	public function getGames() {
93
		return $this->games;
94
	}
95
	public function isPlayed(){
96
		if (count($this->games) === 0) return false;
97
		foreach ($this->groups as $group) {
98
			if (!$group->isPlayed()) return false;
99
		}
100
		return true;
101
	}
102
103
	public function addTeam(Team ...$teams) {
104
		foreach ($teams as $team) {
105
			$this->teams[] = $team;
106
		}
107
		return $this;
108
	}
109
	public function team(string $name = '', $id = null) {
110
		$t = new Team($name, $id);
111
		$this->teams[] = $t;
112
		return $t;
113
	}
114
	public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS) {
115
		if (count($this->teams) == 0) {
116
			$teams = [];
117
			foreach ($this->groups as $group) {
118
				$teams = array_merge($teams, $group->getTeams());
119
			}
120
			$this->teams = $teams;
121
		}
122
		if ($ordered) {
123
			$this->sortTeams($ordering);
124
		}
125
		return $this->teams;
126
	}
127
	public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS) {
128
		Utilis\Sorter\Teams::sortRound($this->teams, $this, $ordering);
129
		return $this->teams;
130
	}
131
132
	public function splitTeams(Group ...$groups) {
133
134
		if (count($groups) === 0) $groups = $this->getGroups();
135
136
		$teams = $this->getTeams();
137
		shuffle($teams);
138
139
		while (count($teams) > 0) {
140
			foreach ($groups as $group) {
141
				if (count($teams) > 0) $group->addTeam(array_shift($teams));
142
			}
143
		}
144
		return $this;
145
	}
146
147
	public function progress(bool $blank = false){
148
		foreach ($this->groups as $group) {
149
			$group->progress($blank);
150
		}
151
		return $this;
152
	}
153
154
	public function simulate() {
155
		Utilis\Simulator::simulateRound($this);
156
		return $this;
157
	}
158
	public function resetGames() {
159
		foreach ($this->groups as $group) {
160
			$group->resetGames();
161
		}
162
		return $this;
163
	}
164
}
165