Conditions | 14 |
Paths | 512 |
Total Lines | 113 |
Code Lines | 71 |
Lines | 0 |
Ratio | 0 % |
Changes | 3 | ||
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 |
||
78 | private function duplicateSession(Session $session, bool $debug, OutputInterface $output): Session |
||
79 | { |
||
80 | // Calculate new session dates based on the duration of the original session |
||
81 | $duration = $session->getAccessEndDate()->diff($session->getAccessStartDate())->days; |
||
|
|||
82 | $newStartDate = (clone $session->getAccessEndDate())->modify('+1 day'); |
||
83 | $newEndDate = (clone $newStartDate)->modify("+{$duration} days"); |
||
84 | |||
85 | if ($debug) { |
||
86 | $output->writeln(sprintf( |
||
87 | 'Duplicating session %d. New start date: %s, New end date: %s', |
||
88 | $session->getId(), |
||
89 | $newStartDate->format('Y-m-d H:i:s'), |
||
90 | $newEndDate->format('Y-m-d H:i:s') |
||
91 | )); |
||
92 | } |
||
93 | |||
94 | // Create a new session with the same details as the original session |
||
95 | $newSession = new Session(); |
||
96 | $newSession |
||
97 | ->setTitle($session->getTitle() . ' (Repetition ' . $session->getId() . ' - ' . time() . ')') |
||
98 | ->setAccessStartDate($newStartDate) |
||
99 | ->setAccessEndDate($newEndDate) |
||
100 | ->setDisplayStartDate($newStartDate) |
||
101 | ->setDisplayEndDate($newEndDate) |
||
102 | ->setCoachAccessStartDate($newStartDate) |
||
103 | ->setCoachAccessEndDate($newEndDate) |
||
104 | ->setVisibility($session->getVisibility()) |
||
105 | ->setDuration(0) |
||
106 | ->setDescription($session->getDescription() ?? '') |
||
107 | ->setShowDescription($session->getShowDescription() ?? false) |
||
108 | ->setCategory($session->getCategory()) |
||
109 | ->setPromotion($session->getPromotion()) |
||
110 | ->setDaysToReinscription($session->getDaysToReinscription()) |
||
111 | ->setDaysToNewRepetition($session->getDaysToNewRepetition()) |
||
112 | ->setParentId($session->getId()) |
||
113 | ->setLastRepetition(false); |
||
114 | |||
115 | // Copy the AccessUrls from the original session |
||
116 | foreach ($session->getUrls() as $accessUrl) { |
||
117 | $newSession->addAccessUrl($accessUrl->getUrl()); |
||
118 | } |
||
119 | |||
120 | // Save the new session |
||
121 | $this->entityManager->persist($newSession); |
||
122 | $this->entityManager->flush(); |
||
123 | |||
124 | if ($debug) { |
||
125 | $output->writeln(sprintf('New session %d created successfully.', $newSession->getId())); |
||
126 | } |
||
127 | |||
128 | $courses = $session->getCourses()->toArray(); |
||
129 | |||
130 | if ($debug) { |
||
131 | $output->writeln('Courses retrieved: ' . count($courses)); |
||
132 | foreach ($courses as $index => $sessionRelCourse) { |
||
133 | $course = $sessionRelCourse->getCourse(); |
||
134 | $output->writeln(sprintf( |
||
135 | 'Course #%d: %s (Course ID: %s)', |
||
136 | $index + 1, |
||
137 | $course ? $course->getTitle() : 'NULL', |
||
138 | $course ? $course->getId() : 'NULL' |
||
139 | )); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | // Extract course IDs |
||
144 | $courseList = array_map(function ($sessionRelCourse) { |
||
145 | $course = $sessionRelCourse->getCourse(); |
||
146 | return $course?->getId(); |
||
147 | }, $courses); |
||
148 | |||
149 | // Remove null values |
||
150 | $courseList = array_filter($courseList); |
||
151 | |||
152 | if ($debug) { |
||
153 | $output->writeln(sprintf( |
||
154 | 'Extracted course IDs: %s', |
||
155 | json_encode($courseList) |
||
156 | )); |
||
157 | } |
||
158 | |||
159 | if (empty($courseList)) { |
||
160 | $output->writeln(sprintf('Warning: No courses found in the original session %d.', $session->getId())); |
||
161 | } |
||
162 | |||
163 | // Add courses to the new session |
||
164 | $courseCount = 0; |
||
165 | foreach ($courses as $sessionRelCourse) { |
||
166 | $course = $sessionRelCourse->getCourse(); |
||
167 | if ($course) { |
||
168 | $newSession->addCourse($course); |
||
169 | $this->entityManager->persist($newSession); |
||
170 | |||
171 | if ($debug) { |
||
172 | $output->writeln(sprintf('Added course ID %d to session ID %d.', $course->getId(), $newSession->getId())); |
||
173 | } |
||
174 | |||
175 | $this->copyEvaluationsAndCategories($course->getId(), $session->getId(), $newSession->getId(), $debug, $output); |
||
176 | |||
177 | $courseCount++; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | foreach ($session->getGeneralCoaches() as $coach) { |
||
182 | $newSession->addGeneralCoach($coach); |
||
183 | } |
||
184 | |||
185 | $newSession->setNbrCourses($courseCount); |
||
186 | $this->entityManager->persist($newSession); |
||
187 | |||
188 | $this->entityManager->flush(); |
||
189 | |||
190 | return $newSession; |
||
191 | } |
||
311 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.