| Total Complexity | 50 |
| Total Lines | 184 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
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 |
||
| 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 | function __construct(string $name = 'team', $id = null) { |
||
| 36 | $this->setName($name); |
||
| 37 | $this->setId(isset($id) ? $id : uniqid()); |
||
| 38 | } |
||
| 39 | public function __toString() { |
||
| 40 | return $this->name; |
||
| 41 | } |
||
| 42 | public function getGamesInfo($groupId) { |
||
| 44 | } |
||
| 45 | |||
| 46 | public function setName(string $name) { |
||
| 47 | $this->name = $name; |
||
| 48 | } |
||
| 49 | public function getName() { |
||
| 50 | return $this->name; |
||
| 51 | } |
||
| 52 | public function setId($id) { |
||
| 53 | if (!is_string($id) && !is_int($id)) { |
||
| 54 | $this->id = uniqid(); |
||
| 55 | throw new Exception('Unsupported id type ('.gettype($id).') - expected type of string or int'); |
||
|
|
|||
| 56 | } |
||
| 57 | $this->id = $id; |
||
| 58 | } |
||
| 59 | public function getId() { |
||
| 60 | return $this->id; |
||
| 61 | } |
||
| 62 | |||
| 63 | public function addGroupResults(Group $group) { |
||
| 64 | $this->groupResults[$group->getId()] = [ |
||
| 65 | 'group' => $group, |
||
| 66 | 'points' => 0, |
||
| 67 | 'score' => 0, |
||
| 68 | 'wins' => 0, |
||
| 69 | 'draws' => 0, |
||
| 70 | 'losses' => 0, |
||
| 71 | 'second' => 0, |
||
| 72 | 'third' => 0 |
||
| 73 | ]; |
||
| 74 | return $this; |
||
| 75 | } |
||
| 76 | public function getGroupResults($groupId = null) { |
||
| 77 | if (isset($groupId)) { |
||
| 78 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Trying to get unexisting group results ('.$groupId.')'); |
||
| 79 | return $this->groupResults[$groupId]; |
||
| 80 | } |
||
| 81 | return $this->groupResults; |
||
| 82 | } |
||
| 83 | |||
| 84 | public function addGameWith(Team $team, Group $group) { |
||
| 85 | if (!isset($this->gamesWith[$group->getId()][$team->getId()])) $this->gamesWith[$group->getId()][$team->getId()] = 0; |
||
| 86 | $this->gamesWith[$group->getId()][$team->getId()]++; |
||
| 87 | return $this; |
||
| 88 | } |
||
| 89 | public function addGroup(Group $group) { |
||
| 90 | if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = []; |
||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | public function addGame(Game $game) { |
||
| 94 | $group = $game->getGroup(); |
||
| 95 | if (!isset($this->games[$group->getId()])) $this->games[$group->getId()] = []; |
||
| 96 | $this->games[$group->getId()][] = $game; |
||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function getSumPoints() { |
||
| 101 | return $this->sumPoints; |
||
| 102 | } |
||
| 103 | public function getSumScore() { |
||
| 105 | } |
||
| 106 | |||
| 107 | public function addWin(string $groupId = ''){ |
||
| 108 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 109 | $this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->winPoints; |
||
| 110 | $this->sumPoints += $this->groupResults[$groupId]['group']->winPoints; |
||
| 111 | $this->groupResults[$groupId]['wins']++; |
||
| 112 | return $this; |
||
| 113 | } |
||
| 114 | public function removeWin(string $groupId = ''){ |
||
| 115 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 116 | $this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->winPoints; |
||
| 117 | $this->sumPoints -= $this->groupResults[$groupId]['group']->winPoints; |
||
| 118 | $this->groupResults[$groupId]['wins']--; |
||
| 119 | return $this; |
||
| 120 | } |
||
| 121 | public function addDraw(string $groupId = ''){ |
||
| 122 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 123 | $this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->drawPoints; |
||
| 124 | $this->sumPoints += $this->groupResults[$groupId]['group']->drawPoints; |
||
| 125 | $this->groupResults[$groupId]['draws']++; |
||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | public function removeDraw(string $groupId = ''){ |
||
| 134 | } |
||
| 135 | public function addLoss(string $groupId = ''){ |
||
| 136 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 137 | $this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->lostPoints; |
||
| 138 | $this->sumPoints += $this->groupResults[$groupId]['group']->lostPoints; |
||
| 139 | $this->groupResults[$groupId]['losses']++; |
||
| 140 | return $this; |
||
| 141 | } |
||
| 142 | public function removeLoss(string $groupId = ''){ |
||
| 143 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 144 | $this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->lostPoints; |
||
| 145 | $this->sumPoints -= $this->groupResults[$groupId]['group']->lostPoints; |
||
| 146 | $this->groupResults[$groupId]['losses']--; |
||
| 147 | return $this; |
||
| 148 | } |
||
| 149 | public function addSecond(string $groupId = ''){ |
||
| 150 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 151 | $this->groupResults[$groupId]['points'] += $this->groupResults[$groupId]['group']->secondPoints; |
||
| 152 | $this->sumPoints += $this->groupResults[$groupId]['group']->secondPoints; |
||
| 153 | $this->groupResults[$groupId]['second']++; |
||
| 154 | return $this; |
||
| 155 | } |
||
| 156 | public function removeSecond(string $groupId = ''){ |
||
| 162 | } |
||
| 163 | public function addThird(string $groupId = ''){ |
||
| 169 | } |
||
| 170 | public function removeThird(string $groupId = ''){ |
||
| 171 | if (!isset($this->groupResults[$groupId])) throw new \Exception('Group '.$groupId.' is not set for this team ('.$this->name.')'); |
||
| 172 | $this->groupResults[$groupId]['points'] -= $this->groupResults[$groupId]['group']->thirdPoints; |
||
| 173 | $this->sumPoints -= $this->groupResults[$groupId]['group']->thirdPoints; |
||
| 174 | $this->groupResults[$groupId]['third']--; |
||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | public function sumPoints(array $groupIds = []) { |
||
| 178 | if (count($groupIds) === 0) return $this->sumPoints; |
||
| 179 | $sum = 0; |
||
| 180 | foreach ($groupIds as $gid) { |
||
| 181 | if (isset($this->groupResults[$gid])) $sum += $this->groupResults[$gid]['points']; |
||
| 182 | } |
||
| 183 | return $sum; |
||
| 184 | } |
||
| 185 | public function sumScore(array $groupIds = []) { |
||
| 192 | } |
||
| 193 | } |
||
| 194 |