| Conditions | 11 |
| Paths | 44 |
| Total Lines | 60 |
| Code Lines | 43 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 79 | public function calc_score($studentId = null, $type = null) |
||
| 80 | { |
||
| 81 | $tbl_stats = Database::get_course_table(TABLE_LP_VIEW); |
||
| 82 | $session_id = $this->get_session_id(); |
||
| 83 | |||
| 84 | $sql = "SELECT * FROM $tbl_stats |
||
| 85 | WHERE lp_id = ".$this->get_ref_id(); |
||
| 86 | |||
| 87 | if (isset($studentId)) { |
||
| 88 | $sql .= ' AND user_id = '.intval($studentId); |
||
| 89 | } |
||
| 90 | |||
| 91 | // order by id, that way the student's first attempt is accessed first |
||
| 92 | $sql .= ' ORDER BY view_count DESC'; |
||
| 93 | |||
| 94 | $scores = Database::query($sql); |
||
| 95 | // for 1 student |
||
| 96 | if (isset($studentId)) { |
||
| 97 | if ($data = Database::fetch_assoc($scores)) { |
||
| 98 | return [$data['progress'], 100]; |
||
| 99 | } else { |
||
| 100 | return null; |
||
| 101 | } |
||
| 102 | } else { |
||
| 103 | // all students -> get average |
||
| 104 | $students = []; // user list, needed to make sure we only |
||
| 105 | // take first attempts into account |
||
| 106 | $rescount = 0; |
||
| 107 | $sum = 0; |
||
| 108 | $bestResult = 0; |
||
| 109 | $sumResult = 0; |
||
| 110 | while ($data = Database::fetch_array($scores)) { |
||
| 111 | if (!(array_key_exists($data['user_id'], $students))) { |
||
| 112 | $students[$data['user_id']] = $data['progress']; |
||
| 113 | $rescount++; |
||
| 114 | $sum += $data['progress'] / 100; |
||
| 115 | $sumResult += $data['progress']; |
||
| 116 | |||
| 117 | if ($data['progress'] > $bestResult) { |
||
| 118 | $bestResult = $data['progress']; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if (0 == $rescount) { |
||
| 124 | return [null, null]; |
||
| 125 | } else { |
||
| 126 | switch ($type) { |
||
| 127 | case 'best': |
||
| 128 | return [$bestResult, 100]; |
||
| 129 | break; |
||
|
|
|||
| 130 | case 'average': |
||
| 131 | return [$sumResult / $rescount, 100]; |
||
| 132 | break; |
||
| 133 | case 'ranking': |
||
| 134 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
| 135 | break; |
||
| 136 | default: |
||
| 137 | return [$sum, $rescount]; |
||
| 138 | break; |
||
| 139 | } |
||
| 252 |
The
breakstatement is not necessary if it is preceded for example by areturnstatement:If you would like to keep this construct to be consistent with other
casestatements, you can safely mark this issue as a false-positive.