| Total Complexity | 40 |
| Total Lines | 146 |
| 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 | 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) { |
||
| 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) { |
||
| 60 | } |
||
| 61 | |||
| 62 | public function getSumPoints() { |
||
| 63 | return $this->sumPoints; |
||
| 64 | } |
||
| 65 | public function getSumScore() { |
||
| 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 = ''){ |
||
| 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 = ''){ |
||
| 124 | } |
||
| 125 | public function addThird(string $groupId = ''){ |
||
| 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 = []) { |
||
| 154 | } |
||
| 155 | } |
||
| 156 |