Completed
Push — master ( a6547d...2340fb )
by Tomáš
02:43
created

Team   C

Complexity

Total Complexity 56

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 113
c 0
b 0
f 0
dl 0
loc 202
ccs 135
cts 135
cp 1
rs 5.5199
wmc 56

27 Methods

Rating   Name   Duplication   Size   Complexity  
A removeWin() 0 6 2
A addWin() 0 6 2
A removeScore() 0 2 1
A getSumScore() 0 2 1
A getGames() 0 4 5
A __construct() 0 3 2
A addGame() 0 5 2
A removeDraw() 0 6 2
A removeSecond() 0 6 2
A addThird() 0 6 2
A addGroup() 0 3 2
A removeLoss() 0 6 2
A getGroupResults() 0 6 3
A sumScore() 0 7 3
A addSecond() 0 6 2
A getSumPoints() 0 2 1
A addGroupResults() 0 12 1
A getGamesInfo() 0 2 1
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 addGameWith() 0 4 2
A getGameWith() 0 16 6
A addPoints() 0 2 1
A removePoints() 0 2 1

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