Completed
Push — master ( 635aa4...d2c914 )
by Tomáš
02:02
created

Group   B

Complexity

Total Complexity 47

Size/Duplication

Total Lines 188
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 95
c 0
b 0
f 0
dl 0
loc 188
ccs 111
cts 111
cp 1
rs 8.64
wmc 47

33 Methods

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