| Conditions | 22 |
| Paths | 2040 |
| Total Lines | 109 |
| Code Lines | 59 |
| 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 |
||
| 49 | private function computeOverview(array $tuitions, array $entries, array $comments, array $substitutions, DateTime $start, DateTime $end): EntryOverview { |
||
| 50 | if($start > $end) { |
||
| 51 | $tmp = $start; |
||
| 52 | $start = $end; |
||
| 53 | $end = $tmp; |
||
| 54 | } |
||
| 55 | |||
| 56 | $tuitions = array_filter($tuitions, fn(Tuition $tuition) => $tuition->isBookEnabled()); |
||
| 57 | |||
| 58 | $lessons = [ ]; |
||
| 59 | |||
| 60 | foreach($entries as $entry) { |
||
| 61 | if($entry->getTuition() === null) { |
||
| 62 | // discard entries without tuition |
||
| 63 | continue; |
||
| 64 | } |
||
| 65 | |||
| 66 | $presentCount = $this->attendanceRepository->countPresent($entry); |
||
| 67 | $absentCount = $this->attendanceRepository->countAbsent($entry); |
||
| 68 | $lateCount = $this->attendanceRepository->countLate($entry); |
||
| 69 | |||
| 70 | for($lessonNumber = $entry->getLessonStart(); $lessonNumber <= $entry->getLessonEnd(); $lessonNumber++) { |
||
| 71 | $key = sprintf('%s-%d-%s', $entry->getLesson()->getDate()->format('Y-m-d'), $lessonNumber, $entry->getTuition()->getUuid()->toString()); |
||
| 72 | |||
| 73 | $lesson = new Lesson(clone $entry->getLesson()->getDate(), $lessonNumber, null, $entry); |
||
| 74 | $lesson->setAbsentCount($absentCount); |
||
| 75 | $lesson->setPresentCount($presentCount); |
||
| 76 | $lesson->setLateCount($lateCount); |
||
| 77 | |||
| 78 | $lessons[$key] = $lesson; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | $weekNumbers = [ ]; |
||
| 83 | $currentWeek = clone $start; |
||
| 84 | while($currentWeek < $end) { |
||
| 85 | $weekNumber = (int)$currentWeek->format('W'); |
||
| 86 | |||
| 87 | if(!in_array($weekNumber, $weekNumbers)) { |
||
| 88 | $weekNumbers[] = $weekNumber; |
||
| 89 | } |
||
| 90 | |||
| 91 | $currentWeek = $currentWeek->modify('+7 days'); |
||
| 92 | } |
||
| 93 | |||
| 94 | $timetableLessons = $this->lessonRepository->findAllByTuitions($start, $end, $tuitions); |
||
| 95 | |||
| 96 | $current = clone $start; |
||
| 97 | while($current <= $end) { |
||
| 98 | $dailyLessons = array_filter($timetableLessons, fn(TimetableLesson $lesson) => $lesson->getDate() == $current); |
||
| 99 | |||
| 100 | foreach($dailyLessons as $dailyLesson) { |
||
| 101 | for($lessonNumber = $dailyLesson->getLessonStart(); $lessonNumber <= $dailyLesson->getLessonEnd(); $lessonNumber++) { |
||
| 102 | $key = sprintf('%s-%d-%s', $current->format('Y-m-d'), $lessonNumber, $dailyLesson->getTuition()->getUuid()->toString()); |
||
| 103 | |||
| 104 | if (!array_key_exists($key, $lessons)) { |
||
| 105 | $lessons[$key] = new Lesson(clone $current, $lessonNumber); |
||
| 106 | } |
||
| 107 | |||
| 108 | $lessons[$key]->setLesson($dailyLesson); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | $current = $current->modify('+1 day'); |
||
| 113 | } |
||
| 114 | |||
| 115 | foreach($substitutions as $substitution) { |
||
| 116 | $tuition = $this->tuitionRepository->findOneBySubstitution($substitution, $this->sectionResolver->getSectionForDate($substitution->getDate())); |
||
| 117 | |||
| 118 | if($tuition === null) { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | $timetableLessons = $this->lessonRepository->findAllByTuitions($substitution->getDate(), $substitution->getDate(), [$tuition]); |
||
| 123 | |||
| 124 | // Filter correct lesson |
||
| 125 | foreach($timetableLessons as $dailyLesson) { |
||
| 126 | for($lessonNumber = $dailyLesson->getLessonStart(); $lessonNumber <= $dailyLesson->getLessonEnd(); $lessonNumber++) { |
||
| 127 | if($substitution->getLessonStart() <= $lessonNumber && $lessonNumber <= $substitution->getLessonEnd()) { |
||
| 128 | $key = sprintf('%s-%d-%s', $substitution->getDate()->format('Y-m-d'), $lessonNumber, $dailyLesson->getTuition()->getUuid()->toString()); |
||
| 129 | |||
| 130 | if (!array_key_exists($key, $lessons)) { |
||
| 131 | $lessons[$key] = new Lesson($substitution->getDate(), $lessonNumber, $dailyLesson, null, $substitution); |
||
| 132 | } |
||
| 133 | |||
| 134 | // Set correct entry |
||
| 135 | /** @var LessonEntry $lessonEntry */ |
||
| 136 | foreach($dailyLesson->getEntries() as $lessonEntry) { |
||
| 137 | if($lessonEntry->getLessonStart() <= $lessonNumber && $lessonNumber <= $lessonEntry->getLessonEnd()) { |
||
| 138 | $lessons[$key]->setEntry($lessonEntry); |
||
| 139 | break; |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | if($lessons[$key]->getLesson() === null) { |
||
| 144 | $lessons[$key]->setLesson($dailyLesson); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | } |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | $groups = $this->grouper->group(array_values($lessons), LessonDayStrategy::class); |
||
| 152 | $this->sorter->sort($groups, LessonDayGroupStrategy::class); |
||
| 153 | $this->sorter->sortGroupItems($groups, LessonStrategy::class); |
||
| 154 | |||
| 155 | $freeTimespans = $this->computeFreeTimespans($start, $end); |
||
| 156 | |||
| 157 | return new EntryOverview($start, $end, $groups, $comments, $freeTimespans); |
||
| 158 | } |
||
| 254 | } |