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

Team   F

Complexity

Total Complexity 61

Size/Duplication

Total Lines 218
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 121
dl 0
loc 218
ccs 144
cts 144
cp 1
rs 3.52
c 0
b 0
f 0
wmc 61

30 Methods

Rating   Name   Duplication   Size   Complexity  
A getGamesInfo() 0 2 1
A __construct() 0 3 2
A getName() 0 2 1
A getId() 0 2 1
A addGroupResults() 0 12 1
A setName() 0 2 1
A __toString() 0 2 1
A addGameWith() 0 4 2
A addGame() 0 5 2
A getGroupResults() 0 6 3
A removeScore() 0 2 1
A getSumScore() 0 2 1
A getGames() 0 4 5
A setId() 0 6 3
A removeDraw() 0 6 2
A removeWin() 0 6 2
A removeSecond() 0 6 2
A addThird() 0 6 2
A addGroup() 0 3 2
A removeLoss() 0 6 2
A sumScore() 0 7 3
A addSecond() 0 6 2
A getSumPoints() 0 2 1
A addWin() 0 6 2
A addScore() 0 2 1
A addLoss() 0 6 2
A sumPoints() 0 7 3
A addDraw() 0 6 2
A removeThird() 0 6 2
A getGameWith() 0 16 6

How to fix   Complexity   

Complex Class

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

1
<?php
2
3
namespace TournamentGenerator;
4
5
/**
6
 *
7
 */
8
class Team
9
{
10
11
	private $name = 'team';
12
	private $id = '';
13
	public $games = [];
14
	public $gamesWith = [];
15
	public $sumPoints = 0;
16
	public $sumScore = 0;
17
18
	/**
19
	* ARRAY WITH GROUPS AND IT'S RESULTS
20
	* array (
21
	* * groupId => array (
22
	* * * "group"  => Group, # GROUP OBJECT
23
	* * * "points" => int 0, # NUMBER OF POINTS AQUIRED
24
	* * * "score"  => int 0, # SUM OF SCORE AQUIRED
25
	* * * "wins"   => int 0, # NUMBER OF WINS
26
	* * * "draws"  => int 0, # NUMBER OF DRAWS
27
	* * * "losses" => int 0, # NUMBER OF LOSSES
28
	* * * "second" => int 0, # NUMBER OF TIMES BEING SECOND (ONLY FOR INGAME OPTION OF 3 OR 4)
29
	* * * "third"  => int 0  # NUMBER OF TIMES BEING THIRD  (ONLY FOR INGAME OPTION OF 4)
30
	* * )
31
	*)
32
	*/
33
	public $groupResults = [];
34
35 85
	function __construct(string $name = 'team', $id = null) {
36 85
		$this->setName($name);
37 85
		$this->setId(isset($id) ? $id : uniqid());
38 85
	}
39 22
	public function __toString() {
40 22
		return $this->name;
41
	}
42
	public function getGamesInfo($groupId) {
43
		return array_filter($this->groupResults[$groupId], function($k) { return $k !== 'group'; }, ARRAY_FILTER_USE_KEY);
44
	}
45
46 85
	public function setName(string $name) {
47 85
		$this->name = $name;
48 85
	}
49 11
	public function getName() {
50 11
		return $this->name;
51
	}
52 85
	public function setId($id) {
53 85
		if (!is_string($id) && !is_int($id)) {
54 1
			$this->id = uniqid();
55 1
			throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int');
56
		}
57 85
		$this->id = $id;
58 85
	}
59 68
	public function getId() {
60 68
		return $this->id;
61
	}
62
63 67
	public function addGroupResults(Group $group) {
64 67
		$this->groupResults[$group->getId()] = [
65 67
			'group' => $group,
66 67
			'points' => 0,
67 67
			'score'  => 0,
68 67
			'wins'   => 0,
69 67
			'draws'  => 0,
70 67
			'losses' => 0,
71 67
			'second' => 0,
72 67
			'third'  => 0
73
		];
74 67
		return $this;
75
	}
76 6
	public function getGroupResults($groupId = null) {
77 6
		if (isset($groupId)) {
78 4
			if (!isset($this->groupResults[$groupId])) throw new \Exception('Trying to get unexisting group results ('.$groupId.')');
79 4
			return $this->groupResults[$groupId];
80
		}
81 2
		return $this->groupResults;
82
	}
83
84 67
	public function addGameWith(Team $team, Group $group) {
85 67
		if (!isset($this->gamesWith[$group->getId()][$team->getId()])) $this->gamesWith[$group->getId()][$team->getId()] = 0;
86 67
		$this->gamesWith[$group->getId()][$team->getId()]++;
87 67
		return $this;
88
	}
89 3
	public function getGameWith(Team $team = null, Group $group = null) {
90 3
		if (isset($group)) {
91 3
			if (isset($team)) return $this->gamesWith[$group->getId()][$team->getId()];
92 3
			return $this->gamesWith[$group->getId()];
93
		}
94 2
		if (isset($team)) {
95 1
			$return = [];
96 1
			foreach ($this->gamesWith as $id => $games) {
97
				$filter = array_filter($games, function($key) use ($team){
98 1
					return $key === $team->getId();
99 1
				}, ARRAY_FILTER_USE_KEY);
100 1
				if (count($filter) > 0) $return[$id] = $filter;
101
			}
102 1
			return $return;
103
		}
104 1
		return $this->gamesWith;
105
	}
106 1
	public function addGroup(Group $group) {
107 1
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
108 1
		return $this;
109
	}
110 67
	public function addGame(Game $game) {
111 67
		$group = $game->getGroup();
112 67
		if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = [];
113 67
		$this->games[$group->getId()][] = $game;
114 67
		return $this;
115
	}
116 4
	public function getGames(Group $group = null, $groupId = null) {
117 4
		if (isset($group) && isset($this->games[$group->getId()])) return $this->games[$group->getId()];
118 4
		if (isset($groupId) && isset($this->games[$groupId])) return $this->games[$groupId];
119 1
		return $this->games;
120
	}
121
122 4
	public function getSumPoints() {
123 4
		return $this->sumPoints;
124
	}
125 5
	public function getSumScore() {
126 5
		return $this->sumScore;
127
	}
128
129 49
	public function addWin(string $groupId = ''){
130 49
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
131 49
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints;
132 49
		$this->sumPoints += $this->groupResults[$groupId]['group']->winPoints;
133 49
		$this->groupResults[$groupId]['wins']++;
134 49
		return $this;
135
	}
136 9
	public function removeWin(string $groupId = ''){
137 9
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
138 9
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints;
139 9
		$this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints;
140 9
		$this->groupResults[$groupId]['wins']--;
141 9
		return $this;
142
	}
143
144 11
	public function addDraw(string $groupId = ''){
145 11
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
146 11
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints;
147 11
		$this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints;
148 11
		$this->groupResults[$groupId]['draws']++;
149 11
		return $this;
150
	}
151 1
	public function removeDraw(string $groupId = ''){
152 1
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
153 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->drawPoints;
154 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->drawPoints;
155 1
		$this->groupResults[$groupId]['draws']--;
156 1
		return $this;
157
	}
158
159 49
	public function addLoss(string $groupId = ''){
160 49
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
161 49
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints;
162 49
		$this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints;
163 49
		$this->groupResults[$groupId]['losses']++;
164 49
		return $this;
165
	}
166 9
	public function removeLoss(string $groupId = ''){
167 9
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
168 9
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints;
169 9
		$this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints;
170 9
		$this->groupResults[$groupId]['losses']--;
171 9
		return $this;
172
	}
173
174 10
	public function addSecond(string $groupId = ''){
175 10
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
176 10
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->secondPoints;
177 10
		$this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints;
178 10
		$this->groupResults[$groupId]['second']++;
179 10
		return $this;
180
	}
181 2
	public function removeSecond(string $groupId = ''){
182 2
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
183 2
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->secondPoints;
184 2
		$this->sumPoints -= $this->groupResults[$groupId]['group']->secondPoints;
185 2
		$this->groupResults[$groupId]['second']--;
186 2
		return $this;
187
	}
188
189 9
	public function addThird(string $groupId = ''){
190 9
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
191 9
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->thirdPoints;
192 9
		$this->sumPoints += $this->groupResults[$groupId]['group']->thirdPoints;
193 9
		$this->groupResults[$groupId]['third']++;
194 9
		return $this;
195
	}
196 1
	public function removeThird(string $groupId = ''){
197 1
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
198 1
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->thirdPoints;
199 1
		$this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints;
200 1
		$this->groupResults[$groupId]['third']--;
201 1
		return $this;
202
	}
203
204 22
	public function sumPoints(array $groupIds = []) {
205 22
		if (count($groupIds) === 0) return $this->sumPoints;
206 22
		$sum = 0;
207 22
		foreach ($groupIds as $gid) {
208 22
			$sum += $this->groupResults[$gid]['points'] ?? 0;
209
		}
210 22
		return $sum;
211
	}
212 14
	public function sumScore(array $groupIds = []) {
213 14
		if (count($groupIds) === 0) return $this->sumScore;
214 14
		$sum = 0;
215 14
		foreach ($groupIds as $gid) {
216 14
			$sum += $this->groupResults[$gid]['score'] ?? 0;
217
		}
218 14
		return $sum;
219
	}
220
221 51
	public function addScore(int $score) {
222 51
		$this->sumScore += $score;
223 51
	}
224 1
	public function removeScore(int $score) {
225 1
		$this->sumScore -= $score;
226 1
	}
227
}
228