| Conditions | 16 |
| Paths | 1020 |
| Total Lines | 116 |
| Code Lines | 58 |
| 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 |
||
| 29 | public function import(array $gradeLessonsHtml, array $subjectLessonsHtml, DateTime $start, DateTime $end): ImportResult { |
||
| 30 | $lessons = [ ]; |
||
| 31 | |||
| 32 | foreach($gradeLessonsHtml as $html) { |
||
| 33 | $result = $this->reader->readHtml($html, TimetableType::Grade); |
||
| 34 | $lessons = array_merge( |
||
| 35 | $lessons, |
||
| 36 | $this->lessonCombiner->combine($result->getLessons()) |
||
| 37 | ); |
||
| 38 | } |
||
| 39 | |||
| 40 | foreach($subjectLessonsHtml as $html) { |
||
| 41 | $result = $this->reader->readHtml($html, TimetableType::Subject); |
||
| 42 | $lessons = array_merge($lessons, |
||
| 43 | $this->lessonCombiner->combine($result->getLessons()) |
||
| 44 | ); |
||
| 45 | } |
||
| 46 | |||
| 47 | $groups = $this->grouper->group($lessons, LessonStrategy::class); |
||
| 48 | |||
| 49 | $lessonsToImport = [ ]; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * This week map can be used to convert from calendar weeks to Untis weeks. It looks something like this: |
||
| 53 | * |
||
| 54 | * [ 1 => 'A', 2 => 'B', 3 => 'A', 4 => 'B', ...] (in case there are A/B-weeks) |
||
| 55 | */ |
||
| 56 | $weeksMap = [ ]; |
||
| 57 | foreach($this->weekRepository->findAll() as $week) { |
||
| 58 | foreach($week->getWeeksAsIntArray() as $weekNumber) { |
||
| 59 | $weeksMap[$weekNumber] = $week->getKey(); |
||
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | /** |
||
| 64 | * This matrix is used to get a list of dates for a given week and day. It looks something like this: |
||
| 65 | * |
||
| 66 | * [ |
||
| 67 | * 'A' => [ |
||
| 68 | * 1 => [ '2022-01-03', '2022-01-17', ... ] |
||
| 69 | * 2 => [ '2022-01-04', '2022-01-18', ... ] |
||
| 70 | * ], |
||
| 71 | * 'B' => ... |
||
| 72 | * ] |
||
| 73 | */ |
||
| 74 | $weekMatrix = [ ]; |
||
| 75 | $current = clone $start; |
||
| 76 | while($current <= $end) { |
||
| 77 | $current->setTime(0, 0); |
||
| 78 | |||
| 79 | $week = $weeksMap[(int)$current->format('W')]; |
||
| 80 | $day = (int)$current->format('w'); |
||
| 81 | |||
| 82 | if(!array_key_exists($week, $weekMatrix)) { |
||
| 83 | $weekMatrix[$week] = [ ]; |
||
| 84 | } |
||
| 85 | |||
| 86 | if(!array_key_exists($day, $weekMatrix[$week])) { |
||
| 87 | $weekMatrix[$week][$day] = [ ]; |
||
| 88 | } |
||
| 89 | |||
| 90 | $weekMatrix[$week][$day][] = $current; |
||
| 91 | |||
| 92 | $current = (clone $current)->modify('+1 day'); |
||
| 93 | } |
||
| 94 | |||
| 95 | $subjectOverrides = $this->getSubjectOverrideMap(); |
||
| 96 | |||
| 97 | /** @var LessonGroup $group */ |
||
| 98 | foreach($groups as $group) { |
||
| 99 | if(count($group->getLessons()) === 0) { |
||
|
|
|||
| 100 | // just in case... |
||
| 101 | continue; |
||
| 102 | } |
||
| 103 | |||
| 104 | $teachers = [ ]; |
||
| 105 | $grades = [ ]; |
||
| 106 | |||
| 107 | foreach($group->getLessons() as $lesson) { |
||
| 108 | if(!in_array($lesson->getTeacher(), $teachers)) { |
||
| 109 | $teachers[] = $lesson->getTeacher(); |
||
| 110 | } |
||
| 111 | |||
| 112 | if(!in_array($lesson->getGrade(), $grades)) { |
||
| 113 | $grades[] = $lesson->getGrade(); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | |||
| 117 | $lesson = $group->getLessons()[0]; |
||
| 118 | |||
| 119 | // Iterate over weeks |
||
| 120 | foreach($lesson->getWeeks() as $week) { |
||
| 121 | if(!isset($weekMatrix[$week][$lesson->getDay()])) { |
||
| 122 | continue; |
||
| 123 | } |
||
| 124 | |||
| 125 | foreach($weekMatrix[$week][$lesson->getDay()] as $date) { |
||
| 126 | $lessonsToImport[] = (new TimetableLessonData()) |
||
| 127 | ->setId($group->getKey() . '-' . $date->format('Y-m-d')) |
||
| 128 | ->setDate($date) |
||
| 129 | ->setGrades($grades) |
||
| 130 | ->setTeachers($teachers) |
||
| 131 | ->setRoom($lesson->getRoom()) |
||
| 132 | ->setSubject($subjectOverrides[$lesson->getSubject()] ?? $lesson->getSubject()) |
||
| 133 | ->setLessonStart($lesson->getLessonStart()) |
||
| 134 | ->setLessonEnd($lesson->getLessonEnd()); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | $data = new TimetableLessonsData(); |
||
| 140 | $data->setStartDate($start); |
||
| 141 | $data->setEndDate($end); |
||
| 142 | $data->setLessons($lessonsToImport); |
||
| 143 | |||
| 144 | return $this->importer->replaceImport($data, $this->strategy); |
||
| 145 | } |
||
| 156 | } |