| Conditions | 10 | 
| Paths | 933 | 
| Total Lines | 73 | 
| Code Lines | 49 | 
| Lines | 0 | 
| Ratio | 0 % | 
| Changes | 2 | ||
| Bugs | 1 | Features | 1 | 
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  | 
            ||
| 49 | public function cacheStatistics()  | 
            ||
| 50 |     { | 
            ||
| 51 |         try { | 
            ||
| 52 | $statistics = [];  | 
            ||
| 53 |             $homeworkProblems = $this->problems->sortBy('order_index'); | 
            ||
| 54 |             $users = $this->group->members()->where('role', '>=', 1)->get(); | 
            ||
| 55 |             $userIDArr = $users->pluck('uid'); | 
            ||
| 56 | $defaultVerdict = [];  | 
            ||
| 57 | |||
| 58 |             foreach ($homeworkProblems as $homeworkProblem) { | 
            ||
| 59 | $statistics['problems'][] = [  | 
            ||
| 60 | 'pid' => $homeworkProblem->problem_id,  | 
            ||
| 61 | 'readable_name' => $homeworkProblem->problem->readable_name,  | 
            ||
| 62 | ];  | 
            ||
| 63 | $defaultVerdict[$homeworkProblem->problem_id] = [  | 
            ||
| 64 | "icon" => "checkbox-blank-circle-outline",  | 
            ||
| 65 | "color" => "wemd-grey-text"  | 
            ||
| 66 | ];  | 
            ||
| 67 | }  | 
            ||
| 68 | |||
| 69 |             foreach ($users as $user) { | 
            ||
| 70 | $statistics['data'][$user->uid] = [  | 
            ||
| 71 | 'name' => $user->user->name,  | 
            ||
| 72 | 'nick_name' => blank($user->nick_name) ? null : $user->nick_name,  | 
            ||
| 73 | 'solved' => 0,  | 
            ||
| 74 | 'attempted' => 0,  | 
            ||
| 75 | 'verdict' => $defaultVerdict  | 
            ||
| 76 | ];  | 
            ||
| 77 | }  | 
            ||
| 78 | |||
| 79 | $endedAt = Carbon::parse($this->ended_at);  | 
            ||
| 80 | |||
| 81 |             foreach ($homeworkProblems as $homeworkProblem) { | 
            ||
| 82 | $userProbIDArr = [];  | 
            ||
| 83 | |||
| 84 |                 foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Accepted'])->get() as $acceptedRecord) { | 
            ||
| 85 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [  | 
            ||
| 86 | "icon" => "checkbox-blank-circle",  | 
            ||
| 87 | "color" => $acceptedRecord['color']  | 
            ||
| 88 | ];  | 
            ||
| 89 | $statistics['data'][$acceptedRecord['uid']]['solved']++;  | 
            ||
| 90 | $userProbIDArr[] = $acceptedRecord['uid'];  | 
            ||
| 91 | }  | 
            ||
| 92 | |||
| 93 |                 foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt, ['Partially Accepted'])->get() as $acceptedRecord) { | 
            ||
| 94 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [  | 
            ||
| 95 | "icon" => "cisco-webex",  | 
            ||
| 96 | "color" => $acceptedRecord['color']  | 
            ||
| 97 | ];  | 
            ||
| 98 | $statistics['data'][$acceptedRecord['uid']]['attempted']++;  | 
            ||
| 99 | $userProbIDArr[] = $acceptedRecord['uid'];  | 
            ||
| 100 | }  | 
            ||
| 101 | |||
| 102 |                 foreach ($homeworkProblem->problem->users_latest_submission($userIDArr->diff($userProbIDArr), null, $endedAt)->get() as $acceptedRecord) { | 
            ||
| 103 | $statistics['data'][$acceptedRecord['uid']]['verdict'][$homeworkProblem->problem_id] = [  | 
            ||
| 104 | "icon" => "cisco-webex",  | 
            ||
| 105 | "color" => $acceptedRecord['color']  | 
            ||
| 106 | ];  | 
            ||
| 107 | $statistics['data'][$acceptedRecord['uid']]['attempted']++;  | 
            ||
| 108 | $userProbIDArr[] = $acceptedRecord['uid'];  | 
            ||
| 109 | }  | 
            ||
| 110 | }  | 
            ||
| 111 | |||
| 112 |             usort($statistics['data'], function ($a, $b) { | 
            ||
| 113 | return $b["solved"] == $a["solved"] ? $b["attempted"] <=> $a["attempted"] : $b["solved"] <=> $a["solved"];  | 
            ||
| 114 | });  | 
            ||
| 115 | |||
| 116 | $statistics['timestamp'] = time();  | 
            ||
| 117 | Cache::tags(['homework', 'statistics'])->put($this->id, $statistics, 360);  | 
            ||
| 118 | return $statistics;  | 
            ||
| 119 |         } catch (Exception $e) { | 
            ||
| 120 | Log::alert($e);  | 
            ||
| 121 | return false;  | 
            ||
| 122 | }  | 
            ||
| 125 |