Conditions | 17 |
Paths | 10 |
Total Lines | 79 |
Code Lines | 41 |
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 |
||
52 | protected function execute(InputInterface $input, OutputInterface $output): int |
||
53 | { |
||
54 | $debug = $input->getOption('debug'); |
||
55 | |||
56 | // 1. Find all lessons with "validity_in_days" > 0 |
||
57 | $learningPaths = $this->lpRepository->findWithValidity(); |
||
58 | |||
59 | /* @var CLp $lp */ |
||
60 | foreach ($learningPaths as $lp) { |
||
61 | $validityDays = $lp->getValidityInDays(); |
||
62 | $sessionId = $this->lpRepository->getLpSessionId($lp->getIid()); |
||
63 | |||
64 | if (!$sessionId) { |
||
|
|||
65 | if ($debug) { |
||
66 | $output->writeln('Session ID not found for Learning Path ID: ' . $lp->getIid()); |
||
67 | } |
||
68 | continue; |
||
69 | } |
||
70 | |||
71 | // 2. Get the session of the lesson |
||
72 | $session = $this->sessionRepository->find($sessionId); |
||
73 | if (!$session) { |
||
74 | if ($debug) { |
||
75 | $output->writeln('Session not found for ID: ' . $sessionId); |
||
76 | } |
||
77 | continue; |
||
78 | } |
||
79 | |||
80 | // Process only if the session is not the last repetition |
||
81 | if ($session->getLastRepetition()) { |
||
82 | if ($debug) { |
||
83 | $output->writeln('Session ' . $session->getId() . ' is the last repetition. Skipping...'); |
||
84 | } |
||
85 | continue; |
||
86 | } |
||
87 | |||
88 | // 3. Find users who completed the lesson and whose validity has expired |
||
89 | $expiredUsers = $this->findExpiredCompletions($lp, $validityDays); |
||
90 | |||
91 | if (count($expiredUsers) === 0) { |
||
92 | if ($debug) { |
||
93 | $output->writeln('No expired users found for Learning Path ID: ' . $lp->getIid()); |
||
94 | } |
||
95 | continue; |
||
96 | } |
||
97 | |||
98 | foreach ($expiredUsers as $user) { |
||
99 | if ($debug) { |
||
100 | $output->writeln('User ' . $user->getUser()->getId() . ' has expired completion for LP ' . $lp->getIid()); |
||
101 | } |
||
102 | |||
103 | // 4. Find the last valid child session |
||
104 | $validChildSession = $this->sessionRepository->findValidChildSession($session); |
||
105 | |||
106 | if ($validChildSession) { |
||
107 | // Reinscribe user in the valid child session |
||
108 | $this->enrollUserInSession($user->getUser(), $validChildSession); |
||
109 | if ($debug) { |
||
110 | $output->writeln('Reinscribed user ' . $user->getUser()->getId() . ' into child session ' . $validChildSession->getId()); |
||
111 | } |
||
112 | } else { |
||
113 | // 5. If no valid child session, find the valid parent session |
||
114 | $validParentSession = $this->sessionRepository->findValidParentSession($session); |
||
115 | if ($validParentSession) { |
||
116 | // Reinscribe user in the valid parent session |
||
117 | $this->enrollUserInSession($user->getUser(), $validParentSession); |
||
118 | if ($debug) { |
||
119 | $output->writeln('Reinscribed user ' . $user->getUser()->getId() . ' into parent session ' . $validParentSession->getId()); |
||
120 | } |
||
121 | } else { |
||
122 | if ($debug) { |
||
123 | $output->writeln('No valid parent or child session found for user ' . $user->getUser()->getId()); |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | return Command::SUCCESS; |
||
131 | } |
||
177 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: