| Total Complexity | 46 |
| Total Lines | 167 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Round 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 Round, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Round |
||
| 9 | { |
||
| 10 | |||
| 11 | public $name = ''; |
||
| 12 | public $id = ''; |
||
| 13 | private $groups = []; |
||
| 14 | private $games = []; |
||
| 15 | private $teams = []; |
||
| 16 | private $allowSkip = false; |
||
| 17 | |||
| 18 | function __construct(string $name = '') { |
||
| 19 | $this->id = uniqid(); |
||
| 20 | $this->name = $name; |
||
| 21 | } |
||
| 22 | public function __toString() { |
||
| 23 | return $this->name; |
||
| 24 | } |
||
| 25 | |||
| 26 | public function addGroup(Group ...$groups){ |
||
| 27 | foreach ($groups as $group) { |
||
| 28 | if ($group instanceof Group) $this->groups[] = $group; |
||
| 29 | else throw new \Exception('Trying to add group which is not an instance of Group class.'); |
||
| 30 | } |
||
| 31 | return $this; |
||
| 32 | } |
||
| 33 | public function group(array $settings = []) { |
||
| 34 | $g = new Group($settings); |
||
| 35 | $this->groups[] = $g->setSkip($this->allowSkip); |
||
| 36 | return $g; |
||
| 37 | } |
||
| 38 | public function getGroups(){ |
||
| 41 | } |
||
| 42 | public function getGroupsIds() { |
||
| 43 | $this->orderGroups(); |
||
| 44 | return array_map(function($a) { return $a->id; }, $this->groups); |
||
| 45 | } |
||
| 46 | public function orderGroups() { |
||
| 49 | }); |
||
| 50 | } |
||
| 51 | |||
| 52 | public function allowSkip(){ |
||
| 53 | $this->allowSkip = true; |
||
| 54 | return $this; |
||
| 55 | } |
||
| 56 | public function disallowSkip(){ |
||
| 57 | $this->allowSkip = false; |
||
| 58 | return $this; |
||
| 59 | } |
||
| 60 | public function setSkip(bool $skip = false) { |
||
| 61 | $this->allowSkip = $skip; |
||
| 62 | return $this; |
||
| 63 | } |
||
| 64 | public function getSkip() { |
||
| 66 | } |
||
| 67 | |||
| 68 | public function genGames(){ |
||
| 69 | foreach ($this->groups as $group) { |
||
| 70 | $group->genGames(); |
||
| 71 | $this->games = array_merge($this->games, $group->orderGames()); |
||
| 72 | } |
||
| 73 | return $this->games; |
||
| 74 | } |
||
| 75 | public function getGames() { |
||
| 77 | } |
||
| 78 | public function isPlayed(){ |
||
| 79 | foreach ($this->groups as $group) { |
||
| 80 | if (!$group->isPlayed()) return false; |
||
| 81 | } |
||
| 82 | return true; |
||
| 83 | } |
||
| 84 | |||
| 85 | public function addTeam(...$teams) { |
||
| 86 | foreach ($teams as $team) { |
||
| 87 | if ($team instanceof Team) { |
||
| 88 | $this->teams[] = $team; |
||
| 89 | } |
||
| 90 | elseif (gettype($team) === 'array') { |
||
| 91 | foreach ($team as $team2) { |
||
| 92 | if ($team2 instanceof Team) $this->teams[] = $team2; |
||
| 93 | $team2->groupResults[$this->id] = [ |
||
| 94 | 'group' => $this, |
||
| 95 | 'points' => 0, |
||
| 96 | 'score' => 0, |
||
| 97 | 'wins' => 0, |
||
| 98 | 'draws' => 0, |
||
| 99 | 'losses' => 0, |
||
| 100 | 'second' => 0, |
||
| 101 | 'third' => 0 |
||
| 102 | ]; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | else throw new \Exception('Trying to add team which is not an instance of Team class'); |
||
| 106 | } |
||
| 107 | return $this; |
||
| 108 | } |
||
| 109 | public function team(string $name = '') { |
||
| 110 | $t = new Team($name); |
||
| 111 | $this->teams[] = $t; |
||
| 112 | return $t; |
||
| 113 | } |
||
| 114 | public function getTeams(bool $ordered = false, $ordering = \POINTS) { |
||
| 126 | } |
||
| 127 | public function sortTeams($ordering = \POINTS) { |
||
| 128 | Utilis\Sorter\Teams::sortRound($this->teams, $this, $ordering); |
||
| 129 | return $this->teams; |
||
| 130 | } |
||
| 131 | |||
| 132 | public function splitTeams(...$groups) { |
||
| 133 | |||
| 134 | if (count($groups) === 0) $groups = $this->getGroups(); |
||
| 135 | |||
| 136 | foreach ($groups as $key => $value) { |
||
| 137 | if (gettype($value) === 'array') { |
||
| 138 | unset($groups[$key]); |
||
| 139 | $groups = array_merge($groups, $value); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | $teams = $this->getTeams(); |
||
| 144 | shuffle($teams); |
||
| 145 | |||
| 146 | while (count($teams) > 0) { |
||
| 147 | foreach ($groups as $group) { |
||
| 148 | if ($group instanceof Group) { |
||
| 149 | if (count($teams) > 0) $group->addTeam(array_shift($teams)); |
||
| 150 | } |
||
| 151 | } |
||
| 152 | } |
||
| 153 | return $this; |
||
| 154 | } |
||
| 155 | |||
| 156 | public function progress(bool $blank = false){ |
||
| 161 | } |
||
| 162 | |||
| 163 | public function simulate() { |
||
| 169 | } |
||
| 170 | public function resetGames() { |
||
| 175 | } |
||
| 176 | } |
||
| 177 |