Conditions | 8 |
Paths | 11 |
Total Lines | 55 |
Lines | 9 |
Ratio | 16.36 % |
Changes | 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 |
||
151 | private function getUsersForUpdate(): array |
||
152 | { |
||
153 | if ($this->input->getOption('all-users')) { |
||
154 | return $this->userRepo->findBy(['removed' => false]); |
||
155 | } |
||
156 | |||
157 | /** @var User $serviceUser */ |
||
158 | try { |
||
159 | $serviceUser = $this->userRepo->findActiveUserWithSubscribers($this->appUserId); |
||
160 | } catch (\Exception $e) { |
||
161 | $this->logger->error('Error while getting active user with subscribers', ['app_user_id' => $this->appUserId]); |
||
162 | |||
163 | throw $e; |
||
164 | } |
||
165 | |||
166 | View Code Duplication | if (!$serviceUser) { |
|
167 | $this->logger->warning('Service user not found or marked as removed. Falling back to API.'); |
||
168 | |||
169 | try { |
||
170 | $serviceUser = $this->api->getUserById($this->appUserId); |
||
171 | } catch (UserNotFoundException $e) { |
||
172 | throw new \RuntimeException('Service user not found in the database and could not be retrieved from API.'); |
||
173 | } |
||
174 | } |
||
175 | |||
176 | $this->logger->info('Getting service subscribers'); |
||
177 | |||
178 | try { |
||
179 | return $this->api->getUserSubscribersById($this->appUserId); |
||
180 | } catch (UserNotFoundException $e) { |
||
181 | $this->logger->critical('Service user deleted or API response is invalid'); |
||
182 | |||
183 | throw $e; |
||
184 | } catch (\Exception $e) { |
||
185 | $this->logger->warning( |
||
186 | 'Error while getting service subscribers. Fallback to local list.', |
||
187 | [ |
||
188 | 'user_login' => $serviceUser->getLogin(), |
||
189 | 'user_id' => $serviceUser->getId(), |
||
190 | 'message' => $e->getMessage(), |
||
191 | 'file' => $e->getFile(), |
||
192 | 'line' => $e->getLine(), |
||
193 | ] |
||
194 | ); |
||
195 | |||
196 | $localSubscribers = []; |
||
197 | |||
198 | /** @var Subscription $subscription */ |
||
199 | foreach ($serviceUser->getSubscribers() as $subscription) { |
||
200 | $localSubscribers[] = $subscription->getSubscriber(); |
||
201 | } |
||
202 | |||
203 | return $localSubscribers; |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.