| Total Complexity | 51 |
| Total Lines | 350 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like LeaderboardController 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 LeaderboardController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class LeaderboardController extends Controller |
||
| 16 | { |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Display the specified leaderboard resource. |
||
| 20 | * |
||
| 21 | * @param int $id |
||
| 22 | * @return \Illuminate\Http\Response |
||
| 23 | */ |
||
| 24 | public function show($id) |
||
| 25 | { |
||
| 26 | try { |
||
| 27 | return response(Leaderboard::findOrFail($id), 200); |
||
| 28 | } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { |
||
| 29 | return response(['error' => 'Leaderboard not found.'], 400); |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | /** |
||
| 34 | * Display the specified leaderboard current timescope data array. |
||
| 35 | * |
||
| 36 | * @param int $id |
||
| 37 | * @return \Illuminate\Http\Response |
||
| 38 | */ |
||
| 39 | public function showCurrent(Request $request, $id) |
||
| 40 | { |
||
| 41 | try { |
||
| 42 | $leaderboard = Leaderboard::findOrFail($id); |
||
| 43 | } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { |
||
| 44 | return response(['error' => 'Leaderboard not found.'], 400); |
||
| 45 | } |
||
| 46 | |||
| 47 | $result = ['end_at' => $leaderboard->currentTimescope->end_at]; |
||
| 48 | $result['score_sum_rewards'] = $leaderboard->scoreSumRewards(); |
||
| 49 | $result['score_sum_rank_rewards'] = $leaderboard->scoreSumRankRewards(); |
||
| 50 | $result['highscore_rank_rewards'] = $leaderboard->highscoreRankRewards(); |
||
| 51 | $result['utc'] = date('Y-m-d H:i:s'); |
||
| 52 | |||
| 53 | return response($result, 200); |
||
| 54 | } |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Display the an array of score-sums of the current leaderboard timescope. |
||
| 58 | * |
||
| 59 | * @param int $id |
||
| 60 | * @return \Illuminate\Http\Response |
||
| 61 | */ |
||
| 62 | public function showScoreSums(Request $request, $id) |
||
| 63 | { |
||
| 64 | try { |
||
| 65 | $leaderboard = Leaderboard::findOrFail($id); |
||
| 66 | } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { |
||
| 67 | return response(['error' => 'Leaderboard not found.'], 400); |
||
| 68 | } |
||
| 69 | |||
| 70 | $scoresCol = $leaderboard->currentTimescope->orderedScoresByScoreSum; |
||
| 71 | |||
| 72 | if ($request->filled('player_id')) { // If specific player |
||
| 73 | |||
| 74 | $scoreCount = $scoresCol->count(); |
||
| 75 | $myScoreCol = $scoresCol->where('player_id', $request->query('player_id')); |
||
| 76 | |||
| 77 | if ($myScoreCol->count() > 0) { // If my score is previously uploaded |
||
| 78 | |||
| 79 | $myRank = 1; |
||
| 80 | |||
| 81 | for ($i = 0; $i < $scoreCount; $i++) { |
||
| 82 | if ($scoresCol[$i]->player_id == $request->query('player_id')) { |
||
| 83 | $myRank = $i + 1; |
||
| 84 | break; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | $myScoreCol->first()->rank = $myRank; |
||
| 89 | |||
| 90 | $aboveScoresCol = $scoresCol->slice(max(0, $myRank - 21), min($myRank - 1, 20)); |
||
| 91 | $aboveRank = ($aboveScoresCol->count() >= 20) ? ($myRank - 20) : 1; |
||
| 92 | foreach ($aboveScoresCol as $aboveScore) { |
||
| 93 | $aboveScore->rank = $aboveRank; |
||
| 94 | $aboveRank++; |
||
| 95 | } |
||
| 96 | |||
| 97 | $belowScoresCol = $scoresCol->slice($myRank, 20); |
||
| 98 | $belowRank = $myRank + 1; |
||
| 99 | foreach ($belowScoresCol as $belowScore) { |
||
| 100 | $belowScore->rank = $belowRank; |
||
| 101 | $belowRank++; |
||
| 102 | } |
||
| 103 | $scores = $aboveScoresCol->merge($myScoreCol)->merge($belowScoresCol); |
||
| 104 | |||
| 105 | } else { // If no my score yet, just show the last 20 rows |
||
| 106 | |||
| 107 | $scoresCol = $scoresCol->slice(-20, 20); |
||
| 108 | $rank = max(1, $scoreCount - 19); |
||
| 109 | foreach ($scoresCol as $score) { |
||
| 110 | $score->rank = $rank; |
||
| 111 | $rank++; |
||
| 112 | } |
||
| 113 | |||
| 114 | $scores = $scoresCol->toArray(); |
||
| 115 | |||
| 116 | $player = Player::find($request->query('player_id')); |
||
| 117 | $myScore = ["player_id" => $request->query('player_id'), "name" => $player->name, "score" => "0"]; |
||
| 118 | |||
| 119 | $scores = array_merge($scores, [$myScore]); |
||
| 120 | |||
| 121 | } |
||
| 122 | |||
| 123 | } else { // If no specific player, show the first 20 rows |
||
| 124 | |||
| 125 | $scoresCol = $scoresCol->slice(0, 20); |
||
| 126 | // Manually add rank 1~20 |
||
| 127 | $rank = 1; |
||
| 128 | foreach ($scoresCol as $score) { |
||
| 129 | $score->rank = $rank; |
||
| 130 | $rank++; |
||
| 131 | } |
||
| 132 | |||
| 133 | $scores = $scoresCol->toArray(); |
||
| 134 | |||
| 135 | } |
||
| 136 | |||
| 137 | return $scores; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Display the an array of highscores of the current leaderboard timescop. |
||
| 142 | * |
||
| 143 | * @param int $id |
||
| 144 | * @return \Illuminate\Http\Response |
||
| 145 | */ |
||
| 146 | public function showHighscores(Request $request, $id) |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Display the rewards of highscores of the current leaderboard timescope. |
||
| 210 | * |
||
| 211 | * @param int $id |
||
| 212 | * @return \Illuminate\Http\Response |
||
| 213 | */ |
||
| 214 | public function showRewards(Request $request, $id) |
||
| 215 | { |
||
| 216 | $validator = Validator::make($request->all(), [ |
||
| 217 | 'player_id' => 'required|numeric', |
||
| 218 | ]); |
||
| 219 | if ($validator->fails()) { |
||
| 220 | return response($validator->messages(), 400); |
||
| 221 | } |
||
| 222 | |||
| 223 | try { |
||
| 224 | $leaderboard = Leaderboard::findOrFail($id); |
||
| 225 | } catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) { |
||
| 226 | return response(['error' => 'Leaderboard not found.'], 400); |
||
| 227 | } |
||
| 228 | $leaderboardTimescope = $leaderboard->currentTimescope; |
||
| 229 | |||
| 230 | $result = []; |
||
| 231 | |||
| 232 | // Get rewarded score |
||
| 233 | $leaderboardPlayerReward = LeaderboardPlayerReward::find($leaderboardTimescope->id, $request->query('player_id')); |
||
| 234 | if ($leaderboardPlayerReward) { // Show the score of the obtained score reward (current timescope period) |
||
| 235 | $result['rewarded_score'] = $leaderboardPlayerReward->score_sum; |
||
| 236 | } |
||
| 237 | |||
| 238 | $lastScoreSumRank = 0; $lastHighscoreRank = 0; |
||
| 239 | // Get last rank |
||
| 240 | $rankReward = LeaderboardPlayerReward::find($leaderboardTimescope->id, $request->player_id); |
||
| 241 | if (!$rankReward) { // Not yet obtained rank reward (last timescope period) |
||
| 242 | $lastLeaderboardTimescope = $leaderboardTimescope->previous(); |
||
| 243 | if ($lastLeaderboardTimescope != null) { |
||
| 244 | $lastScore = LeaderboardScore::find($lastLeaderboardTimescope->id, $request->player_id); |
||
| 245 | if ($lastScore) { // Has last my score |
||
| 246 | $lastScoreSumRank = LeaderboardScore::where('leaderboard_timescope_id', $lastLeaderboardTimescope->id)->where('score_sum', '>', $lastScore->score_sum)->count() + 1; |
||
| 247 | $lastHighscoreRank = LeaderboardScore::where('leaderboard_timescope_id', $lastLeaderboardTimescope->id)->where('highscore', '>', $lastScore->highscore)->count() + 1; |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | if ($lastScoreSumRank > 0) { |
||
| 253 | $result['last_score_sum_rank'] = $lastScoreSumRank; |
||
| 254 | } |
||
| 255 | if ($lastHighscoreRank > 0) { |
||
| 256 | $result['last_highscore_rank'] = $lastHighscoreRank; |
||
| 257 | } |
||
| 258 | |||
| 259 | if (empty($result)) { |
||
| 260 | $result = ['success' => 1]; |
||
| 261 | } |
||
| 262 | |||
| 263 | return response($result, 200); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Update or add a specified leaderboard score resource in storage for a player . |
||
| 268 | * |
||
| 269 | * @param Request $request |
||
| 270 | * @param int $id |
||
| 271 | * @return \Illuminate\Http\Response |
||
| 272 | */ |
||
| 273 | public function updateScore(Request $request, $id) |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Update or add a specified leaderboard reward resource in storage for a player. |
||
| 308 | * |
||
| 309 | * @param Request $request |
||
| 310 | * @param int $id |
||
| 311 | * @return \Illuminate\Http\Response |
||
| 312 | */ |
||
| 313 | public function updateReward(Request $request, $id) |
||
| 368 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths