| Conditions | 9 |
| Paths | 12 |
| Total Lines | 52 |
| Code Lines | 36 |
| 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 |
||
| 65 | protected function execute(InputInterface $input, OutputInterface $output) { |
||
| 66 | $username = $input->getArgument('user'); |
||
| 67 | |||
| 68 | /** @var $user \OCP\IUser */ |
||
| 69 | $user = $this->userManager->get($username); |
||
| 70 | if (is_null($user)) { |
||
| 71 | $output->writeln('<error>User does not exist</error>'); |
||
| 72 | return 1; |
||
| 73 | } |
||
| 74 | |||
| 75 | if ($input->getOption('password-from-env')) { |
||
| 76 | $password = getenv('OC_PASS'); |
||
| 77 | if (!$password) { |
||
| 78 | $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
||
| 79 | return 1; |
||
| 80 | } |
||
| 81 | } elseif ($input->isInteractive()) { |
||
| 82 | /** @var $dialog \Symfony\Component\Console\Helper\QuestionHelper */ |
||
| 83 | $dialog = $this->getHelperSet()->get('question'); |
||
| 84 | |||
| 85 | if (\OCP\App::isEnabled('encryption')) { |
||
| 86 | $output->writeln( |
||
| 87 | '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
||
| 88 | ); |
||
| 89 | if (!$dialog->ask($input, $output, new Question('<question>Do you want to continue?</question>', true))) { |
||
| 90 | return 1; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | $q = new Question('<question>Enter a new password: </question>', false); |
||
| 95 | $q->setHidden(true); |
||
| 96 | $password = $dialog->ask($input, $output, $q); |
||
| 97 | $q = new Question('<question>Confirm the new password: </question>', false); |
||
| 98 | $q->setHidden(true); |
||
| 99 | $confirm = $dialog->ask($input, $output, $q); |
||
| 100 | if ($password !== $confirm) { |
||
| 101 | $output->writeln("<error>Passwords did not match!</error>"); |
||
| 102 | return 1; |
||
| 103 | } |
||
| 104 | } else { |
||
| 105 | $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
||
| 106 | return 1; |
||
| 107 | } |
||
| 108 | |||
| 109 | $success = $user->setPassword($password); |
||
| 110 | if ($success) { |
||
| 111 | $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
||
| 112 | } else { |
||
| 113 | $output->writeln("<error>Error while resetting password!</error>"); |
||
| 114 | return 1; |
||
| 115 | } |
||
| 116 | } |
||
| 117 | } |
||
| 118 |