Conditions | 52 |
Paths | 320 |
Total Lines | 181 |
Code Lines | 120 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
52 | public static function import(array $data) : Base { |
||
53 | // Validate data |
||
54 | ImportValidator::validate($data); |
||
55 | |||
56 | $categories = []; |
||
57 | $rounds = []; |
||
58 | $groups = []; |
||
59 | $teams = []; |
||
60 | $games = []; |
||
61 | |||
62 | $root = null; |
||
63 | if (isset($data['tournament'])) { |
||
64 | $setting = (array) $data['tournament']; |
||
65 | if (empty($setting['type']) || $setting['type'] === 'general') { |
||
66 | $tournament = new Tournament(); |
||
67 | } |
||
68 | else { |
||
69 | $tournament = new $setting['type']; |
||
70 | } |
||
71 | $root = $tournament; |
||
72 | |||
73 | self::setTournament($tournament, $setting); |
||
74 | foreach ($setting['categories'] ?? [] as $id) { |
||
75 | $categories[$id] = $tournament; |
||
76 | } |
||
77 | foreach ($setting['rounds'] ?? [] as $id) { |
||
78 | $rounds[$id] = $tournament; |
||
79 | } |
||
80 | foreach ($setting['groups'] ?? [] as $id) { |
||
81 | $groups[$id] = $tournament; |
||
82 | } |
||
83 | foreach ($setting['teams'] ?? [] as $id) { |
||
84 | $teams[$id] = $tournament; |
||
85 | } |
||
86 | if (isset($setting['games'])) { |
||
87 | $tournament->getGameContainer()->setAutoIncrement(min($setting['games'])); |
||
88 | } |
||
89 | foreach ($setting['games'] ?? [] as $id) { |
||
90 | $games[$id] = $tournament; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | if (isset($data['categories'])) { |
||
95 | foreach ($data['categories'] as $setting) { |
||
96 | $setting = (array) $setting; |
||
97 | $category = new Category($setting['name'] ?? '', $setting['id'] ?? null); |
||
98 | if (!isset($root)) { |
||
99 | $root = $category; |
||
100 | } |
||
101 | self::setSkip($category, $setting); |
||
102 | if (isset($categories[$setting['id'] ?? $category->getId()])) { |
||
103 | $categories[$setting['id'] ?? $category->getId()]->addCategory($category); |
||
104 | } |
||
105 | foreach ($setting['rounds'] ?? [] as $id) { |
||
106 | $rounds[$id] = $category; |
||
107 | } |
||
108 | foreach ($setting['groups'] ?? [] as $id) { |
||
109 | $groups[$id] = $category; |
||
110 | } |
||
111 | foreach ($setting['teams'] ?? [] as $id) { |
||
112 | $teams[$id] = $category; |
||
113 | } |
||
114 | if (isset($setting['games']) && $root === $category) { |
||
115 | $category->getGameContainer()->setAutoIncrement(min($setting['games'])); |
||
116 | } |
||
117 | foreach ($setting['games'] ?? [] as $id) { |
||
118 | $games[$id] = $category; |
||
119 | } |
||
120 | } |
||
121 | } |
||
122 | |||
123 | if (isset($data['rounds'])) { |
||
124 | foreach ($data['rounds'] as $setting) { |
||
125 | $setting = (array) $setting; |
||
126 | $round = new Round($setting['name'] ?? '', $setting['id'] ?? null); |
||
127 | if (!isset($root)) { |
||
128 | $root = $round; |
||
129 | } |
||
130 | self::setSkip($round, $setting); |
||
131 | if (isset($rounds[$setting['id'] ?? $round->getId()])) { |
||
132 | $rounds[$setting['id'] ?? $round->getId()]->addRound($round); |
||
133 | } |
||
134 | foreach ($setting['groups'] ?? [] as $id) { |
||
135 | $groups[$id] = $round; |
||
136 | } |
||
137 | foreach ($setting['teams'] ?? [] as $id) { |
||
138 | $teams[$id] = $round; |
||
139 | } |
||
140 | if (isset($setting['games']) && $root === $round) { |
||
141 | $round->getGameContainer()->setAutoIncrement(min($setting['games'])); |
||
142 | } |
||
143 | foreach ($setting['games'] ?? [] as $id) { |
||
144 | $games[$id] = $round; |
||
145 | } |
||
146 | } |
||
147 | } |
||
148 | |||
149 | $allGroups = []; |
||
150 | if (isset($data['groups'])) { |
||
151 | foreach ($data['groups'] as $setting) { |
||
152 | $setting = (array) $setting; |
||
153 | $group = new Group($setting['name'] ?? '', $setting['id'] ?? null); |
||
154 | $allGroups[$group->getId()] = $group; |
||
155 | if (!isset($root)) { |
||
156 | $root = $group; |
||
157 | } |
||
158 | self::setSkip($group, $setting); |
||
159 | self::setGroup($group, $setting); |
||
160 | if (isset($groups[$setting['id'] ?? $group->getId()])) { |
||
161 | $groups[$setting['id'] ?? $group->getId()]->addGroup($group); |
||
162 | } |
||
163 | foreach ($setting['teams'] ?? [] as $id) { |
||
164 | $teams[$id] = $group; |
||
165 | } |
||
166 | if (isset($setting['games']) && $root === $group) { |
||
167 | $group->getGameContainer()->setAutoIncrement(min($setting['games'])); |
||
168 | } |
||
169 | foreach ($setting['games'] ?? [] as $id) { |
||
170 | $games[$id] = $group; |
||
171 | } |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (isset($data['progressions'])) { |
||
176 | foreach ($data['progressions'] as $setting) { |
||
177 | $setting = (array) $setting; |
||
178 | |||
179 | if (isset($setting['from'], $setting['to'], $allGroups[$setting['from']], $allGroups[$setting['to']])) { |
||
180 | $progression = $allGroups[$setting['from']]->progression($allGroups[$setting['to']], $setting['offset'] ?? 0, $setting['length'] ?? null); |
||
181 | if (isset($setting['filters'])) { |
||
182 | foreach ($setting['filters'] as $filterSetting) { |
||
183 | $filterSetting = (array) $filterSetting; |
||
184 | $groups = array_map(static function($groupId) use ($allGroups) { |
||
185 | return $allGroups[$groupId] ?? null; |
||
186 | }, $filterSetting['groups']); |
||
187 | $filter = new TeamFilter($filterSetting['what'], $filterSetting['how'], $filterSetting['val'], $groups); |
||
188 | $progression->addFilter($filter); |
||
189 | } |
||
190 | } |
||
191 | if (isset($setting['progressed'])) { |
||
192 | $progression->setProgressed($setting['progressed']); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | } |
||
197 | |||
198 | $allTeams = []; |
||
199 | if (isset($data['teams'])) { |
||
200 | foreach ($data['teams'] as $setting) { |
||
201 | $setting = (array) $setting; |
||
202 | $team = new Team($setting['name'] ?? '', $setting['id'] ?? null); |
||
203 | if (!isset($root)) { |
||
204 | $root = $team; |
||
205 | } |
||
206 | $allTeams[$team->getId()] = $team; |
||
207 | if (isset($teams[$setting['id'] ?? $team->getId()])) { |
||
208 | $teams[$setting['id'] ?? $team->getId()]->addTeam($team); |
||
209 | } |
||
210 | } |
||
211 | } |
||
212 | |||
213 | if (isset($data['games'])) { |
||
214 | foreach ($data['games'] as $setting) { |
||
215 | $setting = (array) $setting; |
||
216 | $gameTeams = []; |
||
217 | foreach ($setting['teams'] ?? [] as $teamId) { |
||
218 | $gameTeams[] = $allTeams[$teamId]; |
||
219 | } |
||
220 | if (isset($setting['id'], $games[$setting['id']])) { |
||
221 | $game = $games[$setting['id']]->game($gameTeams); |
||
222 | if (isset($setting['scores'])) { |
||
223 | $scores = array_map(static function($info) { |
||
224 | return ((array) $info)['score'] ?? 0; |
||
225 | }, $setting['scores']); |
||
226 | $game->setResults($scores); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | } |
||
231 | |||
232 | return $root; |
||
|
|||
233 | } |
||
334 | } |