| Conditions | 17 |
| Paths | 649 |
| Total Lines | 132 |
| Code Lines | 78 |
| 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 |
||
| 148 | public function calc_score($stud_id = null, $type = null) |
||
| 149 | { |
||
| 150 | $stud_id = (int) $stud_id; |
||
| 151 | $em = Database::getManager(); |
||
| 152 | $data = $this->get_exercise_data(); |
||
| 153 | |||
| 154 | if (empty($data)) { |
||
| 155 | return []; |
||
| 156 | } |
||
| 157 | $id = $data['id']; |
||
| 158 | $session = $em->find('ChamiloCoreBundle:Session', api_get_session_id()); |
||
| 159 | |||
| 160 | $assignment = $em |
||
| 161 | ->getRepository('ChamiloCourseBundle:CStudentPublication') |
||
| 162 | ->findOneBy([ |
||
| 163 | 'cId' => $this->course_id, |
||
| 164 | 'id' => $id, |
||
| 165 | 'session' => $session, |
||
| 166 | ]) |
||
| 167 | ; |
||
| 168 | |||
| 169 | $parentId = !$assignment ? 0 : $assignment->getId(); |
||
| 170 | |||
| 171 | if (empty($session)) { |
||
| 172 | $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a |
||
| 173 | WHERE |
||
| 174 | a.cId = :course AND |
||
| 175 | a.active = :active AND |
||
| 176 | a.parentId = :parent AND |
||
| 177 | a.session is null AND |
||
| 178 | a.qualificatorId <> 0 |
||
| 179 | '; |
||
| 180 | |||
| 181 | $params = [ |
||
| 182 | 'course' => $this->course_id, |
||
| 183 | 'parent' => $parentId, |
||
| 184 | 'active' => true, |
||
| 185 | ]; |
||
| 186 | } else { |
||
| 187 | $dql = 'SELECT a FROM ChamiloCourseBundle:CStudentPublication a |
||
| 188 | WHERE |
||
| 189 | a.cId = :course AND |
||
| 190 | a.active = :active AND |
||
| 191 | a.parentId = :parent AND |
||
| 192 | a.session = :session AND |
||
| 193 | a.qualificatorId <> 0 |
||
| 194 | '; |
||
| 195 | |||
| 196 | $params = [ |
||
| 197 | 'course' => $this->course_id, |
||
| 198 | 'parent' => $parentId, |
||
| 199 | 'session' => $session, |
||
| 200 | 'active' => true, |
||
| 201 | ]; |
||
| 202 | } |
||
| 203 | |||
| 204 | if (!empty($stud_id)) { |
||
| 205 | $dql .= ' AND a.userId = :student '; |
||
| 206 | $params['student'] = $stud_id; |
||
| 207 | } |
||
| 208 | |||
| 209 | $order = api_get_setting('student_publication_to_take_in_gradebook'); |
||
| 210 | |||
| 211 | switch ($order) { |
||
| 212 | case 'last': |
||
| 213 | // latest attempt |
||
| 214 | $dql .= ' ORDER BY a.sentDate DESC'; |
||
| 215 | break; |
||
| 216 | case 'first': |
||
| 217 | default: |
||
| 218 | // first attempt |
||
| 219 | $dql .= ' ORDER BY a.id'; |
||
| 220 | break; |
||
| 221 | } |
||
| 222 | |||
| 223 | $scores = $em->createQuery($dql)->execute($params); |
||
| 224 | |||
| 225 | // for 1 student |
||
| 226 | if (!empty($stud_id)) { |
||
| 227 | if (!count($scores)) { |
||
| 228 | return ''; |
||
| 229 | } |
||
| 230 | |||
| 231 | $data = $scores[0]; |
||
| 232 | |||
| 233 | return [ |
||
| 234 | $data->getQualification(), |
||
| 235 | $assignment->getQualification(), |
||
| 236 | ]; |
||
| 237 | } |
||
| 238 | |||
| 239 | $students = []; // user list, needed to make sure we only |
||
| 240 | // take first attempts into account |
||
| 241 | $rescount = 0; |
||
| 242 | $sum = 0; |
||
| 243 | $bestResult = 0; |
||
| 244 | $weight = 0; |
||
| 245 | $sumResult = 0; |
||
| 246 | |||
| 247 | foreach ($scores as $data) { |
||
| 248 | if (!(array_key_exists($data->getUserId(), $students))) { |
||
| 249 | if ($assignment->getQualification() != 0) { |
||
| 250 | $students[$data->getUserId()] = $data->getQualification(); |
||
| 251 | $rescount++; |
||
| 252 | $sum += $data->getQualification() / $assignment->getQualification(); |
||
| 253 | $sumResult += $data->getQualification(); |
||
| 254 | |||
| 255 | if ($data->getQualification() > $bestResult) { |
||
| 256 | $bestResult = $data->getQualification(); |
||
| 257 | } |
||
| 258 | $weight = $assignment->getQualification(); |
||
| 259 | } |
||
| 260 | } |
||
| 261 | } |
||
| 262 | |||
| 263 | if ($rescount == 0) { |
||
| 264 | return null; |
||
| 265 | } |
||
| 266 | |||
| 267 | switch ($type) { |
||
| 268 | case 'best': |
||
| 269 | return [$bestResult, $weight]; |
||
| 270 | break; |
||
| 271 | case 'average': |
||
| 272 | return [$sumResult / $rescount, $weight]; |
||
| 273 | break; |
||
| 274 | case 'ranking': |
||
| 275 | return AbstractLink::getCurrentUserRanking($stud_id, $students); |
||
| 276 | break; |
||
| 277 | default: |
||
| 278 | return [$sum, $rescount]; |
||
| 279 | break; |
||
| 280 | } |
||
| 426 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return,dieorexitstatements that have been added for debug purposes.In the above example, the last
return falsewill never be executed, because a return statement has already been met in every possible execution path.