Conditions | 8 |
Paths | 10 |
Total Lines | 77 |
Code Lines | 50 |
Lines | 0 |
Ratio | 0 % |
Changes | 4 | ||
Bugs | 1 | Features | 3 |
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 |
||
155 | public function matchReportAction(Logger $log, Request $request) |
||
156 | { |
||
157 | $log->addNotice("Match data received from " . $request->getClientIp()); |
||
158 | |||
159 | $matchType = $this->params->get('matchType', Match::OFFICIAL); |
||
160 | |||
161 | $teamOneBZIDs = $this->params->get('teamOnePlayers'); |
||
162 | $teamTwoBZIDs = $this->params->get('teamTwoPlayers'); |
||
163 | |||
164 | $teamOnePlayers = $this->bzidsToIdArray($teamOneBZIDs); |
||
165 | $teamTwoPlayers = $this->bzidsToIdArray($teamTwoBZIDs); |
||
166 | |||
167 | if (Match::OFFICIAL === $matchType) { |
||
168 | $teamOne = $this->getTeam($teamOnePlayers); |
||
169 | $teamTwo = $this->getTeam($teamTwoPlayers); |
||
170 | |||
171 | // If we fail to get the the team ID for either the teams or both reported teams are the same team, we cannot |
||
172 | // report the match due to it being illegal. |
||
173 | |||
174 | // An invalid team could be found in either or both teams, so we need to check both teams and log the match |
||
175 | // failure respectively. |
||
176 | $error = true; |
||
177 | if (!$teamOne->isValid()) { |
||
178 | $log->addNotice("The BZIDs ($teamOneBZIDs) were not found on the same team. Match invalidated."); |
||
179 | } elseif (!$teamTwo->isValid()) { |
||
180 | $log->addNotice("The BZIDs ($teamTwoBZIDs) were not found on the same team. Match invalidated."); |
||
181 | } else { |
||
182 | $error = false; |
||
183 | } |
||
184 | |||
185 | if ($error) { |
||
186 | throw new ForbiddenException("An invalid player was found during the match. Please message a referee to manually report the match."); |
||
187 | } |
||
188 | |||
189 | if ($teamOne->isSameAs($teamTwo)) { |
||
190 | $log->addNotice("The '" . $teamOne->getName() . "' team played against each other in an official match. Match invalidated."); |
||
191 | throw new ForbiddenException("Holy sanity check, Batman! The same team can't play against each other in an official match."); |
||
192 | } |
||
193 | } |
||
194 | |||
195 | $map = Map::fetchFromAlias($this->params->get('mapPlayed')); |
||
196 | |||
197 | $match = Match::enterMatch( |
||
198 | (isset($teamOne)) ? $teamOne->getId() : null, |
||
199 | (isset($teamTwo)) ? $teamTwo->getId() : null, |
||
200 | $this->params->get('teamOneWins'), |
||
201 | $this->params->get('teamTwoWins'), |
||
202 | $this->params->get('duration'), |
||
203 | null, |
||
204 | $this->params->get('matchTime'), |
||
205 | $teamOnePlayers, |
||
206 | $teamTwoPlayers, |
||
207 | $this->params->get('server'), |
||
208 | $this->params->get('port'), |
||
209 | $this->params->get('replayFile'), |
||
210 | $map->getId(), |
||
211 | $this->params->get('matchType'), |
||
212 | $this->params->get('teamOneColor'), |
||
213 | $this->params->get('teamTwoColor') |
||
214 | ); |
||
215 | |||
216 | $log->addNotice("Match reported automatically", array( |
||
217 | 'winner' => array( |
||
218 | 'name' => $match->getWinner()->getName(), |
||
219 | 'score' => $match->getScore($match->getWinner()), |
||
220 | ), |
||
221 | 'loser' => array( |
||
222 | 'name' => $match->getLoser()->getName(), |
||
223 | 'score' => $match->getScore($match->getLoser()) |
||
224 | ), |
||
225 | 'eloDiff' => $match->getEloDiff(), |
||
226 | 'map' => $map->getName() |
||
227 | )); |
||
228 | |||
229 | // Output the match stats that will be sent back to BZFS |
||
230 | return $match->getName(); |
||
231 | } |
||
232 | |||
285 |
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: