Conditions | 15 |
Paths | 82 |
Total Lines | 81 |
Code Lines | 53 |
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 |
||
88 | public function calc_score($studentId = null, $type = null) |
||
89 | { |
||
90 | $studentId = (int) $studentId; |
||
91 | $assignment = $this->getStudentPublication(); |
||
92 | |||
93 | if (empty($assignment)) { |
||
94 | return []; |
||
95 | } |
||
96 | $session = api_get_session_entity($this->get_session_id()); |
||
97 | $course = api_get_course_entity($this->course_id); |
||
98 | |||
99 | $qb = Container::getStudentPublicationRepository() |
||
100 | ->getStudentAssignments($assignment, $course, $session, null, $studentId ? api_get_user_entity($studentId) : null); |
||
101 | |||
102 | $order = api_get_setting('student_publication_to_take_in_gradebook'); |
||
103 | |||
104 | switch ($order) { |
||
105 | case 'last': |
||
106 | $qb->orderBy('resource.sentDate', 'DESC'); |
||
107 | break; |
||
108 | case 'first': |
||
109 | default: |
||
110 | $qb->orderBy('resource.iid', 'ASC'); |
||
111 | break; |
||
112 | } |
||
113 | |||
114 | $scores = $qb->getQuery()->getResult(); |
||
115 | |||
116 | // for 1 student |
||
117 | if (!empty($studentId)) { |
||
118 | if (!count($scores)) { |
||
119 | return [null, null]; |
||
120 | } |
||
121 | |||
122 | $data = $scores[0]; |
||
123 | |||
124 | return [ |
||
125 | $data->getQualification(), |
||
126 | $assignment->getQualification(), |
||
127 | api_get_local_time($assignment->getDateOfQualification()), |
||
128 | 1, |
||
129 | ]; |
||
130 | } |
||
131 | |||
132 | // multiple students |
||
133 | $students = []; |
||
134 | $rescount = 0; |
||
135 | $sum = 0; |
||
136 | $bestResult = 0; |
||
137 | $weight = 0; |
||
138 | $sumResult = 0; |
||
139 | |||
140 | foreach ($scores as $data) { |
||
141 | if (!array_key_exists($data->getUserId(), $students)) { |
||
142 | if (0 != $assignment->getQualification()) { |
||
143 | $students[$data->getUserId()] = $data->getQualification(); |
||
144 | $rescount++; |
||
145 | $sum += $data->getQualification() / $assignment->getQualification(); |
||
146 | $sumResult += $data->getQualification(); |
||
147 | |||
148 | if ($data->getQualification() > $bestResult) { |
||
149 | $bestResult = $data->getQualification(); |
||
150 | } |
||
151 | $weight = $assignment->getQualification(); |
||
152 | } |
||
153 | } |
||
154 | } |
||
155 | |||
156 | if (0 == $rescount) { |
||
157 | return [null, null]; |
||
158 | } |
||
159 | |||
160 | switch ($type) { |
||
161 | case 'best': |
||
162 | return [$bestResult, $weight]; |
||
163 | case 'average': |
||
164 | return [$sumResult / $rescount, $weight]; |
||
165 | case 'ranking': |
||
166 | return AbstractLink::getCurrentUserRanking($studentId, $students); |
||
167 | default: |
||
168 | return [$sum, $rescount]; |
||
169 | } |
||
277 |
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: