Total Complexity | 44 |
Total Lines | 155 |
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 | private $name = ''; |
||
12 | private $id = ''; |
||
13 | private $groups = []; |
||
14 | private $games = []; |
||
15 | private $teams = []; |
||
16 | private $allowSkip = false; |
||
17 | |||
18 | function __construct(string $name = '', $id = null) { |
||
19 | $this->setName($name); |
||
20 | $this->setId(isset($id) ? $id : uniqid()); |
||
21 | } |
||
22 | public function __toString() { |
||
23 | return $this->name; |
||
24 | } |
||
25 | |||
26 | public function setName(string $name) { |
||
27 | $this->name = $name; |
||
28 | } |
||
29 | public function getName() { |
||
30 | return $this->name; |
||
31 | } |
||
32 | public function setId($id) { |
||
33 | if (!is_string($id) && !is_int($id)) { |
||
34 | $this->id = uniqid(); |
||
35 | throw new \Exception('Unsupported id type ('.gettype($id).') - expected type of string or int'); |
||
36 | } |
||
37 | $this->id = $id; |
||
38 | } |
||
39 | public function getId() { |
||
40 | return $this->id; |
||
41 | } |
||
42 | |||
43 | public function addGroup(Group ...$groups){ |
||
44 | foreach ($groups as $group) { |
||
45 | $this->groups[] = $group; |
||
46 | } |
||
47 | return $this; |
||
48 | } |
||
49 | public function group(string $name, $id = null) { |
||
50 | $g = new Group($name, $id); |
||
51 | $this->groups[] = $g->setSkip($this->allowSkip); |
||
52 | return $g; |
||
53 | } |
||
54 | public function getGroups(){ |
||
55 | $this->orderGroups(); |
||
56 | return $this->groups; |
||
57 | } |
||
58 | public function getGroupsIds() { |
||
59 | $this->orderGroups(); |
||
60 | return array_map(function($a) { return $a->getId(); }, $this->groups); |
||
61 | } |
||
62 | public function orderGroups() { |
||
63 | usort($this->groups, function($a, $b){ |
||
64 | return $a->getOrder() - $b->getOrder(); |
||
65 | }); |
||
66 | return $this->groups; |
||
67 | } |
||
68 | |||
69 | public function allowSkip(){ |
||
70 | $this->allowSkip = true; |
||
71 | return $this; |
||
72 | } |
||
73 | public function disallowSkip(){ |
||
74 | $this->allowSkip = false; |
||
75 | return $this; |
||
76 | } |
||
77 | public function setSkip(bool $skip = false) { |
||
78 | $this->allowSkip = $skip; |
||
79 | return $this; |
||
80 | } |
||
81 | public function getSkip() { |
||
82 | return $this->allowSkip; |
||
83 | } |
||
84 | |||
85 | public function genGames(){ |
||
86 | foreach ($this->groups as $group) { |
||
87 | $group->genGames(); |
||
88 | $this->games = array_merge($this->games, $group->orderGames()); |
||
89 | } |
||
90 | return $this->games; |
||
91 | } |
||
92 | public function getGames() { |
||
93 | return $this->games; |
||
94 | } |
||
95 | public function isPlayed(){ |
||
96 | if (count($this->games) === 0) return false; |
||
97 | foreach ($this->groups as $group) { |
||
98 | if (!$group->isPlayed()) return false; |
||
99 | } |
||
100 | return true; |
||
101 | } |
||
102 | |||
103 | public function addTeam(Team ...$teams) { |
||
104 | foreach ($teams as $team) { |
||
105 | $this->teams[] = $team; |
||
106 | } |
||
107 | return $this; |
||
108 | } |
||
109 | public function team(string $name = '', $id = null) { |
||
110 | $t = new Team($name, $id); |
||
111 | $this->teams[] = $t; |
||
112 | return $t; |
||
113 | } |
||
114 | public function getTeams(bool $ordered = false, $ordering = \TournamentGenerator\Constants::POINTS) { |
||
126 | } |
||
127 | public function sortTeams($ordering = \TournamentGenerator\Constants::POINTS) { |
||
130 | } |
||
131 | |||
132 | public function splitTeams(Group ...$groups) { |
||
133 | |||
134 | if (count($groups) === 0) $groups = $this->getGroups(); |
||
135 | |||
136 | $teams = $this->getTeams(); |
||
137 | shuffle($teams); |
||
138 | |||
139 | while (count($teams) > 0) { |
||
140 | foreach ($groups as $group) { |
||
141 | if (count($teams) > 0) $group->addTeam(array_shift($teams)); |
||
142 | } |
||
143 | } |
||
144 | return $this; |
||
145 | } |
||
146 | |||
147 | public function progress(bool $blank = false){ |
||
148 | foreach ($this->groups as $group) { |
||
149 | $group->progress($blank); |
||
150 | } |
||
152 | } |
||
153 | |||
154 | public function simulate() { |
||
157 | } |
||
158 | public function resetGames() { |
||
165 |