| Conditions | 1 |
| Paths | 1 |
| Total Lines | 113 |
| Code Lines | 50 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 7 | public function showAction($season, Request $request) |
||
|
|
|||
| 8 | { |
||
| 9 | $term = $year = ''; |
||
| 10 | $this->parseSeason($season, $term, $year); |
||
| 11 | |||
| 12 | // Because this query can't be created efficiently using our QueryBuilder, let's do things manually |
||
| 13 | $db = Database::getInstance(); |
||
| 14 | $seasonQuery = sprintf(" |
||
| 15 | SELECT %s, e.elo_new AS elo FROM players p |
||
| 16 | INNER JOIN player_elo e ON e.user_id = p.id |
||
| 17 | INNER JOIN ( |
||
| 18 | SELECT |
||
| 19 | user_id, |
||
| 20 | MAX(match_id) AS last_match |
||
| 21 | FROM |
||
| 22 | player_elo |
||
| 23 | WHERE |
||
| 24 | season_period = ? AND season_year = ? |
||
| 25 | GROUP BY |
||
| 26 | user_id |
||
| 27 | ) i ON i.user_id = p.id AND i.last_match = e.match_id |
||
| 28 | WHERE p.status = 'active' |
||
| 29 | ORDER BY elo DESC, p.username ASC LIMIT 10; |
||
| 30 | ", Player::getEagerColumns('p')); |
||
| 31 | $results = $db->query($seasonQuery, [$term, $year]); |
||
| 32 | $players_w_elos = Player::createFromDatabaseResults($results); |
||
| 33 | |||
| 34 | $seasonRange = Season::getCurrentSeasonRange($term); |
||
| 35 | $matchQuery = Match::getQueryBuilder(); |
||
| 36 | $matchQuery |
||
| 37 | ->active() |
||
| 38 | ->where('time')->isAfter($seasonRange->getStartOfRange($year), true) |
||
| 39 | ->where('time')->isBefore($seasonRange->getEndOfRange($year), true) |
||
| 40 | ; |
||
| 41 | |||
| 42 | $fmQuery = clone $matchQuery; |
||
| 43 | $offiQuery = clone $matchQuery; |
||
| 44 | |||
| 45 | $fmCount = $fmQuery->where('type')->equals(Match::FUN)->count(); |
||
| 46 | $offiCount = $offiQuery->where('type')->equals(Match::OFFICIAL)->count(); |
||
| 47 | |||
| 48 | Map::getQueryBuilder()->addToCache(); |
||
| 49 | $mapQuery = ' |
||
| 50 | SELECT |
||
| 51 | map AS map_id, |
||
| 52 | COUNT(*) AS match_count |
||
| 53 | FROM |
||
| 54 | matches |
||
| 55 | WHERE |
||
| 56 | timestamp >= ? AND timestamp <= ? AND map IS NOT NULL |
||
| 57 | GROUP BY |
||
| 58 | map |
||
| 59 | HAVING |
||
| 60 | match_count > 0 |
||
| 61 | ORDER BY |
||
| 62 | match_count DESC |
||
| 63 | '; |
||
| 64 | $results = $db->query($mapQuery, [ |
||
| 65 | $seasonRange->getStartOfRange($year), |
||
| 66 | $seasonRange->getEndOfRange($year), |
||
| 67 | ]); |
||
| 68 | |||
| 69 | $mapIDs = array_column($results, 'map_id'); |
||
| 70 | $maps = Map::arrayIdToModel($mapIDs); |
||
| 71 | $mapCount = array_combine($mapIDs, $results); |
||
| 72 | |||
| 73 | $matchCount = " |
||
| 74 | SELECT |
||
| 75 | p.user_id, |
||
| 76 | SUM(m.match_type = ?) AS match_count |
||
| 77 | FROM |
||
| 78 | match_participation p |
||
| 79 | INNER JOIN |
||
| 80 | matches m ON m.id = p.match_id |
||
| 81 | WHERE |
||
| 82 | m.timestamp >= ? AND m.timestamp < ? |
||
| 83 | GROUP BY |
||
| 84 | p.user_id |
||
| 85 | ORDER BY |
||
| 86 | match_count DESC |
||
| 87 | LIMIT 10 |
||
| 88 | "; |
||
| 89 | $fmResults = $db->query($matchCount, [ |
||
| 90 | 'fm', |
||
| 91 | $seasonRange->getStartOfRange($year), |
||
| 92 | $seasonRange->getEndOfRange($year), |
||
| 93 | ]); |
||
| 94 | $offiResults = $db->query($matchCount, [ |
||
| 95 | 'official', |
||
| 96 | $seasonRange->getStartOfRange($year), |
||
| 97 | $seasonRange->getEndOfRange($year), |
||
| 98 | ]); |
||
| 99 | |||
| 100 | return [ |
||
| 101 | 'season' => ucfirst($term), |
||
| 102 | 'year' => $year, |
||
| 103 | 'players' => $players_w_elos, |
||
| 104 | 'fmCount' => $fmCount, |
||
| 105 | 'offiCount' => $offiCount, |
||
| 106 | 'maps' => $maps, |
||
| 107 | 'mapCount' => $mapCount, |
||
| 108 | 'player_matches' => [ |
||
| 109 | 'fm' => [ |
||
| 110 | 'players' => Player::arrayIdToModel(array_column($fmResults, 'user_id')), |
||
| 111 | 'count' => array_column($fmResults, 'match_count'), |
||
| 112 | ], |
||
| 113 | 'official' => [ |
||
| 114 | 'players' => Player::arrayIdToModel(array_column($offiResults, 'user_id')), |
||
| 115 | 'count' => array_column($offiResults, 'match_count'), |
||
| 116 | ], |
||
| 117 | ], |
||
| 118 | ]; |
||
| 119 | } |
||
| 120 | |||
| 172 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.