Conditions | 6 |
Paths | 32 |
Total Lines | 61 |
Code Lines | 39 |
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 |
||
81 | private function duplicateSession(Session $session, bool $debug, OutputInterface $output): Session |
||
82 | { |
||
83 | // Calculate new session dates based on the duration of the original session |
||
84 | $duration = $session->getAccessEndDate()->diff($session->getAccessStartDate()); |
||
85 | $newStartDate = (clone $session->getAccessEndDate())->modify('+1 day'); |
||
86 | $newEndDate = (clone $newStartDate)->add($duration); |
||
87 | |||
88 | if ($debug) { |
||
89 | $output->writeln(sprintf( |
||
90 | 'Duplicating session %d. New start date: %s, New end date: %s', |
||
91 | $session->getId(), |
||
92 | $newStartDate->format('Y-m-d H:i:s'), |
||
93 | $newEndDate->format('Y-m-d H:i:s') |
||
94 | )); |
||
95 | } |
||
96 | |||
97 | // Create a new session with the same details as the original session |
||
98 | $newSession = new Session(); |
||
99 | $newSession |
||
100 | ->setTitle($session->getTitle() . ' (Repetition ' . $session->getId() . ' - ' . time() . ')') |
||
101 | ->setAccessStartDate($newStartDate) |
||
102 | ->setAccessEndDate($newEndDate) |
||
103 | ->setDisplayStartDate($newStartDate) |
||
104 | ->setDisplayEndDate($newEndDate) |
||
105 | ->setCoachAccessStartDate($newStartDate) |
||
106 | ->setCoachAccessEndDate($newEndDate) |
||
107 | ->setVisibility($session->getVisibility()) |
||
108 | ->setDuration($session->getDuration()) |
||
109 | ->setDescription($session->getDescription() ?? '') |
||
110 | ->setShowDescription($session->getShowDescription() ?? false) |
||
111 | ->setCategory($session->getCategory()) |
||
112 | ->setPromotion($session->getPromotion()) |
||
113 | ->setDaysToReinscription($session->getDaysToReinscription()) |
||
114 | ->setDaysToNewRepetition($session->getDaysToNewRepetition()) |
||
115 | ->setParentId($session->getId()) |
||
116 | ->setLastRepetition(false); |
||
117 | |||
118 | // Copy the AccessUrls from the original session |
||
119 | foreach ($session->getUrls() as $accessUrl) { |
||
120 | $newSession->addAccessUrl($accessUrl->getUrl()); |
||
121 | } |
||
122 | |||
123 | // Copy the courses from the original session |
||
124 | foreach ($session->getCourses() as $course) { |
||
125 | $newSession->addCourse($course); |
||
126 | } |
||
127 | |||
128 | // Copy the general coaches from the original session |
||
129 | foreach ($session->getGeneralCoaches() as $coach) { |
||
130 | $newSession->addGeneralCoach($coach); |
||
131 | } |
||
132 | |||
133 | // Save the new session |
||
134 | $this->entityManager->persist($newSession); |
||
135 | $this->entityManager->flush(); |
||
136 | |||
137 | if ($debug) { |
||
138 | $output->writeln(sprintf('New session %d created successfully.', $newSession->getId())); |
||
139 | } |
||
140 | |||
141 | return $newSession; |
||
142 | } |
||
207 |
This check looks for private methods that have been defined, but are not used inside the class.