Conditions | 10 |
Paths | 192 |
Total Lines | 77 |
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 |
||
155 | function findResults(array $formValues, EntityManagerInterface $em, ExerciseFocusedPlugin $plugin) |
||
156 | { |
||
157 | $cId = api_get_course_int_id(); |
||
158 | |||
159 | $qb = $em->createQueryBuilder(); |
||
160 | $qb |
||
161 | ->select('te AS exe, q.title, te.startDate , u.firstname, u.lastname, u.username') |
||
162 | ->from(TrackEExercises::class, 'te') |
||
163 | ->innerJoin(CQuiz::class, 'q', Join::WITH, 'te.exeExoId = q.iid') |
||
164 | ->innerJoin(User::class, 'u', Join::WITH, 'te.exeUserId = u.id'); |
||
165 | |||
166 | $params = []; |
||
167 | |||
168 | if ($cId) { |
||
169 | $qb->andWhere($qb->expr()->eq('te.cId', ':cId')); |
||
170 | |||
171 | $params['cId'] = $cId; |
||
172 | } |
||
173 | |||
174 | $sessionItemIdList = getSessionIdFromFormValues( |
||
175 | $formValues, |
||
176 | $plugin->getSessionFieldList() |
||
177 | ); |
||
178 | |||
179 | if ($sessionItemIdList) { |
||
180 | $qb->andWhere($qb->expr()->in('te.sessionId', ':sessionItemIdList')); |
||
181 | |||
182 | $params['sessionItemIdList'] = $sessionItemIdList; |
||
183 | } else { |
||
184 | $qb->andWhere($qb->expr()->isNull('te.sessionId')); |
||
185 | } |
||
186 | |||
187 | if (!empty($formValues['username'])) { |
||
188 | $qb->andWhere($qb->expr()->eq('u.username', ':username')); |
||
189 | |||
190 | $params['username'] = $formValues['username']; |
||
191 | } |
||
192 | |||
193 | if (!empty($formValues['firstname'])) { |
||
194 | $qb->andWhere($qb->expr()->eq('u.firstname', ':firstname')); |
||
195 | |||
196 | $params['firstname'] = $formValues['firstname']; |
||
197 | } |
||
198 | |||
199 | if (!empty($formValues['lastname'])) { |
||
200 | $qb->andWhere($qb->expr()->eq('u.lastname', ':lastname')); |
||
201 | |||
202 | $params['lastname'] = $formValues['lastname']; |
||
203 | } |
||
204 | |||
205 | if (!empty($formValues['start_date'])) { |
||
206 | $qb->andWhere( |
||
207 | $qb->expr()->andX( |
||
208 | $qb->expr()->gte('te.startDate', ':start_date'), |
||
209 | $qb->expr()->lte('te.exeDate', ':end_date') |
||
210 | ) |
||
211 | ); |
||
212 | |||
213 | $params['start_date'] = api_get_utc_datetime($formValues['start_date'].' 00:00:00', false, true); |
||
214 | $params['end_date'] = api_get_utc_datetime($formValues['start_date'].' 23:59:59', false, true); |
||
215 | } |
||
216 | |||
217 | if (empty($params)) { |
||
218 | return []; |
||
219 | } |
||
220 | |||
221 | if ($cId && !empty($formValues['id'])) { |
||
222 | $qb->andWhere($qb->expr()->eq('q.iid', ':q_id')); |
||
223 | |||
224 | $params['q_id'] = $formValues['id']; |
||
225 | } |
||
226 | |||
227 | $qb->setParameters($params); |
||
228 | |||
229 | $query = $qb->getQuery(); |
||
230 | |||
231 | return $query->getResult(); |
||
232 | } |
||
233 |
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: