Conditions | 15 |
Paths | 23 |
Total Lines | 95 |
Lines | 0 |
Ratio | 0 % |
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 |
||
120 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
121 | $username = $input->getArgument('user'); |
||
122 | $emailLink = $input->getOption('send-email'); |
||
123 | $displayLink = $input->getOption('output-link'); |
||
124 | |||
125 | /** @var $user \OCP\IUser */ |
||
126 | $user = $this->userManager->get($username); |
||
127 | if ($user === null) { |
||
128 | $output->writeln('<error>User does not exist</error>'); |
||
129 | return 1; |
||
130 | } |
||
131 | |||
132 | if ($input->getOption('password-from-env')) { |
||
133 | $password = $this->environmentHelper->getEnvVar('OC_PASS'); |
||
134 | if (!$password) { |
||
135 | $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
||
136 | return 1; |
||
137 | } |
||
138 | } elseif ($emailLink | $displayLink) { |
||
139 | $userId = $user->getUID(); |
||
140 | $token = $this->secureRandom->generate(21, |
||
141 | ISecureRandom::CHAR_DIGITS, |
||
142 | ISecureRandom::CHAR_LOWER, ISecureRandom::CHAR_UPPER); |
||
|
|||
143 | $this->config->setUserValue($user->getUID(), 'owncloud', |
||
144 | 'lostpassword', $this->timeFactory->getTime() . ':' . $token); |
||
145 | $mailData = [ |
||
146 | 'link' => $this->urlgenerator->linkToRouteAbsolute('core.lost.resetform', ['userId' => $userId, 'token' => $token]) |
||
147 | ]; |
||
148 | |||
149 | if ($emailLink && $user->getEMailAddress() !== null) { |
||
150 | $mail = new TemplateResponse('core', 'lostpassword/email', $mailData, 'blank'); |
||
151 | $mailContent = $mail->render(); |
||
152 | |||
153 | $subject = $this->l10n->t('Your password is reset'); |
||
154 | try { |
||
155 | $message = $this->mailer->createMessage(); |
||
156 | $message->setTo([$user->getEMailAddress() => $userId]); |
||
157 | $message->setSubject($subject); |
||
158 | $message->setHtmlBody($mailContent); |
||
159 | $message->setFrom([Util::getDefaultEmailAddress('no-reply') => $this->defaults->getName()]); |
||
160 | $this->mailer->send($message); |
||
161 | } catch (\Exception $e) { |
||
162 | $output->writeln('<error>Can\'t send new user mail to ' . $user->getEMailAddress() . ': ' . $e->getMessage()); |
||
163 | return 1; |
||
164 | } |
||
165 | } else { |
||
166 | if ($emailLink) { |
||
167 | $output->writeln('<error>Email address is not set for the user ' . $user->getUID() . '</error>'); |
||
168 | return 1; |
||
169 | } |
||
170 | } |
||
171 | $output->writeln('The password reset link is: ' . $mailData['link']); |
||
172 | return 0; |
||
173 | } elseif ($input->isInteractive()) { |
||
174 | /** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */ |
||
175 | $dialog = $this->getHelperSet()->get('question'); |
||
176 | |||
177 | if (\OCP\App::isEnabled('encryption')) { |
||
178 | $output->writeln( |
||
179 | '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
||
180 | ); |
||
181 | if (!$dialog->ask($input, $output, new Question('<question>Do you want to continue?</question>', true))) { |
||
182 | return 1; |
||
183 | } |
||
184 | } |
||
185 | |||
186 | $q = new Question('<question>Enter a new password: </question>', false); |
||
187 | $q->setHidden(true); |
||
188 | $password = $dialog->ask($input, $output, $q); |
||
189 | if ($password === false) { |
||
190 | // When user presses RETURN key or no password characters are entered, |
||
191 | // $password gets a boolean value false. |
||
192 | $output->writeln("<error>Password cannot be empty!</error>"); |
||
193 | return 1; |
||
194 | } |
||
195 | $q = new Question('<question>Confirm the new password: </question>', false); |
||
196 | $q->setHidden(true); |
||
197 | $confirm = $dialog->ask($input, $output, $q); |
||
198 | if ($password !== $confirm) { |
||
199 | $output->writeln("<error>Passwords did not match!</error>"); |
||
200 | return 1; |
||
201 | } |
||
202 | } else { |
||
203 | $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
||
204 | return 1; |
||
205 | } |
||
206 | |||
207 | $success = $user->setPassword($password); |
||
208 | if ($success) { |
||
209 | $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
||
210 | } else { |
||
211 | $output->writeln("<error>Error while resetting password!</error>"); |
||
212 | return 1; |
||
213 | } |
||
214 | } |
||
215 | } |
||
216 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.