Completed
Push — master ( 5d9f44...4b85e8 )
by Tomáš
03:31
created

Team   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 146
rs 9.2
c 0
b 0
f 0
wmc 40

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A addGame() 0 5 2
A addGroup() 0 3 2
A getGamesInfo() 0 2 1
A __toString() 0 2 1
A addGameWith() 0 4 2
A getSumScore() 0 2 1
A removeDraw() 0 6 2
A removeWin() 0 6 2
A removeSecond() 0 6 2
A addThird() 0 6 2
A sumScore() 0 7 4
A removeLoss() 0 6 2
A addSecond() 0 6 2
A getSumPoints() 0 2 1
A addWin() 0 6 2
A addLoss() 0 6 2
A sumPoints() 0 7 4
A removeThird() 0 6 2
A addDraw() 0 6 2

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
	public $name = 'team';
12
	public $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
	function __construct(string $name = 'team', $id = null) {
36
		$this->name = $name;
37
		$this->id = (isset($id) ? $id : uniqid());
38
	}
39
	public function __toString() {
40
		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
	public function addGameWith(Team $team, Group $group) {
47
		if (!isset($this->gamesWith[$group->id][$team->id])) $this->gamesWith[$group->id][$team->id] = 0;
48
		$this->gamesWith[$group->id][$team->id]++;
49
		return $this;
50
	}
51
	public function addGroup(Group $group) {
52
		if (!isset($this->games[$group->id])) $this->games[$group->id] = [];
53
		return $this;
54
	}
55
	public function addGame(Game $game) {
56
		$group = $game->getGroup();
57
		if (!isset($this->games[$group->id])) $this->games[$group->id] = [];
58
		$this->games[$group->id][] = $game;
59
		return $this;
60
	}
61
62
	public function getSumPoints() {
63
		return $this->sumPoints;
64
	}
65
	public function getSumScore() {
66
		return $this->sumScore;
67
	}
68
69
	public function addWin(string $groupId = ''){
70
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
71
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints;
72
		$this->sumPoints += $this->groupResults[$groupId]['group']->winPoints;
73
		$this->groupResults[$groupId]['wins']++;
74
		return $this;
75
	}
76
	public function removeWin(string $groupId = ''){
77
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
78
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints;
79
		$this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints;
80
		$this->groupResults[$groupId]['wins']--;
81
		return $this;
82
	}
83
	public function addDraw(string $groupId = ''){
84
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
85
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints;
86
		$this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints;
87
		$this->groupResults[$groupId]['draws']++;
88
		return $this;
89
	}
90
	public function removeDraw(string $groupId = ''){
91
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
92
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->drawPointsPoints;
93
		$this->sumPoints -= $this->groupResults[$groupId]['group']->drawPoints;
94
		$this->groupResults[$groupId]['draws']--;
95
		return $this;
96
	}
97
	public function addLoss(string $groupId = ''){
98
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
99
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints;
100
		$this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints;
101
		$this->groupResults[$groupId]['losses']++;
102
		return $this;
103
	}
104
	public function removeLoss(string $groupId = ''){
105
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
106
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints;
107
		$this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints;
108
		$this->groupResults[$groupId]['losses']--;
109
		return $this;
110
	}
111
	public function addSecond(string $groupId = ''){
112
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
113
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->secondPoints;
114
		$this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints;
115
		$this->groupResults[$groupId]['second']++;
116
		return $this;
117
	}
118
	public function removeSecond(string $groupId = ''){
119
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
120
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->secondPoints;
121
		$this->sumPoints -= $this->groupResults[$groupId]['group']->secondPoints;
122
		$this->groupResults[$groupId]['second']--;
123
		return $this;
124
	}
125
	public function addThird(string $groupId = ''){
126
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
127
		$this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->thirdPoints;
128
		$this->sumPoints += $this->groupResults[$groupId]['group']->thirdPoints;
129
		$this->groupResults[$groupId]['third']++;
130
		return $this;
131
	}
132
	public function removeThird(string $groupId = ''){
133
		if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')');
134
		$this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->thirdPoints;
135
		$this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints;
136
		$this->groupResults[$groupId]['third']--;
137
		return $this;
138
	}
139
	public function sumPoints(array $groupIds = []) {
140
		if (count($groupIds) === 0) return $this->sumPoints;
141
		$sum = 0;
142
		foreach ($groupIds as $gid) {
143
			if (isset($this->groupResults[$gid])) $sum += $this->groupResults[$gid]['points'];
144
		}
145
		return $sum;
146
	}
147
	public function sumScore(array $groupIds = []) {
148
		if (count($groupIds) === 0) return $this->sumScore;
149
		$sum = 0;
150
		foreach ($groupIds as $gid) {
151
			if (isset($this->groupResults[$gid])) $sum += $this->groupResults[$gid]['score'];
152
		}
153
		return $sum;
154
	}
155
}
156