| Total Complexity | 56 |
| Total Lines | 224 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Group 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 Group, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class Group |
||
| 9 | { |
||
| 10 | |||
| 11 | private $generator = null; |
||
| 12 | private $teams = []; // ARRAY OF TEAMS |
||
| 13 | private $progressed = []; // ARRAY OF TEAMS ALREADY PROGRESSED FROM THIS GROUP |
||
| 14 | public $name = ''; // DISPLAYABLE NAME |
||
| 15 | private $ordering = \POINTS; // WHAT TO DECIDE ON WHEN ORDERING TEAMS |
||
| 16 | private $progressions = []; // ARRAY OF PROGRESSION CONDITION OBJECTS |
||
| 17 | private $games = []; // ARRAY OF GAME OBJECTS |
||
| 18 | public $id = ''; // UNIQID OF GROUP FOR IDENTIFICATIONT |
||
| 19 | public $winPoints = 3; // POINTS AQUIRED FROM WINNING |
||
| 20 | public $drawPoints = 1; // POINTS AQUIRED FROM DRAW |
||
| 21 | public $lostPoints = 0; // POINTS AQUIRED FROM LOOSING |
||
| 22 | public $secondPoints = 2; // POINTS AQUIRED FROM BEING SECOND (APPLIES ONLY FOR 3 OR 4 INGAME VALUE) |
||
| 23 | public $thirdPoints = 1; // POINTS AQUIRED FROM BEING THIRD (APPLIES ONLY FOR 4 INGAME VALUE) |
||
| 24 | public $progressPoints = 50; // POINTS AQUIRED FROM PROGRESSING TO THE NEXT ROUND |
||
| 25 | public $order = 0; // ORDER OF GROUPS IN ROUND |
||
| 26 | |||
| 27 | function __construct(array $settings = []) { |
||
| 28 | $this->id = uniqid(); |
||
| 29 | $this->generator = new Utilis\Generator($this); |
||
| 30 | foreach ($settings as $key => $value) { |
||
| 31 | switch ($key) { |
||
| 32 | case 'name': |
||
| 33 | $this->name = (string) $value; |
||
| 34 | break; |
||
| 35 | case 'type': |
||
| 36 | $this->generator->setType($value); |
||
| 37 | break; |
||
| 38 | case 'ordering': |
||
| 39 | $this->setOrdering($value); |
||
| 40 | break; |
||
| 41 | case 'inGame': |
||
| 42 | $this->generator->setInGame((int) $value); |
||
| 43 | break; |
||
| 44 | case 'maxSize': |
||
| 45 | $this->generator->setMaxSize((int) $value); |
||
| 46 | break; |
||
| 47 | case 'order': |
||
| 48 | $this->order = (int) $value; |
||
| 49 | break; |
||
| 50 | } |
||
| 51 | } |
||
| 52 | } |
||
| 53 | public function __toString() { |
||
| 54 | return 'Group '.$this->name; |
||
| 55 | } |
||
| 56 | |||
| 57 | public function allowSkip(){ |
||
| 58 | $this->generator->allowSkip(); |
||
| 59 | return $this; |
||
| 60 | } |
||
| 61 | public function disallowSkip(){ |
||
| 62 | $this->generator->disallowSkip(); |
||
| 63 | return $this; |
||
| 64 | } |
||
| 65 | public function setSkip(bool $skip) { |
||
| 68 | } |
||
| 69 | public function getSkip() { |
||
| 70 | return $this->generator->getSkip(); |
||
| 71 | } |
||
| 72 | |||
| 73 | public function addTeam(...$teams) { |
||
| 74 | foreach ($teams as $team) { |
||
| 75 | if (gettype($team) === 'array') { |
||
| 76 | foreach ($team as $team2) { |
||
| 77 | $this->setTeam($team); |
||
|
|
|||
| 78 | } |
||
| 79 | continue; |
||
| 80 | } |
||
| 81 | $this->setTeam($team); |
||
| 82 | } |
||
| 83 | return $this; |
||
| 84 | } |
||
| 85 | private function setTeam(Team $team) { |
||
| 86 | $this->teams[] = $team; |
||
| 87 | $team2->groupResults[$this->id] = [ |
||
| 88 | 'group' => $this, |
||
| 89 | 'points' => 0, |
||
| 90 | 'score' => 0, |
||
| 91 | 'wins' => 0, |
||
| 92 | 'draws' => 0, |
||
| 93 | 'losses' => 0, |
||
| 94 | 'second' => 0, |
||
| 95 | 'third' => 0 |
||
| 96 | ]; |
||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | public function getTeams($filters = []) { |
||
| 100 | $teams = $this->teams; |
||
| 101 | |||
| 102 | if (gettype($filters) !== 'array' && $filters instanceof TeamFilter) $filters = [$filters]; |
||
| 103 | elseif (gettype($filters) !== 'array') $filters = []; |
||
| 104 | |||
| 105 | // APPLY FILTERS |
||
| 106 | $filter = new Filter($this, $filters); |
||
| 107 | $filter->filter($teams); |
||
| 108 | |||
| 109 | return $teams; |
||
| 110 | } |
||
| 111 | |||
| 112 | public function team(string $name = '') { |
||
| 113 | $t = new Team($name); |
||
| 114 | $this->teams[] = $t; |
||
| 115 | $t->groupResults[$this->id] = [ |
||
| 116 | 'group' => $this, |
||
| 117 | 'points' => 0, |
||
| 118 | 'score' => 0, |
||
| 119 | 'wins' => 0, |
||
| 120 | 'draws' => 0, |
||
| 121 | 'losses' => 0, |
||
| 122 | 'second' => 0, |
||
| 123 | 'third' => 0 |
||
| 124 | ]; |
||
| 125 | return $t; |
||
| 126 | } |
||
| 127 | public function sortTeams($filters = [], $ordering = null) { |
||
| 128 | if (!isset($ordering)) $ordering = $this->ordering; |
||
| 129 | Utilis\Sorter\Teams::sortGroup($this->teams, $this, $ordering); |
||
| 130 | return $this->getTeams($filters); |
||
| 131 | } |
||
| 132 | |||
| 133 | public function setType(string $type = \R_R) { |
||
| 134 | $this->generator->setType($type); |
||
| 135 | return $this; |
||
| 136 | } |
||
| 137 | public function getType() { |
||
| 138 | return $this->generator->getType(); |
||
| 139 | } |
||
| 140 | |||
| 141 | public function setOrdering(string $ordering = \POINTS) { |
||
| 142 | if (!in_array($ordering, orderingTypes)) throw new \Exception('Unknown group ordering: '.$ordering); |
||
| 143 | $this->ordering = $ordering; |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | public function getOrdering() { |
||
| 148 | } |
||
| 149 | |||
| 150 | public function setInGame(int $inGame) { |
||
| 151 | $this->generator->setInGame($inGame); |
||
| 152 | return $this; |
||
| 153 | } |
||
| 154 | public function getInGame() { |
||
| 155 | return $this->generator->getInGame(); |
||
| 156 | } |
||
| 157 | |||
| 158 | public function addProgression(Progression $progression) { |
||
| 159 | $this->progressions[] = $progression; |
||
| 160 | return $this; |
||
| 161 | } |
||
| 162 | public function progression(Group $to, int $start = 0, int $len = null) { |
||
| 163 | $p = new Progression($this, $to, $start, $len); |
||
| 164 | $this->progressions[] = $p; |
||
| 165 | return $p; |
||
| 166 | } |
||
| 167 | public function progress(bool $blank = false) { |
||
| 168 | foreach ($this->progressions as $progression) { |
||
| 169 | $progression->progress($blank); |
||
| 170 | } |
||
| 171 | } |
||
| 172 | public function addProgressed(...$teams) { |
||
| 173 | foreach ($teams as $team) { |
||
| 174 | if ($team instanceOf Team) $this->progressed[] = $team->id; |
||
| 175 | elseif (gettype($team) === 'array') { |
||
| 176 | $this->progressed = array_merge($this->progressed, array_filter($team, function($a) { |
||
| 177 | return ($a instanceof Team); |
||
| 178 | })); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | public function isProgressed(Team $team) { |
||
| 184 | return in_array($team->id, $this->progressed); |
||
| 185 | } |
||
| 186 | |||
| 187 | public function genGames() { |
||
| 188 | $this->generator->genGames(); |
||
| 189 | return $this->games; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function game(array $teams = []) { |
||
| 196 | } |
||
| 197 | public function addGame(...$games){ |
||
| 198 | foreach ($games as $key => $game) { |
||
| 208 | } |
||
| 209 | public function getGames() { |
||
| 210 | return $this->games; |
||
| 211 | } |
||
| 212 | public function orderGames() { |
||
| 213 | if (count($this->games) <= 4) return $this->games; |
||
| 214 | $this->games = $this->generator->orderGames(); |
||
| 215 | return $this->games; |
||
| 216 | } |
||
| 217 | |||
| 218 | public function simulate(array $filters = [], bool $reset = true) { |
||
| 219 | return Utilis\Simulator::simulateGroup($this, $filters, $reset); |
||
| 220 | } |
||
| 221 | public function resetGames() { |
||
| 226 | } |
||
| 227 | public function isPlayed(){ |
||
| 228 | foreach ($this->games as $game) { |
||
| 229 | if (!$game->isPlayed()) return false; |
||
| 230 | } |
||
| 231 | return true; |
||
| 232 | } |
||
| 233 | |||
| 234 | } |
||
| 235 |