| Conditions | 23 |
| Paths | 13824 |
| Total Lines | 104 |
| Code Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 24 | public function resolve(Tuition $tuition, DateTime $date, int $lesson) { |
||
| 25 | $students = $tuition->getStudyGroup()->getMemberships()->map(fn(StudyGroupMembership $membership) => $membership->getStudent())->toArray(); |
||
| 26 | |||
| 27 | $suggestions = [ ]; |
||
| 28 | |||
| 29 | $absences = $this->absenceResolver->resolve($date, $lesson, $students); |
||
| 30 | |||
| 31 | foreach($this->filterAbsencesWithAreExcused($absences) as $absentStudent) { |
||
| 32 | if(array_key_exists($absentStudent->getStudent()->getId(), $suggestions)) { |
||
| 33 | continue; // prevent duplicates |
||
| 34 | } |
||
| 35 | |||
| 36 | $excuseStatus = ($absentStudent instanceof AbsentStudentWithAbsenceNote && $absentStudent->getAbsence()->getType()->isAlwaysExcused()) ? LessonAttendanceExcuseStatus::Excused : LessonAttendanceExcuseStatus::NotSet; |
||
| 37 | |||
| 38 | $suggestions[$absentStudent->getStudent()->getId()] = [ |
||
| 39 | 'student' => Student::fromEntity($absentStudent->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 40 | 'reason' => $absentStudent->getReason()->value, |
||
| 41 | 'label' => $absentStudent->getAbsence()->getType()->getName(), |
||
| 42 | 'zero_absent_lessons' => $absentStudent->getAbsence()->getType()->isTypeWithZeroAbsenceLessons(), |
||
| 43 | 'excuse_status' => $excuseStatus |
||
| 44 | ]; |
||
| 45 | } |
||
| 46 | |||
| 47 | foreach($this->filterAbsencesWithoutZeroAbsenceLessons($absences) as $absentStudent) { |
||
| 48 | if(array_key_exists($absentStudent->getStudent()->getId(), $suggestions)) { |
||
| 49 | continue; // prevent duplicates |
||
| 50 | } |
||
| 51 | |||
| 52 | $excuseStatus = ($absentStudent instanceof AbsentStudentWithAbsenceNote && $absentStudent->getAbsence()->getType()->isAlwaysExcused()) ? LessonAttendanceExcuseStatus::Excused : LessonAttendanceExcuseStatus::NotSet; |
||
| 53 | |||
| 54 | $suggestions[$absentStudent->getStudent()->getId()] = [ |
||
| 55 | 'student' => Student::fromEntity($absentStudent->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 56 | 'reason' => $absentStudent->getReason()->value, |
||
| 57 | 'label' => $absentStudent->getAbsence()->getType()->getName(), |
||
| 58 | 'zero_absent_lessons' => false, |
||
| 59 | 'excuse_status' => $excuseStatus |
||
| 60 | ]; |
||
| 61 | } |
||
| 62 | |||
| 63 | // Absence in previous lesson |
||
| 64 | foreach($this->attendanceRepository->findAbsentByStudentsAndDate($students, $date) as $attendance) { |
||
| 65 | if(array_key_exists($attendance->getStudent()->getId(), $suggestions)) { |
||
| 66 | continue; // prevent duplicates |
||
| 67 | } |
||
| 68 | |||
| 69 | if($attendance->getEntry()->getLessonEnd() < $lesson) { |
||
| 70 | $suggestions[$attendance->getStudent()->getId()] = [ |
||
| 71 | 'student' => Student::fromEntity($attendance->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 72 | 'reason' => 'absent_before' |
||
| 73 | ]; |
||
| 74 | } |
||
| 75 | } |
||
| 76 | |||
| 77 | // Exam |
||
| 78 | foreach($this->filterAbsencesWithExam($absences) as $absentExamStudent) { |
||
| 79 | if(array_key_exists($absentExamStudent->getStudent()->getId(), $suggestions)) { |
||
| 80 | continue; // prevent duplicates |
||
| 81 | } |
||
| 82 | |||
| 83 | if($absentExamStudent->getExam()->getTuitions()->count() === 1 && $absentExamStudent->getExam()->getTuitions()->first() === $tuition) { |
||
| 84 | continue; // do not show exam of current tuition |
||
| 85 | } |
||
| 86 | |||
| 87 | $suggestions[$absentExamStudent->getStudent()->getId()] = [ |
||
| 88 | 'student' => Student::fromEntity($absentExamStudent->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 89 | 'reason' => $absentExamStudent->getReason()->value, |
||
| 90 | 'zero_absent_lessons' => true, |
||
| 91 | 'excuse_status' => LessonAttendanceExcuseStatus::Excused |
||
| 92 | ]; |
||
| 93 | } |
||
| 94 | |||
| 95 | // Absences with zero absent lessons |
||
| 96 | foreach($this->filterAbsencesWithZeroAbsenceLessons($absences) as $absentStudent) { |
||
| 97 | if(array_key_exists($absentStudent->getStudent()->getId(), $suggestions)) { |
||
| 98 | continue; // prevent duplicates |
||
| 99 | } |
||
| 100 | |||
| 101 | $excuseStatus = ($absentStudent instanceof AbsentStudentWithAbsenceNote && $absentStudent->getAbsence()->getType()->isAlwaysExcused()) ? LessonAttendanceExcuseStatus::Excused : LessonAttendanceExcuseStatus::NotSet; |
||
| 102 | |||
| 103 | $suggestions[$absentStudent->getStudent()->getId()] = [ |
||
| 104 | 'student' => Student::fromEntity($absentStudent->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 105 | 'reason' => $absentStudent->getReason()->value, |
||
| 106 | 'label' => $absentStudent->getAbsence()->getType()->getName(), |
||
| 107 | 'zero_absent_lessons' => true, |
||
| 108 | 'excuse_status' => $excuseStatus |
||
| 109 | ]; |
||
| 110 | } |
||
| 111 | |||
| 112 | // Excuse |
||
| 113 | foreach($this->excuseNoteRepository->findByStudentsAndDate($students, $date) as $note) { |
||
| 114 | if(array_key_exists($note->getStudent()->getId(), $suggestions)) { |
||
| 115 | continue; // prevent duplicates |
||
| 116 | } |
||
| 117 | |||
| 118 | if($note->appliesToLesson($date, $lesson)) { |
||
| 119 | $suggestions[$note->getStudent()->getId()] = [ |
||
| 120 | 'student' => Student::fromEntity($note->getStudent(), $this->sectionResolver->getCurrentSection()), |
||
| 121 | 'reason' => 'excuse', |
||
| 122 | 'excuse_status' => LessonAttendanceExcuseStatus::Excused |
||
| 123 | ]; |
||
| 124 | } |
||
| 125 | } |
||
| 126 | |||
| 127 | return array_values($suggestions); |
||
| 128 | } |
||
| 157 | } |