| Conditions | 11 |
| Paths | 91 |
| Total Lines | 103 |
| Code Lines | 72 |
| 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 |
||
| 31 | public function __invoke(CreateSessionWithUsersAndCoursesInput $data): Session |
||
| 32 | { |
||
| 33 | $this->validator->validate($data); |
||
| 34 | |||
| 35 | $session = new Session(); |
||
| 36 | $session |
||
| 37 | ->setTitle($data->getTitle()) |
||
| 38 | ->setDescription($data->getDescription() ?? '') |
||
| 39 | ->setVisibility($data->getVisibility() ?? 1) |
||
| 40 | ->setNbrCourses(count($data->getCourseIds())) |
||
| 41 | ->setNbrUsers(count($data->getStudentIds()) + count($data->getTutorIds())) |
||
| 42 | ->setShowDescription($data->getShowDescription() ?? false) |
||
| 43 | ->setDuration($data->getDuration() ?? 0) |
||
| 44 | ->setDisplayStartDate($data->getDisplayStartDate() ?? new \DateTime()) |
||
| 45 | ->setDisplayEndDate($data->getDisplayEndDate() ?? new \DateTime()) |
||
| 46 | ->setAccessStartDate($data->getAccessStartDate() ?? new \DateTime()) |
||
| 47 | ->setAccessEndDate($data->getAccessEndDate() ?? new \DateTime()) |
||
| 48 | ->setCoachAccessStartDate($data->getCoachAccessStartDate() ?? new \DateTime()) |
||
| 49 | ->setCoachAccessEndDate($data->getCoachAccessEndDate() ?? new \DateTime()) |
||
| 50 | ->setValidityInDays($data->getValidityInDays() ?? 0); |
||
| 51 | |||
| 52 | if ($data->getCategory()) { |
||
|
|
|||
| 53 | $category = $this->em->getRepository(SessionCategory::class)->find($data->getCategory()); |
||
| 54 | if (!$category) { |
||
| 55 | throw new \RuntimeException("Invalid category ID: " . $data->getCategory()); |
||
| 56 | } |
||
| 57 | $session->setCategory($category); |
||
| 58 | } |
||
| 59 | |||
| 60 | $this->em->persist($session); |
||
| 61 | |||
| 62 | $relCourses = []; |
||
| 63 | $courses = []; |
||
| 64 | foreach ($data->getCourseIds() as $courseId) { |
||
| 65 | $course = $this->courseRepo->find($courseId); |
||
| 66 | if (!$course) { |
||
| 67 | continue; |
||
| 68 | } |
||
| 69 | |||
| 70 | $courses[$courseId] = $course; |
||
| 71 | |||
| 72 | $relCourse = new SessionRelCourse(); |
||
| 73 | $relCourse->setSession($session); |
||
| 74 | $relCourse->setCourse($course); |
||
| 75 | $relCourse->setNbrUsers(0); |
||
| 76 | $this->em->persist($relCourse); |
||
| 77 | |||
| 78 | $relCourses[$courseId] = $relCourse; |
||
| 79 | } |
||
| 80 | |||
| 81 | foreach ($data->getStudentIds() as $userId) { |
||
| 82 | $user = $this->userRepo->find($userId); |
||
| 83 | if (!$user) { |
||
| 84 | continue; |
||
| 85 | } |
||
| 86 | |||
| 87 | $rel = new SessionRelUser(); |
||
| 88 | $rel |
||
| 89 | ->setSession($session) |
||
| 90 | ->setUser($user) |
||
| 91 | ->setRelationType(Session::STUDENT); |
||
| 92 | |||
| 93 | $this->em->persist($rel); |
||
| 94 | |||
| 95 | foreach ($courses as $courseId => $course) { |
||
| 96 | $relCourseUser = new SessionRelCourseRelUser(); |
||
| 97 | $relCourseUser |
||
| 98 | ->setSession($session) |
||
| 99 | ->setUser($user) |
||
| 100 | ->setCourse($course) |
||
| 101 | ->setStatus(Session::STUDENT) |
||
| 102 | ->setVisibility(1) |
||
| 103 | ->setProgress(0) |
||
| 104 | ->setLegalAgreement(0); |
||
| 105 | |||
| 106 | $this->em->persist($relCourseUser); |
||
| 107 | |||
| 108 | if (isset($relCourses[$courseId])) { |
||
| 109 | $relCourses[$courseId]->setNbrUsers( |
||
| 110 | $relCourses[$courseId]->getNbrUsers() + 1 |
||
| 111 | ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | foreach ($data->getTutorIds() as $userId) { |
||
| 117 | $user = $this->userRepo->find($userId); |
||
| 118 | if (!$user) { |
||
| 119 | continue; |
||
| 120 | } |
||
| 121 | |||
| 122 | $rel = new SessionRelUser(); |
||
| 123 | $rel |
||
| 124 | ->setSession($session) |
||
| 125 | ->setUser($user) |
||
| 126 | ->setRelationType(Session::SESSION_ADMIN); |
||
| 127 | |||
| 128 | $this->em->persist($rel); |
||
| 129 | } |
||
| 130 | |||
| 131 | $this->em->flush(); |
||
| 132 | |||
| 133 | return $session; |
||
| 134 | } |
||
| 136 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: