Conditions | 11 |
Paths | 17 |
Total Lines | 67 |
Code Lines | 44 |
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 |
||
70 | protected function execute(InputInterface $input, OutputInterface $output): int { |
||
71 | $username = $input->getArgument('user'); |
||
72 | |||
73 | $user = $this->userManager->get($username); |
||
74 | if (is_null($user)) { |
||
75 | $output->writeln('<error>User does not exist</error>'); |
||
76 | return 1; |
||
77 | } |
||
78 | |||
79 | if ($input->getOption('password-from-env')) { |
||
80 | $password = getenv('OC_PASS'); |
||
81 | if (!$password) { |
||
82 | $output->writeln('<error>--password-from-env given, but OC_PASS is empty!</error>'); |
||
83 | return 1; |
||
84 | } |
||
85 | } elseif ($input->isInteractive()) { |
||
86 | /** @var QuestionHelper $helper */ |
||
87 | $helper = $this->getHelper('question'); |
||
88 | |||
89 | if (\OCP\App::isEnabled('encryption')) { |
||
90 | $output->writeln( |
||
91 | '<error>Warning: Resetting the password when using encryption will result in data loss!</error>' |
||
92 | ); |
||
93 | |||
94 | $question = new ConfirmationQuestion('Do you want to continue?'); |
||
95 | if (!$helper->ask($input, $output, $question)) { |
||
96 | return 1; |
||
97 | } |
||
98 | } |
||
99 | |||
100 | $question = new Question('Enter a new password: '); |
||
101 | $question->setHidden(true); |
||
102 | $password = $helper->ask($input, $output, $question); |
||
103 | |||
104 | if ($password === null) { |
||
105 | $output->writeln("<error>Password cannot be empty!</error>"); |
||
106 | return 1; |
||
107 | } |
||
108 | |||
109 | $question = new Question('Confirm the new password: '); |
||
110 | $question->setHidden(true); |
||
111 | $confirm = $helper->ask($input, $output, $question); |
||
112 | |||
113 | if ($password !== $confirm) { |
||
114 | $output->writeln("<error>Passwords did not match!</error>"); |
||
115 | return 1; |
||
116 | } |
||
117 | } else { |
||
118 | $output->writeln("<error>Interactive input or --password-from-env is needed for entering a new password!</error>"); |
||
119 | return 1; |
||
120 | } |
||
121 | |||
122 | |||
123 | try { |
||
124 | $success = $user->setPassword($password); |
||
125 | } catch (\Exception $e) { |
||
126 | $output->writeln('<error>' . $e->getMessage() . '</error>'); |
||
127 | return 1; |
||
128 | } |
||
129 | |||
130 | if ($success) { |
||
131 | $output->writeln("<info>Successfully reset password for " . $username . "</info>"); |
||
132 | } else { |
||
133 | $output->writeln("<error>Error while resetting password!</error>"); |
||
134 | return 1; |
||
135 | } |
||
136 | return 0; |
||
137 | } |
||
151 |