Complex classes like LeagueOverseerHookController 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 LeagueOverseerHookController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class LeagueOverseerHookController extends PlainTextController |
||
9 | { |
||
10 | /** |
||
11 | * The API version of the server performing the request |
||
12 | * @var int |
||
13 | */ |
||
14 | private $version; |
||
15 | |||
16 | /** |
||
17 | * The parameter bag representing the $_GET or $_POST array |
||
18 | * @var Symfony\Component\HttpFoundation\ParameterBag |
||
19 | */ |
||
20 | private $params; |
||
21 | |||
22 | /** |
||
23 | * {@inheritdoc} |
||
24 | */ |
||
25 | 22 | public function setUp() |
|
60 | |||
61 | /** |
||
62 | * @ApiDoc( |
||
63 | * description="Query the LeagueOverseer API", |
||
64 | * parameters={ |
||
65 | * {"name"="query", "dataType"="string", "required"=true, "description"="query type"}, |
||
66 | * {"name"="apiVersion", "dataType"="integer", "required"=false, "description"="LeagueOverseer API version"} |
||
67 | * } |
||
68 | * ) |
||
69 | * @todo Test/improve/revoke support for API version 0 |
||
70 | */ |
||
71 | 22 | public function queryAction() |
|
88 | |||
89 | public function teamNameAction() |
||
113 | |||
114 | public function teamNameDumpAction() |
||
154 | |||
155 | 22 | public function matchReportAction(Logger $log, Request $request) |
|
156 | { |
||
157 | 22 | $log->addNotice("Match data received from " . $request->getClientIp()); |
|
158 | 22 | $log->addDebug("Debug match data query: " . http_build_query($this->params->all())); |
|
159 | |||
160 | 22 | $matchType = $this->params->get('matchType', Match::OFFICIAL); |
|
161 | |||
162 | 22 | $teamOneBZIDs = $this->params->get('teamOnePlayers'); |
|
163 | 22 | $teamTwoBZIDs = $this->params->get('teamTwoPlayers'); |
|
164 | |||
165 | 22 | $teamOnePlayers = $this->bzidsToIdArray($teamOneBZIDs); |
|
166 | 22 | $teamTwoPlayers = $this->bzidsToIdArray($teamTwoBZIDs); |
|
167 | |||
168 | 22 | $teamOne = $teamTwo = null; |
|
169 | |||
170 | 22 | if (Match::OFFICIAL === $matchType) { |
|
171 | 21 | $teamOne = $this->getTeam($teamOnePlayers); |
|
172 | 21 | $teamTwo = $this->getTeam($teamTwoPlayers); |
|
173 | |||
174 | 21 | if ($teamOne->isValid() && $teamTwo->isValid() && $teamOne->isSameAs($teamTwo)) { |
|
175 | 1 | $log->addNotice("The '" . $teamOne->getName() . "' team played against each other in an official match. Match invalidated."); |
|
176 | 1 | throw new ForbiddenException("Holy sanity check, Batman! The same team can't play against each other in an official match."); |
|
177 | } |
||
178 | 21 | } elseif (Match::FUN === $matchType) { |
|
179 | 1 | if (count($teamOnePlayers) < 2 || count($teamTwoPlayers) < 2) { |
|
180 | throw new ForbiddenException("You are not allowed to report a match with less than 2 players per team."); |
||
181 | } |
||
182 | 1 | } |
|
183 | |||
184 | 21 | $map = Map::fetchFromAlias($this->params->get('mapPlayed')); |
|
185 | |||
186 | 21 | $match = Match::enterMatch( |
|
187 | 21 | ($teamOne !== null) ? $teamOne->getId() : null, |
|
188 | 21 | ($teamTwo !== null) ? $teamTwo->getId() : null, |
|
189 | 21 | $this->params->get('teamOneWins'), |
|
190 | 21 | $this->params->get('teamTwoWins'), |
|
191 | 21 | $this->params->get('duration'), |
|
192 | 21 | null, |
|
193 | 21 | $this->params->get('matchTime'), |
|
194 | 21 | $teamOnePlayers, |
|
195 | 21 | $teamTwoPlayers, |
|
196 | 21 | $this->params->get('server'), |
|
197 | 21 | $this->params->get('replayFile'), |
|
198 | 21 | $map->getId(), |
|
199 | 21 | $this->params->get('matchType'), |
|
200 | 21 | $this->params->get('teamOneColor'), |
|
201 | 21 | $this->params->get('teamTwoColor') |
|
202 | 21 | ); |
|
203 | |||
204 | 21 | $log->addNotice("Match reported automatically", array( |
|
205 | 'winner' => array( |
||
206 | 21 | 'name' => $match->getWinner()->getName(), |
|
207 | 21 | 'score' => $match->getScore($match->getWinner()), |
|
208 | 21 | ), |
|
209 | 'loser' => array( |
||
210 | 21 | 'name' => $match->getLoser()->getName(), |
|
211 | 21 | 'score' => $match->getScore($match->getLoser()) |
|
212 | 21 | ), |
|
213 | 21 | 'eloDiff' => $match->getEloDiff(), |
|
214 | 21 | 'type' => $matchType, |
|
215 | 21 | 'map' => $map->getName() |
|
216 | 21 | )); |
|
217 | |||
218 | 21 | $bzfsAnnouncement = $match->getName(); |
|
219 | |||
220 | 21 | if (!$match->getWinner()->supportsMatchCount() || !$match->getLoser()->supportsMatchCount()) { |
|
221 | 13 | if ($match->getWinner()->supportsMatchCount()) { |
|
222 | 4 | $bzfsAnnouncement .= sprintf("\n %s: +%d", $match->getWinner()->getName(), $match->getEloDiff()); |
|
223 | 13 | } elseif ($match->getLoser()->supportsMatchCount()) { |
|
224 | 4 | $diff = -$match->getEloDiff(); |
|
225 | 4 | $bzfsAnnouncement .= sprintf("\n %s: %d", $match->getLoser()->getName(), $diff); |
|
226 | 4 | } |
|
227 | 13 | } |
|
228 | |||
229 | 21 | if ($match->isOfficial()) { |
|
230 | $inverseSymbol = ( |
||
231 | 20 | $match->getTeamMatchType() === Match::TEAM_V_TEAM && $match->isDraw() && |
|
232 | 6 | ($match->getWinner()->isSameAs($match->getTeamB()) || $match->getPlayerEloDiff(false) < 0) |
|
233 | 20 | ); |
|
234 | |||
235 | 20 | $symbol = ($inverseSymbol) ? '-/+' : '+/-'; |
|
236 | 20 | $bzfsAnnouncement .= sprintf("\n player elo: %s %d", $symbol, $match->getPlayerEloDiff()); |
|
237 | 20 | } |
|
238 | |||
239 | // Output the match stats that will be sent back to BZFS |
||
240 | 21 | return $bzfsAnnouncement; |
|
241 | } |
||
242 | |||
243 | /** |
||
244 | * Convert a comma-separated list of bzids to player IDs so we can pass |
||
245 | * them to Match::enterMatch() |
||
246 | * |
||
247 | * @param string $players A comma-separated list of BZIDs |
||
248 | * @return int[] A list of Player IDs |
||
249 | */ |
||
250 | 22 | private function bzidsToIdArray($players) |
|
251 | { |
||
252 | 22 | $players = explode(',', $players); |
|
253 | |||
254 | 22 | foreach ($players as &$player) { |
|
255 | 22 | $player = Player::getFromBZID($player)->getId(); |
|
256 | 22 | } |
|
257 | |||
258 | 22 | return $players; |
|
259 | } |
||
260 | |||
261 | /** |
||
262 | * Queries the database to get the team which a conversation of players belong to |
||
263 | * |
||
264 | * @param int[] $players The IDs of players |
||
265 | * @return Team The team |
||
266 | */ |
||
267 | 21 | private function getTeam($players) |
|
268 | { |
||
269 | 21 | $team = null; |
|
270 | |||
271 | 21 | foreach ($players as $id) { |
|
272 | 21 | $player = Player::get($id); |
|
273 | |||
274 | 21 | if ($player->isTeamless()) { |
|
275 | 12 | return Team::invalid(); |
|
276 | 21 | } elseif ($team == null) { |
|
277 | 21 | $team = $player->getTeam(); |
|
278 | 21 | } elseif ($team->getId() != $player->getTeam()->getId()) { |
|
279 | // This player is on a different team from the previous player! |
||
280 | return Team::invalid(); |
||
281 | } |
||
282 | 21 | } |
|
283 | |||
284 | 17 | return $team; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * {@inheritdoc} |
||
289 | */ |
||
290 | 22 | protected static function getLogChannel() |
|
294 | } |
||
295 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: